I would like a computer algorithm, preferably in Java or C/C++. Its
kinda physics-like. I'm not sure if this is easy for most
programmers, or easily found on the web. It also should have lots of
comments in the code so I can easily understand and learn from it.
This is for a 2D demo.
There is a heat seeking missile, that has a top (tip) and bottom (base
or thruster). There is gravity, a certain amount of fuel, and a
maximum speed. When first launching this missile it will have a speed
of 0 and steadily increase to the maximum speed and to the target. If
the target moves, the missile will adjust and follow, turning
accordingly. The speed won't always start at 0, it could be fired
from a moving object.
It should act like real missile, but of course doesn't have to be too
real. For example lets say on screen there is a missile facing up on
the ground, and a target helicopter flying across the screen far to
the right. The missile will launch, slowly accelerating, and turning
right towards the copter (like a car, it cannot turn without moving
forward). If the helicopter rises up to avoid it, the missile will
then try to turn upwards. It may miss if the target's angle is too
sharp and have to angle its way towards it. If the copter moves
around enough that the missile wastes its fuel, it will fall
to the ground.
Here is what the Missile object might look like:
class Missile {
double x,y; //Location on screen, ground=y=0, air=negative
double angle; //The angle of the missile, tip and base 0=up
double speed,max_speed;
double acceleration,weight;
//Possibly a lot more variables
void start(int x,int y);
void seek(int x,int y); //Heat seek to this coordinate
}
Thanks! |
Clarification of Question by
evanstaul-ga
on
30 Oct 2002 16:39 PST
I was thinking that this kind of algorithm is very common in games and
could be found on the web somehow in one computer language or another.
So far I can't find it easily on the web. If one of the researchers
were a programmer they could find the algorithm, or something close to
it, and:
1) If it was a different language like Pascal, Basic, then convert it.
2) If it were pieces of a larger code then cut the minimal code and
show it.
3) Or explain/comment it well enough that I could do it myself.
This site contains some interesting behaviors:
http://www.atomicmedia.com/autonomous/
The seek function, probably the only piece of code Ineed to do this,
would be something like:
//Pseudo java code, do the next frame of animation
void seek(int target_x,int target_y) {
//Find the slope of the target to missile
slope_x=target_x/x;
slope_x=target_y/y; //More stuff than this
//Angle towards target
if(angle>slope??) angle++;
//Detection
if(y>=0) explode=true; //Hit floor
els if(x==target_x&&y==target_y) explode=true; //Hit target
//Add gravity
y=y+GRAVITY;
}
Of course thats wrong, but I imagine the function would be kinda like
that. So, I'm not expecting any graphics code or anything just the
basic physics of it. I estimate it will go around one page of code.
Pseudo code is fine too, but Java or C is preferred, because most of
the time when people write pseudo code it doesn't translate smoothly
to real code.
|