The other cubes are mightily impressed by your new-found cube chasing skills. Can you change the code sample so that you can pick which cube you want to chase, simply by looking at it? Use the ray casting method that you learned in the earlier target picking examples!
Since you will detect cubes with the help of ray casting, create a class field for the ray object.
private Ray ray = new Ray();
Completely remove the code pertaining to
scaredCube
—a real cube chaser doesn't need an extra class field to pick a cube.Put the following code in the
simpleUpdate()
method loop to keep the ray up-to-date. Use theray.setOrigin(cam.getLocation())
method and theray.setDirection(cam.getDirection())
method to aim the ray in the direction of the camera. Identify collisions and pick the closest geometry as the target. Test the distance and change the target's location, just as before.public void simpleUpdate(float tpf) { CollisionResults results = new CollisionResults...