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 ) 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 .
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.
@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?
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)