Skip to content

Commit

Permalink
PhysicsRigidBody: implement the friction parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 20, 2024
1 parent 1923aef commit 25e1374
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ public Vec3d getAngularVelocityDp(Vec3d storeResult) {
return result;
}

/**
* Return the body's friction parameter.
*
* @return the parameter value (≥0)
*/
public float getFriction() {
float result = joltBody.getFriction();
assert result >= 0f : result;
return result;
}

/**
* Copy the linear velocity of the body's center of mass. The body must be
* in dynamic mode.
Expand Down Expand Up @@ -441,6 +452,18 @@ public void setCollisionShape(CollisionShape desiredShape) {
rebuildRigidBody();
}

/**
* Alter the body's friction.
*
* @param friction the desired friction value (≥0, default=0.5)
* <p>
* Note: the jolt-java default is 0.2 .
*/
public void setFriction(float friction) {
Validate.nonNegative(friction, "friction");
joltBody.setFriction(friction);
}

/**
* Alter the linear velocity of the body's center of mass.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class RigidBodySnapshot {
// *************************************************************************
// fields

/**
* friction parameter
*/
private float friction;
/**
* angular velocity (in physics-space coordinates)
*/
Expand All @@ -71,6 +75,7 @@ public class RigidBodySnapshot {
* Instantiate a snapshot with default parameters.
*/
public RigidBodySnapshot() {
this.friction = 0.5f;
this.angularVelocity = new Vec3d();
this.linearVelocity = new Vec3d();
}
Expand All @@ -81,6 +86,9 @@ public RigidBodySnapshot() {
* @param body the body to capture (not null)
*/
public RigidBodySnapshot(PhysicsRigidBody body) {
this.friction = body.getFriction();

// Vec3d
if (body.isDynamic()) {
this.angularVelocity = body.getAngularVelocityDp(null);
this.linearVelocity = body.getLinearVelocityDp(null);
Expand All @@ -98,6 +106,7 @@ public RigidBodySnapshot(PhysicsRigidBody body) {
* @param body the target body (not null, modified)
*/
public void applyTo(PhysicsRigidBody body) {
body.setFriction(friction);
if (body.isDynamic()) {
body.setAngularVelocityDp(angularVelocity);
body.setLinearVelocityDp(linearVelocity);
Expand Down

0 comments on commit 25e1374

Please sign in to comment.