Hi, I am a total noob at python, so how can I find out the velocity at which an object is moving when it hits something? I would like to use this in multiple things such as fall damage, crashing vehicles, dropping an object from a height etc.
Example of what I would like to do:
Have a python script edit a property float that tells us what speed the object is moving at.
On collision with another object, compare the velocity to certain outcomes (if the velocity is < 2m/s do nothing, if <5>2 take damage, if >5 die)
what you have to do is compare the old velocity with the current velocity.
the old velocity not exist as default property , you have to do it your self:
just be aware to the order , first compare , then store :
def main(cont):
own = cont.owner
if not "init" in own :
own["init"] = 1
own["oldVel"] = own.localLinearVelocity.copy()
#comparison
impactVector = own.localLinearVelocity - own["oldVel"]
impactSpeed = impactVector.magnitude
if impactSpeed > 5.0 :
print("die for the impact ->", impactSpeed )
#store the current velocity
own["oldVel"] = own.localLinearVelocity.copy()
PS:
not should be so hard to implement, this is my impactSensor custom: (that require of course other code)
import xxx
import bge
def m(cont):
own=cont.owner
if not "init" in own:
own["init"] = 1
own["impact"] = bge.xxx.createImpactSensor(own)
impact = own["impact"]
if impact.magnitude > 5:
#die()
own.endObject()