Public Group

#### Archived

This topic is now archived and is closed to further replies.

# math problem

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

## Recommended Posts

Hi, i have a small math problem that i can''t fiqure out.I am making a game something like ''gorrilas'' or ''worms'', the problem is when i get the angle from the user, i don''t know how to calculate angle in pixels so the shot can be fired at that direction. For example: i know that when the user inputs 45 as the angle, the movement of the shot will be : pixelX + 1 and pixelY + 1 ! But what if the user inputs 25 as the angle? It would be something like this pixelX + 3 and pixelY + 1 or something. What i want is to calculate that. Maybe this is not the best way to do this if you know the proper way or a better one please tell me. The inputs are :''power and angle'' i want in some way for the shot to be fired at the right angle. (sorry for the grammar or spelling)

##### Share on other sites
It''s all basic trigonometry and physics. Here''s a diagram:
       /|      / |  C  /  | A    /   |   /    |a /_____|     BA = sin(a)*CB = cos(a)*C

The ''power'' that you want becomes side C. You use sine and cosine to find the up (A) and forward (B) vectors from C. The up vector (A) will be affected by gravity, but the forward vector (B) will not be.

##### Share on other sites
Since you are talking in degrees and sin, cos accept arguments in radians you'll also need to convert from degrees to radians...

360 deg = 2 * PI radians

so radians = ( degrees / 180 ) * PI

[edited by - Paradigm Shifter on August 29, 2002 7:17:19 AM]

##### Share on other sites
This is all nice and correct but when i try to convert the numbers to pixels it get confusing. For example i do A and B with 45 angle and the result is A=B which is correct. But when i convert A to pixelsY and B to pixelsX i want the movement X Y (loop x++ y++) of the shot to finish in equal times. Anyway the point is that i get confused with too much math problem solving, i haven''t done trigonometry for 5 years.
Can anyone show a piece of code that gets power and angle from the input and fires a shot in that direction including the force of gravity, im sure its not very hard to do but im just a noob at game programming(this is my first game).

Sorry for being such a pain, any help is more than just thanks!

##### Share on other sites
Use floating point values and convert to ints for drawing purposes only.

Look into vectors (maths not C++), there''s lots of resources here or on the web.

Also look into ballistics, that''s the area of maths you are dealing with.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
And brush up on your trig! You''re gonna need it obviously!

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 27
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001791
×