4

I have some objects that I need to know how fast they are going. I have a property, "speed," and I would like it to show the object's speed in blender units. For the unit, it doesn't matter that much as long as I can read it. Python or logic would work, but if it's a script I'll have it running at all times. In other words, I'll be using this every logic tic so it needs to be efficient. Thanks.

EDIT: I've figured out how to find velocity on my own, but how would I use that xyz info to find speed?

blackhole
  • 2,390
  • 4
  • 26
  • 64

2 Answers2

5

Keep track of the objects previous position. Algorithm:

on each logic tick:
     speed_vector = current_position - previous_position
     speed = speed_vector.length()
     previous_position = current_position
beiller
  • 2,170
  • 12
  • 11
2

I have found a nice script from here, and it is the following:

# get the controller
controller = GameLogic.getCurrentController()

# get the game object that the controller is attached to.
obj = controller.owner

# get local (game object) linear velocity
linVelocity = obj.getLinearVelocity(True)

I have modified it for my own scene, and I've also defined speed.

p2or
  • 15,860
  • 10
  • 83
  • 143
blackhole
  • 2,390
  • 4
  • 26
  • 64