I'm making a top down shooter type game. And obviously with the weapons and bullets this will involve some trigonometry. For some reason I can't seem to get the correct angle (in degrees).
Heres the function for getting the angle:
[code]
float Weapon::Angle(sf::RenderWindow &RW)
{
sf::Vector2f mouse = RW.ConvertCoords(RW.GetInput().GetMouseX(), RW.GetInput().GetMouseY()); //The mouse pos on the screen
sf::Vector2f center(b_box.Left + (b_box.Width / 2), b_box.Top + (b_box.Height / 2)); //Center of the player
float opposite = mouse.y - center.y;
float adjacent = mouse.x - center.x;
sf::Shape draw;
draw.AddPoint(center.x, center.y);
draw.AddPoint(center.x + adjacent, center.y);
draw.AddPoint(center.x + adjacent, center.y + opposite);
RW.Draw(draw);
RW.Draw(sf::Shape::Line(center.x, center.y, center.x + adjacent, center.y + opposite,2,sf::Color(255,0,0))); //This is to draw the hypotenuse for checking / debugging
return atan(tan(opposite / adjacent)) * (180.0 / 3.14); //Convert to degrees.
}
[/code]
According to this site just to be sure: [url]http://mathforum.org/~sarah/hamilton/ham.2sides.known.html[/url]
To get an angle of a triangle only knowing 2 sides, you can use the tangent function.
I decided to debug even more (which is why you see draw functions in the angle function), just to visually see what's going on. Here are a few pics and results:
[img]http://i38.tinypic.com/9u7nec.jpg[/img]
Result: -0.01 (somewhere around 0). This looks correct
[img]http://i37.tinypic.com/2rppqhx.jpg[/img]
Result: 5.555 Shouldn't it be at least somewhere around 80 or 90 degrees?
[b]EDIT PLEASE IGNORE PREVIOUS: [/b]
I just fount out about the atan2 function. And the code works. For those curious on the code, here's the source:
[code]
float Weapon::Angle(sf::RenderWindow &RW)
{
sf::Vector2f mouse = RW.ConvertCoords(RW.GetInput().GetMouseX(), RW.GetInput().GetMouseY()); //The mouse pos on the screen
sf::Vector2f center(b_box.Left + (b_box.Width / 2), b_box.Top + (b_box.Height / 2)); //Center of the player
float opposite = mouse.y - center.y;
float adjacent = mouse.x - center.x;
sf::Shape draw;
draw.AddPoint(center.x, center.y);
draw.AddPoint(center.x + adjacent, center.y);
draw.AddPoint(center.x + adjacent, center.y + opposite);
RW.Draw(draw);
RW.Draw(sf::Shape::Line(center.x, center.y, center.x + adjacent, center.y + opposite,2,sf::Color(255,0,0))); //This is to draw the hypotenuse for checking / debugging
return atan2(mouse.y - center.y, mouse.x - center.x) * (180.0 / 3.14); //Convert to degrees.
}
[/code]
Why did you have atan(tan(o/a)) to begin with? It's like doing 5*x/5. You're just undoing your own conversion.
It should've been atan(o/a) because tan(theta)=o/a by definition.
I need help doing this in Java, I'm pretty new. I have the location of input (touch-screen press) as X and Y coordinates, and the location of the player at X and Y coordinates. How do I get the degree measure to point the player to?
First make sure both the player's coordinates and the touch event are in the same coordinate system (scale, rotate, flip, whatever).
Subtract the touch event position from the player's position to get the vector from the player to the touch event, then use arctangent(y/x) to get the angle.
I don't know how to use Vectors :saddowns:
[QUOTE=ilolled;24477391]I don't know how to use Vectors :saddowns:[/QUOTE]
You're already using them.
The coordinate of the player and the coordinate of the touch event are 2D vectors.
Not entirely sure what you mean by "subtract the position." Do player X - touch X, in a vector with player Y - touch Y?
[QUOTE=ilolled;24481835]Not entirely sure what you mean by "subtract the position." Do player X - touch X, in a vector with player Y - touch Y?[/QUOTE]
direction.x = touch.x - player.x
direction.y = touch.y - player.y
Yes, that is what he means except you want to do y then x.
There is a function called atan which will find the arc tangent, but a limitation of this is that it only has a -90 to 90 degree range. So you want to use atan2 which will give you a 360 degree range. It takes two parameters, a y value and then an x value and it will return the angle in radians. To convert to degrees just do multiply it by 180 / PI.
Make sure you are familiar with some trig and what is going on in this circle.
[IMG]http://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Atan2_60.svg/350px-Atan2_60.svg.png[/IMG]
It works! :love: you both.
Sorry, you need to Log In to post a reply to this thread.