Hi,
I try to check if a npc which I can drag around the scene intersects with the sceneCollider, if I drop it.
This is the code I use:
[java] private boolean checkDropPossibility(CollisionResult closest) {
if (closest.getGeometry().getName().equals(WorldManager.GROUND)) {
Vector3f closPos = closest.getContactPoint();
Vector3f goToPos = new Vector3f(closPos.x, closPos.y + 7, closPos.z);
However, getOverlappingCount is always 0. The GhostControl is at the right place when I enable Debug. How is that possible?
EDIT: Propably the problem is caused because none Physic Tick has passed when I check for overlappingObjects. The method is called in the simple application update loop. How can I make sure one physic Tick has passed and then check for collisions?
Call the method in the physicsTick() method? This will be called after a physicsTick. prePhysicsTick() can be used for updating things before a physicsTick.
Alternatively, You can use the integrated PhysicsCollisionListener: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners
You can have your npc-control implement this and do all your stuff in the control instead of in your simpleapplication. If you do so, donât forget to register your listener to the PhysicsSpace (if you donât do this, the collision(PhysicsCollisionEvent event) will never be triggered). But given your code, it does not seem you are using physics, although you are refering to physic Ticks?
Hey, thanks for your answer :).
The problem is that the npc is dragged into the scene when you press a mouse button. This calls the âadd npcâ method, which calls the method in the first post to check if the npc can be added, which only happens when he doesnt intersect with the World Collider. The mouse click gets recognized in the apps update loop. So how is it possible to instantly check if a collision occured with the ghostControl, or something like that if I do not call the method in a physicsTick, as I canât do it in my case.
There is some information in here regarding collisions between non physical objects. It also tells you the requirements of the objects to be able to detect collisions between them. Maybe that will get you further?
I still donât see why you are using physics. Is this only for the collision detection, or are you using it for other purposes as well? If it is only for collision detection, it might be over-kill, especially when you can implement it they way as described in above mentioned link.
I donât want to use physics in this case, but I donât know what else I could do. If I try it with collide with, the only object I get collision with is the object itself, even if it isnât a child of WorldCollider.
This is the code I use:
[java] private boolean checkDropPossibility(Vector3f mouse3d, Vector3f dir) {
Ray ray = new Ray(mouse3d, dir);
CollisionResults results = new CollisionResults();
((Node)((Node)rootNode.getChild(WorldManager.WORLD)).getChild(WorldManager.LANDSCAPE)).getChild(WorldManager.GROUND).collideWith(ray, results);
CollisionResult closest = results.getClosestCollision();
Vector3f closPos = closest.getContactPoint();
Vector3f goToPos = new Vector3f(closPos.x, closPos.y + 7, closPos.z);
Node testNode = new Node("TestNode");
Box box = new Box(3,6,3);
Geometry test = new Geometry("Test1",box);
test.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
testNode.attachChild(test);
rootNode.attachChild(testNode);
testNode.setLocalTranslation(goToPos);
CollisionResults results2 = new CollisionResults();
testNode.collideWith(rootNode.getChild(WorldManager.WORLDCOLLIDER).getWorldBound(), results2);
if (results2.size() > 0) {
for(int i = 0; i < results2.size();i++){
CollisionResult r = results2.getCollision(i);
System.out.println(r.getGeometry().getName());
}
return false;
}
return true;
}[/java]
When I swap the nodes and do rootNode.getChild(WorldManager.WORLDCOLLIDER).collideWith(testNode.getWorldBound(), results2); instead everything works fine. Thanks for your help :).
When I understand your code correctly, you first get the position where you have clicked with your mouse on the ground, then create a box 7 units above that position and after that, you check if this newly added box is colliding with the WORLDCOLLIDER. Seems good to me. Is your WORLDCOLLIDERâs bounding box where you expect it to be, or is it somewhere far away and thus not colliding with the NPC?
And you are saying âSystem.out.println(r.getGeometry().getName());â is giving you âTest1â? That would be strangeâŚ
Just a wild guess, but would adding a boundingbox to your geometry help? Maybe it is not calculated by JME before you try to check for collisions? You could also quickly try to create your testNode during initialization and just move it out of the way for a while. As soon as you are going to place it, set its location to âgoToPosâ. Just to see if it has to do with timing.
[EDIT] --> Never mind, I just see you have solved it