Collision location wrong after updating vertices

The world my smart little guys live on, is a big icosphere with over 40 thousand points. The terrain height is generated randomly to form mountains and seas. After generation en thus after changing the mesh vertices, I can use collision with a ray to determine where on the terrain the user is moving the mouse.

At this moment picking the location is very precise.

When I reload a saved world after that, the mesh vertices are again modified to set the height of the saved terrain. After this picking the location is suddenly very off most of the time. And never in the same direction.

Before saved world:

After restoring saved world:

After changing the mesh vertices, I do this:

                    vertBuffer.updateData(vertData);
                    globeGeo.getMesh().updateBound();
                    globeGeo.updateModelBound();

(both after generating and loading)

It looks like after loading a saved world, the collision is looking at an older version of the mesh. There is only one globe mesh, no copies and it is not replaced when loading.

What am I missing here?

1 Like

Not sure what there is in this documentation that I not already use…

I like to think I have a firm grip on manipulating the mesh and its vertices. That part works: the changes are visible.

And I do use .updateBound()

The strange thing is that it works after the first manipulation (generating a terrain) and not after the second manipulation (restoring the world that was loaded). The way I change the vertices is similar:

Generating the terrain:

        // -------------------------------------------------
        // Nieuwe vertex-vector maken
        pos = _idx * 3;
        //Dan halen we de vector op 
        Vector3f vec = new Vector3f(
                vertData.get(pos),
                vertData.get(pos + 1),
                vertData.get(pos + 2));
        // Bereken de hoogte, bereken de nieuwe vector
        Vector3f delta = vec.normalize();
        delta = delta.mult(_height + GLOB.GLOBE_BASE_SIZE);
        vec = delta;

        vertData.put(pos, vec.x);
        vertData.put(pos + 1, vec.y);
        vertData.put(pos + 2, vec.z);

Restoring the terrain:

    public void setHeight(float _h, boolean _reGrowGrass){
        $Vec.set( globe.vertData.get(vertPtr), globe.vertData.get(vertPtr + 1), globe.vertData.get(vertPtr + 2)   );
        $Vec.normalizeLocal();
        $Vec.multLocal(_h);
        
        globe.vertData.put(vertPtr,     $Vec.x);
        globe.vertData.put(vertPtr + 1, $Vec.y);
        globe.vertData.put(vertPtr + 2, $Vec.z);        
        
        h=_h-GLOB.GLOBE_BASE_SIZE;

        if (_reGrowGrass) reGrowGrass();
   
        
        if (obj!=null){            
            obj.positionHandle(idx);
        }
        
    }

And both processes result in a visible changed mesh.

Did you click the link? I gave you the link to the exact method you need to clear the cached collision data on the mesh.

1 Like

Ah!

I did click it, but it did not move the anchor. But I found it now…

And it works. Thanks. I need to remember to put you in my will…

2 Likes

lol