NWH Common
Search Results for

    Variable Center of Mass

    Control Rigidbody center of mass and inertia for realistic physics behavior.

    Overview

    VariableCenterOfMass allows precise control over a Rigidbody's:

    • Center of Mass (CoM) - The balance point of the object
    • Inertia Tensor - Resistance to rotation around each axis
    • Mass Distribution - How mass is distributed throughout the object

    Proper CoM setup is critical for realistic vehicle physics.

    Why Center of Mass Matters

    Center of Mass affects:

    • Stability - Lower CoM = more stable, less likely to tip
    • Handling - CoM position affects oversteer/understeer
    • Suspension - CoM determines weight distribution on wheels
    • Rotation - Determines pivot point for physics forces

    Example: A car with high CoM (like an SUV) tips more in corners than one with low CoM (like a sports car).

    Component Setup

    Adding to GameObject

    1. Select GameObject with Rigidbody
    2. Add Component → NWH → Common → Variable Center of Mass
    3. Adjust CoM position and inertia in inspector

    Inspector Fields

    Center of Mass:

    • Position - XYZ offset from GameObject origin (local space)
    • Visualize - Show CoM gizmo in Scene view (yellow sphere)

    Inertia:

    • Inertia Tensor - Resistance to rotation [X, Y, Z]
    • Auto-Calculate - Automatically calculate from colliders
    • Manual Inertia - Manually set inertia values

    Setting Center of Mass

    Visual Adjustment

    With Visualize enabled:

    1. Enter Play mode or enable gizmos
    2. Yellow sphere shows CoM position
    3. Adjust Position values to move CoM
    4. Lower Y value = lower CoM = more stable

    Typical Values

    Cars:

    Position: (0, -0.3, 0)
    // Slightly below origin, centered laterally
    

    Trucks/SUVs:

    Position: (0, -0.1, 0)
    // Higher CoM than cars
    

    Race Cars:

    Position: (0, -0.5, 0)
    // Very low CoM for maximum stability
    

    Bikes/Motorcycles:

    Position: (0, 0.3, 0)
    // Higher CoM, naturally unstable
    

    Effects of CoM Position

    Vertical (Y-axis):

    • Lower (-Y): More stable, less roll, harder to flip
    • Higher (+Y): Less stable, more roll, easier to flip

    Longitudinal (Z-axis):

    • Forward (+Z): Front-heavy, understeer
    • Backward (-Z): Rear-heavy, oversteer

    Lateral (X-axis):

    • Left/Right (±X): Causes vehicle to lean to one side

    Setting Inertia

    Inertia tensor controls rotation resistance around each axis.

    Auto-Calculate

    Most common approach:

    VariableCenterOfMass vcm = GetComponent<VariableCenterOfMass>();
    vcm.autoCalculate = true;
    

    Unity calculates inertia from collider shapes automatically.

    Manual Inertia

    For precise control:

    vcm.autoCalculate = false;
    vcm.inertiaTensor = new Vector3(700, 1400, 400);
    

    Typical Car Inertia (mass ~1400kg):

    • X (Roll): 400-800 - Resistance to rolling (tipping sideways)
    • Y (Yaw): 1200-1800 - Resistance to spinning (turning)
    • Z (Pitch): 400-800 - Resistance to pitching (nose up/down)

    Inertia Effects

    Too Low:

    • Vehicle jitters
    • Floaty, unrealistic motion
    • Over-responsive to forces

    Too High:

    • Sluggish rotation
    • Vehicle feels extremely heavy
    • Unresponsive handling

    Rule of Thumb: Inertia values should be roughly proportional to mass.

    Usage Examples

    Example 1: Standard Car

    GameObject vehicle = ...; // Your vehicle
    Rigidbody rb = vehicle.GetComponent<Rigidbody>();
    VariableCenterOfMass vcm = vehicle.AddComponent<VariableCenterOfMass>();
    
    // Set low CoM for stability
    vcm.centerOfMassOffset = new Vector3(0, -0.3f, 0);
    
    // Auto-calculate inertia
    vcm.autoCalculate = true;
    

    Example 2: High-Performance Race Car

    // Very low CoM
    vcm.centerOfMassOffset = new Vector3(0, -0.5f, 0);
    
    // Manual inertia for precise handling
    vcm.autoCalculate = false;
    vcm.inertiaTensor = new Vector3(600, 1600, 500);
    

    Example 3: Truck

    // Higher CoM (less stable)
    vcm.centerOfMassOffset = new Vector3(0, -0.1f, 0);
    
    // Higher inertia (heavier vehicle)
    vcm.autoCalculate = false;
    vcm.inertiaTensor = new Vector3(1200, 2400, 800);
    

    Example 4: Adjusting CoM for Balance

    If vehicle pulls to one side:

    // Vehicle pulls left, shift CoM right
    vcm.centerOfMassOffset = new Vector3(0.05f, -0.3f, 0);
    
    // Front-heavy (understeer), shift back
    vcm.centerOfMassOffset = new Vector3(0, -0.3f, -0.1f);
    

    Runtime Adjustment

    CoM can be changed at runtime for dynamic effects:

    Load-Based CoM

    Simulate cargo weight:

    public class CargoSystem : MonoBehaviour
    {
        public VariableCenterOfMass vcm;
        private Vector3 baseCoM = new Vector3(0, -0.3f, 0);
    
        public void AddCargo(float cargoMass, Vector3 cargoPosition)
        {
            Rigidbody rb = GetComponent<Rigidbody>();
    
            // Shift CoM toward cargo position
            Vector3 shiftedCoM = Vector3.Lerp(
                baseCoM,
                cargoPosition,
                cargoMass / rb.mass
            );
    
            vcm.centerOfMassOffset = shiftedCoM;
        }
    }
    

    Fuel-Based CoM

    Simulate fuel consumption:

    void UpdateCoMForFuel(float fuelPercentage)
    {
        // Fuel tank at rear
        Vector3 fuelTankPosition = new Vector3(0, -0.2f, -1.5f);
    
        // Shift CoM based on fuel level
        vcm.centerOfMassOffset = Vector3.Lerp(
            baseCoMEmpty,  // Empty tank
            baseCoMFull,   // Full tank
            fuelPercentage
        );
    }
    

    Debugging CoM Issues

    Visualizing CoM

    Enable visualization:

    vcm.visualize = true;
    

    Yellow sphere shows current CoM in Scene view.

    Common Problems

    Vehicle tips over easily:

    • CoM too high → Lower Y value
    • Inertia too low → Increase roll inertia (X-axis)

    Vehicle feels floaty:

    • Inertia too low → Increase all values proportionally

    Vehicle too stable (unrealistic):

    • CoM too low → Raise Y value slightly
    • Inertia too high → Decrease values

    Vehicle spins uncontrollably:

    • Yaw inertia too low → Increase Y-axis inertia

    Vehicle doesn't respond to inputs:

    • Inertia too high → Decrease values
    • CoM position incorrect → Adjust X/Z offsets

    Testing CoM

    Test procedure:

    1. Static Test: Vehicle should sit level on flat ground
    2. Cornering Test: Observe roll amount in turns
    3. Jump Test: Vehicle should pitch/roll realistically in air
    4. Collision Test: Vehicle should react naturally to impacts

    Integration with NWH Assets

    NWH Vehicle Physics 2

    VariableCenterOfMass is used automatically:

    • VehicleController creates it if not present
    • CoM auto-calculated from collider
    • Can be manually adjusted for tuning

    Wheel Controller 3D

    Recommended for standalone WC3D setups:

    // Add to vehicle using WheelController
    VariableCenterOfMass vcm = vehicle.AddComponent<VariableCenterOfMass>();
    vcm.centerOfMassOffset = new Vector3(0, -0.3f, 0);
    

    Dynamic Water Physics 2

    Used for boats/ships:

    // Boat CoM typically at water line
    vcm.centerOfMassOffset = new Vector3(0, 0, 0);
    

    Aerodynamics

    Used for aircraft:

    // Aircraft CoM critical for stability
    vcm.centerOfMassOffset = new Vector3(0, 0, 0);
    // Often at or slightly ahead of wing center
    

    Best Practices

    1. Start with Auto-Calculate: Let Unity calculate inertia, then fine-tune manually if needed
    2. Test in Play Mode: CoM effects are only visible during physics simulation
    3. Lower is Usually Better: For ground vehicles, lower CoM improves stability
    4. Match Real World: Research real vehicle CoM positions for realism
    5. Use Visualization: Always visualize CoM when tuning
    6. Document Values: Note final CoM/inertia values in comments

    Advanced: Physics Behind CoM

    Moment of Inertia Formula

    For a point mass rotating around an axis:

    I = m * r²
    
    Where:
    I = moment of inertia
    m = mass
    r = distance from rotation axis
    

    For complex shapes, Unity calculates from colliders.

    Torque and Angular Acceleration

    Torque = I * α
    
    Where:
    α = angular acceleration (how fast rotation changes)
    

    Higher inertia = more torque needed = slower rotation.

    Parallel Axis Theorem

    Moving CoM changes inertia:

    I_new = I_cm + m * d²
    
    Where:
    I_cm = inertia about center of mass
    d = distance CoM shifted
    

    This is why changing CoM position affects rotation behavior.

    API Reference

    For detailed API documentation, see the API reference in the navigation menu for:

    • VariableCenterOfMass
    • Rigidbody.centerOfMass
    • Rigidbody.inertiaTensor
    • Edit this page
    In this article
    Back to top Copyright © NWH - Vehicle Physics, Aerodynamics, Dynamic Water Physics