Euler rotation and forward vector

Started by
4 comments, last by Zoultrex 13 years, 4 months ago
I'm trying to implement a guided missile NPC and i cant figure out how to update its forward vector after I change its rotation(in euler)

this is what I am using to update the rotation
Vector3 lookAtPoint = new Vector3( x, y, z );Vector3 myPosition = this.transform.position;lookAtPoint -= myPosition;float r = Mathf.Sqrt( lookAtPoint.x*lookAtPoint.x + lookAtPoint.z*lookAtPoint.z );float yaw = Mathf.Atan2(lookAtPoint.x, lookAtPoint.z);float pitch = Mathf.Atan2(lookAtPoint.y * -1.0f, r);//yaw and pitch are converted with Mathf.Rad2Deg


There is nothing special that i need in my implementation, so far i do not need to take account velocities and acceleration.
For now i really just need to rotate the missile towards the target and then move the missile towards its z axis.
I already have this implemented for a normal missile that just goes forward, now this one I want to follow the target.

I have a math library to help me with quaternions and matrices if need (Sharp3D.Math), but i dont know for example how to get a forward vector our of a matrix or out of a quaternion.

The non-guided missile NPC that I have now, can only move forward because when I shoot it I get the forward vector of the ship that just shot it, and store it on the server npc object.

So this guided missile npc that I want to implement would be a variation of the missile npc that I already have, only that it has to change its forward vector once it changes the rotation so it can go straight but in the new forward vector direction right?

Does it sound like it would work? If there is another better approach to this please let me know, the reason I want to keep using the forward vector is that its already implemented in the npc class and I just want to extend it.
Advertisement
With a Matrix, the forward Vector is the 3rd column, n13, 23 and 33.

But of course what you need to do is rotate your matrix according to the angle between the current position and target.
Well i dont have a matrix, all i have is xyz position and xyz rotation angles as Euler angles, that from both, target and missile.

What kind of matrix would I have to build in order to extract the third column?
If you don't have a matrix, then what is "this.transform"?

And if you really want to go pure euler, then use raw trigonometry rather than a matrix if you really don't have one, using the angles you've calculated with Math.atan2.


sinX = Math.sin(pitch);
cosX = Math.cos(pitch);
sinY = Math.sin(yaw);
cosY = Math.cos(yaw);

velocity.x += force * sinY;
velocity.y += force * sinX;
velocity.z += force * cosY;

Something like that.
Quote:Original post by rumblesushi
With a Matrix, the forward Vector is the 3rd column, n13, 23 and 33.
That's actually a little inaccurate. A transform by itself has no concept of a 'forward' vector. 'Forward' is a concept that's imposed externally and is dependent on the nature of the object being represented and how it's oriented in local space. The transform for a marble, for example, probably can't be said to have a forward vector, per se. Also, in a 3D game where 'z' is considered to be the up-down axis, the x axis (for example) might be considered to be forward.

That said, it's certainly common for objects to be oriented in local space such that z is forward.
Wow this is perfect you saved my life rumblesushi, only i noticed the the Y of the velocity was inverted I just made the sinX negative and it was all good.

The transform is a variable of the game engine(Unity3D) that holds quite a few information about the gameObject on the scene of the game, it already computes rotations positions and other things to make the game programmer life easier.
What I'm trying to achieve is easily done in the game but I want to implement this on the server.

Thank you so much for you help rumblesushi.

Just wondering what would I have to google, or study about trigonometry to be able to know how to do this. The basic that I know about trigonometry just relates to angles and circumference, I really didnt imagine this could be achieved with trigonometry too.

[Edited by - Zoultrex on December 5, 2010 8:40:56 PM]

This topic is closed to new replies.

Advertisement