Math problem!

Started by InCreator, Sun 25/12/2005 23:12:46

Previous topic - Next topic

InCreator

Well, at my experiments, I reached the point where my math is absolutely not enough.

Problem, though it's simple, is quite hard to explain. I'll try.



Note: This is not neccessarily AGS related, but more like math and programming overall.

An object needs to move from point A to point B. Simple.

But he needs to follow the arc C to do this.

The distance between A and B is known, but the highest point of arc should be adjustable, so it generation of different arcs would be possible.

A and B are 2-d coordinates. Green lines are just to explain it.

Is there a simple way to do this?

What for, you may ask?
Imagine a random 2D basketball game. How did they make ball fly on arc towards the basket from random position on field?

Or - imagine a random "tank" type game. Like Scorched Earth, Tank Wars, Worms, QBasic Gorillas, etc.


Like those

In these games, you adjusted the angle and velocity but could only guess where shell actually lands.

In my version, you don't know the velocity or angle, but you DO know where the shell lands. A code which is usable for making a perfect AI where computer never misses... etc.

There's no particular project I need this for, but I've always wondered how to do this.

Anyone?

SSH

Like my snowfight game, you mean? ;)

keep an x velocity and y velocity and the y velocity simply decreases by some constant G every game cycle...
12

InCreator

#2
No, not like in your snowball game.

Well, something like this, yes, but imagine now that every ball you throw, DOES hit another.

Point B is always what ball DOES reach, just the arc it takes may vary.

Like, you make AI for computer player and he never misses. And uses different angles to throw. Producing different trajectories of the ball.

So you have to reverse engineer perfect, hitting shots.

How would you code this?

Kweepa

#3
This is a question I sometimes ask interview candidates.
To make it easier, you need to divide the problem up in two ways
- first, divide the arc into an upward and a downward portion
- then divide each part into a vertical and horizontal component






You need to use the equations (1) d = v0*t + 0.5*a*t^2 and (2) v1 = v0 + a*t.





The heights at A, B, and C are hA, hB, and hC. Then the height of the rising arc is hAC and the height of the falling arc is hCB.
Consider vertical component of the rising arc. Using equation (2), hAC = 0.5*g*t1^2, so t1 = sqrt(2*hAC/g).
You can solve for the initial vertical component of velocity using (2) to get sqrt(2*g*hAC).
Similarly for the falling arc, t2 = sqrt(2*hCB/g).
The total time is t1+t2.
If the horizontal distance is dAB, then the horizontal component of velocity (a = 0) from (1) is dAB/(t1+t2).

[EDIT] Fixed initial vertical velocity (was using hCB, now correctly using hAC). Oops.
Still waiting for Purity of the Surf II

Las Naranjas

It's high school calculus really.

....the part of calculus I failed
"I'm a moron" - LGM
http://sylpher.com/novomestro
Your resident Novocastrian.

fred

Hey SteveMcCrea, that's pretty cool. I always wanted to learn this kind of math and physics, but I never had any formal introduction - do you know of any good books? I considered buying "Physics for Game Developers" from the O'Reilly's series, but I'm wondering if it's too high level for someone who took language courses and philosophy instead of math and physics in high school. Do you know that book, or any other good introductions?

Kweepa

fred, it's been a long time since I did dynamics at school, but I did flick through "Physics for Game Developers" and it looked like a decent book.

A quick read of the reviews on amazon suggests that "Physics Modeling for Game Programmers" is a more basic introduction. I hesitate to recommend any book that has typos though.

InC, note that I edited the solution somewhat.
Still waiting for Purity of the Surf II

InCreator

#7
Err.... thanks Steve.

QuoteThis is a question I sometimes ask interview candidates.
Anyone passed yet?   :-\

I actually thought that it can be done much simpler?

When programming a game - even that simple tank-wars type one, all I adjust is X and Y values for the shell. And probably want to modify either "angle" or "velocity" type variables, where changing one should result in approriate another to actually reach the point B.

Your formula... well, I have no clue how x or y changes every step  :(
This is waaaay over my head.

Okay, a simple game. there's "velocity", "angle", "x" and "y" for the shell. And also some kind of "gravity". By setting these values, shell flies off and lands.

Now I want to pre-program a shot. To hit one sure target.

x, y, and gravity are automatic/preset.
I adjust "angle". To something random.

How to get correct "velocity" from this?
and vice versa?





Ginny

Steve, those equations are right out of my physics book. :D For some reason that struck me as amusing.

Quote from: InCreator on Mon 26/12/2005 14:47:28
Okay, a simple game. there's "velocity", "angle", "x" and "y" for the shell. And also some kind of "gravity". By setting these values, shell flies off and lands.

Now I want to pre-program a shot. To hit one sure target.

x, y, and gravity are automatic/preset.
I adjust "angle". To something random.

How to get correct "velocity" from this?
and vice versa?

If I get this correctly, you want a (hypothetical) game that has the following:
Preset G constant.
X and Y of your object, that can be changed (moving), but let's assume they're preset.
X and Y of point B where you need to get.

This kind of movement (ballistic movement) can probably happen with more than one combination of angle and initial velocity. Also, there is the time factor to consider, a certain combo of angle and velocity will take a certain amount of time, but if you only choose a random angle, there is still more than one option. Should I assume the time is set?
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

Kweepa

#9
Unfortunately, InC, you can't get much simpler than that.
For your reformulation of the problem...

h = v*sin(angle)*t - 0.5*g*t^2     ... (1)
d = v*cos(angle)*t                        ... (2)
t = d/(v*cos(angle))
h = d*tan(angle) - 0.5*g*(d/v*cos(angle))^2
v^2 = g*d^2/[2*cos^2(angle)*(d*tan(angle) - h)]
v = d/cos(angle)*sqrt{g/[2*(d*tan(angle)-h)]}

Note that if h > d*tan(angle), it's impossible. That makes sense because the original angle isn't pointing higher than the end point.

[EDIT] I am an idiot. Fixed equations.

Finally, if you know the velocity and want the angle:

Rearranging (2) gives:
cos(angle) = d/(v*t)                            ... (A)
=> sin(angle) = sqrt(1 - (d/(v*t))^2)
Substituting into (1) gives:
h = v*sqrt(1 - (d/(v*t))^2)*t - 0.5*g*t^2
=> h = sqrt(v^2*t^2 - d^2) - 0.5*g*t^2
=> (h + 0.5*g*t^2)^2 = v^2*t^2 - d^2
This gives a quadratic equation in t^2 which can be solved and substituted back into (A) to get (angle).
Still waiting for Purity of the Surf II

evenwolf

Hey Steve McCrea.   I live in Austin too.  Fancy that!
"I drink a thousand shipwrecks.'"

Kweepa

Aye, you'll be one of those fancy-schmancy UT film students, eh? :=
Still waiting for Purity of the Surf II

evenwolf

Pegged.

Uh, while I'm not actually in the film school itself I do wrangle them from time to time.

You should go to Open Screen Night at the drafthouse some time.  Its nuts.
"I drink a thousand shipwrecks.'"

SMF spam blocked by CleanTalk