Local Velocity

How does one find the local velocity of an object? I’m trying to make a movement script that limits objects to a maximum speed.


import math
#get velocity ([0] = x, [1] = y, [2] = z), so if you want just x-velocity type owner.getVelocity()[0]
speed=owner.getVelocity()
#calculate total speed (x+y+z velocity)
speedtotal=math.sqrt(math.pow(speed[0],2)+math.pow(speed[1],2)+math.pow(speed[2],2))

Thanks, but I don’t think that’s it. I searched the forum and found some code that I converted to a function. It gives velocity using the local coordinate system and is fairly accurate.

def getLocalVelocity(obj): #created by Ridiculous of BlenderArtists.org (as far as I know) with minor modifications by me
    Vel = obj.getLinearVelocity()
    Ori = obj.getOrientation()
 
    Vel = [
    [Vel[0], 0.0, 0.0],
    [0.0, Vel[1], 0.0],
    [0.0, 0.0, Vel[2]]
    ]
    
    LocVel = [] #same as "LocVel = MultMatrix (Vel, Ori)"
    for r in range(3):
        row = []
        for c in range(3):
            row.append(Vel[r][0]*Ori[0][c]+Vel[r][1]*Ori[1][c]+Vel[r][2]*Ori[2][c])
        LocVel.append(row)
     
    # I'm getting the sum of the values in the columns to get the 
    # local velocity. It works, but it might be wrong because it's not 
    # like getting the magnitude. As you may know, magnitude is always 
    # positive, but I need negative and positive values, so I'm just using 
    # the sum
    LVelX = LocVel[0][0] + LocVel[1][0] + LocVel[2][0]
    LVelY = LocVel[0][1] + LocVel[1][1] + LocVel[2][1]
    LVelZ = LocVel[0][2] + LocVel[1][2] + LocVel[2][2]
    
    return [LVelX, LVelY, LVelZ]

### I added this code to test it
cont=GameLogic.getCurrentController()
own = cont.getOwner()

print getLocalVelocity(own)

The topic was here: http://blenderartists.org/forum/showthread.php?t=49213 post #11

Oops, sorry… I somehow missed that local-word and thought you only want to know the velocity of the object. Looks like it gets much more complicated if you want local velocity.