Skip to content

Commit

Permalink
library: add the ContactManager and DefaultContactManager classes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 19, 2024
1 parent 9da9a4d commit 74f60cd
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 1 deletion.
54 changes: 54 additions & 0 deletions library/src/main/java/com/jme3/bullet/ContactManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.bullet;

/**
* Manage notifications when collision objects in a specific PhysicsSpace come
* into contact.
*
* @author Stephen Gold [email protected]
*/
public interface ContactManager {
// *************************************************************************
// new methods exposed

/**
* Update the associated PhysicsSpace. This method should be invoked from
* the thread that created the space.
*
* @param timeInterval the time interval to simulate (in seconds, ≥0)
* @param maxSteps the maximum number of simulation steps of size
* {@code accuracy} (≥1) or 0 for a single simulation step of size
* {@code timeInterval}
*/
void update(float timeInterval, int maxSteps);
}
89 changes: 89 additions & 0 deletions library/src/main/java/com/jme3/bullet/DefaultContactManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2023-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.bullet;

import java.util.logging.Logger;
import jme3utilities.Validate;

/**
* The default implementation of the ContactManager interface.
*
* @author Stephen Gold [email protected]
*/
public class DefaultContactManager implements ContactManager {
// *************************************************************************
// constants and loggers

/**
* message logger for this class
*/
final static Logger logger
= Logger.getLogger(DefaultContactManager.class.getName());
// *************************************************************************
// fields

/**
* PhysicsSpace whose notifications are being managed
*/
final private PhysicsSpace space;
// *************************************************************************
// constructors

/**
* Instantiate a manager for the specified PhysicsSpace.
*
* @param space the PhysicsSpace whose notifications will be managed (not
* null, alias created)
*/
public DefaultContactManager(PhysicsSpace space) {
Validate.nonNull(space, "space");
this.space = space;
}
// *************************************************************************
// ContactManager methods

/**
* Update the associated PhysicsSpace. This method should be invoked from
* the thread that created the space.
*
* @param timeInterval the time interval to simulate (in seconds, ≥0)
* @param maxSteps the maximum number of steps of size {@code accuracy}
* (≥1) or 0 for a single step of size {@code timeInterval}
*/
@Override
public void update(float timeInterval, int maxSteps) {
assert Validate.nonNegative(timeInterval, "time interval");
assert Validate.nonNegative(maxSteps, "max steps");

space.update(timeInterval, maxSteps);
}
}
29 changes: 28 additions & 1 deletion library/src/main/java/com/jme3/bullet/PhysicsSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public class PhysicsSpace extends CollisionSpace {
*/
final private Collection<PhysicsTickListener> tickListeners
= new SafeArrayList<>(PhysicsTickListener.class);
/**
* manage contact/collision listeners and events
*/
private ContactManager manager = new DefaultContactManager(this);
/**
*
*/
private final JobSystem jobSystem;
private final PhysicsSystem physicsSystem;
private final TempAllocator tempAllocator;
Expand Down Expand Up @@ -292,6 +299,16 @@ public BodyInterface getBodyInterface() {
return result;
}

/**
* Access the current ContactManager.
*
* @return the pre-existing instance (not null)
*/
public ContactManager getContactManager() {
assert manager != null;
return manager;
}

/**
* Return the type of contact-and-constraint solver in use.
*
Expand Down Expand Up @@ -360,6 +377,16 @@ public void setAccuracy(float accuracy) {
this.accuracy = accuracy;
}

/**
* Replace the current ContactManager with the specified one.
*
* @param manager the desired manager (not null)
*/
public void setContactManager(ContactManager manager) {
Validate.nonNull(manager, "manager");
this.manager = manager;
}

/**
* Alter the gravitational acceleration acting on newly-added bodies.
* <p>
Expand Down Expand Up @@ -423,7 +450,7 @@ public void update(float timeInterval) {
interval = timeInterval;
assert maxSubSteps > 0 : maxSubSteps;
}
update(interval, maxSubSteps);
manager.update(interval, maxSubSteps);
}

/**
Expand Down

0 comments on commit 74f60cd

Please sign in to comment.