A complete 3D game development suite written purely in Java.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jmonkeyengine/engine/src/test/jme3test/bullet/TestSweepTest.java

72 lines
2.5 KiB

package jme3test.bullet;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.PhysicsSweepTestResult;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Transform;
import com.jme3.scene.Node;
import java.util.List;
/**
*
* A spatial moves and sweeps its next movement for obstacles before moving
* there Run this example with Vsync enabled
*
* @author
* @wezrule
*/
public class TestSweepTest extends SimpleApplication {
private BulletAppState bulletAppState = new BulletAppState();
private CapsuleCollisionShape obstacleCollisionShape = new CapsuleCollisionShape(0.3f, 0.5f);
private CapsuleCollisionShape capsuleCollisionShape = new CapsuleCollisionShape(1f, 1f);
private Node capsule;
private Node obstacle;
private float dist = .5f;
public static void main(String[] args) {
new TestSweepTest().start();
}
@Override
public void simpleInitApp() {
stateManager.attach(bulletAppState);
capsule = new Node("capsule");
capsule.addControl(new RigidBodyControl(capsuleCollisionShape, 1));
capsule.getControl(RigidBodyControl.class).setKinematic(true);
capsule.move(-2, 0, 0);
bulletAppState.getPhysicsSpace().add(capsule);
rootNode.attachChild(capsule);
obstacle = new Node("obstacle");
RigidBodyControl bodyControl = new RigidBodyControl(obstacleCollisionShape, 0);
obstacle.move(2, 0, 0);
obstacle.addControl(bodyControl);
bulletAppState.getPhysicsSpace().add(obstacle);
rootNode.attachChild(obstacle);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
}
@Override
public void simpleUpdate(float tpf) {
float move = tpf * 1;
List<PhysicsSweepTestResult> sweepTest = bulletAppState.getPhysicsSpace().sweepTest(capsuleCollisionShape, new Transform(capsule.getWorldTranslation()), new Transform(capsule.getWorldTranslation().add(dist, 0, 0)));
if (sweepTest.size() > 0) {
PhysicsSweepTestResult get = sweepTest.get(0);
PhysicsCollisionObject collisionObject = get.getCollisionObject();
fpsText.setText("Almost colliding with " + collisionObject.getUserObject().toString());
} else {
// if the sweep is clear then move the spatial
capsule.move(move, 0, 0);
}
}
}