## 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 on other sites

get the mouse postilion. you know the ship position,   from those two points you can calculate the angle to the mouse.  rotate your ship by that amount.

##### 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. Pls keep that in mind. And again LOL for your answer @norman barrows. But thanks for that.

##### 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 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 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 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 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 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 on other sites

But for completeness, atan2 returns the angle in radians.

##### Share on other sites

But for completeness, atan2 returns the angle in radians.

Both atans should return radians, atan2 adds an extra computation to get the correct sector. Regular atan is only one argument so it can't specify the correct quadrant.

##### Share on other sites
but thing is how to calculate that angle.

if x is right, and y is up, and theta is measured in a counter clockwise direction from the pos x axis to the pos y axis, then tan  = opposite (y)   / adjacent (x) , so tan(theta) = y/x.   and theta = atan (y/x).     trig 101.

if you use screen coords, then y is down, so y2 = y-vert_resolution, and theta = atan (y2/x)

if your sprite routine rotates going clocckwise from vertical, not counter clockwise from the 3oclock position, then theta2 = -theta-90 degrees

use atan2

this code returns compass heading in degrees. positive x is right, positive z is up.  it uses atan, and does the appropriate fixup based on quadrant. atan2 does the quadrant specific fixup for you automatically.

// trig function version
int heading(int x4,int z4,int x5,int z5)    // returns heading from point x1,z1 to point x2,z2 in degrees
{
double x3,z3,theta;
int h;
long long x1,z1,x2,z2;
x1=(long long)x4;
z1=(long long)z4;
x2=(long long)x5;
z2=(long long)z5;
x2-=x1;
z2-=z1;
if (x2==0)
{
if (z2==0) return(0);
else if (z2>0) return(0);
else return(180);
}
else if (z2==0)
{
if (x2<0) return(270);
else return(90);
}
else
{
x3=(double)x2;
z3=(double)z2;
if (x2>0)
{
if (z2>0)
{
}
else
{
theta=atan(-z3/x3);
theta+=90.0;
}
}
else
{
if (z2>0)
{
theta=atan(z3/-x3);
theta+=270.0;
}
else
{
theta=atan(-x3/-z3);
theta+=180.0;
}
}
h=(int)theta;
while(h<0) h+=360;
while(h>359) h-=360;
return(h);
}
}


Edited by Norman Barrows

##### Share on other sites
Thank you all. I'm really very sorry about the silly questions I asked. But what can I do, I'm really bad at mathe and geometry. So forgive me for that. Thanks all again.

##### Share on other sites

But what can I do, I'm really bad at mathe and geometry.

Improve on those?
It's just like programming, except they use Greek letters instead of variable names, and they don't care about performance.

Give it a try, when you get stuck, post in the math section.

##### 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. Pls keep that in mind. And again LOL for your answer @norman barrows. But thanks for that.

Don't work in customer service. :lol:

I'm really bad at mathe and geometry. So forgive me for that. Thanks all again.

Also don't work as a game programmer. Try some other type of programming or 3d modelling, Although modelling requires patience, which is something you seem not to have in abundance, but it's better to try, you never know where your talent lies until you try it.

Edited by codeBoggs

##### Share on other sites

It's just like programming, except they use Greek letters instead of variable names, and they don't care about performance.

Sometimes I feel bad for mathematicians.

Then I remember they don't have to care if they spend 40 hours on a single math problem whereas I can't even spend a 4th of a second on one.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982242

• 10
• 9
• 11
• 24
• 11