Jump to content

[Newbie Question] Prevent camera from moving beyond a plain with collision detection


steverob
 Share

Recommended Posts

Hi, I'm trying to build a room like scene with a ground mesh and 4 walls. I've enabled collision detection on all the meshes and also the camera and have enabled gravity in the scene. Still I am able to move the FreeCamera beyond the wall and I fall off the ground :( 

Here is my code - 

var createScene = function() {
      // create a basic BJS Scene object
      var newScene = new BABYLON.Scene(engine);

      newScene.ambientColor = new BABYLON.Color3(0.3, 0.3, 0.3);
      var skybox = BABYLON.Mesh.CreateBox("skyBox", 10000.0, newScene);
      var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", newScene);
      skyboxMaterial.backFaceCulling = false;
      skyboxMaterial.disableLighting = true;
      skybox.material = skyboxMaterial;
      skybox.infiniteDistance = true;
      skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
      skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
      

      // create a FreeCamera, and set its position to (x:0, y:5, z:-10)
      camera = new BABYLON.FreeCamera('camera1', new BABYLON.Vector3(0, 2, 0), newScene);
      // specify earth like gravity
      newScene.gravity = new BABYLON.Vector3(0, -9.81, 0);
      camera.applyGravity = true;

      // collision region around the camera
      camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
      // Enable Collisions
      newScene.collisionsEnabled = true;
      camera.checkCollisions = true;

      // target the camera to scene origin
      // camera.setTarget(BABYLON.Vector3.Zero());

      // attach the camera to the canvas
      camera.attachControl(canvas, false);

      // create a basic light, aiming 0,1,0 - meaning, to the sky
      var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), newScene);

      // create a built-in "ground" shape; its constructor takes the same 5 params as the sphere's one
      var ground = BABYLON.Mesh.CreateGround('ground1', 200, 200, 2, newScene);
      ground.checkCollisions = true;

      var wallMaterial = new BABYLON.StandardMaterial("texture1", newScene);
      var wall_north = BABYLON.MeshBuilder.CreatePlane("wall", {width: 200, height: 30, sideOrientation: BABYLON.Mesh.DOUBLESIDE}, newScene);
      wall_north.position = {
        x: 0,
        y: 15,
        z: 100
      };
      wall_north.material = wallMaterial;
      wall_north.checkCollisions = true;

      var wall_south = BABYLON.MeshBuilder.CreatePlane("wall1", {width: 200, height: 30, sideOrientation: BABYLON.Mesh.DOUBLESIDE}, newScene);
      wall_south.position = {
        x: 0,
        y: 15,
        z: -100
      };
      wall_south.material = wallMaterial;
      wall_south.checkCollisions = true;

      var wall_east = BABYLON.MeshBuilder.CreatePlane("wall1", {width: 200, height: 30, sideOrientation: BABYLON.Mesh.DOUBLESIDE}, newScene);
      wall_east.position = {
        x: 100,
        y: 15,
        z: 0
      };
      wall_east.rotation.y = 4.71239;
      wall_east.material = wallMaterial;
      wall_east.checkCollisions = true;

      var wall_west = BABYLON.MeshBuilder.CreatePlane("wall1", {width: 200, height: 30, sideOrientation: BABYLON.Mesh.DOUBLESIDE}, newScene);
      wall_west.position = {
        x: -100,
        y: 15,
        z: 0
      };
      wall_west.rotation.y = 4.71239;
      wall_west.material = wallMaterial;
      wall_west.checkCollisions = true;

    //   var plane = BABYLON.Mesh.CreatePlane("plane", 1, newScene);
    // plane.position.y = -5;
    // plane.rotation.x = Math.PI / 2;

      // return the created scene
      return newScene;
    }

Help appreciated :)

Here is the playground link - https://www.babylonjs-playground.com/#F6MR6L 

It seems like if I move sideways only I am able to escape the room.

Link to comment
Share on other sites

Hiya @steverob, welcome.

I'm wingnut, I get into everyone's business, but rarely helpful.  :)  Howyadoon?  I hope well.

I was playing... https://www.babylonjs-playground.com/#F6MR6L#5

Took gravity off camera, turned-on bounding boxes, changed the skybox, made the walls higher... and yep... you can INDEED escape the box... with sideways-forward and sideways-backward camera moves (cursor keys).  Are these walls made-of cardboard?  They seem a little weak, eh?

I'm far from an expert on BJS colliders.  Hopefully, smart people will answer soon.  I just wanted to say HI and show you how I messed-up your playground demo.  :)

A forum search for "workers" might give results.  There might be a "check-collision step-speed" that could be increased.  *shrug*  I tested camera.ellipsoid = new BABYLON.Vector3(2, 2, 2); ...made it more difficult, but was still able to escape.  hmm.  Talk soon.

Update:  Box walls version, with 2, 2, 2 elipsoid.  Escape-proof, I think!  :)  Perhaps, test with planes that slightly overlap at seams?  hmm.

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.

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

Loading...
 Share

  • Recently Browsing   0 members

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