- Home /
Relative Velocity for Spaceship Braking
Hello. I'm building a spaceflight simulator game, and having a maddening problem.
I want my "Brake" function to apply thrust to my ship (which is a Rigidbody) to match the velocity of another object, ReferenceBody (which is not necessarily a Rigidbody, because it might be something huge like a planet that needs a mass>10).
This seems like it should be very simple.
Relative Velocity = My Velocity - Reference Velocity, Thrust Vector = -Relative Velocity
But when I run the game and step on the brakes, ReferenceBody keeps getting farther away, even though after some time holding down the breaks, my velocity reads as equal to ReferenceBody's. Braking slows me down, but then I appear to be stopped in absolute coordinates, not relative ones.
I must be doing something really dumb. I've stripped the operations down to their simplest form and the problem won't go away, but I don't see my mistake. Hopefully it will be obvious to someone else?
Relevant bits of code here:
//ReferenceBody Class
void FixedUpdate() {
//everything here is a Vector3
position = _transform.position;
velocity = (position - lastPosition);
lastPosition = position;
}
//Ship Class
void FixedUpdate ()
{
_velocity = rigidBody.velocity;
_position = rigidBody.position;
_relativePosition = (rigidBody.position - ReferenceBody.position);
_relativeVelocity = (rigidBody.velocity - ReferenceBody.velocity);
}
public void Brake ()
{
Vector3 _reverseVector = -_relativeVelocity;
rigidBody.AddForce (_reverseVector);
}
Thanks for any help!
Answer by max1975 · Mar 01, 2014 at 07:37 AM
I was finally able to fix the problem by using _transform.position-_lastPosition in the ship class to determine my absolute velocity, instead of Rigidbody.velocity.
I still don't understand why this worked. Was Rigidbody.velocity misreporting? Or maybe it is calculated differently, leading to a discrepancy with the ReferenceBody which was not a Rigidbody. But the discrepancy was much larger that I would expect from two different programs trying to arrive at the same number.
You could just have set the velocity to match I guess. I'm wondering if the problem is where Brake is called perhaps?
Also the calculation of the position of the reference body will probably not be correct - the problem being that physics runs before everything else (or not at all) and so if the reference body is not moved by physics or in its FixedUpdate there will be a frame inaccuracy.
Setting the velocity to match sort of worked (I think; it was in one of many iterations of trying different things) but then when I took my foot off the brakes my previous velocity was restored and I'd go slam$$anonymous$$g into the wall of the station where I'd just gently landed.
It almost seems like the physics engine and Transform are actually using different units. After fixing the brakes as described above, I noticed all my velocities were reading much slower than they had previously (though I seemed to be moving the same speed). So I decided to make the ReferenceBody a Rigidbody also, use the Rigidbody.velocity for all the calculations, and solve whatever problems that causes as they arise.
Brake is called (if a key is down) from FixedUpdate() on a controller class.
Thanks for the reply!
Your answer
Follow this Question
Related Questions
How do I make a child stay with parent object? 1 Answer
Relative Movement & Momentum problem - Space sim 1 Answer
How can I roll a sphere relative to the camera's Y axis but not X axis? 1 Answer
Reset position for rigidbody2d 1 Answer
How do i make local velocity?,How do i make velocity move the player the way the object is facing? 1 Answer