- Home /
How to do inertia damping
If you have ever played Space Engineers you shoudl be familiar with how inertia damping works, if not, its basically a toggle mechanism that allows you to cancel undesired energy(any energy that is not the one that not in the direction you are heading. if not pressing any movement key, it cancels all energy). this works in space where there should be low friction. i can't just add a certain amount of drag to cancel most of undesired movement since that would also cancel all the desired energy. so that prety much leaves me with coding and i have no idea on how to do it. please not I am not asking nor wanting any of you to write a script for me but simply to give me ideas on how to do this since I dont have a clue. thanks to all, i appreciate all your help.
Answer by supernat · May 26, 2014 at 04:50 AM
I think you have to code this either way, but there's no limitation of the engine that would prevent you from just increasing drag. SE actually applies forces in the opposite directions with varying thruster flame sizes, so that wouldn't be so easily simulated by just increasing drag.
It's really fairly simple. You just calculate your forward moving velocity (let's assume for a minute that you can't move the camera, it always faces forward), then compute the x and y values in the local body coordinates that make up that velocity. For instance, let's say your velocity in world coordinates is 10.0, 0, 10.0, so you are moving at a 45 degree angle down the X/Z plane. You just need to apply -10.0 force in the world X direction at the same time applying -10.0 force in the world Z direction (i.e. just negate the velocity vector (oh but also do it in small amounts, otherwise it would stop immediately)). But if you want it to look realistic or handle moving in one direction while pointing in a different direction, you have to figure out what thrust amounts forward, right, and down (from your body's perspective) would equivalence to -10.0, -10.0 X/Z thrust. I hope that makes sense.
It's just a coordinate transform at that point. If you make the forward velocity (in world coordinates) a unit vector (i.e. a direction cosine), you can rotate that vector by applying the inverse transform for the body to world matrix. Or just multiply your vector by the Transform's worldToLocal matrix, which will put your (-10.0, 0, -10.0) world vector into body (local) coordinates. Then you can set the flame size coming out of your thruster pack on the front of your body, the side of your body, and whatever it does for up/down, based on the size of each component in the new vector. You then multiply the new vector (i.e. the body direction cosine vector) by the original forward velocity magnitude, and that will give you the velocity relative to your body. So, for instance if you were moving due north along the +Z axis, but you rolled the camera around to look behind you, you would still be moving in the +Z axis (0, 0, 1) direction cosine. But from your body's (camera's) perspective, you are moving backwards (0, 0, -1).
I know you didn't ask for code, but maybe this will help clear up my mumble jumble.
Vector3 velocity = new Vector3(10, 0, 10);
void Update {
// What direction are we moving?
Vector3 worldDir = velocity.normalized;
// Convert velocity to direction, then from world coordinates to local coords
Vector3 localDir = -transform.InverseTransformDirection(worldDir);
// Do whatever with localDir.x, and localDir.z to simulate thruster fuel burn
velocity -= 0.05f*Time.deltaTime*worldDir;
// Add some logic to check if velocity is near zero and shut down all of this code
}
In this case, I just used the convenience method InverseTransformDirection, and there really wasn't any need to compute a local velocity, reduce it by the local value, and convert that local value back to world coordinates, because it will do essentially the same thing as just reducing the original world velocity by some small amount. For a more realistic appearance, adjust the 0.05f constant based on the thruster outputs and how much inertia you need to dampen. As your velocity approaches zero, you would reduce 0.05 more and more, and as it increases, you would increase it but limit it to some max value as the thrusters can only put out so much force.
Thanks a lot for taking your time to answer my question, it has been really useful.
Your answer
Follow this Question
Related Questions
How to add inertia damping? 1 Answer
What does the "compute mesh inertia tensor failed" error mean? 6 Answers
Why am I being asked to change the mesh geometry or supply a tensor manually? 1 Answer
Healthbar Energy Bar and MiniMaps? 0 Answers
2d rigidbody stops on collision if velocity smaller than 1 1 Answer