Jump to content

Moving Mesh with Free Camera does not collide with another mesh


Recommended Posts

Hi all


This might sound a simple question but i don't seem to to figure out the solution.

I have a mesh for example a box (box = BABYLON.Mesh.CreateBox("box", 40, scene);.

I have enabled collisions by using scene.collisionsEnabled = true;

I also have a freecamera and another imported mesh for example a shelve which is at a fixed position. .

If I try to move the freecamera through the sheve it collides and everything is fine.

However, if I attach the box to the freecamera so that the box is constantly ahead of the freecamera

scene.registerBeforeRender(function () {

box.position.z = scene.activeCamera.position.z + 100 ;


and as I move the camera and the box through the shelve the box passes through the shelve, whilst the camera collides with it.

Using the code below I can verify that the box intersects with the shelve

if (box.intersectsMesh(shelve, false)) {
                                      box.material.emissiveColor = new BABYLON.Color4(1, 0, 0, 1);
                                    } else {
                                      box.material.emissiveColor = new BABYLON.Color4(1, 1, 1, 1);

I have also added ellipsoid to the box but no difference

box.ellipsoid = new BABYLON.Vector3(0.5, 1.0, 0.5);
box.ellipsoidOffset = new BABYLON.Vector3(0, 1.0, 0);


What am I missing? 

Link to comment
Share on other sites

Hi Hagop.  I'm just saying hi, and wondering if you have had any success.  I think iceman meant... use mesh.moveWithCollisions to move your box with keypresses.  And parent the camera to the box... or use a follow[Arc]Cam targeted to follow the box. 

Here... http://www.babylonjs-playground.com/#LYCSQ#315   ...   is one of the nicest playgrounds EVER (not mine).  It uses cursor keys and its full-powered moveWithCollisions sits at line 166.  This uses a standard ArcRotateCam, but notice the sweet camera LERP in line 174 of the renderLoop.  That gives the camera a nice lagging follow, eh?  That's PRO stuff there, baby.  :)

Would your project work okay with ONLY the box having collisions, and no collisions on the camera itself?  Or do you NEED camera collisions (along with box collisions)?  Thoughts?

Link to comment
Share on other sites

Hi Wingnut

Thank you for the link. It looks great.

No need for parenting.No need for keyboard entry as well (freecamera silently listens to them). I didn't want to use movewithcollisions but non the less. Here is part of the code I used. I need to check rotations further...

scene.registerBeforeRender(function () {
                        registerCameraPosition = scene.activeCamera.position.clone();
                        registerCameraRotation = scene.activeCamera.rotation.clone();
                        xDiff = registerCameraPosition.x - registerCamera_lastPosition.x;
                        yDiff = registerCameraPosition.y - registerCamera_lastPosition.y;
                        zDiff = registerCameraPosition.z - registerCamera_lastPosition.z;
                        globalCart.rotation.y = scene.activeCamera.rotation.y ;
                        globalCart.moveWithCollisions(new BABYLON.Vector3(xDiff,yDiff,zDiff));

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...