I found some code by Social that calculates impact forces. I have integrated it into a script I am currently working on but I have come across a problem. To start with here is the code:
def collision(cn):
ow = cn.owner
collision = cn.sensors["collision"]
if collision.positive:
linv_now = ow.getLinearVelocity()
applied_vec = linv_now - ow["linv_before"]
print ("For object:", ow.name)
print ("--Impact magnitude:", applied_vec.magnitude)
applied_vec.normalize()
print ("--Direction vector:", applied_vec.magnitude)
print ("") # Space
#ow["health"] -= ???? < what to put here?
else:
ow["linv_before"] = ow.getLinearVelocity()
print(ow["linv_before"].magnitude)
Now when my objects collide with anything this script outputs the force of the collision. The thing is, I would love to use this number so it can be subtracted from the objects health property (in the line I annotated with ???). I have tried a couple of approaches but I get a ‘cannot use with noneType’ error so I am guessing I need to convert it somehow…
It sounds like you want the magnitude of the vector (which it looks like you know how to get).
Just a guess here, but you probably already tried using applied_vec.magnitude, and still got the noneType error. If this is the case I would look into ow[“health”], and make sure it is actually initialized (and not returning none).
The collisions list returns a list of all the collision sensors attatched to the object’s controller, then retreives the first one in the list.
The multiplier basically allows you to change the strength of the damage.
You do not need an “init” property. This is old style object initialization.
Just check for the existence of “vel” as this is the property the code is using.
“bean style syntax” is not old. It is just a style. The “property syntax” is another style (with the meaning of Python properties not BGE properties ;)).