jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.45k stars 419 forks source link

How to set mass? #965

Closed LittleCodingFox closed 6 months ago

LittleCodingFox commented 6 months ago

Hello,

I've been looking around and can't seem to find the API for setting a body's mass, and I noticed the body I created is falling very slowly with a gravity of (0, -9.81, 0).

Is there a way I can either set the mass or make it fall faster?

Thank you!

MrOnlineCoder commented 6 months ago

Hi! From my understanding, you have to specify JPH::MassProperties in body settings during body creation, like:

JPH::MassProperties msp;
msp.ScaleToMass(10.0f); //actual mass in kg

bodySettings.mMassPropertiesOverride = msp;
bodySettings.mOverrideMassProperties = JPH::EOverrideMassProperties::CalculateInertia;

JPH::MassProperties have several methods for setting the mass and the inertia: https://jrouwe.github.io/JoltPhysics/class_mass_properties.html

Respectfully, adjust the EOverrideMassProperties flag if you need to calculate the inertia or not

EDIT: you can also change the gravity of the physics system itself, which I am currently experimenting with to achieve more dynamic and fast-paced world, although I don't know yet if it affects the simulation in any negative way: https://jrouwe.github.io/JoltPhysics/class_physics_system.html#ad09c12009bbcb0d0e6dbfd9582593be4