Advertisement

Calculating Maximum Velocity

Started by January 18, 2001 03:50 PM
4 comments, last by FrigidHelix 24 years ago
My project has a ship floating in space. It has two velocities, lets call them: double velocityX; double veolocityY; These are the X and Y components of the ship''s speed respectively. I also define the maximum velocity that the ship can fly at: double maxVelocity; Now, when I accelerate, I want to make sure the new speed does not exceed the maximum speed. The way I currently do it, I use a square root. Can this code be optimized for speed, since I know square roots are not very fast.
  
double currentVelocity = sqrt(velocityX*velocityX + velocityY*velocityY);

// Going too fast. Scale the speed down to maxVelocity

if(currentVelocity > maxVelocity)
{
  // Recalculate Y velocity

  velocityY = velocityY * maxVelocity / currentVelocity;
  velocityX = velocityX * maxVelocity / currentVelocity;
}
  
One obvious optimization would be to compare currentVelocitySquared to maxVelocitySquared to see if speed needs to be scaled. But this only saves time when the ship is going slowly. Is there some way to optimize/improve the use of sqrt?
Keep track of maxVelocity2 where
maxVelocity2 = maxVelocity * maxVelocity;

Your comparison becomes

float currentVelocity2 = velocityX*velocityX +
velocityY*velocityY;
if( currentVelocity2 > maxVelocity2 )
{
...
}


I hope this helps.
himh
Advertisement
Um... how are you calculating xvelocity and yvelocity in the first place? I assume you''re using the sine / cosine of the angle you''re currently facing... if not, how are you allowing your ship to thrust in a particular direction? The way i''ve always done it would simply involve something like:

xv = cos(angle)*c_vel;
yv = sin(angle)*c_vel;

I''m just making that up... but c_vel should be the current velocity... when you thrust up, that should increase... that way all you have to do is check if you''re exceeding the maximum thrust...

if (c_vel > MAX_VEL)
c_vel = MAX_VEL;

Simple... right?

Insomnia
Insomnia
Rather than define the motion by an angle and a velocity, I chose to do it using a vector velocity.

Is it better to do it as an angle and velocity? Input anyone?

Actually, I tried doing it as angle and velocity, but I ran into problems. For example, If I apply thrust in direction A for a certain amount of time, my directional angle is A. However, if I now rotate the ship to new direction B (without applying thrust yet), and then thrust from this new direction, how would I calculate my new direction angle?
The angle isnt B (yet), since I have only thrusted a short amount of time, so I am now heading somewhere between A and B. I didnt know how to solve this using the angle/velocity model.

However, if you have and up velocity and sideways velocity, it is easy to figure out the new velocity. You just add thrust*sin(shipdirection) or thrust*cos(shipdirection).

Edited by - FrigidHelix on January 18, 2001 9:14:49 PM
Hi FrigidHelix,

I''ve been doing exactly the same thing lately. I''m afraid I can''t answer your questions. I couldn''t work out how to calculate the ship''s velocity when storing current velocity and thrust as angle/speed either. Whenever I thought about it I just started thinking in terms of vectors. However, I''ll explain what I did and it may or may not be suitable for your project.

The way I overcame the problem of limiting maximum speed was by adding "friction" to the system. I treat it as a force acting in the opposite direction to the ship''s velocity. The magnitude of this force is proportional to the ship''s velocity and so, once the velocity of the ship is high enough, will equal the force of the thrust, limiting the maximum speed. To calculate it, take a copy of the velocity vector, swap the signs of the X and Y parts, multiply it by a constant (change this constant to vary the amount of friction) and add it to the velocity.

I realise this might sound a bit complicated compared to just limiting the speed but there are very few extra calculations involved and it means everything works "correctly".

In your current system, why does comparing squared velocities only save time when the ship is going slowly?

Hope this helps. I''m by no means an expert, so please treat this as additional input rather than knowledgable advice, but my spaceship is currently zooming around exactly how I want it to and has a nice "realistic" feel to it.
Thanks for all the suggestions. I think I have it working ok now.

This topic is closed to new replies.

Advertisement