Force to apply after a deceleration to keep velocity constant

Started by
6 comments, last by Smjert 10 years, 8 months ago

Here is the problem:

I have a spaceship flying into space (3D world), so no gravity, no frictions etc, with a certain velocity and direction (heading).

The spaceship changes heading, the velocity direction clearly remain the same, then i apply a force of X Netwon in the opposite direction of the velocity. Now i want to apply a force in the heading direction so that it counterbalance the deceleration due to the previous force.. meaning that the velocity length vector has to be constant (but clearly the direction won't be).

It "should" be simple but for the life of me i can't get the amount of Netwon i have to give.

Advertisement

I think what you need to make sure is that the total force applied is perpendicular to the velocity. From that, all you need to do is compute the projection of the heading direction onto the velocity direction and scale the force applied in the heading direction so its projection is X Newton (with the appropriate sign).

EDIT: The answer should be something like X/cos(velocity,heading) Newton.

I think what you need to make sure is that the total force applied is perpendicular to the velocity. From that, all you need to do is compute the projection of the heading direction onto the velocity direction and scale the force applied in the heading direction so its projection is X Newton (with the appropriate sign).

EDIT: The answer should be something like X/cos(velocity,heading) Newton.

So let me write this in Unity (C#) code and see if i understood it right (just testing there):


Vector3 currentVelocityDir = gameObject.rigidbody.velocity.normalized;
Vector3 currentHeadingDir = transform.forward; // This is kept up to date by Unity
float objMass = 10;
gameObject.rigidbody.AddForce(objMass * (-currentVelocityDir)); // Force to decelerate at 1 m/s^2

float cosAngle = Mathf.Cos(Vector3.Angle(currentVelocityDir, currentHeadingDir));

gameObject.rigidbody.AddForce((objMass / cosAngle) * transform.forward);

So this should be right? Because i'm not getting the correct result (the obj accelerates then suddenly lose a lot of speed, then accelerates again etc).

EDIT: Maybe i should add a bit more information, basicly i'm trying to simulate some assit engines that helps the spaceship to bank like an airplane but in space, but this engines have limited power. So the idea is that until the velocity direction doesn't correspond to the heading (or basicly the difference is less than some error value) i simulate an engine that decelerate on the opposite direction of the velocity and the main engines accelerating back to have constant speed, in the spaceship heading direction. Evochron uses this idea for instance (but i'm not sure about the method).

Computing the cosine of the angle is wasteful, since the angle is probably obtained as the arccosine of a dot product:

cos(angle(a,b)) = dot(a,b)/sqrt(dot(a,a)*dot(b,b))

If your vectors are both normalized (it's not entirely clear from your code that the heading is normalized), it's simply dot(a,b).

Plug in some values and see what the intermediate computations do. Is the total force perpendicular to the velocity?

Yes transform.forward is normalized.

I retried to write it with the dot product Vector3.Dot(currentVelocityDir, transform.forward); and using that value to divide the X Netwons and.. it worked!

Or at least this is what it looks like even if, and i think this is due to rounding errors etc, i have a 0.001 increase of speed every second.

The dot between the velocity and the total force is not always 0 (for instance 7.723626E-08), but still a very small value and that's why there's that increase in speed i suppose.

And to keep velocity really constant i have to set it manually if > then expected one.

Even if the vector is perfectly perpendicular, the integrator you are using might end up increasing the speed. For instance, if you use the very basic Euler integration scheme, you would compute

new_velocity = old_velocity + dt * acceleration

In that case the new speed would be sqrt(old_speed^2 + dt^2*length_of_acceleration^2), which is systematically larger than old_speed.

I was thinking out loud hehe, but thank you for your help.

Now i'm left with calculating the correct amount of deceleration needed for a particular turn, but that should be easy with circular motion equations.

Ok unfortunately the method i use is not the correct one.

I mean the idea of slowing down the current velocity and then give thrust with the main engines in the heading direction doesn't make the velocity turn beyond a certain point and it will never make the velocity vector near enough to the heading.

What i should'have though earlier is: what is the "best" force that drags the velocity vector "near" the heading (especially since in the end i want a circular motion)? The centripetal force! I simulate one using the right local vector (because i'm currently dealing only with right turns tongue.png).

Now the problem though is to calculate the component of that force vector that acts as a deceleration for the current velocity.

What i thought is to obtain the magnitude of that deceleration vector doing dot(-velocityDir, forceVector.normalized) and then multiplying it to the magnitude of forceVector.

Then as earlier i do dot(velocityDir, headingDir) and then use it to divide the previous calculated magnitude and then use the result for the force with heading direction.

It kinda works, but the speed grows a bit too fast (like 10mm/s) so i think there's something wrong.

I've checked that the dot product between the resultant force and the heading direction is always around 0.

Also probably this doesn't support the case where the angle between the velocityDir and the centripetal force applied is > 180, where i have probably to flip some signs or the like..

This topic is closed to new replies.

Advertisement