Jump to content

Issue while using moveWithCollisions to avoid collision


Recommended Posts

Hi Team,

I have got a demo for 2D planner here  DEMO 1 : http://www.babylonjs-playground.com/#SUXKHY#28

The yellow box meshes are draggable horizontally and vertically(go give it a try....).

Now, I wanted to prevent dragging over the places where yellow box already exist(prevent collision of yellow box).

In order to do that, I have made some changes in demo 1 to implement moveWithCollisions : 


Demo 2(using moveWithCollisions) - http://www.babylonjs-playground.com/#SUXKHY#32


Changes done:

1) Used checkCollisions and set ellipsoid(equal to width,height,depth of box) for yellow boxes:


At Line : 39 - 40

            box.checkCollisions = true;
            box.ellipsoid = new BABYLON.Vector3(sWidth, sHeight, sDepth);


2) set gravity to scene and gravity with ellipsoid to camera(not sure if this is needed as i want prevent collision among yellow boxes):

At Line : 72-75

            scene.gravity = new BABYLON.Vector3(0, -9.81, 0);
            camera1.applyGravity = true; 
            camera1.ellipsoid = new BABYLON.Vector3(1, 1, 1);


3) enable collision for scene and camera


At Line : 76-77

            scene.collisionsEnabled = true;
            camera1.checkCollisions = true;


4) For horizontal dragging, moveWithCollisions for translating x instead of setting position.x (commented that code, line 194) :


At Line : 195

       currentMesh.moveWithCollisions(new BABYLON.Vector3(currentMesh.minxpos + Math.round((currentCursorXPosition - currentMesh.minxpos) / horizontalSlotWidth) * horizontalSlotWidth, currentMesh.position.Y, currentMesh.position.Z));


4) For vertical dragging, Use moveWithCollisions for translating z instead of setting position.z (commented that code, line 201) :

At Line : 202

       currentMesh.moveWithCollisions(new BABYLON.Vector3(currentMesh.position.Z, currentMesh.position.Y, currentMesh.minzpos + Math.round((currentCursorZPosition - currentMesh.minzpos) / verticalSlotHeight) * verticalSlotHeight));


However, the dragging is completely gone now. what am i missing here




Link to comment
Share on other sites


Hi, why don't you create an index of the walls and check against that when dragging ? no need for collisions at all if you ask me.

if draggedObjest.position is within another yellow box's position +/- width/height,

don't allow dropping it,

otherwise allow dropping it. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...