Is there a way to implement fall damage?

I’m wandering if there is a way to implement fall damage into my game. So if the player jumps of the top floor they will take damage, if they jump off the bottom floor the won’t take damage. Is there a way to do this, here is a diagram:

[Floor 1]

[Floor 2]

             [Ground]

When they jump remember the height they started:

[lua]player.jumpHeight = player.y – their vertical location on the screen[/lua]

When they hit the ground see how far they’ve fallen:

[lua]if player.y - player.jumpHeight >= TAKEFALLDAMAGE then

   …do something here

end[/lua]

Of course, set TAKEFALLDAMAGE earlier to whatever value you want to use for the distance they can fall before taking damage.

 Jay

I think a good way to do this would be getting the players velocity right before they hit the ground. Im pretty sure you can do this using preCollisions, I’ve never used them but they look interesting. I just read up on it and I’ll test it out in a bit but I think this code will work:
[lua]

local platform = display.newImage( “platform.png” )

platform.collType = “passthru”

physics.addBody( platform, “static”, { bounce=0.0, friction=0.3 } )

function character:preCollision( event )

     local collideObject = event.other

     if ( collideObject.collType == “passthru” ) then

          event.contact.isEnabled = false --disable this specific collision!

     end

end

[/lua]

So I THINK that in the preCollision function you could then get the players velocity and add damage depending on how fast he is going. I’ll write up a function once I get it working.

Alright so I tested it out on my other computer and here is the code that I have:

[lua]

    require(“physics”)

    physics.start()

    local platform = display.newRect(0, ch-50, cw, 50)

    physics.addBody(platform, “static”)

    local player = display.newRect(cw/2 - 25, 50, 50, 50)

    player:setFillColor(100, 100, 100)

    physics.addBody(player)

    function player:preCollision( event )

        local v,y = player:getLinearVelocity()

        local damage = 0.2 * y – change 0.2 to change damage value

        print("Velocity = ", y)

        print("Damage = ", damage)

    end

    player:addEventListener(“preCollision”)

[/lua]

So basically it creates a preCollision function for the player which gets the linear velocity of him right before he hits the platform. The listener then uses that to calculate an amount of damage to inflict which you can change, maybe depending on what boots he wears. Hope this helps!

When they jump remember the height they started:

[lua]player.jumpHeight = player.y – their vertical location on the screen[/lua]

When they hit the ground see how far they’ve fallen:

[lua]if player.y - player.jumpHeight >= TAKEFALLDAMAGE then

   …do something here

end[/lua]

Of course, set TAKEFALLDAMAGE earlier to whatever value you want to use for the distance they can fall before taking damage.

 Jay

I think a good way to do this would be getting the players velocity right before they hit the ground. Im pretty sure you can do this using preCollisions, I’ve never used them but they look interesting. I just read up on it and I’ll test it out in a bit but I think this code will work:
[lua]

local platform = display.newImage( “platform.png” )

platform.collType = “passthru”

physics.addBody( platform, “static”, { bounce=0.0, friction=0.3 } )

function character:preCollision( event )

     local collideObject = event.other

     if ( collideObject.collType == “passthru” ) then

          event.contact.isEnabled = false --disable this specific collision!

     end

end

[/lua]

So I THINK that in the preCollision function you could then get the players velocity and add damage depending on how fast he is going. I’ll write up a function once I get it working.

Alright so I tested it out on my other computer and here is the code that I have:

[lua]

    require(“physics”)

    physics.start()

    local platform = display.newRect(0, ch-50, cw, 50)

    physics.addBody(platform, “static”)

    local player = display.newRect(cw/2 - 25, 50, 50, 50)

    player:setFillColor(100, 100, 100)

    physics.addBody(player)

    function player:preCollision( event )

        local v,y = player:getLinearVelocity()

        local damage = 0.2 * y – change 0.2 to change damage value

        print("Velocity = ", y)

        print("Damage = ", damage)

    end

    player:addEventListener(“preCollision”)

[/lua]

So basically it creates a preCollision function for the player which gets the linear velocity of him right before he hits the platform. The listener then uses that to calculate an amount of damage to inflict which you can change, maybe depending on what boots he wears. Hope this helps!