Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 15th Jul 2006 11:44
In a game i'm making, I need to have a targeting system for my tanks. I need to find the two angles which will hit a point. The equation only needs to be 2D eg.


I need to find the two different angles from the green tank that will hit the red cross when a shell is fired at that angle. The speed of the shell when fired is constant, there is no air-resistance and gravity is a constant value applied to the Ymotion every loop.

If anyone knows the equation, plz help.
Thanks

There are three types of people, those that can count and those that can't.
Pasky
18
Years of Service
User Offline
Joined: 7th Jun 2005
Location: Spain
Posted: 15th Jul 2006 14:07 Edited at: 15th Jul 2006 14:08
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 15th Jul 2006 16:48
No, I need it the other way around. I need to find the angle based on X and Y points that it should travel through.

There are three types of people, those that can count and those that can't.
Teh Go0rfmeister
20
Years of Service
User Offline
Joined: 17th Aug 2003
Location:
Posted: 15th Jul 2006 17:12 Edited at: 15th Jul 2006 17:12
hmm this is a tough one. if the land was flat or at least with a constant gradiant then i'd be able to help, but with the enemy tank being at a variable height, makes things difficult. i'd just use trial and error. send out a series of invisible shells ay increasing angles, and see which one hits, remember that angle, and then fire a real shell the same way.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 15th Jul 2006 17:29
Actually, I just made something which works reasonably well. Click to see angle, press space as well to fire.




Set it to full screen exclusive - 640x480

There are three types of people, those that can count and those that can't.
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 15th Jul 2006 18:12
you might want to check here for ballistic trajectories.
http://forum.thegamecreators.com/?m=forum_view&t=78132&b=6
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 15th Jul 2006 18:33 Edited at: 15th Jul 2006 18:34
See attached for the equation you need!

Theta is the angle you need, R is the range to the tank, g is the acceleration due to gravity (in real life -9.8ms-2, but you'll need to work it out for your game - it needs to be negative), v is the velocity at which your shell leaves the tank.

Trust your resident physicist!

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001

Attachments

Login to view attachments
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 15th Jul 2006 19:43
Thanks, but that only works for the X, I need the Y as well.

There are three types of people, those that can count and those that can't.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 16th Jul 2006 02:26
Uh, if there's only two points then there's only one angle between them.. try firing it at angle 'theta' (that 8-like thing) and see what happens.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 11:08
No, that equation only works if the two tanks are at the same height. Also there are two different angles, one where you shoot high into the air, so that it comes down on the enemy, the other is when you shoot almost directly at the other tank. I will try to change the equation to work for different heights.

There are three types of people, those that can count and those that can't.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 16th Jul 2006 11:13
R is your range, i.e. your difference in x-values. This you know. The equation gives you the angle (from horizontal) at which to fire the shell. Your constant acceleration due to gravity will put the bend in the y-axis.

As Zotoaster said, you only need 1 angle!

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 11:37
I know that I only need 1 angle but there are two angles which will hit the tank.

Also, the measurement with the ? is the one not included in your equation
It doesn't matter if I can only get one of the angles.

There are three types of people, those that can count and those that can't.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 16th Jul 2006 13:11
Sin-1 always has two solutions: 1 gives you the direct shot, one the mortar shot - two for the price of one!

However, you are right, the equation above is for a target in the same y-plane as the tank.

The correct equation, taking into account a difference in height, deltah, where the target is deltah higher than the firing tank, is attached.

x is the distance across (similar to Range in the previous equation, however, strictly speaking Range is a maximum in the same plane, and x is the distance in the y-plane to the target projected onto the y-plane). v is again your firing velocity and g your gravity deceleration.

Your best bet is to set this up in a function that will solve it numerically. (It is possible to solve absolutely, but my algebra is a bit rusty! Numerically is a lot easier, but if you can invest the time in an absolute solution, the speed reward could be worth it!)

E.g.
ComputeFiringAngle(InitalVelocity,Gravity,HeightDif)
which will return a string of "angle1,angle2", which you can then split for the two firing solutions.

If you get into problems, let me know and I'll work something up for you - it's time I dusted of my old MPhys cap!

"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library

First you Dream, then you ... - Neil Gaiman, 2001

Attachments

Login to view attachments
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 14:25
Thanks I'll try and do something with this.

There are three types of people, those that can count and those that can't.
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 16th Jul 2006 15:39 Edited at: 16th Jul 2006 15:42
Quote: "If anyone knows the equation, plz help."


The equation of trajectory is



where x / y are offsets in the two axes from the starting point, u is the blastoff speed in ms-1, and g is the strength of gravity (normally 9.81).

To solve for theta you'll need to use the substitution sec^2 = tan^2 + 1 which gives you a quadratic in tan(theta)....



then simply using the quadratic formula should give you to results you're after.

Nice. I've just done loads of exams in this recently, its not pleasant

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 17:17
In code plz? And what do you mean
Quote: "then simply using the quadratic formula should give you to results you're after"

It doesn't seem so simple

I think i'll use the other way until I understand that

There are three types of people, those that can count and those that can't.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 16th Jul 2006 17:20
I think what David T is trying to saying is



Hallowed are the ori.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 18:46 Edited at: 16th Jul 2006 18:47
I just did
Here is the program I used to test the formulas. Click and hold to view angle and gun. Press space as well to fire a shell. As you will see, this works for most of the positions; however, the gun will not tilt lower than 90°. I have tried everything, and I cannot get it to go lower

Oh well everything else works




There are three types of people, those that can count and those that can't.
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 16th Jul 2006 19:26
Quote: "In code plz? And what do you mean"


You wanted the equation to give you the angles for a given 2d position and blastoff speed - I gave it

The first equation is your standard equation of trajectory. allows you to plot the path of the shell by giving you a y value for a given x value. There the angle is a constant.

So with a bit of algebraic jiggery pokery you can rearrange the equation into



That is a quadratic equation in tan(theta). To solve quadratics of hte form

ax^2 + bx + c = 0

use the quadratic formula:



here, our value for a is (gx^2 / 2u^2), b is x and c is ((gx^2/2u^2) + y). So bung those values into the above equation and you'll get a nice big equation to give you the values of tan(angle) where angle is the firing angle. So, rather than simply give a formula I gave you to derivation too - two for the price of one (it's easy enough to derive the equation of trajectory too...)

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Jul 2006 19:48
Sorry, I still don't know what you mean - I think when you say tan2 you mean tan^2+1 but wahts with the quadratic wotnot? I'm only used to tan/cos/sin and I only found out what . meant not long ago. It took me a while to find out what Δ meant as well. But with your x =

-b (+/-)sqrt(b^2 - 4 * a * c)
_______________________________________
2a

I haven't a clue what you're on about!

Have you tried my code above, that works.

There are three types of people, those that can count and those that can't.
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 16th Jul 2006 22:20
Just wondering, what standard is your albegra?

Quote: "Sorry, I still don't know what you mean - I think when you say tan2 you mean tan^2+1"


When I write tan2 it's just me being lazy. I mean tan * tan, or tan^2.

If you look at the original equation of trajectory there's a sec^2(theta) term in there - not good as the rest of the expression has 'tan' in. We can use the relationship



to convert all terms with theta in to tan's. Then it's a simple matter or rearranging to get a quadratic equation in tan.

but wahts with the quadratic wotnot?

A quadratic equation is an equation where the term you wish to find exists in both its squard and normal form. You can't isolate the term as you normally would so you either factorise or use the quadratic formula to solve the equation.

Quote: "I only found out what . meant not long ago"


I just wrote . because I was being lazy - it's the same as x or *.

Quote: "-b (+/-)sqrt(b^2 - 4 * a * c)
_______________________________________
2a

I haven't a clue what you're on about!"


I was just trying to be artistic and represent a fraction in text form. Here's the same formula but written properly...



So for any equation of the form

ax^2 + bx + c = 0

you put values of a,b,c into the above and two possible solutions for x come out. The equation above is of the same form, except that instead of 'x' we have 'tan(theta)'.

You can equally use formulas and code given to you like above, I was just trying to give you a derivation and solution the way most mathmaticians might approach the problem.

CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 16th Jul 2006 23:26
here is a web site to teach you ballistic trajectories.
http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html

And finally i insist looking at the codebase for DBPro converted sample, which gives you angle, flight time, range..etc.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Jul 2006 15:51
Thanks David T, you're much better than my maths teacher

Based on your equation and your teachings, I have come up with this code. It now completely works and does not use trial and error. Also, you can choose if you want a direct shot or a lob. You can choose the velocity of the shell, and the shell will always hit the mouse if it is in range



^
run in 640*480

If you knew my age, you'd realise why I didn't understand that at first.
Thanks again for explaining it.

There are three types of people, those that can count and those that can't.
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 17th Jul 2006 20:59 Edited at: 17th Jul 2006 21:05
Ace, I especially like that solve quadratic function. Good stuff!

If you're interested you can predetermine if there's only one way to hit the target (i.e. any higher an angle / lower an angle results in a miss). Here the target point is known as being on the 'envelope of projection' - an invisible curve within which all points are hittable. Any point which lies on this curve can only be reached with one angle.

If you look at the quadratic formula, the bit that gives you two roots is the +/- square root bit. The bit inside the square root is b^2 - 4ac, also know as the discriminant.

If the discriminant is greater than 0, the target is within the envelope and can be hit two ways.

If the discriminant is zero, then the target is on the envelope and can only be hit one way (is just in range).

If the discriminant is less than zero, then the target is out of range.

Clever how maths works its way into everyday life!

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 18th Jul 2006 00:28
Thanks, I also posted this in the code snippets board so that other people can benefit from it as well Hopefully, it will teach them how it works.

There are three types of people, those that can count and those that can't.

Login to post a reply

Server time is: 2024-05-18 05:53:31
Your offset time is: 2024-05-18 05:53:31