ScriptSpot

Forum topic · General Scripting

How to calculate an object's speed?

By ausn · 2009-04-15

Description

Hi everyone!

I've animated an object and how could I get its speed at each frame?

Thanks!

Comments (3)

Anubis · 2009-04-21

This method is suitable for straightforward motion but if you change the direction it needs a little math to calculate a trajectory and I am not very good at math :)

aphexx · 2009-04-16

hi, this is very cool! although i'm no scripter at all :)

ausn · 2009-04-16

I figured out this method:

prePos = at time (currentTime - 1t) self.pos
nextPos = at time (currentTime + 1t) self.pos
d = distance prePos nextPos
v = d*4800/2 -- unit:inch/s
v = v*2.54/1000 -- unit:m/s
v = v*60*60/1000 -- unit:km/h

It could be precise enough for most circumstances.

And a friend showed me this method:

prePos = at time (currentTime - 1t) self.pos
curPos = self.pos
nextPos = at time (currentTime + 1t) self.pos
d1 = distance prePos curPos
d2 = distance curPos nextPos
d = (d1+d2)/2
v = d*4800/2 -- unit:inch/s
v = v*2.54/1000 -- unit:m/s
v = v*60*60/1000 -- unit:km/h