Hello everyone,
I am experiencing a problem where I am not able to detect collisions on the terrain. I am trying to implement an RTS-style move function where the user selects a single unit and then clicks on the terrain to move there.
I created a scene using the scene editor and added flat terrain to it and proceeded to add a “Static RigidBody” control to that terrain (yes I made sure the Static RigidBody was under the terrain node). In the code, I load the scene as follows:
Spatial scene = assetManager.loadModel(“Scenes/testscene.j3o”);
BulletAppState bulletAppState = new BulletAppState();
app.getStateManager().attach(bulletAppState);
bulletAppState.getPhysicsSpace().addAll(scene);
localRootNode.attachChild(scene);
And I use this code for collision detection, which works with other models that I attach to the rootnode but not the terrain(always returns zero) (code found on the forums, works well):
CollisionResults results = new CollisionResults();
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d);
Ray ray = new Ray(click3d, dir);
rootNode.collideWith(ray, results);
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
moveMark.setLocalTranslation(closest.getContactPoint().setY(moveMark.getLocalTranslation().y));
moveSwitch = true;
}
Am I missing something obvious? I am really wondering why it’s not working. I even colored the rays to make sure they were being shot correctly, and they really are. It just seems that the terrain patch is not being treated as a solid that can be collided with…
Any help would be greatly appreciated as I am starting to go coo coo for coco puffs.
-hardboss
More about the game: turn-based strategy game with rpg elements and no physics. kinda like heroes of might and magic.
The terrain doesn’t need a rigid body in order to pick it with a ray. Also make sure to normalize your direction ray.
Do the test cases work for you? (TerrainTestModifyHeight)
What version are you using? There was a bug a long while ago that caused flat terrain to not collide with rays, but it has been fixed since before beta.
1 Like
The normalizing worked perfectly! Thanks so much for the quick reply and help.
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalize();
This did the trick!
-hardboss