Sign in to follow this  
zealotgi

Mesh Rotation Problem

Recommended Posts

Hey All I'm creating a game with a top-down view where a cannon will be exactly in the middle. The cannon will shoot a cannon ball at the spot where the mouse lies when the player clicks the mouse. Now, as the player is aiming with the mouse, the cannon should rotate relative to where the mouse is on the screen. Heres an ASCII example... [O] = Cannon - = Turret [+] = Current Mouse Position

             Screen
---------------------------------
|                               |
|                               |
|                               |
|                               |
|                         (35,0)|
|              [O]-       [+]   |
|                               |
|                               |
|                               |
|                               |
|                               |
---------------------------------


Okay, so the cannon would point and the cannon ball would land at 35,0 . I am familiar on how to rotate objects/meshes in the language (directX), and know how to translate 2D Coordinates to 3D, so the problem lies in the mathmatics on how to do rotate the object properly with the mouse. I have tried numerous mathmatical forumulas in the rotate function, such as RotateObject(0.0f, sin(MouseX),0.0f) but I can never seem to get it right. Does anyone have a suggestion on how to do this?

Share this post


Link to post
Share on other sites
You can use trigonometry like so ...

---------------------------------
| [+] |
| . . |
| h . . |
| . .o |
| . . |
| [O]......... |
| a |
| |
| |
| |
| |
---------------------------------



If we call the angle between 'h' and 'a' theta then ...
theta = arctan(o / a)
The cannon can be 'C' and the mouse can be 'M'. We now then have ...
a = C.x + M.x
o = C.y + M.y
So then ...
theta = arctan( (C.y + M.y) / (C.x + M.x) )

That should work.

EDIT: I hope no-one saw my original post ... I forgot about tangents and used sines ... [embarrass]

Share this post


Link to post
Share on other sites
Okay, that works. Now, I have another situation.

I am using a spherical mesh for the cannonball, and cant use the same method as before because the function is SetVelocity(X,Y,Z), so X and Z have to be relative to one another to accomplish lanching the cannon ball in the right direction.

SetVelocity(atan(c/m),0.0f,atan(c/m) doesn't seem to work out...

Edit: forgot to mention that SetVelocity X and Z need to be * by the speed so it would be like SetVelocity(atan(c/m)*120.0f,0.0f,atan(c/m)*120.0f) where 120.0f is the speed of the mesh

Share this post


Link to post
Share on other sites
Well if you set the X and Z velocity to the distances from the cannon to the mouse the cannon ball will move their instantly, so by dividing by some set number (any number that makes the cannon ball move like you want it) it will keep the ratio between X and Z the same. This will let the cannon ball travel in the correct direction but also will slow it down so it doesn't 'warp' to the mouse position right away.

Share this post


Link to post
Share on other sites
Well, here is the original procedure, so you can see what I'm doing...

[source lang = "cpp"]
D3DXVECTOR3 MeshCurPos, MeshCurVel;

void SetVelocity(D3DXVECTOR Velocity)
{
MeshCurVel = Velocity
}

// In game frame

MeshCurPos += MeshCurVel






Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this