Loading object and cloning with collisions

I have an object with like 40 meshes in it, which I am loading using AssetsManager

const assetsManager = new BABYLON.AssetsManager(scene);
const meshTask = assetsManager.addMeshTask('part_of_wall', '', './assets/', 'part_of_wall.obj');
meshTask.onSuccess = (task) => {
   task.loadedMeshes.forEach(function(e) { e.checkCollisions = true;});
}
assetsManager.load();

I have couple of things to accomblish with this object.

  1. Copy 10 times of object and repeat in Z position to create a long wall.
    I found that I could use Use Instances - Babylon.js Documentation, so I copied _addInstance function to create multiple copies

    var _addInstance = function (m,n,x,y,z){
        var i= m.createInstance(n);
        i.position.x = x||0;
        i.position.y = y||0;
        i.position.z = z||0;
        return i;
     } 
    

but how can I copy whole object and repeat in Z position, instead going in loop and copying each mesh.

  1. Add collisions detection
    to accomplish collision detection, in above code I added a loop
    task.loadedMeshes.forEach(function(e) { e.checkCollisions = true;});

but its not very efficient , is there a way to add collision to whole object instead each mesh?

ok , I was able to accomplish copying by merging meshes:

let part_of_wall = BABYLON.Mesh.MergeMeshes(task.loadedMeshes);
part_of_wall.checkCollisions = true;
part_of_wall.isVisible = false;
_addInstance(part_of_wall,'root')
_addInstance(part_of_wall,'0',0,0,1523)

but for collisions, there are holes on wall so I am still looking for solution to collision, to not cross the wall

Could you reproduce the issue in the playground as it might definitely help the community addressing the issue ?

I have created a PG here:
https://www.babylonjs-playground.com/#4DAIHA#2

I am able to create long wall by using instance in _addInstance function, I have issue with collision, there are holes in wall so I am able to cross the wall. I want to stop this behaviour happening and keep the player inside of wall.

Unfortunately collisions will be very expensive if you use the real object
Furthermore they are only working for convex objects

I would recommend to use an impostor (a simple invisible boxe or multiple boxes). This is what most of the games do for instance

1 Like

Thank you once again, I successfully created a cube in front of wall and only applied collisions to it.

https://www.babylonjs-playground.com/#4DAIHA#4

1 Like