cloudsketcher Posted December 15, 2013 Share Posted December 15, 2013 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) Thanks, code: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(); });}; Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 15, 2013 Share Posted December 15, 2013 it should work....Do the collisions never work or do the collisions sometimes work? Quote Link to comment Share on other sites More sharing options...
cloudsketcher Posted December 15, 2013 Author Share Posted December 15, 2013 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) Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 15, 2013 Share Posted December 15, 2013 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? Quote Link to comment Share on other sites More sharing options...
cloudsketcher Posted December 15, 2013 Author Share Posted December 15, 2013 Ahhhh I see, would you be able to suggest what is best practice if I want the moving object to push the camera out of the way? Thank you Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 16, 2013 Share Posted December 16, 2013 You can make small movements (in this case the camera will compensate) You can also detect a collision with the camera and then stick the camera on your object Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.