- add Tests for physics raytest and sweeptest (thanks to @wezrule)
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9435 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
33a69d4536
commit
9292ac9898
@ -0,0 +1,60 @@ |
||||
package jme3test.bullet; |
||||
|
||||
import com.jme3.app.SimpleApplication; |
||||
import com.jme3.bullet.BulletAppState; |
||||
import com.jme3.bullet.collision.PhysicsCollisionObject; |
||||
import com.jme3.bullet.collision.PhysicsRayTestResult; |
||||
import com.jme3.bullet.collision.shapes.CollisionShape; |
||||
import com.jme3.bullet.control.RigidBodyControl; |
||||
import com.jme3.bullet.util.CollisionShapeFactory; |
||||
import com.jme3.font.BitmapText; |
||||
import com.jme3.scene.Node; |
||||
import com.jme3.scene.Spatial; |
||||
import java.util.List; |
||||
/** |
||||
* |
||||
* @author @wezrule |
||||
*/ |
||||
public class TestPhysicsRayCast extends SimpleApplication { |
||||
|
||||
private BulletAppState bulletAppState = new BulletAppState(); |
||||
|
||||
public static void main(String[] args) { |
||||
new TestPhysicsRayCast().start(); |
||||
} |
||||
|
||||
@Override |
||||
public void simpleInitApp() { |
||||
stateManager.attach(bulletAppState); |
||||
initCrossHair(); |
||||
|
||||
Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml"); |
||||
s.setLocalScale(0.1f); |
||||
|
||||
CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(s); |
||||
Node n = new Node("elephant"); |
||||
n.addControl(new RigidBodyControl(collisionShape, 1)); |
||||
n.getControl(RigidBodyControl.class).setKinematic(true); |
||||
bulletAppState.getPhysicsSpace().add(n); |
||||
rootNode.attachChild(n); |
||||
bulletAppState.getPhysicsSpace().enableDebug(assetManager); |
||||
} |
||||
|
||||
@Override |
||||
public void simpleUpdate(float tpf) { |
||||
List<PhysicsRayTestResult> rayTest = bulletAppState.getPhysicsSpace().rayTest(cam.getLocation(), cam.getLocation().add(cam.getDirection())); |
||||
if (rayTest.size() > 0) { |
||||
PhysicsRayTestResult get = rayTest.get(0); |
||||
PhysicsCollisionObject collisionObject = get.getCollisionObject(); |
||||
//do stuff
|
||||
fpsText.setText(collisionObject.getUserObject().toString()); |
||||
} |
||||
} |
||||
|
||||
private void initCrossHair() { |
||||
BitmapText bitmapText = new BitmapText(guiFont); |
||||
bitmapText.setText("+"); |
||||
bitmapText.setLocalTranslation((settings.getWidth() - bitmapText.getLineWidth())*0.5f, (settings.getHeight() + bitmapText.getLineHeight())*0.5f, 0); |
||||
guiNode.attachChild(bitmapText); |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
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); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue