Jump to content

Collision between camera and moving mesh unreliable?


Recommended Posts

Hi there,


Collision detection worked very well for me between the camera and stationary meshes.

However when the mesh also moves, the collision is often not detected? (The camera moves right through)

For example a mesh moves into the camera, or the camera moves into a moving mesh.


Is this an area that I need to code for? Or am I not using the collision detection feature correctly?

(The attached code moves the mesh via animation, but explicitly changing mesh.position also yields same result - the camera moves right through the mesh)





var main = function(canvasId) {    var canvas = document.getElementById(canvasId);    if (!BABYLON.Engine.isSupported()) {        window.alert('Browser not supported');        return;    }    var engine = new BABYLON.Engine(canvas, true);    var scene = new BABYLON.Scene(engine);    scene.gravity = new BABYLON.Vector3(0, -9.8, 0);    scene.collisionsEnabled = true;    var sunLight = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene);        var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, 0, -20), scene);    camera.checkCollisions = true;    camera.applyGravity = true;    camera.ellipsoid = new BABYLON.Vector3(1, 2, 1);    // Ground    var groundMaterial = new BABYLON.StandardMaterial("groundMat", scene);    groundMaterial.diffuseColor = new BABYLON.Color3(1, 1, 1);    groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);    var ground = BABYLON.Mesh.CreatePlane("ground", 1000.0, scene);    ground.material = groundMaterial;    ground.position = new BABYLON.Vector3(5, -10, -15);    ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);    ground.checkCollisions = true;    // Simple box    var boxMaterial = new BABYLON.StandardMaterial("boxMat", scene);    boxMaterial.diffuseColor = new BABYLON.Color3(1, 0, 0);    boxMaterial.specularColor = new BABYLON.Color3(1, 0, 0);    var box = new BABYLON.Mesh.CreateBox("crate", 5, scene);    box.material = boxMaterial;    box.position = new BABYLON.Vector3(5, -9, -10);    box.checkCollisions = true;    box.counter = 20;    var animationBox = new BABYLON.Animation("animation", "position.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT,            BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);    var keys = [ {        frame : 0,        value : 5    }, {        frame : 50,        value : 50    }, {        frame : 100,        value : 5    } ];    animationBox.setKeys(keys);    box.animations.push(animationBox);    scene.beginAnimation(box, 0, 100, true);    // Attach the camera to the scene    scene.activeCamera.attachControl(canvas);    // Once the scene is loaded, just register a render loop to render it    engine.runRenderLoop(function() {        scene.render();    });    window.addEventListener("resize", function() {        engine.resize();    });};
Link to comment
Share on other sites

Hi - hm it sometimes work. 


So against stationary object it always work.


If I move the camera into a moving object, it sometimes works, but if I persist, then the camera will move through the object after a few tries.


If I sit still and let the object move into me, then it never works (the object will always move through the camera as though it is transparent)

Link to comment
Share on other sites

Collisions are implemented in the camera move functions not in the animation engines so if you stand still collisions will not be applied :(


If you move towards an animated object the problem is that the camera will evaluate collisions but not the animated object (nothing will prevent it for moving)


Sounds clear?

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