Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 10, 2024, 03:32:34 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Calculating the angle to launch an object.
Pages: [1]
Print
Author Topic: Calculating the angle to launch an object.  (Read 4327 times)
M.D.K.
Level 0
**



View Profile
« on: November 26, 2009, 06:49:43 PM »

Basically allow me to illustrate my problem




Basically I want to find the angle that I need to launch a certain object in order to hit something that is located at a certain distance.

the variables I'm using are

Code:
_angle //for the angle that I want to throw the thing.
_dist  // the distance between the objects
_spd   //the speed I'm throwing the object
_grav  //the gravity.

About last year, I  shuffled between several formulas and somehow I McGuiver'd one that worked (or shoud I say Ork'd my way to it?  Concerned)

Anyway, I lost that valuable piece of code, and I can't find a way to make it back. I was wondering if any of you guys know the solution to this particular problem.

I have the distance and the speed to launch an object, but I don't know at wich angle should I throw it.
« Last Edit: November 26, 2009, 07:17:33 PM by M.D.K. » Logged
DYRE
Guest
« Reply #1 on: November 26, 2009, 08:04:13 PM »

Wikipedia gives the range of a projectile as (v2/g)sin(2θ)
Rearranging this, we get (rg/v2)=sin(2θ)
and so, θ=arcsin(rg/v2)/2, I think.
r is range, θ is angle, g is gravity, and v is speed.
Logged
M.D.K.
Level 0
**



View Profile
« Reply #2 on: November 26, 2009, 08:08:01 PM »

Thanks!, this is what happens when you don't look the english version of wikipedia first, silly me  WTF

I converted it into something like this and it works wonders
Code:
angle= radtodeg((arcsin(_dist*_grav/sqr(_spd) ) )/2);
angle= 90-angle;

Yes, I'm using Gamek Maker  Concerned
Logged
Glaiel-Gamer
Guest
« Reply #3 on: November 27, 2009, 02:02:05 AM »

Thanks!, this is what happens when you don't look the english version of wikipedia first, silly me  WTF

I converted it into something like this and it works wonders
Code:
angle= radtodeg((arcsin(_dist*_grav/sqr(_spd) ) )/2);
angle= 90-angle;

Yes, I'm using Gamek Maker  Concerned

I MAY BE incrediubally drunks right now but maye I can help

y = a*x^2 = v*x + d

SO you simply need to calculate V0 since acceleration is consant (-9.81 m/s^2) so


     /|
    / | dy
   /  |
  /A  |
 - - - - - - -
 -----|
   dx


A = tan(dy/dx)

HOPEULLY that helped but i cant think straight right nwo anyuwa

Vx[initial] = cos(d)+
Logged
bateleur
Level 10
*****



View Profile
« Reply #4 on: November 27, 2009, 02:30:47 AM »

One thing nobody's mentioned explicitly yet it that there are two launch angles which will give you any given distance between 0 and the maximum.

Also, it's occasionally worth knowing that the distance the object will go is linear in both the x and y components of the launch velocity. So in any case where you don't need to fix the launch velocity you can get different ranges really easily by just knowing how far a 45 degree shot will go and then scaling either the x component (for a high arc) or the y component (for a low arc) of the launch velocity.

Variable launch velocities like this may not be very authentic for (eg.) cannons, but can work better in some types of game where very high or very low arcs look a bit silly.
Logged

M.D.K.
Level 0
**



View Profile
« Reply #5 on: November 27, 2009, 02:44:57 AM »

Thanks Glaiel, but I already had it fixed. I appreciate the code nevertheless.

Bateleur: yes, I noticed while checking the wikipedia. I noticed that to obtain the angle that will throw the objects on a high arc all I had to do was.

angle=90-angle.


I made it this way for several reasons. one,is that I found  it simpler that if the object is in the other direction I would just substrack the angle from 180 degrees.


I may not be drunk, but I definatelly seem to code like one, but it works! and I rather not fix something that isn't broken.
Logged
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #6 on: November 27, 2009, 04:23:25 AM »

Damm, I really needed this and tried (and failed) to work this out based on my knowelege of A-Level physics. All I had to do was wikipedia it...
Logged
powly
Level 4
****



View Profile WWW
« Reply #7 on: November 27, 2009, 04:29:16 AM »

Wut, my physics test yesterday had a question directly related to this: there was a pirate ship (yarr!) that shot a cannonball 350 km/s at a 25 degree angle and the question was how far it could hit an enemy ship and could it hit a seagull flying at a certain distance. (23m)

Well, the answer can already be found in this thread so I won't bother repeating it.
Logged
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #8 on: November 27, 2009, 08:20:26 AM »

only thing is, I was trying to automate it. computers dont like re-arranging equations...
Logged
___
Vice President of Marketing, Romeo Pie Software
Level 10
*


View Profile
« Reply #9 on: November 27, 2009, 04:01:45 PM »

Now do it with wind.
Logged
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #10 on: November 28, 2009, 02:23:24 AM »

Easy, you just have it as another accelleration along the x axis
Logged
bateleur
Level 10
*****



View Profile
« Reply #11 on: November 28, 2009, 05:01:03 AM »

Easy, you just have it as another accelleration along the x axis

That's fine for the modelling part. The resulting problem is tricky to solve, though, because time-in-air suddenly matters, so you end up with a sin(a)*cos(a) term in the equation.

Better at that point to switch to simulating shots behind the scenes and iterating to a sufficiently accurate solution. Well, I say "simulating", but it's not like shelling Paris. It's exact precalculation of where the shot will land!
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #12 on: November 28, 2009, 05:45:25 AM »

Solution with horizontal wind, flat plane (inclined plan, angled wind is just as easy):
Code:
Initial equations:

distance = speed * cos(theta) * t + 0.5 * wind * t * t
0 = speed * sin(theta) * t - 0.5 * gravity * t * t
t != 0

=>
t = speed * sin(theta) * 2 / gravity
  = k sin(theta)
for k a constant.

Plugging back in, and omitting "(theta)" for brevity:

distance = speed * cos * k * sin + 0.5 * wind * k * k * sin * sin

Pythagoras:
distance = distance * (cos * cos + sin * sin)

So:
0 = - distance * cos * cos + speed * k * cos * sin + (0.5 * wind * k * k - distance)
0 = a * cos * cos + b * cos * sin + c * sin * sin
for a,b,c constants.

Dividing through by cos * cos

0 = a + b * tan + c * tan * tan

Quadradic formula gives tan(theta), which gives theta. Problem solved.

Cool
Logged
Pishtaco
Level 10
*****


View Profile WWW
« Reply #13 on: November 28, 2009, 06:12:24 AM »

To do wind you need to do drag, which should be proportional to the square of the speed. Probably it's safe to assume the rotation and shape of the object behave themselves in some nice way.

I would just simulate it for lots of angles and save this as a lookup table, or use an AI which can do bracketing. I think both of these things are what artillery uses in practice.
Logged

Sos
Level 8
***


I make bad games


View Profile WWW
« Reply #14 on: November 28, 2009, 06:49:08 AM »

i would do it this way:
Code:
for (bullet_x = me_x; x < target_x; x += SOME_FRACTION)
bullet_y =  me_y +( -BULLET_FLIGHT_HEIGHT * ( bullet_x - me_x ) * ( bullet_x - target_x ));

It's a polynomial function that calculates flight of a bullet on a parabole and you always have a direct hit. I think that's not what you meant, but i think it's easier.
Logged

Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #15 on: December 06, 2009, 06:27:05 AM »

Solution with horizontal wind, flat plane (inclined plan, angled wind is just as easy):
Code:
Initial equations:

distance = speed * cos(theta) * t + 0.5 * wind * t * t
0 = speed * sin(theta) * t - 0.5 * gravity * t * t
t != 0

=>
t = speed * sin(theta) * 2 / gravity
  = k sin(theta)
for k a constant.

Plugging back in, and omitting "(theta)" for brevity:

distance = speed * cos * k * sin + 0.5 * wind * k * k * sin * sin

Pythagoras:
distance = distance * (cos * cos + sin * sin)

So:
0 = - distance * cos * cos + speed * k * cos * sin + (0.5 * wind * k * k - distance)
0 = a * cos * cos + b * cos * sin + c * sin * sin
for a,b,c constants.

Dividing through by cos * cos

0 = a + b * tan + c * tan * tan

Quadradic formula gives tan(theta), which gives theta. Problem solved.

Cool

Cool indeed! Now, what if the source and target are at different heights (say plateaus)?  Gentleman
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #16 on: December 06, 2009, 10:11:46 AM »

It's the same problem with a raised target point. Just rotate the problem until it's horizontal. Doesn't change the problem, as gravity and wind are both constant acclerations, at right angles, so I can express their rotations also as constant accelerations at right angles.

As usual, it's easier to express with vectors.
We're solving:

r= v * t + 0.5 * a * t * t
|v| = speed

For r the final position, v the initial velocity and a the accelaration (wind + gravity).
Dot and cross by r to give two scalar equations

0     = v×r * t + 0.5 a×r * t * t
|r|^2 = v.r * t + 0.5 a.r * t * t

If theta is the angle v makes with r,
v×r = speed |r| sin theta
v.r = speed |r| cos theta

And we can solve from there as before. (probably can solve it a better way now i've switched to vectors, but cannot be bothered.
Logged
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #17 on: December 06, 2009, 02:02:09 PM »

This I will attempt. Muchos thankos.  Hand Any KeyWizard
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic