Addforce Vs. Direct Velocity Setting: Balancing Control, Realism And Cooperativity

When programming physics-based behavior in games and simulations, two primary approaches exist for manipulating rigidbody objects: AddForce and SetVelocity. Applying forces over time with AddForce leads to emergent, realistic motion based on mass and momentum. In contrast, directly setting an object’s velocity with SetVelocity allows for more direct, predetermined control. There are tradeoffs to consider when choosing which one to use for different gameplay scenarios.

The Core Tradeoff: Control vs. Realism

The central tradeoff between AddForce and SetVelocity comes down to control versus realism. Using AddForce results in physics-based behavior that seems more alive and real, with momentum building up over time and conveying a sense of weight and presence. However, it means giving up direct control over the final velocities and trajectories. Conversely, calling SetVelocity allows one to precisely dictate an object’s speed and direction on each frame. But this can look robotic and synthetic if overused, breaking immersion. The two approaches also have implications for predictability versus emergent gameplay.

How AddForce Works

The AddForce method applies a physical force to a rigidbody over one or more frames. This incremental force gets added to the rigidbody’s internal velocity based on its mass, resulting in accelerations that mimic real-world physical reactions. Some key characteristics of using AddForce include:

  • Forces can be applied over multiple frames for continual acceleration
  • Works well for modeling physical interactions between game entities
  • Leads to emergent trajectories and velocities based on incrementally added forces
  • Conveys realistic sensations of weight, momentum and inertia

For example, the following code incrementally adds an upward force to a rigidbody representing a ball, launching it up into the air with a realistic arc:

Rigidbody ballRigidbody;

void Update() {
  ballRigidbody.AddForce(Vector3.up * 5.0f); 
}

Notice how applying the force over multiple frames would incrementally increase the velocity, resulting in a smooth upwards trajectory as momentum builds up. This emergent motion makes the physics seem authentic.

How SetVelocity Works

Unlike AddForce, the SetVelocity method immediately changes the velocity state of a rigidbody to a directly defined vector value. Some characteristics of using SetVelocity include:

  • Velocities can be precisely dictated each frame
  • Better for scripted, predetermined behavior
  • Less realistic motion since no forces are accumulating
  • Can result in robotic, synthetic-looking movement

For example, the following code would instantly set the velocity of the rigidbody to a constant up vector on each frame:

  
Rigidbody ballRigidbody;
  
void Update() {
  ballRigidbody.velocity = Vector3.up * 5.0f;
} 

This would make the ball move straight up at a fixed speed every frame, rather than gaining velocity incrementally based on momentum over time as with AddForce. The result may look less natural depending on the context.

Guidelines for When to Use Each

When should AddForce versus SetVelocity be used? Here are some general guidelines:

  • AddForce for increased realism and emergent gameplay: Use AddForce when you want physics interactions and trajectories to seem authentic, conveying realistic sensations of weight and momentum. This also leads to more emergent gameplay as velocities and motions arise organically from additive forces.
  • SetVelocity for more direct control and predictability: Use SetVelocity when you need precise control over object velocities/rotations each frame, or want predetermined scripted paths. SetVelocity allows implementing behaviors you can directly control vs. AddForce’s emergent realism.

However, many games combine both techniques situationally depending on the desired effect.

Hybrid Approaches

Rather than take an AddForce-only or SetVelocity-only approach, most physics-based games use a combination of the two techniques in different scenarios. Some examples include:

  • Using AddForce for player/entity movements and interactions to feel realistic and responsive
  • Applying SetVelocity on certain objects/components along preset paths
  • Adding “hint forces” with AddForce to nudge objects towards scripted targets

The following code shows an example hybrid approach, applying AddForce on the spaceship main body for emergent physics, while rotation and thruster particles are directly set for more control:

Rigidbody spaceshipMainBody; 

void FixedUpdate() {

  // Add force for emergent bouncing/collisions
  spaceshipMainBody.AddForce(thrust * transform.forward);  

  // Directly set rotational velocity 
  spaceshipMainBody.angularVelocity = calculatedTorque;
  
  // Set particle systems velocity directly
  thrusterParticles.velocity = calculatedDirection * thrustIntensity;

}

This balances realistic primary motion via AddForce, while using SetVelocity on components where precision is more important. Mixing the two can help capitalize on their respective benefits.

Optimizing Performance: Things to Keep in Mind

Both AddForce and SetVelocity impact performance, so when building large physics-based games, optimization is key. Here are some performance considerations:

  • Reduce calls to AddForce/SetVelocity to minimum necessary. Cache and reuse.
  • Favor fewer big AddForce calls over many small ones.
  • For direct control at speed, prefer SetVelocity over AddForce.
  • Use Objects.Destroy() and disabling when possible to reduce active physics objects.
  • Bake simplified meshes rather than per-vertex computations.

By optimizing scenes and being judicious in applying forces/velocities only where needed, fluid simulations supporting vast numbers of concurrent physics objects can be achieved.

Summary and Key Takeaways

In summary, AddForce and SetVelocity represent contrasting approaches of trading control for realism when programming physics-based behaviors. Key takeaways include:

  • AddForce gives emergent, realistic trajectories through incremental momentum
  • SetVelocity allows direct, scriptable control of velocities each frame
  • Hybrid approaches utilize both situationally for optimal balance
  • Performance optimizations help scale to Complex scenes
  • Choose method based on goals for responsiveness, predictability and authenticity

By understanding these core differences in behavior and performance, game developers can leverage AddForce and SetVelocity appropriately for crafting immersive, responsive physics gameplay.

Leave a Reply

Your email address will not be published. Required fields are marked *