Getting the object velocity.

Hello.
I kinda got stuck into getting the velocity of an object and placing it into a float/integer property.
How would I go? I have tried getVelocity() but it seems like I cant manage to make it work.

Thank you.

Object['velo'] = 5 #property velo

#set the speeds
x = 0 
y = Object['velo']
z = Object.localLinearVelocity.z

#set the speeds
Object.localLinearVelocity = [x, y, z]

I need to read a specific axis velocity from an object and place it into a property,not assign it. Your script right now assigns a speed.

Please do not confuse velocity with speed.

Velocity is as a vector with a direction and a length.
Speed is the length of the velocity.

therefore:
demo.py


def putSpeedInValueField(currentController);
  currentObject = currentController.owner
  speed = currentObject.worldLinearVelocity.magnitude
  setValueFieldAndActivateActuators(currentController, speed)

def setValueFieldAndActivateActuators(controller, value):
  valueAsString = str(value)
  for actuator in controller.actuators:
    try:
      actuator.value = valueAsString 
    except AttributeError:
      continue
    cont.activate(actuator)

Call it with an Python Controller with Module: demo.putSpeedInValueField
connected with at least one PropertyActuator that writes to the property you want.

E.g.
Always (True Pulse)
–> Python Controller Module: demo.putSpeedInValueField
----> PropertyActuator Property: speed

getLinearVelocity() doesn’t help? I can get the values just fine…

Thanks Monster! All i needed was “speed = currentObject.worldLinearVelocity.magnitude” , and I implemented succesfully into my script!!

not that hard to change it :wink:

# get localLinearVelocity
x = Object.localLinearVelocity.x 
y = Object.localLinearVelocity.y
z = Object.localLinearVelocity.z
# set localLinearVelocity in property
Object['vel_x'] = x
Object['velo_y'] = y
Object['velo_z'] = z

Cotax please be aware these are components of the velocity. This is not speed. This values are projections of the velocity on a specified axis. They are useful under very specific conditions only.