- Home /
A Force perpendicular to the Velocity isn't working
I want to apply a force perpendicular to the velocity of the object. (I'm simulating a magnetic field exerting a force on a charge). My code looks like this:
function FixedUpdate () {
this.rigidbody.AddForce(Vector3.Cross(this.rigidbody.velocity, bField));
}
The cross product makes sure the force is perpendicular to the velocity and the bField. I set bField in the Z direction and initial velocity were in the Y direction. If it were working optimally, the direction of the velocity would change but the magnitude of the velocity would stay constant, the object would move in a circle of constant radius in the XY plane. However the object is speeding up, meaning that the force is not exactly perpendicular (even though the Vector3.Cross gives a perpendicular force vector).
Any thoughts on how I can keep the Magnitude of the Velocity constant? thanks
Answer by Owen-Reynolds · Feb 07, 2015 at 11:07 PM
Sounds like the usual discrete steps simulation problem. Like the fundamental theorem of calculus, physics simulations are just breaking a continuous process into small steps. If the steps are small enough, you get very little error. But you're still simulating a curve using a series of tiny straight lines, so you will get wrong numbers.
Standard solution is to reduce the physics timestep, from the standard 0.02 seconds.
That will just give smaller errors, small enough for most people. But I suspect if you let it orbit a few times, it will very slowly grow. If you aren't bouncing and spinning crazily, you don't really need the physics system. If you can get the continuous equations for whatever your system is, can just use those to compute the position each frame.
Shrinking the timestep behaves as you predicted. It looks better but still slowly creeps outward. I can always take drastic measures and adjust the components of the velocity perpendicular to the magnetic field. thanks.