- Home /
simulate gravity in a 2D game
Is there a way to apply acceleration directly to RigidBody2D.velocity? If not, is there any way to simulate gravity as a gameobject's velocity? I am really new to Unity, and I am making a 2D game where you can use direction keys to change the gravity directions. Since the game is multiplayer, using physics2d.gravity might not be applicable.
What part are you stuck at?
I keep a separate position and velocity pair, and pass that into my acceleration function. Then when I'm done adjusting the position and velocity, I call game_object.transform.position = temp_position;
-- I do not use the transform.velocity variable.
Here is the Runge-$$anonymous$$utta order 4 integrator:
void proceed_rk4(ref tennis_parameters t, ref Vector3 pos, ref Vector3 vel, Vector3 ang_vel, float dt)
{
const float one_sixth = 1.0f / 6.0f;
Vector3 k1_velocity = vel;
Vector3 k1_acceleration = acceleration(ref t, pos, k1_velocity, ang_vel);
Vector3 k2_velocity = vel + k1_acceleration * dt * 0.5f;
Vector3 k2_acceleration = acceleration(ref t, pos + k1_velocity * dt * 0.5f, k2_velocity, ang_vel);
Vector3 k3_velocity = vel + k2_acceleration * dt * 0.5f;
Vector3 k3_acceleration = acceleration(ref t, pos + k2_velocity * dt * 0.5f, k3_velocity, ang_vel);
Vector3 k4_velocity = vel + k3_acceleration * dt;
Vector3 k4_acceleration = acceleration(ref t, pos + k3_velocity * dt, k4_velocity, ang_vel);
vel += (k1_acceleration + (k2_acceleration + k3_acceleration) * 2.0f + k4_acceleration) * one_sixth * dt;
pos += (k1_velocity + (k2_velocity + k3_velocity) * 2.0f + k4_velocity) * one_sixth * dt;
}
And here is the acceleration function:
Vector3 acceleration(ref tennis_parameters t, Vector3 pos, Vector3 vel, Vector3 ang_vel)
{
// $$anonymous$$agnus effect, in metres per second, per second
// http://farside.ph.utexas.edu/$$anonymous$$ching/329/lectures/node43.html
// http://spiff.rit.edu/richmond/baseball/traj/traj.html
Vector3 magnus_accel = Vector3.Cross(vel, ang_vel);
magnus_accel *= 0.5f * t.air_density * t.lift_coeff * t.ball_cross_section / t.ball_mass;
// Wind and drag, in metres per second, per second
Vector3 drag_vel = t.wind_velocity - vel;
Vector3 drag_accel = drag_vel * drag_vel.magnitude * 0.5f * t.air_density * t.drag_coeff * t.ball_cross_section / t.ball_mass;
return t.gravitational_acceleration + magnus_accel + drag_accel;
}
I forgot to mention:
gravitational_acceleration = new Vector3(0, -9.81f, 0);
Answer by tormentoarmagedoom · Feb 18, 2020 at 06:50 PM
Hello.
Simulate gravity is not add a velocity to an object. Is to add a force, in this case to the rigidbody component.
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
This is the correct way to do it, so investigate it and do some tests.
Good luck"!
Thanks for replying, but as I said, I am using RigidBody2D, and using AddForce won't give me an acceleration. Plus, Force$$anonymous$$ode2D doesn't have Acceleration as Force$$anonymous$$ode
Hello.
Gravity is an acceleration, Acceleration is Force*mass. If you apply a force each frame in a direction, it will do the same as the gravity.
As manual of Addforce says: "Force is applied continuously along the direction of the force vector. Specifying the Force$$anonymous$$ode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change."
Bye.
Answer by Sinnamonn · Sep 21, 2020 at 10:07 PM
It is very late for me to answer now, but I just came across the same issue, and was able to fix it Since Force = acceleration * mass, when you use AddForce, the object gets acceleration of Force/mass so if you want to give a specific amount of acceleration, (-9.81 for example), you simply have to multply the force you are sending by the mass of the rigidbody object.
Long story short.
AddForce(rb.mass * GravityVector);
should solve the issue
Answer by Sinnamonn · Sep 20, 2020 at 07:18 AM
It is very late for me to answer now, but I just came across the same issue, and was able to fix it Since Force = acceleration * mass, when you use AddForce, the object gets acceleration of Force/mass so if you want to give a specific amount of acceleration, (-9.81 for example), you simply have to multply the force you are sending by the mass of the rigidbody object.
Long story short.
AddForce(rb.mass * GravityVector);
should solve the issue