Problem: I am trying to create an RTS style interaction. I want to be able to click on the map and have characters move to the click location. I haven't been able to figure this out, even after reveiwing tutorials from the one and only xoppa and several other's inqueries on the same concept. Other's seem to be happy utilizing collision with a near or far plane or a basic collision shape. However, I have a map with variations in height. I can't tell if I'm not creating my complex collision shape correctly or I'm just making things more complicated than need be. The code I think is relevelvant is as follows.

A model is loaded from a G3DB scene in the exact same manner of xoppa loading a 3d scene tutorial. After loading, I call the following method in my ModelInstance class.

private Array<Mesh> modMesh = new Array<~>();
public void createShape() {
    modMesh = model.meshes;

When a user clicks the mouse or taps the touch screen, I call this method to check collision of the ray from the camera to presumably the part of the map we were trying to touch.

public Vector3 checkSelection(Ray ray) {
    Vector3 intersection = new Vector3();
    for(int i = 0; i < modMesh.size; i++) {
        mesh = modMesh.get(i);
        float[] v = new float[mesh.getNumIndices() * mesh.getNumVertices() * mesh.getVertexSize()];
        short[] ind = new short[(mesh.getNumIndices() * 6 - 6) * (mesh.getNumVertices() - 1)];
        Intersector.intersectRayTriangles(ray, mesh.getVertices(v), ind, mesh.getVertexSize(), intersection);
    return intersection;

So, when I run this code, it returns Vector3 values. They seem to be off though. For instance, my camera is looking at point (0,0,0). If I click where (0,0,0) appears on the screen, it will return a value like (-14.56, -18, -85). I imagine I am not creating a proper collision shape for the map. Which again leads to my question. How do I obtain the collision coordinate on a 3D map with variations in height in LibGDX?

Side Note: My intersecting collisions with normal collision shapes work perfectly. This is the reason I'm assuming I am completely wrong in the way I create a collision shape for the map itself. Thank you in advance for any input and assistance offered.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.