Skip to content

Commit

Permalink
library: refactor {add/remove}CollisionObject() to match Minie
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 19, 2024
1 parent 74f60cd commit 0ecdc24
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 30 deletions.
27 changes: 27 additions & 0 deletions library/src/main/java/com/jme3/bullet/CollisionSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
package com.jme3.bullet;

import com.jme3.bullet.collision.PhysicsCollisionObject;
import java.lang.foreign.MemorySession;
import java.util.logging.Logger;
import jme3utilities.Validate;
Expand Down Expand Up @@ -86,6 +87,19 @@ public CollisionSpace(int numSolvers) {
// *************************************************************************
// new methods exposed

/**
* Add the specified collision object to the space.
*
* @param pco the collision object to add (not null, modified)
*/
public void addCollisionObject(PhysicsCollisionObject pco) {
Validate.nonNull(pco, "collision object");

String typeName = pco.getClass().getCanonicalName();
String msg = "Unknown type of collision object: " + typeName;
throw new IllegalArgumentException(msg);
}

/**
* Count the worker threads.
*
Expand Down Expand Up @@ -123,6 +137,19 @@ public static CollisionSpace getCollisionSpace() {
CollisionSpace result = physicsSpaceTL.get();
return result;
}

/**
* Remove the specified collision object from the space.
*
* @param pco the collision object to remove (not null, modified)
*/
public void removeCollisionObject(PhysicsCollisionObject pco) {
Validate.nonNull(pco, "collision object");

String typeName = pco.getClass().getCanonicalName();
String msg = "Unknown type of collision object: " + typeName;
throw new IllegalArgumentException(msg);
}
// *************************************************************************
// new protected methods

Expand Down
98 changes: 68 additions & 30 deletions library/src/main/java/com/jme3/bullet/PhysicsSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
package com.jme3.bullet;

import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
Expand Down Expand Up @@ -225,23 +226,6 @@ public byte getBroadPhaseLayer(short layer) {
// *************************************************************************
// new methods exposed

/**
* Add the specified collision object to the space.
*
* @param pco the collision object to add (not null, modified)
*/
public void addCollisionObject(PhysicsRigidBody pco) {
BodyInterface bodyInterface = getBodyInterface();
int bodyId = (int) pco.nativeId();
float mass = pco.getMass();
if (mass > 0f) {
bodyInterface.addBody(bodyId, Activation.ACTIVATE);
} else {
bodyInterface.addBody(bodyId, Activation.DONT_ACTIVATE);
}
pco.setAddedToSpaceInternal(this);
}

/**
* Register the specified tick listener with the space.
* <p>
Expand Down Expand Up @@ -338,19 +322,6 @@ public float maxTimeStep() {
return maxTimeStep;
}

/**
* Remove the specified collision object from the space.
*
* @param rbc the collision object to remove (not null, modified)
*/
public void removeCollisionObject(PhysicsRigidBody rbc) {
BodyInterface bodyInterface = getBodyInterface();
int bodyId = (int) rbc.nativeId();
bodyInterface.removeBody(bodyId);

rbc.setAddedToSpaceInternal(null);
}

/**
* De-register the specified tick listener.
*
Expand Down Expand Up @@ -502,8 +473,62 @@ public void update(float timeInterval, int maxSteps) {
}
}
// *************************************************************************
// CollisionSpace methods

/**
* Add the specified collision object to the space.
*
* @param pco the collision object to add (not null)
*/
@Override
public void addCollisionObject(PhysicsCollisionObject pco) {
Validate.nonNull(pco, "collision object");

if (pco instanceof PhysicsRigidBody) {
addRigidBody((PhysicsRigidBody) pco);
} else {
super.addCollisionObject(pco);
}
}

/**
* Remove the specified collision object from the space.
*
* @param pco the collision object to remove (not null)
*/
@Override
public void removeCollisionObject(PhysicsCollisionObject pco) {
Validate.nonNull(pco, "collision object");

if (pco instanceof PhysicsRigidBody) {
removeRigidBody((PhysicsRigidBody) pco);
} else {
super.removeCollisionObject(pco);
}
}
// *************************************************************************
// Java private methods

/**
* Add the specified PhysicsRigidBody to the space.
* <p>
* NOTE: When a rigid body is added, its gravity gets set to that of the
* space.
*
* @param rigidBody the body to add (not null, modified)
*/
private void addRigidBody(PhysicsRigidBody rigidBody) {
BodyInterface bodyInterface = getBodyInterface();
int bodyId = (int) rigidBody.nativeId();
float mass = rigidBody.getMass();
if (mass > 0f) {
bodyInterface.addBody(bodyId, Activation.ACTIVATE);
} else {
bodyInterface.addBody(bodyId, Activation.DONT_ACTIVATE);
}
rigidBody.setAddedToSpaceInternal(this);
}

/**
* Invoked just after the physics is stepped.
*
Expand All @@ -525,4 +550,17 @@ private void preTick(float timeStep) {
listener.prePhysicsTick(this, timeStep);
}
}

/**
* Remove the specified PhysicsRigidBody from the space.
*
* @param rigidBody the body to remove (not null, modified)
*/
private void removeRigidBody(PhysicsRigidBody rigidBody) {
BodyInterface bodyInterface = getBodyInterface();
int bodyId = (int) rigidBody.nativeId();
bodyInterface.removeBody(bodyId);

rigidBody.setAddedToSpaceInternal(null);
}
}

0 comments on commit 0ecdc24

Please sign in to comment.