Rotation an Object to point somewhere

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've been writing a program for quite a few months now, and i've been avoiding one issue that i really need to address. My game is a 'space sim'. Objects need to be able to point towards an arbitrary x,y,z position in space. Right now my object points towards the camera (the mesh's orientation upon loading). Suppose my mesh is sitting at 0,0,0 and i need to point at position X,y,Z. The issue i'm having is the math.. I understand the concepts of what i need to do (Find the angles in each of the X,Y,Z axis) then apply the appropriate rotations. I have the following code, which i'm honestly not even sure is correct <code> D3DVECTOR NormVector(D3DVECTOR vector) { float length; length = sqrt((vector.x * vector.x)+(vector.y * vector.y)+ (vector.z * vector.z)); vector.x /= length; vector.y /= length; vector.z /= length; return vector; } float DotVector(D3DVECTOR origin, D3DVECTOR target) { return ((origin.x*target.x) + (origin.y*target.y) + origin.z*target.z)); } float FindAngleRadians(D3DVECTOR origin, D3DVECTOR target) { return acos(DotVector(NormVector(origin),NormVector(target))); } </code> Those functions make me wonder if i'm correctly even doing the basic math. Now assuming that i get the Angle change in the X,Y,Z vectors... then i just need to apply a D3DXMatrixAxisRotation() correct? Then set it? If my problems a little vague let me know and i can repost it to try and clarify... but its making my head hurt quite a bit here :( (also sorry about the code not being in a /code box... i couldn't figure out the syntax for it)

Share on other sites
This forum uses "forum tags," not HTML. Instead of <>, you use [] for tags. You can put your code inside <code></code> tags (replace <> with []), or, if you'd like a syntax-highlighted scrollable box, use the <source></source> tags.

Check out my post on this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=336156 for one solution.

Also, is there any reason why you're not using the D3DX vectors and their associated functions? D3DX provides a very nice mathlib which is optimized to use things like SSE and MMX on processors that support it.

Share on other sites
Hi Avengre,
How are you doing?
[The Logic]
The logic behind pointing an object towards a point will be able to push you in the right direction.
What you need to get is the point on the object that you want to point into a direction. The rest is very simple depending if it's 2d or 3d.

You take that point and rotate it using D3DXMatrixRotationX, D3DXMatrixRotationY, D3DXMatrixRotationZ or whatever...

[Example]
The objects point is in (0.0f, 0.0f, 0.0f) and pointing towards (1.0f, 0.0f, 0.0f) and you want it to rotate it to the point (0.0f, 1.0f, 0.0f).

Like so
^(0.0f, 1.0f, 0.0f)
|
|
|
|
|
-----------> (1.0f, 0.0f, 0.0f)

The best way to solve problems like this is take an example where you know what the angle is. The angle here would be 90CCW right?

[So how did we get there?
Finding the angle between 2 lines

[Conclusion]
You can play around with this if you have a 3D scene.
This solution will work well for a 2D game.

Share on other sites
Hi this question is similar to one that i asked and Mr Robert Dunlop replied here's what was said:

By me:
Quote:
 Does any one know how I can make a mesh look in the direction it's > 'walking'.>> I want it to face a given Vector.>> At the moment i can rotate my model using a transform matrix and then use> the m31,m32,m33 components of this matrix to determin which direction it> faces, however this doesnt help me for the above problem

By Robert Dunlop
Quote:
 Actually, it is the same problem in reverse, you can construct a matrix by the same means, though you also need to figure an "up" and "right" direction for the mesh. The you would put the forward vector in m31,m32,m33, the up vector in m21,m22,m23, the right vector in m11,m12,m13, and the location in m41,m42,m43 (and of course set the last column (m11,m21,m31,m41) to 0,0,0,1).You can calculate the right and up vectors in a manner similar to that used by Matrix.LookAtLH. Assuming your object will stay within a pitch range of +/- 90 degrees and does not need to roll (otherwise you would need more info than a forward vector), you can calculate the matrix like this (pseudocode) :forward (m31,m32,m33) = normalize(forward)right (m11,m12,m13) = normalize(cross(vector(0,1,0),forward))up (m21,m22,m23) = cross(forward,right)(m41,m42,m43) = location-- Robert DunlopThe X-Zonehttp://www.directxzone.com/Microsoft DirectX MVP

Share on other sites
The D3DXMatrixLookAt() function... Can you use it on a world matrix? Then set the transform for that? OR does that only setup a view matrix.

If it can be used to setup a rotation matrix for a World object... it'll be not too difficult..

Share on other sites
Sorry to dig this topic up again.

Would i want to normalize the Vector from the Objects position to the Target?

Or would that not matter

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• Forum Statistics

• Total Topics
633409
• Total Posts
3011722
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!