# Shooting at an Angle

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

## Recommended Posts

Hey everyone, Im creating a game where I need some stationary "towers" to be able to shoot at some moving targets, to do this i need the towers to be able to shoot at angles rather than just left, right, up and down. I am writing this and Java and would appreciate some help on how to accomplish this or a tutorial. But if its in another language its fine (but must be english lol) since i should be able to just see what they are doing and translate it. Ive searched around but havent came across anything, so any help will be appreciated. Thanks!

##### Share on other sites
First you need to be able to simulate projectile that can move in any direction, by using 2d vector math:
//My java is rusty, I don't know how to do a square root, or even if these class definitions are validclass Vec2{public float x;public float y;public float Length() { return sqrt( x*x + y*y ); }}class Projectile{public Vec2 pos = new Vec2;public Vec2 vel = new Vec2;public void Update( float elapsedTime ) {   pos.x += vel.x * elapsedTime;   pos.y += vel.y * elapsedTime; }}

Then you can use trigonometry to convert between angles and 2d vectors. You can then multiply those (normalised, i.e. vector.length==1) vectors by a speed scalar value to get the velocity of the projectile.

E.g. lets say up is 0º, right is 90º, down is 180º and left is 270º.
If you want to shoot to the right then you can convert 90º into the vector (0,1).
vec.x = sin( pi/2 ) //90 degrees == Pi/2 radians
vec.y = cos( pi/2 ) //90 degrees == Pi/2 radians

[Edited by - Hodgman on January 31, 2008 12:10:09 AM]

##### Share on other sites
Hey thanks man!!, Ill have to try this out, I knew my pre-cal class would come in handy:O...too bad my teacher didnt teach us crap. At least he gave everyone a C or better though....:(

Also, do i need to create the Vec2 Class? Im not sure im undestanding..

[Edited by - Swattkidd on January 30, 2008 10:18:58 PM]

##### Share on other sites
Quote:
 Original post by HodgmanE.g. lets say up is 0º, right is 90º, down is 180º and left is 270º.If you want to shoot to the right then you can convert 90º into the vector (0,1).vec.x = sin( pi/4 ) //90 degrees == Pi/4 radians vec.y = cos( pi/4 ) //90 degrees == Pi/4 radians

90 degrees = pi/2, isn't it ? :)

Anyway, as input data, you need to know:
- x and y: the current position of your projectile.
- a: The angle, in radians. 0 is right, pi/2 is up, pi is left, 3pi/2 is down and 2pi=0 is right.
- velocity: Its velocity in pixel/second.
- elapsedTime: The elapsed time since the last frame, in seconds.

Then to get the new position of your object:
newX = x + Math.cos(a) * velocity * elapsedTime;
newY = y + Math.sin(a) * velocity * elapsedTime;

To get the angle in radians between (x1,y1) and (x2,y2) relative to the horizontal axis:
a = Math.atan2(y2-y1, x2-x1);

##### Share on other sites
Quote:
 Original post by Splo90 degrees = pi/2, isn't it ? :)

Oops! Thanks for that. In my head I was thinking "2*Pi in circle, divided by 4", but then decided to (but half-forgot to) convert it to a fraction of Pi while typing...

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 18
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632662
• Total Posts
3007702
• ### Who's Online (See full list)

There are no registered users currently online

×