Jump to content

Strange camera behavior with collisions enabled


Recommended Posts


I would like to understand a strange behavior of my camera and how to prevent it. Let me explain.

I have a mesh that I like to see from different point of views with an ArcRotateCamera. If collisions are enabled, if the camera move from viewpoint1 to viewpoint2 and the translation between these 2 points intersects the mesh, the new camera position is not the expected one. If collisions are disabled, it works. If the translation doesn't intersect the mesh, it also works.

Here is a PG to illustrate my issue :

  • you can change viewpoints by pressing keys  '1', '2', '3', '4', '5' or '6' to see respectively the cube from front, left, back, right, top or bottom viewpoint
  • you can toggle collision by pressing 'c' 

If collisions are enabled, my issue happens when :

  • switching from opposite viewpoints (1 -> 3, 2 -> 4, 5 -> 6), you obviously intersect the cube
  • if you are close enough of the cube, moving to any viewpoint will intersect the cube and pressing consecutively the same key will not have the same result until the camera is not in the intended position

If collisions are disabled, camera's behavior is the expected one.

I don't know why my camera moves like this. I assume that the camera is not teleported from a viewpoint to another but it is translated which implies a collision with my mesh.  Can it be fix in Babylon.js ? Do I misuse the library ? How can I prevent this behavior ? I succeed to avoid it but I have to use window.setTimeout twice to disable collisions, then move my camera and enable collisions.

    function setViewpoint(viewpoint) {
        camera.checkCollisions = false;
        window.setTimeout(function() {
            camera.alpha = viewpoint.alpha;
            camera.beta = viewpoint.beta;
            window.setTimeout(function() {
                camera.checkCollisions = true;
            }, 100);
        }, 100);

It is a bit tricky and I'd like to find another way to do it.




Link to comment
Share on other sites

Hi @yuccai, good to see you again.  http://www.babylonjs-playground.com/#1XDIJF#4

First, you had a split code-line around line 38... maybe no big deal.  The seemingly important changes happened in your setViewpoint() where I added camera.checkCollision toggling AND some scene.render()'s.  What the heck, it started working... but I'm not sure why. 

A famous adage:  Even a blind squirrel (Wingnut) finds an occasional acorn (a lucky fix).  heh

The reason I thought that a scene.render() or camera.update() was needed... was because of your DUAL (nested) setTimeout.  That got me thinkin' (which usually just makes me need more coffee, but not THIS time)  :D  Hope this helps.  Lines 54 and 55 might need to swap order.

We should probably study this, because this solve is rather kludgy, eh?  Perhaps unavoidable.  Hopefully, smarter people than I... will reply.

PS:  I don't think there is a camera.update().  :)  We had a similar thing with gravity on ELEVATED cameras.  The camera wouldn't start dropping when the scene finished loading (it needed a cursor keypress first).  Then one day, somebody pointed-out... camera. _needMoveForGravity ... a non-public boolean property (available on SOME cameras).  Setting that true...  made the camera fall when sceneReady.  Wild, huh?  I bet you didn't expect an exciting story like that... as a little tag-on gift (snore).  :)

Link to comment
Share on other sites

Thanks for this fast answer. I'm glad your solution is working even if it's still tricky. I'll use it temporarily.

I think that this behavior is not expected and Babylon need a fix. What's your point of view @Deltakosh ?

From the few I know, the camera position is correct before this line. The new position is computed here and especially here. Then it becomes more complicated, it mainly uses Collider class.


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