# How to test orientation and speed?

Hi all, I’ve just started playing with the blender game engine (which is lots of fun btw) and I’ve got a couple of questions.

Firstly how can i query the rotation/orientation of an object to set off an actuator . eg if object is pointing in a certain direction and i push a certain key then do something.

the second thing is how can i test the speed of an object. eg if object is travelling above x untis per second do something.

I’m sure I’ll have many more questions the more a use blender but if anyone can help me with this for the moment it would be really handy.
thanks:)

If anyone can help to answer to this, I’d appreciate it too.

Glenn

Me three, I could really use the speed calculation for my car sounds,

For those things, you will need python.

Speed is easy, here’s a simple script that turns the X, Y, and Z components of velocity into accessible properties:

``````
cont = GameLogic.getCurrentController()
own = cont.getOwner()

vel = own.getLinearVelocity()

own.velX = vel[0]
own.velY = vel[1]
own.velZ = vel[2]

``````

You’ll need 3 properties defined, velX, velY, and velZ, on the object. You can then use those properties in sensors (though you cannot SET the velocity with the property actuator… :p).

Orientation / rotation is a little tougher. Do a search on Social’s tutorial of the orientation matrix. I haven’t really taken the time to figure it out quite yet, but it is a little complex and not as simple as “RotX RotY RotZ.”

Hope that helps!

Cool!
Thank you for your help! Now we can start on some of our more complex sounds!

If a map is scaled to 1 meter = 1 blender unit, how would we convert the speed reported by the script to KPH and MPH? (miles per hour, kilometers per hour).

Dimensional analysis. It sounds complicated, but it really isn’t, and once you learn it, you’ll be able to convert from any unit to virtually any other unit. Its incredibly useful.

Look here for some tutorials (especially the “basic conversion problems”)
http://chemistry.alanearhart.org/Tutorials/DimAnal/

(1 u / sec) * (1 m / u ) * (1 km / 1000 m) * (60 sec / min) * (60 min / hr) = 3.6
So multiply your speed in bu by 3.6 to get the speed in km/hr.
(1 u / sec) * (1m / u) * (3.2808399 ft / m) * (1 mi / 5280 ft) * (60 sec/min) * (60 min / hr) = 2.2369362954

wow that was easy! thank you!

No problem! Like I said, converting units can be tricky until you know dimensional analysis.

Terve

I don’t understand the matrix at all but I found this pretty useful for myself. Its from Apple2 or 3 ‘collection’ by [who it was?]

``````
# # Get attitude
def getOrientationAxes(ob):
ori=ob.getOrientation()
x=ori[0]
y=ori[1]
z=ori[2]

neworiX=[x[0],-x[1],-x[2]]
neworiY=[-y[0],y[1],-y[2]]
neworiZ=[-z[0],-z[1],z[2]]

return [neworiX,neworiY,neworiZ]

vz = round(degrees(acos(getOrientationAxes(Cube)[2][2])),0)
dz = round(degrees(acos(getOrientationAxes(Cube)[2][0])),0) # roll
pz = round(degrees(acos(getOrientationAxes(Cube)[2][1])),0)-90 # pitch

vy = round(degrees(acos(getOrientationAxes(Cube)[1][2])),0)
dy = round(degrees(acos(getOrientationAxes(Cube)[1][0])),0)
py = round(degrees(acos(getOrientationAxes(Cube)[1][1])),0)

vx = round(degrees(acos(getOrientationAxes(Cube)[0][2])),0)
dx = round(degrees(acos(getOrientationAxes(Cube)[0][0])),0)
px = round(degrees(acos(getOrientationAxes(Cube)[0][1])),0)
``````

(edit)
The degrees() and acos() requires the python math module import

The orientation matrix is basically the x-, y-, and z-coordinates of three points, relative to the object’s center. These three points are where the x-, y-, and z- axis of the object point. So if you mess it up, the object will be distorted. I would suggest that you go back and try to learn it again, it will make your life easier.

here is what I put together from J09’s information about KPH.

``````
cont = GameLogic.getCurrentController()
own = cont.getOwner()

vel = own.getLinearVelocity(1)

own.velX = vel[0]
own.velY = vel[1]
own.velZ = vel[2]
own.KPH = own.velY * 3.6
own.MPH = own.KPH / 1.609344

#make and object with the propertys MPH,KPH, velX, velY, and velZ
#make an always sensor and make sure pulse is ON!

``````

It just Sambassitors script with the KPH conversion. I added a property to my object with KPH as well as the velX,velY,and velZ.

EDIT:
I changed “vel = own.getLinearVelocity()” to “vel = own.getLinearVelocity(1)” the 1 means to get the local Y axis rather than the global one.

To get the other axis it is 0 for X, 1 for Y, and 2 for Z

Thanks everyone, I’ll dig into this this weekend.

Glenn

Your right about the learning the new stuff… but for me it IS about matter of the time. I finally figured it out the airplane does not care about the attitude.

wow,

I was wrong about my script, I just fixed it.
own.getLinearVelocity() , means to return the own linvol … I changed the vel = own.getLinearVelocity(1)… the 1 make it local Y rather than global axis.

To get the other axis it is 0 for X, 1 for Y, and 2 for Z

Great, thanks guys. This stuff should help a lot.

The Oracle, if you’re only interested in the Y linear velocity, then why bother saving the other two? This just means you have to make more properties. If getLinearVelocity(1) just returns the Y linear velocity then:

``````cont = GameLogic.getCurrentController()
own = cont.getOwner()

vel = own.getLinearVelocity(1)

own.KPH = vel * 3.6
own.MPH = vel / 1.609344

#make and object with the propertys MPH and KPH
#make an always sensor and make sure pulse is ON!
``````

I you want to test for a direction without too much python you could just vertex parent an invisible plane to your object and test with a ray if the object is facing that way.

WOW! thanks. I did find a use for the X speed, it will control the tire sceech/slide sounds when there is any major local X movement.I still have no use for Z though, I may just prune off that little part .
Thanks for the help.