Make objects disappear when "walking into them"

Ok I didn’t think about that so I guess I will try it out!

I made one node which holds the terrain and another node that holds the tree (in the scene explorer). And in the code I load the scene and apply the physics like this:

sceneModel = (Node) assetManager.loadModel("Scenes/newScene.j3o");
sceneModel.setLocalScale(1f);
    
CollisionShape sceneShape = 
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);

And I still collide with the dumb tree… what am I doing wrong? I just want it to be like a “ghost” but still visible, and then I will go with your idea and check the distance.

Hi thanks for answer, I will check that one out too :smile:

When your tree is part of your scene you will collide with it which means you need an extra j3o for non collidables or iterate over the scenegraph and add rigidbodycontrols based on their Name or something.

The other thing you are looking for is a GhostListenerControl or what it was called, it’s definately in the wiki and it uses bullet, but you don’t seem to need that :smile:

Hi thanks for answering!

Ok but the funny thing is that I tried moving the tree with setlocatranslation, and the tree moves, but not “its physics”, why is that? :stuck_out_tongue: I do not understand how I can add an object in the scene graph, and make it non physical… I dont want to do it by code because that’s way to complicated.

I will read about that too!

You should only add the collisionshape to the terrain then. Something like this:

Spatial terrain = sceneModel.getChild("Terrain-Name")
CollisionShapeFactory.createMeshShape((Node) terrain);

And for the other part, simply check the distance. Believe normen, it is

Of course there are several other ways, but even if they might be “fancier” (however you define it) they will most certainly be slower and more difficult to implement.

You have to use setPhysicsLocation or the colissionshape will stay and only the spatial will move. That should be mentioned in the Physics wiki though

@mathiasj Thank you now the physics only is applied to the terrain and not the trees!! :slight_smile: :slight_smile: Ok I will go with the least CPU intense way. It seems like the best solution.

But do should I just check the the distance “manually”, or should I use some colission detection class?
With manually I mean looping through an awway of trees and check the distance. I was thinking of ghostcollision but I can’t get it to work.

I would probably create a Control for each coin or create an AppState which has a List of all the coins. In so doing, you can easily check the distance in the update method without having to use your SimpleApplication update.

Hi!

Do you mean creating a class that extends AbstractAppState which has the list of the coins (I’m using trees now for testing) :stuck_out_tongue:

But someone was mensioning GhostControl som I tried adding a Ghost control to the treeNode (which has the trees), and then I added the control to the node (Not the trees, only their parent node). But I do’nt understand how it works because when collision is detected, I want the tree to be detached from the rootNode.

Yes that’s what I mean.

If you want to use a GhostControl have a look at this http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:physics_listeners. Notice that this is probably more difficult to implement and slower than the first solution. However, it is pretty good if simple distance checks are not enough for you. This depends entirely on the object but for coins as you described it, I’d definetely go for the first one.

Okey but then I will skip the ghostcontrol. But the list that holds the trees, should it be List of Spatial or Node?

Is there any method for checking the distance (like getDistance())? Is there an example using this method that you’re mentioning. Should I do this in my update loop?

To check the distance call Vector3f.distance(Vector3f other). So your code should look something like this:

public void update(float tpf) {
for(Spatial coin : coins) {
    if(coin.getWorldTranslation().distance(playerTranslation) < 0.5f) {
         player.increaseCoins();
         rootNode.detachChild(coin);
    }
}

You should really take a look at the Javadoc to find out which methods exist.
You would probably create a list of spatials as this is the superclass but this is basic Java. Just take a look at the docs again.

Ok thank you now I understand exactly! (finally) :stuck_out_tongue:

It detects collision!!!

I used:
spatial.removeFromParent();
And now everything works, really thank you @mathiasj !

Your question on collision of objects really depends on the amount and size of the objects. If they were indeed just coins and your scene was dissected properly, a hashmap<coords, object> would be very simple to deal with. Most importantl thing first is getting it to work, and writing the code that makes the most sense to you. Only look for optimizations if it is actually an issue.

Thanks for answering!

Okey but isn’t arraylist faster if you just want to search through it (like I do in the update loop with the “coins”)?

And another thing that I wonder, how big can a list be in the update loop until it starts slowing down the game big time? :stuck_out_tongue: If I would have 1000 “coins” in the list, would that slow everything down?

A loop over 1000 objects is nothing, no.

Okey great! Thank you!

As others said, write your game first, then worry about optimizing - when you see where your actual bottlenecks are.

I will do that from now on. I haven’t written any code for the game yet, just testing code for different stuff which the game will use. Thanks for all the great tips!

Collections are pretty well optimized these days. This website gives you an insight into how the different collections work, though these comparisons were made 4 years ago. It still holds true for their advantages and disadvantages. The speeds will almost certainly be quicker now. Just use it as a guide for which collection you should most likely use for your situation, and which work best for a given task.