Archived

This topic is now archived and is closed to further replies.

The Frugal Gourmet

Convincing AI Spaceship Movement (Arc/Parabola)

Recommended Posts

Hi, I'm not the world's greatest mathematician, but I would love to solve this little pathing/movement problem with my AI spaceship movement. Obviously, to make a space ship or "jet" move realistically, it has to only move forward in the direction that it is facing. Currently, when I move a spaceship in a path from point (x0, y0) to point (x1, y1) I simply move the ship in the direction of its current forward vector and gradually turn the ship to face my target as I move. When I am facing the correct angle, I stop turning and simply move in a straight path. This has the effect of a relatively realistic "arc" ship path (see example I). The ship is always moving forward and always approaching its destination. However, there are some situations wherein I would like the ship to reach its destination REALISTICALLY while at the same time facing a particular direction. In other words, I would like it to always be moving forward, but somehow wind up facing in a particular way (note: I cannot arbitrarily change its angle, or it looks silly). There are two reasons: 1) When space ship passes to edge of the screen, I would like it to be heading directly off the screen for scrolling. 2) When a ship enters orbit around a planet or space station, I would like it enter that orbit already near facing the direction it needs to move in orbit (otherwise an abrupt or unrealistic arbitrary turn is necessary). I realize this exaplantion is a little tricky, so I quickly drew a diagram of what I mean. Assume that I want the ship to facing north as it leaves the screen. Example I is how the movement currently works. Example II is how I would like it to work (always facing a target direction -- due north in this case -- as it leaves the screen). In any case, I hope I explained myself clearly. I would really, really appreciate any help or insight I can get on this matter. Thanks in advance. [edited by - The Frugal Gourmet on January 22, 2004 4:45:08 PM] [edited by - The Frugal Gourmet on January 22, 2004 7:11:47 PM]

Share this post


Link to post
Share on other sites
lets see, from your drawing it seems that your ship is reaching its destination after only turning about 45 degrees
but realistically it should have to turn through a full 90 degress and follow a wider arcing path

dunno if i have a fix, but heres a few observations:

your ideal path, is a circular arc, with a definite center of curvature and radius
real aircraft have maximum and minimum turn radiuses
also, the angles it is facing to begin and end are important, since at all times the facing angle is tangent to your arc
so, some changes of position may not be possible

also, this turn radius model, does not apply in space, since the reason jets are able to remain tangent to their turning arcs is due to wings cutting through air to provide centripetal forces.
in space there is no air to allow such forces, so you either need extra thrusters on the sides of your ship, or you need to over-rotate so that your main engine faces to the outside of your curve

play with Asteroids a bit and youll see the effect, the ship has momentum that needs to be compensated for, so often times your engine is not going to be facing the same way you want to go, since you have to cancel out your momentum first

[edited by - haphazardlynamed on January 23, 2004 7:42:35 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by haphazardlynamed
lets see, from your drawing it seems that your ship is reaching its destination after only turning about 45 degrees
but realistically it should have to turn through a full 90 degress and follow a wider arcing path.


Ok, I realize now that my drawing sucked! What I was trying to show, actually, is that the ship presently only rotates (and thursts forward at the same time, moving in an arc) until it is headed toward its destination target. Then, it moves straight towards it.

However, I would like it to be turning smoothly the enter time so that when it reaches its target direction it is facing some Vector of my choosing (in my example, this is due north).

quote:

your ideal path, is a circular arc, with a definite center of curvature and radius


In this case, yes. However, I feel that it would not always be a circular arc. Suppose I was headed "south" when it started? The movement path would look very un-circular.

The difficulty -- in truth -- is in figuring out some definitive mathematical formula to use. Yesterday, I researched "Hermite Interpolation" a bit because I had read that it smoothly calculates curves between two points giving both a starting and ending vector. However, in my case I seem to have 3 vectors that determine a ship's facing, and I can't seem to compute the ideal path (so far). Maybe I just am not understanding it correctly (I'm not the greatest at math).

quote:

also, this turn radius model, does not apply in space, since the reason jets are able to remain tangent to their turning arcs is due to wings cutting through air ...

play with Asteroids a bit and youll see the effect, the ship has momentum that needs to be compensated for, so often times your engine is not going to be facing the same way you want to go, since you have to cancel out your momentum first




This is an excellent point. If I am thursting in the right direction, I should be able to arbitrarily adjust my facing vector (without thrust) and still be headed in the right direction. I hadn't thought of that. This may be what I wind up doing, since I am struggling with this particular mathematical problem. Since, I can't end my curve facing a paricular direction, at least I should be able to then adjust my facing vector towards it without it looking too silly.


[edited by - The Frugal Gourmet on January 23, 2004 12:58:54 PM]

Share this post


Link to post
Share on other sites
Bucket_Head    308
(Ha, back from hibernation!)

First, I second haphazardlynamed in that if you intend to set this in space, the always-moving-forward bit is not going to be very believable. According to Newton, bodies in motion maintain their velocities unless acted on by a force, and presumably your ships apply forces by thrusting in the direction they''re facing. If your ships can change their translational velocities without spinning and rocketing in a direction away from where they''re moving, to slow down and accelerate in another direction, it''s going to be particularly unrealistic - and you did mention you were going for realism, no?

If you''re going for an atmospheric setting, you''ll have wind resistance and differences in atmospheric pressure come into play, which changes things. As I recall from studying Bernoulli, what keeps planes (and birds) aloft is the fact that the air flowing against the wing is pushed up and over by its curve, which decreases the air pressure above (since it''s moving so fast) while the air pressure below stays the same, and the difference in air pressure is enough that the flyer is pushed up...

To that end, in an atmospheric setting, crafts should roll to the side so that they pitch back to turn - that will look realistic. In this setting it will also look fine and normal for crafts to only move forward in the direction they''re facing at, which is of course very different from the aforementioned space setting.

Now, for working on your math problem, think of it this way - abstractly, what you want is a function that takes inital and destination positions and vectors, and returns a smooth path from A to B. I imagine that ideally you want something that turns at a constant rate until you have reached the both destinaitons, optimally at the same time.

There are a few things we''re going to need to know. Do you want the turns to be at a constant rate for the turn? Is there a single forward speed that you''re always moving at, or can that vary? Is there a maximum turning rate?

If there is a maximum, it is conceivable that there could impossibly tight turns which you could only make by giving yourself a wider radius by going out in a wider turn, starting out by turning towards the opposite direction, then arcing back to your destination.

Another question: is it acceptable to rotate and move so that you end up with the correct final direction and velocity but on a linear path towards your desination position?

- Hai, watashi no chichi no kuruma ga oishikatta desu!
...or, in other words, "Yes, my dad''s car was deliscious!"

Share this post


Link to post
Share on other sites
quote:
Original post by Bucket_Head
First, I second haphazardlynamed in that if you intend to set this in space, the always-moving-forward bit is not going to be very believable. According to Newton, bodies in motion maintain their velocities unless acted on by a force, and presumably your ships apply forces by thrusting in the direction they''re facing. If your ships can change their translational velocities without spinning and rocketing in a direction away from where they''re moving, to slow down and accelerate in another direction, it''s going to be particularly unrealistic - and you did mention you were going for realism, no?


Haha. You got me there. I realize now that "realism" isn''t *exactly* my primary concern. Possibly a better phrase would be "like all the Sci-Fi movies". You know, with the Enterprise or X-wing fighters zipping around in space you do *sometimes* see one change direction while thrusting elsewhere (like occassionally, when they are hit by an enemy weapon). But mainly, I''m concerned with the usual Sci-fi spaceship thing: moving forward in the direction that I am thrusting.
Honestly, I''m way more concerned with looks then real physics, although I admit there is a relationship between the two.

Because this problem appears to be over my head for the meantime, I have settled on a compromise which I am currently using in my game, and it looks decent but not great:

Step 1) Compute a "waypoint" which leads my ship somewhere closer to my destination, but also is the the desired vector away from that destination (which will ultimately lead me to turn more towards that correct vector after the waypoint has been reached).
Step 2) Thrust and turn towards that waypoint.
Step 3) Thrust and turn towards my final destination.
Step 4) I should already be close to facing my desired target vector, but I turn the engine trails/glow off and rotate towards my desired vector without any new thrust...

This has the effect of reaching my destination facing the right target vector in a "somewhat" decent-looking fashion. Adding the waypoint in, makes it a generally smoother-looking path of movement and ensures that I am closer to facing the right direction when I arrive there.

I''ve also experimented with more waypoints, but they haven''t improved things much.

quote:

Now, for working on your math problem, think of it this way - abstractly, what you want is a function that takes inital and destination positions and vectors, and returns a smooth path from A to B. I imagine that ideally you want something that turns at a constant rate until you have reached the both destinaitons, optimally at the same time.

Another question: is it acceptable to rotate and move so that you end up with the correct final direction and velocity but on a linear path towards your desination position?


Yes, I think you''ve defined it well. Also, your question: can I reach the right final direction ahead of time and simply path there linearly? I don''t see why not. I think that would be just fine actually.

As far as your other questions:

"Do you want the turns to be at a constant rate for the turn?"

Yes, I think. Currently, I just find the cross vector between my target and destination and rotate around that vector by a set rate (i.e., 5 degrees per frame) until I reach that angle.

"Is there a single forward speed that you''re always moving at, or can that vary? "

Currently, no. My ship begins at a set velocity, then it decelerates a little as it turns, and then it accelerates a little as it straightens out (I just think this makes it look neater). However, to be honest I have no firm restrictions on this. If it can be done much more easily with a constant speed, then so be it.

"Is there a maximum turning rate?"

Yes, because I assumed a constant rotation rate. I wanted the ship to make no abrupt rotational changes.

"If there is a maximum, it is conceivable that there could impossibly tight turns which you could only make by giving yourself a wider radius by going out in a wider turn, starting out by turning towards the opposite direction, then arcing back to your destination."

Almost certainly.

Anyway, thanks for taking the time to probe my problem, guys. I really appreciate it. Already, I''ve begun thinking about things in a different way...


Share this post


Link to post
Share on other sites