2012-05-28 16:52:56 +00:00
|
|
|
package jme3test.bullet;
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
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;
|
2018-09-09 12:26:42 -07:00
|
|
|
import com.jme3.math.Vector3f;
|
2012-05-28 16:52:56 +00:00
|
|
|
import com.jme3.scene.Node;
|
|
|
|
import com.jme3.scene.Spatial;
|
|
|
|
import java.util.List;
|
2018-09-09 12:26:42 -07:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author @wezrule
|
|
|
|
*/
|
|
|
|
public class TestPhysicsRayCast extends SimpleApplication {
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
private BulletAppState bulletAppState = new BulletAppState();
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
public static void main(String[] args) {
|
|
|
|
new TestPhysicsRayCast().start();
|
|
|
|
}
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
@Override
|
|
|
|
public void simpleInitApp() {
|
|
|
|
stateManager.attach(bulletAppState);
|
|
|
|
initCrossHair();
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml");
|
|
|
|
s.setLocalScale(0.1f);
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
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);
|
2015-04-26 19:21:00 -04:00
|
|
|
bulletAppState.setDebugEnabled(true);
|
2012-05-28 16:52:56 +00:00
|
|
|
}
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
@Override
|
|
|
|
public void simpleUpdate(float tpf) {
|
2018-09-09 12:26:42 -07:00
|
|
|
float rayLength = 50f;
|
|
|
|
Vector3f start = cam.getLocation();
|
|
|
|
Vector3f end = cam.getDirection().scaleAdd(rayLength, start);
|
|
|
|
List<PhysicsRayTestResult> rayTest
|
|
|
|
= bulletAppState.getPhysicsSpace().rayTest(start, end);
|
2012-05-28 16:52:56 +00:00
|
|
|
if (rayTest.size() > 0) {
|
|
|
|
PhysicsRayTestResult get = rayTest.get(0);
|
|
|
|
PhysicsCollisionObject collisionObject = get.getCollisionObject();
|
2018-09-09 12:26:42 -07:00
|
|
|
// Display the name of the 1st object in place of FPS.
|
2012-05-28 16:52:56 +00:00
|
|
|
fpsText.setText(collisionObject.getUserObject().toString());
|
2018-09-09 12:26:42 -07:00
|
|
|
} else {
|
|
|
|
// Provide prompt feedback that no collision object was hit.
|
|
|
|
fpsText.setText("MISSING");
|
2012-05-28 16:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-26 19:21:00 -04:00
|
|
|
|
2012-05-28 16:52:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|