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.setDebugEnabled(true);
    }

    @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);
    }
}

@@ -398,16 +398,47 @@
 apText;%0A
+import com.jme3.math.Vector3f;%0A
 import c
@@ -511,16 +511,17 @@
 l.List;%0A
+%0A
 /**%0A *%0A 
@@ -1433,24 +1433,169 @@
 loat tpf) %7B%0A
+        float rayLength = 50f;%0A        Vector3f start = cam.getLocation();%0A        Vector3f end = cam.getDirection().scaleAdd(rayLength, start);%0A
         List
@@ -1624,16 +1624,32 @@
  rayTest
+%0A               
  = bulle
@@ -1688,68 +1688,18 @@
 est(
-cam.getLocation(), cam.getLocation().add(cam.getDirection())
+start, end
 );%0A 
@@ -1883,16 +1883,60 @@
   //
-do stuff
+ Display the name of the 1st object in place of FPS.
 %0A   
@@ -2001,24 +2001,154 @@
 oString());%0A
+        %7D else %7B%0A            // Provide prompt feedback that no collision object was hit.%0A            fpsText.setText(%22MISSING%22);%0A
         %7D%0A  

fix for issue #886