• Advertisement
Sign in to follow this  

About character movements

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a game in which I need the hero ship to face the mouse cursor wherever it is located. Like in the game called Zuma. In which the frog faces the mouse cursor and shoots balls in that particular direction only. I want to make exactly that but in space shooter genre. Currently my ship faces the top of screen or bottom of screen. I'm using Dev c++ with allegro5 library on windows 7. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Seriously @norman barrows. I know that, but thing is how to calculate that angle. I need the formula for that. I know the 2 positions. I have a function available to rotate my ship. But it requires an angle, an angle between my ship and my mouse cursor. I just want the formulae to find that ANGLE. Pls keep that in mind. And again LOL for your answer @norman barrows. But thanks for that.

Share this post


Link to post
Share on other sites

Given the new forward direction u as the *normalized* difference between your two positions. You can find an orthogonal vector to u by simply swapping the elements and making one negative. E.g. v = ( -u.y, u.x ). These two vectors define your new orientation matrix R = ( u v ) assuming the local x-axis is your ship's forward direction. I would expect Allegro to provide an API to also set a rotation matrix and not only an angle. This is the standard approach for this problem and doesn't involve any trigonometry and it also extends to 3D.

 

The new angle would be simply alpha = atan2( u.y, u.x ). I would try to avoid angles if you can though and work with vectors and matrices instead.

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

Seriously @norman barrows. I know that, but thing is how to calculate that angle. I need the formula for that. I know the 2 positions. I have a function available to rotate my ship. But it requires an angle, an angle between my ship and my mouse cursor. I just want the formulae to find that ANGLE.

It helps a lot if you also explicitly state what part you are stuck. Norman responded to the general question that you asked. Maybe you didn't know that you could ask the mouse position, or you didn't understand you needed to construct a line between them.
All these and others were not excluded in your question.

Share this post


Link to post
Share on other sites
Sorry guys for being rude. I just got mad because I searched the net for about 5-6 hours to find a solution, but found nothing. And still I can't get your answers, I'm really bad at trigonometry, mathe and geometry. So could you pls give me a simpler solution (for my sake). I'd use a simpler mathe. Thank you. Again sorry for my comment which I did before

Share this post


Link to post
Share on other sites
From the above link, bottom picture with the circle, you can consider point O to be the ship, and point P to be the mouse.
Both have a (x, y) position.

In the picture, the X is the horizontal difference between both positions (ie subtract x position of the mouse from, x position of the ship), and the Y is the vertical difference (idem, but with y of mouse and y of ship)

now, by definition, tan(theta) = Y/X, where 'theta' is the angle you are looking for, and Y/X is (you guessed it), divide Y by X.
This "tan" function is a bit nasty, but no worries, smart people have invented 'atan' which does the opposite of 'tan'.

If "tan(theta) = Y/X", then "theta = atan(Y/X)". This is a whole lot better, Y/X can be computed, "atan" exists in your programming library, and the result is 'theta', the angle you're looking for.

There are a few caveats, Y/X doesn't exist if X==0 (can't divide by 0), so you have to special case this. Not a major problem, if X==0, then the difference in x positions between the ship and the mouse is 0, ie they are vertically aligned.
That should be solvable.

Secondly, the atan function doesn't return an angle from 0 to 360 (or 0 to 2*PI, if it speaks radians), you get a smaller range a few times. This can be solved by checking the sign of X and/or Y (so you know in which quadrant the mouse is, relative to the ship), and adjusting the angle separately for each quadrant.

Share this post


Link to post
Share on other sites

Dirk Gregorius and Alberth provided the answers already, but to try to give some more context...

 

First, you need the delta (difference) vector from the player to the mouse cursor.

Delta = PlayerPostion - MouseCursorPosition

 

Then you can use atan2 to find the angle.

Angle = atan2(Delta.y, Delta.x)

 

This tells you the angle the player should be facing, and you can set it to that.

 

Note that this does not tell you the amount to rotate, but the actual rotation angle the player should end up with. You most likely have some sort of SetAngle or SetRotation function available you can use.

Share this post


Link to post
Share on other sites
And also I want to know that this atan2 function, does it give angle in radian or degree..?? It's my last question pls help it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement