Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses bugs in physics plugin : #265 #242 #308

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gama.extension.physics/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/Libbulletjme-18.6.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Libbulletjme-21.2.1.jar" sourcepath="lib/Libbulletjme-21.2.1-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
Expand Down
2 changes: 1 addition & 1 deletion gama.extension.physics/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Require-Bundle: gama.core,
gama.dependencies,
org.eclipse.osgi
Bundle-ClassPath: .,
lib/Libbulletjme-18.6.0.jar
lib/Libbulletjme-21.2.1.jar
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-21
Automatic-Module-Name: gama.extension.physics
Expand Down
2 changes: 1 addition & 1 deletion gama.extension.physics/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ bin.includes = META-INF/,\
plugin.xml,\
models/,\
lib/,\
lib/Libbulletjme-18.6.0.jar
lib/Libbulletjme-21.2.1.jar
jre.compilation.profile = JavaSE-21

Binary file not shown.
Binary file removed gama.extension.physics/lib/Libbulletjme-18.6.0.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
49 changes: 22 additions & 27 deletions gama.extension.physics/src/com/bulletphysics/BulletGlobals.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* BulletGlobals.java, in gaml.extensions.physics, is part of the source code of the
* GAMA modeling and simulation platform .
* BulletGlobals.java, in gaml.extensions.physics, is part of the source code of the GAMA modeling and simulation
* platform .
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/

package com.bulletphysics;
Expand All @@ -25,28 +25,28 @@ public class BulletGlobals {

/** The Constant CONVEX_DISTANCE_MARGIN. */
public static final float CONVEX_DISTANCE_MARGIN = 0.04f;

/** The Constant FLT_EPSILON. */
public static final float FLT_EPSILON = 1.19209290e-07f;

/** The Constant SIMD_EPSILON. */
public static final float SIMD_EPSILON = FLT_EPSILON;

/** The Constant SIMD_2_PI. */
public static final float SIMD_2_PI = 6.283185307179586232f;

/** The Constant SIMD_PI. */
public static final float SIMD_PI = SIMD_2_PI * 0.5f;

/** The Constant SIMD_HALF_PI. */
public static final float SIMD_HALF_PI = SIMD_2_PI * 0.25f;

/** The Constant SIMD_RADS_PER_DEG. */
public static final float SIMD_RADS_PER_DEG = SIMD_2_PI / 360f;

/** The Constant SIMD_DEGS_PER_RAD. */
public static final float SIMD_DEGS_PER_RAD = 360f / SIMD_2_PI;

/** The Constant SIMD_INFINITY. */
public static final float SIMD_INFINITY = Float.MAX_VALUE;

Expand All @@ -62,11 +62,11 @@ protected BulletGlobals initialValue() {

/** The contact breaking threshold. */
private float contactBreakingThreshold = 0.02f;

/** The deactivation time. */
// RigidBody
private float deactivationTime = 2f;

/** The disable deactivation. */
private boolean disableDeactivation = false;

Expand All @@ -77,14 +77,13 @@ protected BulletGlobals initialValue() {
*
* @return the contact breaking threshold
*/
public static float getContactBreakingThreshold() {
return threadLocal.get().contactBreakingThreshold;
}
public static float getContactBreakingThreshold() { return threadLocal.get().contactBreakingThreshold; }

/**
* Sets the contact breaking threshold.
*
* @param threshold the new contact breaking threshold
* @param threshold
* the new contact breaking threshold
*/
public static void setContactBreakingThreshold(final float threshold) {
threadLocal.get().contactBreakingThreshold = threshold;
Expand All @@ -95,32 +94,28 @@ public static void setContactBreakingThreshold(final float threshold) {
*
* @return the deactivation time
*/
public static float getDeactivationTime() {
return threadLocal.get().deactivationTime;
}
public static float getDeactivationTime() { return threadLocal.get().deactivationTime; }

/**
* Sets the deactivation time.
*
* @param time the new deactivation time
* @param time
* the new deactivation time
*/
public static void setDeactivationTime(final float time) {
threadLocal.get().deactivationTime = time;
}
public static void setDeactivationTime(final float time) { threadLocal.get().deactivationTime = time; }

/**
* Checks if is deactivation disabled.
*
* @return true, if is deactivation disabled
*/
public static boolean isDeactivationDisabled() {
return threadLocal.get().disableDeactivation;
}
public static boolean isDeactivationDisabled() { return threadLocal.get().disableDeactivation; }

/**
* Sets the deactivation disabled.
*
* @param disable the new deactivation disabled
* @param disable
* the new deactivation disabled
*/
public static void setDeactivationDisabled(final boolean disable) {
threadLocal.get().disableDeactivation = disable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* BulletPhysicalWorld.java, in gaml.extensions.physics, is part of the source code of the
* GAMA modeling and simulation platform .
* BulletPhysicalWorld.java, in gaml.extensions.physics, is part of the source code of the GAMA modeling and simulation
* platform .
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package gama.extension.physics.java_version;

Expand Down Expand Up @@ -41,14 +41,15 @@ public class BulletPhysicalWorld extends AbstractPhysicalWorld<DiscreteDynamicsW

/** The config. */
private final CollisionConfiguration config = new DefaultCollisionConfiguration();

/** The dispatcher. */
private final CollisionDispatcher dispatcher = new CollisionDispatcher(config);

/**
* Instantiates a new bullet physical world.
*
* @param physicalSimulationAgent the physical simulation agent
* @param physicalSimulationAgent
* the physical simulation agent
*/
public BulletPhysicalWorld(final PhysicalSimulationAgent physicalSimulationAgent) {
super(physicalSimulationAgent);
Expand Down Expand Up @@ -91,12 +92,8 @@ public void unregisterAgent(final IAgent agent) {
@Override
public void updateAgentsShape() {
// We update the agents
for (IAgent a : updatableAgents) {
unregisterAgent(a);
}
for (IAgent a : updatableAgents) {
registerAgent(a);
}
for (IAgent a : updatableAgents) { unregisterAgent(a); }
for (IAgent a : updatableAgents) { registerAgent(a); }
updatableAgents.clear();
}

Expand Down Expand Up @@ -145,7 +142,7 @@ public void dispose() {

@Override
public void updatePositionsAndRotations() {
world.getCollisionObjectArray().forEach((b) -> {
world.getCollisionObjectArray().forEach(b -> {
RigidBody rb = (RigidBody) b;
IBody bw = (IBody) rb.getUserPointer();
if (rb.isActive() && !rb.isStaticObject()) { bw.transferLocationAndRotationToAgent(); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************************************
*
* NativeBulletPhysicalWorld.java, in gaml.extensions.physics, is part of the source code of the GAMA modeling
* and simulation platform .
* NativeBulletPhysicalWorld.java, in gaml.extensions.physics, is part of the source code of the GAMA modeling and
* simulation platform .
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand All @@ -10,6 +10,7 @@
********************************************************************************************************/
package gama.extension.physics.native_version;

import java.lang.Thread.State;
import java.util.concurrent.Semaphore;

import com.jme3.bullet.PhysicsSpace;
Expand Down Expand Up @@ -93,7 +94,7 @@
Thread thread = new Thread(this);

/** The lock. */
Semaphore semaphore = new Semaphore(1);
volatile Semaphore semaphore = new Semaphore(1);

@Override
public void run() {
Expand All @@ -112,7 +113,8 @@
} catch (InterruptedException e) {
e.printStackTrace();
}
getWorld().update(timeStep.floatValue(), maxSubSteps, false, false, true);
PhysicsSpace world = getWorld();
if (world != null) { world.update(timeStep.floatValue(), maxSubSteps, false, false, true); }
// DEBUG.OUT("Actually updating world in thread " + Thread.currentThread().getName());
continueStep = true;
}
Expand All @@ -137,7 +139,7 @@
public void updateEngine(final Double timeStep, final int maxSubSteps) {
this.timeStep = timeStep;
this.maxSubSteps = maxSubSteps;
DEBUG.OUT("Asking to update the world in thread " + Thread.currentThread().getName());
// DEBUG.OUT("Asking to update the world in thread " + Thread.currentThread().getName());
continueStep = false;
semaphore.release();
while (!continueStep) { Thread.yield(); }
Expand All @@ -151,22 +153,26 @@
@Override
public PhysicsSpace createWorld() {
if (world != null) return world;
thread.start();
if (thread.getState() == State.NEW) { thread.start(); }
while (doInit) { Thread.yield(); }
return world;
}

@Override
public void registerAgent(final IAgent agent) {
NativeBulletBodyWrapper b = new NativeBulletBodyWrapper(agent, this);
getWorld().addCollisionObject(b.getBody());
b.setCCD(simulation.getCCD(simulation.getScope()));
PhysicsSpace world = getWorld();
if (world != null) {
NativeBulletBodyWrapper b = new NativeBulletBodyWrapper(agent, this);
world.addCollisionObject(b.getBody());
b.setCCD(simulation.getCCD(simulation.getScope()));
}
}

@Override
public void unregisterAgent(final IAgent agent) {
NativeBulletBodyWrapper wrapper = (NativeBulletBodyWrapper) agent.getAttribute(BODY);
getWorld().remove(wrapper.getBody());
Object body = agent.getAttribute(BODY);
PhysicsSpace world = getWorld();
if (world != null && body instanceof NativeBulletBodyWrapper wrapper) { world.remove(wrapper.getBody()); }
}

@Override
Expand All @@ -182,13 +188,18 @@

@Override
public void setGravity(final GamaPoint g) {
PhysicsSpace world = getWorld();
if (world != null) { world.setGravity(toVector(g)); }
}

@Override
public void dispose() {
if (world == null) return;
world.getRigidBodyList().forEach(world::removeCollisionObject);
// Doesnt seem to be necessary as the "CleanerThread" is running. See
// https://hub.jmonkeyengine.org/t/solved-how-to-close-a-physics-space-to-free-up-ram/47684/9
// world.destroy();
// NativePhysicsObject.freeUnusedObjects();
// CollisionSpace.physicsSpaceTL;
doUpdate = false;
semaphore.release();
try {
Expand All @@ -197,11 +208,15 @@
e.printStackTrace();
}
world = null;
// The goal here is to get rid of bridge Java/C++ objects as soon as possible
System.gc();

Check warning on line 212 in gama.extension.physics/src/gama/extension/physics/native_version/NativeBulletPhysicalWorld.java

View workflow job for this annotation

GitHub Actions / SpotBugs

DM_GC

gama.extension.physics.native_version.NativeBulletPhysicalWorld.dispose() forces garbage collection; extremely dubious except in benchmarking code
Raw output
Code explicitly invokes garbage collection. Except for specific use in benchmarking, this is very dubious.

In the past, situations where people have explicitly invoked the garbage collector in routines such as close or finalize methods has led to huge performance black holes. Garbage collection can be expensive. Any situation that forces hundreds or thousands of garbage collections will bring the machine to a crawl.

}

@Override
public void updatePositionsAndRotations() {
PhysicsSpace world = getWorld();
if (world == null) return;
for (PhysicsRigidBody b : world.getRigidBodyList()) {
NativeBulletBodyWrapper bw = (NativeBulletBodyWrapper) b.getUserObject();
if (b.isActive() && !b.isStatic()) { bw.transferLocationAndRotationToAgent(); }
Expand Down