Mesh Rotation Problem

Started by
5 comments, last by zealotgi 18 years, 9 months ago
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?
---------------------------------The Shadow Sun - Want Your Writing Exposed?
Advertisement
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]
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
---------------------------------The Shadow Sun - Want Your Writing Exposed?
Try this ...
setVelocity( (C.x + M.x) / constant * speed), 0.0f, (C.y + M.y) / constant * speed) );
What is constant?
---------------------------------The Shadow Sun - Want Your Writing Exposed?
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.
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 frameMeshCurPos += MeshCurVel




---------------------------------The Shadow Sun - Want Your Writing Exposed?

This topic is closed to new replies.

Advertisement