XY Speed => Orientation

Does anyone have a script handy, or know how to, convert XY motion into an orientation?

For example: if I have a character (let’s say suzanne :stuck_out_tongue: ) who i set to move around via an X and Y speed (no Z for simplicity’s sake), I would also want suzanne to orient herself to be facing the direction she is moving in.

So yeah, any help would be lovely. And please don’t just give me a link to social’s orientation matrix tutorial :stuck_out_tongue: .

Well… If you treat the x and y velocities as separate vectors (which they are), and then combine them and calculate the angle of the vector, you can find the direction in which the object is traveling. Try something like this:

vel = own.getVelocity() #own is the object
theta = atan(vel[1]/vel[0])
#this is the angle from the horizontal. 
#It may be necessary to do some transformations to get the range and the +/-s right. 

Then use social’s great matrix tutorial (Specifically the example .blend) to turn the object (around the z-axis) the angle you have calculated.

i dont know if yours works, but i figured it out. Good ol’ atan2. Thanks though!!

the simplest one (for 2.47)


own.alignAxisToVect(own.getLinearVelocity(), 1)

that aligns the y axis to the velocity vector (in all axis)

Amazing! Is there an updated API listing somewhere? The only one I have is really old.

@Jo9… that was my next question…

@cyborg AR. thanks that’s pretty cool! What would I do to get it to work in 3d?. .eg. I have a ball falling and driving around… and I put an arrow on top that orientates to the balls 3d vector?

just uploaded it http://bgepython.isgreat.org/

updated, but it has some little errors

wow, that’s handy. thanks cy.

Just for fun, here’s what i did:


g = GameLogic
import math as m
cont = g.getCurrentController()
own = cont.getOwner()
y = 1
x = 1
angle = m.atan2(y,x)
orient = [[m.cos(angle), -m.sin(angle), 0],
          [m.sin(angle), m.cos(angle), 0],
          [0, 0, 1]]
own.setOrientation(orient)

x and y are the speeds

it already works in 3d, aligns in all the axis

Hooray!!! Thank you so much!! I found the API I was using was from 2.34.

Hey cyborg_ar Thanks a lot, I’ve travelled for 4 days to understand how do!

I didn’t know it was so simple!

Thanks again :evilgrin:

i love making one liners :3

:smiley: i love you and kind of guy like you lol, i read the post where you implement the alignaxistovect, it is very usefull for my RTS :slight_smile:

Actual blender.org docs are here:
http://www.blender.org/documentation/249PythonDoc/GE/index.html

Cool command cyborg! didn’t know that it existed.

I read doc :smiley: forum too, just didn’t know how to apply velocity vector or which vector should pass to alignaxis :wink: