Fall damage

Hi!
I have made some sort of game basics, but next thing I need is to make a fall damage. It should be smart. I want my object to loose value of health property according to fall height. How do I make it?

Maybe using a collision sensor, and with python get the velocity of the object in z cordinate (world), and use it value to rest life. You need to have a min value, of this manner the player won’t injured everytime he touch the floor, or when he jump a small height.

You can use worldLinearVelocity to get the world linear velocity.

EDIT: VegetableJuiceF’s idea is the best.

How do I make player lose, example 5 health when falling from 5 m, but 20 health when falling from 15 m etc?

It’s not the speed or height that kills you, it’s the change in velocity that kills you.
If you calculate it on based fall height, someone with a parachute will explode on (soft) contact with ground.

Store the current velocity (vector) on the object and on the next frame find the difference and if it’s (length) above a threshold/limit subtract health.

Well, could you give me a script example and logic bricks screenshot, or ready example blen? it is hard to understand it in this way. Actually, I want to know how to make a fall damage at all:D

It is not easy to get the velocity change, as Bullet (the Physics engine) does not provide this data (at least not in the current BGE api).

I made such a system quite a while ago: Collision - activate actuator dependent on the collision impact (2.49-2.5+)

Could you just explain me how to add any of fall damage cripts to player and how to make it work. When I try to read all of theese, there is just a mud in my head(maybe because I don’t know few words in English)…

you can also look at the movement script in my sig, but read the post u need to change a setting.

Hi, I’m not sure how to code this kind of thing yet but I hope this pseudo code will help you get started, its very crude and choppy…


fall damage = 0
speed = self.own.getLinearVelocity(False)

if speed[2] < 0: #compares your Z axis
    fall damage = speed[2] * own.mass

hp - fall damage


own = bge.logic.getCurrentController().owner

if not "_old_velocity" in own: #this is just functional, not to read
    own["_old_velocity"] = own.worldLinearVelocity.copy()

vel = own.worldLinearVelocity.copy()
own["impact_magnitude"] = (vel - own["_old_velocity"]).magnitude
own["_old_velocity"] = vel


######
if own["impact_magnitude"] > 20.0:
    print("death")
    


is not hard, just need an always true , and know what you want.
if by chance you want know also the direction you have to add one line in the right point before to update the oldvelocity

As said earlier checking the speed (length of the velocity) is just one aspect. You should consider the object might bounce back (reflection). This results in a very low speed change but still kill the character. In your situation this might be sufficient for you as I do not think you let your character bounce back.

You could use a script that calculates all acceleration damage, also if the character gets rammed or something like that.
It could look something like that (not tested):

from bge import logicfrom mathutils import Vector


own = logic.getCurrentController().owner


if not "vel" in own:
    own["vel"] = Vector((0.0, 0.0, 0.0))
    
new_vel = own.worldLinearVelocity().copy()
vel_difference = new_vel - own["vel"]


damage = vel_difference.length


#do something with damage, for example
if damage > 1.0:
    own.endObject()


own["vel"] = new_vel

But usually you don’t really need such a “physically correct” script, it would be more straightforward and reliable to check whether your character is on the ground (for example by refering the onGroud variable if you use character physics) and check the last velocity when it left the ground and hits it again.

@Monster: I think this script (just like the one from MarcoIT) considers all velocity changes, including bounces.

OK! Please, tell me, which code works like Minecraft fall damage- test for fall height and than just removes health on collision with ground(or imediate stop)…

I made a basic one, here. I added some tweakables to it so the values can be changed as needed. It only works if while the player is falling, there is not collisions with it, until the ground, other wise the fall time would reset.

Arrow keys increase height, and mouse(left) click to add the collision boxes.

Attachments

Coll1.blend (473 KB)