Advertisement

Spaceship vector physics

Started by December 28, 2002 01:30 PM
8 comments, last by doctorsixstring 22 years, 1 month ago
I have a spaceship at a given point in 2-D space. The spaceship has a given acceleration and maximum speed. The spaceship''s movement is governed by Asteroids-style "real space physics" in which I use vector math to move the spaceship around. At any given time, the spaceship is probably accelerating towards a destination point. Unfortunately, if I change my spaceship''s destination point while it is en route to its previous destination, the spaceship will never be able to arrive at its new destination. This is because my current AI code tells the ship to simply face its destination and apply full thrust. Thus, the ship will get close to the new destination point, but will never be able to actually get there, due to its previously set vector. My first attempt to correct this problem was to set a "false" destination point. This false destination point is set D pixels away from the ship''s true destination point at an angle of A, where D = ship''s velocity and A = reciprocal of ship''s angle to the true destination point. Unfortunately, that equation fails to take acceleration into effect. So now I am stuck. I haven''t taken a calculus class since last year (my sophomore year in college), so my advanced math is a little rusty. If anyone can give me some pointers/equations/links/etc. I would really appreciate it. Thanks, Mike
The problem is that your acceleration always points towards the destination. You want the acceleration to cancel out the ship''s old velocity while applying the correct velocity to get to the destination. However, unless the velocity and acceleration are exact opposites, the old velocity will never quite cancel out, and you miss the target.

You had the right idea of altering the acceleration vector. However, it is quite difficult to find an exact equation to figure out where to point the acceleration vector, because you also have to cancel out the old velocity while taking into account the new, AND not forget that you only have a certain amount of time to do it and that position changes as a function of time as well. A timestep method would probably be easiest, where you adjust the acceleration each frame to accomodate.
Advertisement
So the best solution would probably be to simply have the ship estimate a good heading every few frames? Thus, as the ship gets closer to the destination, it continues to adjust its thrust/facing so that it will approximately end up at the destination point? I thought about doing it that way, but I was hoping I could find a better way. Unfortunately, I am a programmer first and a mathematician tenth, so this problem is a little beyond me. Does anyone else have any thoughts?

-Mike
The way a human would aim "by eye" would be to look straight ahead, see how far off the target they''re going, and steer sideways until the current velocity points at the destination. Without bothering to calculate any equations, just recalculating the current projected course and modifying the angle of thrust appropriately every so often should give a good approximation.

I notice you don''t mention the ship stopping on arrival... Is this because you didn''t think it worth mentioning, or just don''t intend the ship to do that? If you do have something capable of calculating the accelerations to stop at a location, then you should probably be able to tweak that code to take a non-zero initial velocity into account.
Try to minimize the ''bad'' velocity component at the same time you use the ''good'' velocity component to stear towards your goal.
If you intend to reach your goal with zero velocity you cannot always accelerate towards it. The current midpoint combined with your current ''good'' velocity component can be used for this calculation.
If you intend to reach your goal with non-zero velocity you only need to bother about eliminating the ''bad'' velocity component and use the rest of the acceleration to accelerate towards your goal.


Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Also you could read this topic in this forum: Homing Missile AI in 3space with momentum (hard!)[^]

Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Advertisement
offtopic: where did you get that picture generated from ?
~V''lion

Bugle4d
~V'lionBugle4d
quote:
Original post by Vlion
offtopic: where did you get that picture generated from ?

Start->Programs->Accessories->Paint

MS Paint RuleZ! (or something)

Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Use the "dalleboy" method. It is good and stable.

I use now (for seek action) some like :
vector3 ToTargetVel = BodyVel - TargetVel;
vector3 ToTargetPos = BodyPos - TargetPos;
vector3 DesiredVel = -ToTargetPos;
// DesiredVel can norm to MaxSpeed here
DesiredAcceleration = (DesiredVel - ToTargetVel)*(1.0/DeltaTime);
// where DeltaTime your time step or some part of time to aproximated time to terget.
//------------------------------
To solve your problem exacly you need to solve a 4nd level equation. Or approximate to speed projectile, solve Time To collision , and by this time solve acceleration direction.
I use some close to exac solution in a Pursuit behavior.


quote:
Original post by minorlogic
To solve your problem exacly you need to solve a 4nd level equation. Or approximate to speed projectile, solve Time To collision , and by this time solve acceleration direction.

I also went for a (somewhat) simplified version in my design.

Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement