Ray casting issues

Ok, so i managed to get ray casting to work. Kind of.

Im trying to make a series of cubes in a tight grouping (a 10x3x10 box made up of cubes).

Whats the best way to click and remove one of the cubes at a time AND remove its hits collisions box?

[java] public void simpleInitApp()


bulletAppState = new BulletAppState();


shootables = new Node(“Shootables”);


viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));


CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

player = new CharacterControl(capsuleShape, 0.05f);




player.setPhysicsLocation(new Vector3f(0,10,0));






for(int i = 0; i < 10; i++)


for(int j = 0; j < 3; j++)


for(int k = 0; k < 10; k++)




x = x + 4;


x = 0;

y = y + 4;


y = 0;

z = z + 4;



protected Geometry makeCube()


Box cubeTerrain = new Box(Vector3f.ZERO, cubeLength, cubeHeight, cubeWidth);

Geometry geom = new Geometry(“Box”, cubeTerrain);

geom.setLocalTranslation(new Vector3f(x,y,z));

Material mat = new Material(assetManager,


mat.setColor(“Color”, ColorRGBA.randomColor());



cube_phy = new RigidBodyControl(0);



return geom;


That is the code that generates my cubes into said box.

[java]private ActionListener actionListener = new ActionListener()


public void onAction(String name, boolean keyPressed, float tpf)


CollisionResults results = new CollisionResults();

if (name.equals(“Shoot”) )


//CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

shootables.collideWith(ray, results);

System.out.println("---- Collisions? " + results.size() + “----”);

for (int i = 0; i < results.size(); i++)


float dist = results.getCollision(i).getDistance();

Vector3f pt = results.getCollision(i).getContactPoint();

String target = results.getCollision(i).getGeometry().getName();

System.out.println("* Collision #" + i);

System.out.println(" You shot " + target + " at " + pt + ", " + dist + “Wu away.”);



if (results.size() > 0)






Is my ray casting code that doesnt quite work anymore, just because i cant figure out how to single out my cubes and their “hit boxes”.

Can’t you just look at the 3d co-ordinate of the hit and refer that back to your original box positions?

@zarch said:
Can't you just look at the 3d co-ordinate of the hit and refer that back to your original box positions?

Would that involve using the Vector3f pt variable and then basically rounding to see which cube is hit? The problem with that is that the ray casts through cubes and hits several, so i need to figure out how to stop it on a hit?

I see what youre saying and where to go from there, but i need to stop the rays.

Just take the first collision. That collision point will then be on the boundary between two cubes (or on the outside of all the cubes in which case just one).

Check those positions, one must be empty (since this is first collision), so the other is the one hit.

I suppose technically there is a (highly unlikely) edge case where it hits the border between 3 or 4 cubes in which case just pick the first filled one you come to.

Ok so i figured out why its going through and i managed to disable it for the most part.

The for loop was doing that, so i commented it out, and it seems to only give me the cube i want now.

Unfortunately i have no way of knowing which cube im actually hitting because they all have the same name, and the collision results seem to be based on the object, not the world.

So i need a way of figuring out the world coordinates, knowing that i can probably find the cube.

As I said above…there is a way to get the hit co-ordinates from the result. It’s all covered in the tutorial: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking

Ok, so when i went back and messed around a little bit, i found that i can set the box name to

[java]Geometry geom = new Geometry("Box " + cubeNumber, cubeTerrain);[/java]

And the cubeNumber increments every time a cube is made. So that when i click on the cube, it actually spits back "Box : " with an appropriate number.

The problem is, i dont know how i use that information to then select a box for deletion. I thought about using 3d arrays, but those will get very big very fast in my game, and i STILL dont know who to plug it all back in to remove them.

Well there has to be some reason you put these spatials there. E.g. You game creates “10 enemies”. As you have to store the data about these “enemies” somewhere anyway you can also save a link to the spatial and any other scenegraph data that initiatied for that “enemy” there as well. jME also doesn’t store the objects in “thin air”, having lists or maps for your objects (which mostly are just storing references and some small data packages anyway) isn’t bad or anything.

Im basically trying to set up terrain like Minecraft for lack of a better way of putting it. Right now im trying to make it so that i can remove terrain in the same way. Press B for build mode (my own thing afaik) and then left-click to delete a cube.

So ill be making a level that will probably be more then 1000x1000x50 or something which means i need to store data on that many objects, which seems intensive. Maybe it isnt because its not terribly complicated data, but thats the fear im having.

That and i have to figure out how to store that data inside 3 nested arrays or maybe just 1 array, but then i worry about needing a place saver for an empty box that i might want to fill in again.

You follow a completely wrong approach, search the forum for “voxels”. Box worlds are not made out of boxes. And btw this kind of game is one of the harder ones to implement, you really should bring some game programming experience to pull off a minecraft-like game.

Thanks for the input, i found some good stuff, but im gonna take your other, much more important advice, and go a different route the voxeling or box worlding. Just keep it simple for now.