Advertisement

Restricting max speed of space ship

Started by April 02, 2003 09:43 PM
18 comments, last by g7tommyB 21 years, 7 months ago
Hello Everyone, Here is my dilema: I calculate the acceleration of my space ship n the following manner (in pseudo-code), where acceleration and velocity are vectors: if(accelerate ON) { velocity = velocity + accelration } else { velocity = velocity } posX = velocity.getX() posY = velocity.gteY() The problem is that I want to cap the velocity at MAX_VELOCITY. What would be the best approach to do this, since if I just put in the following restriction: if (accelerate ON) { if (velocity.getMagnitude() < MAX_VELOCITY) { velocity = velocity + acceleration } } else { velocity = velocity } posX = velocity.getX() posY = velocity.getY() then the Ship will not be able to change direction easily when it is already at maximum speed. I could combat this by separating the components of the velocity vector and capping them individually, i.e. if (velocity.x < MAX_X) velocity.x = velocity.x + accelearation.x } if (velocity.y < MAX_Y) velocity.y = velocity.y + accelearation.y } Even though, the ship behaves a lot better now at maximum speeds, there is a problem of the ship being able to go a lot faster on the diagnal then straight: If MAX_X and MAX_Y are 1, for example, the ship can go maximum of 1 in the X direction, maximum of 1 in the Y direction, but a whooping maximum of sqrt(2) in the XY direction. Does anyone know the way to implement this velocity restriction properly and in an elegant manner??? Thanks a bunch to everyone! TommyB
g7tommyB
I suggest converting to a axis-angle system so that you have a magnitude and a direction- that would be more intuitive...

~V''lion
~V'lionBugle4d
Advertisement
Nope - that is less intuitive for applying physics.
Velocity and acceleration are vectors.
Conversion to axis based system (i.e. direction + magnitude) is out of the question

TommyB
g7tommyB
2 ideas come to mind:

1) Brute force:

if (accelerate ON) {
velocity = velocity + acceleration;
}
if (velocity.getMagnitude() > MAX_VELOCITY) {
velocity = velocity * MAX_VELOCITY / velocity.getMagnitude();
}

2) or, a more "natural" way to restrict velocity is to add friction or damping. I don''t have those equations right now but they''re fairly simple.

Tom
"E-mail is for geeks and pedophiles." -Cruel Intentions
Yup; Paradigm Shift's answer is right on. The brute force approach is bound to look unnatural, but it should work. To use damping,

f = -k * v

You want that, at maximum velocity, the friction force compensates for acceleration, so

m * a = -k * vmax

k = -m * a / vmax

where a is the "undamped" acceleration of the ship, m is its mass and vmax is the maximum velocity.

BTW, you should probably use a variation in time in your equations. i.e.: v = v + a * dt instead of v = v + a. Even if you define dt = 1, it makes more sense and it might be useful later on.
quote:
Velocity and acceleration are vectors.
Conversion to axis based system (i.e. direction + magnitude) is out of the question

Vector addition is also possible in polar coordinates. You only have to use the law of cosines.

Cédric

[edited by - cedricl on April 3, 2003 9:44:39 AM]
Why not velocity=velocity+max_acceleation*(1-velocity/max_velocity)? Perhaps not correct, but the faster you go the less acceleration.
Keys to success: Ability, ambition and opportunity.
Advertisement
Guys,
Thank you for all the great suggestions, but I don''t think you understood me correctly.
I know I can add friction and slow the ship down at certain point, however, the problem occurs when the ship is already travelling at MAX_VELOCITY and the player changes the direction of the sihp''s throttle from let''s say 0 degrees to 30 degrees.
Now the ship should start to move in the direction of it''s new acceleration, however, since the velocity is already at MAX_VELOCITY and since adding the acceleration vector for the 30 degree change will actually increase the magnitude of the final velocity vector - it is not added...
So the ship keeps on travelling in much the same direction, unless of course its throttle is changed by like 90 degrees, in which case adding the acceleration vector to the MAX_VELOCITY vector actually reduces the magnitude of the final velocity vector.
Do you get me?
I looked at all of the suggestions and I am not convinced that any of them will address this particular problem which occurs with slight directional changes when the ship is already travelling at its MAX_VELOCITY...

Thanks a lot for all your comments thus far.

TommyB
g7tommyB
In any game where I have an asteroids style ship flying around, I just add the thrust adjustment to the current velocity vector, then multiply the velocity vector by a constant less than 1.0 every step. (The steps need to be a constant amount of time for equal effect on any machine).

This naturally drives a maximum velocity for the craft, slows the craft to a standstill when not thrusting, and even straightens out the craft's travel path when it executes a turn.

A little math will help you determine what the actual max velocity is based on the thrust, time step, and the constant. A constant closer to 1.0 represents less drag on the craft, obviously.

The thrust and constant apply a balance to the acceleration and max velocity you want to achieve. You want the player to be able to maneuver and adjust well, but not fly too fast.

I haven't proven this mathematically, but I think the thrust vector applied to the current velocity vector int he same direction is the absolute fastest you will get. Executing a turn adds the thrust vector to the velocity vector in that direction, and it could never be as long as applying the thrust to current velocity in the same direction.


[edited by - Waverider on April 3, 2003 2:31:48 PM]
It's not what you're taught, it's what you learn.
Waverider,
Since in space there is no friction, why would you want to slow down your craft to a standstill when not thrusting?
Also, when you say that you apply the thrust difference to the velocity, when the ship is thrusting you are also not really following physics, since you should be applying a "brand new" acceleration vector calculated based on acceleration constant and direction of the ship per every itteration.
I do agree with the timestep multiplication, but I am not sure how this will address the issue of wanting to change the direction of the velocity vector when the velocity vector is already at MAX_VELOCITY and when adding the new acceleration vector will make the magnitude of the velocity vector even bigger...
I am not looking for a solution that is based on a wholly new technique. I am trying to follow the real laws of physics in my game... I wonder how capping the speed of a REAL WORLD space craft can be represented using physics equations...


TommyB
g7tommyB
Ok, I follow. My solution is more of a video gamish style implementation. If you're looking for realism, and yet want to cap the velocity of a ship in a medium that has no air resistance (meaning when you stop thrusting you continue coasting at the same velocity)... hmm...

Then one of the other solutions mentioned, where you just see if the overall velocity is greater than a certain amount, and if so, make it equal, would suffice.

However, if the craft performs a side thrust while moving forward, then you're right, it will be faster than max. But what you can do is take that overall velocity (which will be askew from the forward movement), and you will be able to determine that its length is too long, and perform the adjustment to reduce its length. Just do that anytime you are about to adjust the ship's location, and it should never go over your max velocity, no matter what combinations of movement and thrust are taking place.

Just chopping off the velocity when it hits max isn't exactly how physics works, though.

Maybe you could do a hybrid between the two - there is a resistance when the velocity is faster, but let the thrust off, and the ship slows down to a certain point and then begins to coast?


[edited by - Waverider on April 3, 2003 3:39:16 PM]
It's not what you're taught, it's what you learn.

This topic is closed to new replies.

Advertisement