0
votes

I want to place some objects (ModelInstance) on the floor (also a ModelInstance) of my game world. To get the position for these objects, I let a Ray intersect the floor. The point of intersection should then be the required position.

My plan is to set the origin of the ray below the floor, so that the direction of the ray goes straight up and hits the floor from below. Both ModelInstances are .g3db Models made in Blender.

        Vector3 dir = new Vector3(0, 10, 0); //Vector points upwards
        Ray ray = new Ray(new Vector3(), dir.cpy());

        Mesh mesh = landscape.model.meshes.first(); //The floor ModelInstance, has only a single mesh

        int fac = mesh.getVertexSize();
        float[] verts = new float[mesh.getNumVertices() * fac];
        short[] inds = new short[mesh.getNumIndices()];
        mesh.getVertices(verts);
        mesh.getIndices(inds);

        for (int j = 0; j < 10; j++) { //add 10 objects to the floor
            Vector3 out = new Vector3(- 15, -50f, - j * 5); 
            ray.origin.set(out.cpy()); //set the origin of the vector below the floor

            if (Intersector.intersectRayTriangles(ray, verts, inds, fac, out)) {
                System.out.println(j + " out = " + out); //out should be the position for my objects
            }
        }

The output of the intersectRayTriangles Method is exactly the initial position below the floor. But this point is not anywhere close to the floor. How do I get the proper point of intersection?

1
Have you found an answer? I'm working on the same problem.kruzexx
Yes I found a solution, just posted an answerchrfwow

1 Answers

1
votes

I finally found a (semi optimal) solution which works. landscape is a ModelInstance, created with Blender.

ArrayList<Vector3> vertices = new ArrayList<>();

landscape.calculateTransforms();
    Renderable rend = new Renderable();
    Mesh mesh = landscape.getRenderable(rend).meshPart.mesh;

    int vertexSize = mesh.getVertexSize() / 4;
    float[] verts = new float[mesh.getNumVertices() * vertexSize];
    short[] inds = new short[mesh.getNumIndices()];
    mesh.getVertices(verts);
    mesh.getIndices(inds);

    for (int i = 0; i < inds.length; i++) {
        int i1 = inds[i] * vertexSize;
        Vector3 v = new Vector3(verts[i1], verts[i1 + 1], verts[i1 + 2]);

        v.set(v.prj(rend.worldTransform));


        vertices.add(v);
    }

    Vector3 dir = new Vector3(0, 10, 0);
    Vector3 pos = new Vector3(random.nextFloat(),random.nextFloat(),random.nextFloat());
    Ray ray = new Ray(pos, dir.cpy());

        for (int i = 0; i < vertices.size() - 3; i+=3){
            if (Intersector.intersectRayTriangle(ray, vertices.get(i), vertices.get(i + 1), vertices.get(i + 2), pos)) {
                //pos now contains the correct coordinates
                break;
            }
        }

Note that the y-Axis faces upwards