Public Group

# Mesh Rotation Problem

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

## 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 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 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 on other sites
Try this ...
setVelocity( (C.x + M.x) / constant * speed), 0.0f, (C.y + M.y) / constant * speed) );

##### Share on other sites
What is constant?

##### 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 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 frameMeshCurPos += MeshCurVel

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632590
• Total Posts
3007244

×