Skip to content

Commit

Permalink
PhysicsRigidBody: cache body interface, body ID, and motion properties
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 20, 2024
1 parent 4413bb6 commit db8e2c9
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions library/src/main/java/com/jme3/bullet/objects/PhysicsRigidBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,26 @@ public class PhysicsRigidBody extends PhysicsBody {
// *************************************************************************
// fields

/**
* jolt-java interface for modifications, or null if not added to a space
*/
private BodyInterface bodyInterface;
/**
* copy of the mass (>0) of a dynamic body, or 0 for a static body
*/
protected float mass = 1f;
/**
* jolt-java body ID, or -1 if not added to a space
*/
private int bodyId = -1;
/**
* underlying jolt-java object
*/
private MutableBody joltBody;
/**
* jolt-java motion properties, or null if not created
*/
private MutableMotionProperties motionProperties;
/**
* temporary storage for properties
*/
Expand Down Expand Up @@ -141,6 +153,9 @@ public PhysicsRigidBody(CollisionShape shape, float mass, Vec3d location,
super.setCollisionShape(shape);
this.mass = mass;
this.joltBody = createRigidBody(shape, mass, location, orientation);

this.bodyId = joltBody.getId();
this.motionProperties = joltBody.getMotionProperties();
}
// *************************************************************************
// new methods exposed
Expand Down Expand Up @@ -181,15 +196,14 @@ public Vector3f getAngularVelocity(Vector3f storeResult) {
assert isDynamic();
Vector3f result = (storeResult == null) ? new Vector3f() : storeResult;

MutableMotionProperties properties = joltBody.getMotionProperties();
if (properties == null) {
if (motionProperties == null) {
Vec3d vec3d = new Vec3d();
snapshot.getAngularVelocity(vec3d);
result.set((float) vec3d.x, (float) vec3d.y, (float) vec3d.z);
} else {
MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
properties.getAngularVelocity(fvec3);
motionProperties.getAngularVelocity(fvec3);
result.set(fvec3.getX(), fvec3.getY(), fvec3.getZ());
}

Expand All @@ -207,13 +221,12 @@ public Vec3d getAngularVelocityDp(Vec3d storeResult) {
assert isDynamic();
Vec3d result = (storeResult == null) ? new Vec3d() : storeResult;

MutableMotionProperties properties = joltBody.getMotionProperties();
if (properties == null) {
if (motionProperties == null) {
snapshot.getAngularVelocity(result);
} else {
MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
properties.getAngularVelocity(fvec3);
motionProperties.getAngularVelocity(fvec3);
result.set(fvec3.getX(), fvec3.getY(), fvec3.getZ());
}

Expand Down Expand Up @@ -260,15 +273,14 @@ public Vector3f getLinearVelocity(Vector3f storeResult) {
assert isDynamic();
Vector3f result = (storeResult == null) ? new Vector3f() : storeResult;

MutableMotionProperties properties = joltBody.getMotionProperties();
if (properties == null) {
if (motionProperties == null) {
Vec3d vec3d = new Vec3d();
snapshot.getLinearVelocity(vec3d);
result.set((float) vec3d.x, (float) vec3d.y, (float) vec3d.z);
} else {
MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
properties.getLinearVelocity(fvec3);
motionProperties.getLinearVelocity(fvec3);
result.set(fvec3.getX(), fvec3.getY(), fvec3.getZ());
}

Expand All @@ -287,13 +299,12 @@ public Vec3d getLinearVelocityDp(Vec3d storeResult) {
assert isDynamic();
Vec3d result = (storeResult == null) ? new Vec3d() : storeResult;

MutableMotionProperties properties = joltBody.getMotionProperties();
if (properties == null) {
if (motionProperties == null) {
snapshot.getLinearVelocity(result);
} else {
MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
properties.getLinearVelocity(fvec3);
motionProperties.getLinearVelocity(fvec3);
result.set(fvec3.getX(), fvec3.getY(), fvec3.getZ());
}

Expand All @@ -312,9 +323,6 @@ public Vector3f getPhysicsLocation(Vector3f storeResult) {

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
BodyInterface bodyInterface
= ((PhysicsSpace) getCollisionSpace()).getBodyInterface();
int bodyId = joltBody.getId();
bodyInterface.getCenterOfMassPosition(bodyId, fvec3);
result.x = fvec3.getX();
result.y = fvec3.getY();
Expand All @@ -336,9 +344,6 @@ public Vec3d getPhysicsLocationDp(Vec3d storeResult) {

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena);
BodyInterface bodyInterface
= ((PhysicsSpace) getCollisionSpace()).getBodyInterface();
int bodyId = joltBody.getId();
bodyInterface.getCenterOfMassPosition(bodyId, fvec3);
result.x = fvec3.getX();
result.y = fvec3.getY();
Expand Down Expand Up @@ -442,6 +447,8 @@ public void rebuildRigidBody() {

CollisionShape shape = getCollisionShape();
this.joltBody = createRigidBody(shape, mass, location, orientation);
this.bodyId = joltBody.getId();
this.motionProperties = joltBody.getMotionProperties();
if (logger2.isLoggable(Level.INFO)) {
if (oldBody != null) {
logger2.log(
Expand Down Expand Up @@ -481,8 +488,9 @@ public void reposition(Vec3d location, Quaternion orientation) {
}

CollisionShape shape = getCollisionShape();
Vec3d vec3d = new Vec3d(location);
this.joltBody = createRigidBody(shape, mass, vec3d, orientation);
this.joltBody = createRigidBody(shape, mass, location, orientation);
this.bodyId = joltBody.getId();
this.motionProperties = joltBody.getMotionProperties();
if (logger2.isLoggable(Level.INFO)) {
if (oldBody != null) {
logger2.log(
Expand Down Expand Up @@ -514,14 +522,10 @@ public void setAngularVelocity(Vector3f omega) {
Validate.finite(omega, "angular velocity");
assert isDynamic();

PhysicsSpace physicsSpace = (PhysicsSpace) getCollisionSpace();
if (physicsSpace == null) {
if (bodyInterface == null) {
Vec3d vec3d = new Vec3d(omega);
snapshot.setAngularVelocity(vec3d);
} else {
BodyInterface bodyInterface = physicsSpace.getBodyInterface();
int bodyId = joltBody.getId();

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena, (float) omega.x, (float) omega.y,
(float) omega.z);
Expand All @@ -539,13 +543,9 @@ public void setAngularVelocityDp(Vec3d omega) {
Validate.nonNull(omega, "angular velocity");
assert isDynamic();

PhysicsSpace physicsSpace = (PhysicsSpace) getCollisionSpace();
if (physicsSpace == null) {
if (bodyInterface == null) {
snapshot.setAngularVelocity(omega);
} else {
BodyInterface bodyInterface = physicsSpace.getBodyInterface();
int bodyId = joltBody.getId();

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena, (float) omega.x, (float) omega.y,
(float) omega.z);
Expand Down Expand Up @@ -600,14 +600,10 @@ public void setLinearVelocity(Vector3f velocity) {
Validate.finite(velocity, "velocity");
assert isDynamic();

PhysicsSpace physicsSpace = (PhysicsSpace) getCollisionSpace();
if (physicsSpace == null) {
if (bodyInterface == null) {
Vec3d vec3d = new Vec3d(velocity);
snapshot.setLinearVelocity(vec3d);
} else {
BodyInterface bodyInterface = physicsSpace.getBodyInterface();
int bodyId = joltBody.getId();

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena, (float) velocity.x, (float) velocity.y,
(float) velocity.z);
Expand All @@ -625,13 +621,9 @@ public void setLinearVelocityDp(Vec3d velocity) {
Validate.nonNull(velocity, "velocity");
assert isDynamic();

PhysicsSpace physicsSpace = (PhysicsSpace) getCollisionSpace();
if (physicsSpace == null) {
if (bodyInterface == null) {
snapshot.setLinearVelocity(velocity);
} else {
BodyInterface bodyInterface = physicsSpace.getBodyInterface();
int bodyId = joltBody.getId();

MemorySession arena = PhysicsSpace.getArena();
FVec3 fvec3 = FVec3.of(arena, (float) velocity.x, (float) velocity.y,
(float) velocity.z);
Expand Down

0 comments on commit db8e2c9

Please sign in to comment.