My attempt at replicating collision detection

Hi all, over the past couple of days I have been trying to port a popular FPS example from Three JS to Babylon JS. Here is the example:

https://threejs.org/examples/?q=fp#games_fps

I have heard that three js has deprecated the octree system being used to make the collision detection, and I think it is a shame because I have not seen something that has quite the benefits this one has:

  • damping on walking
  • no slide down slopes
  • no collision fighting (or kept at very minimal)

I have noticed within the collision detection at babylon js, you can find heaps of threads on this forum of people complaining about these three issues (hundreds!). Most of the time I have seen the solutions offered to fix these issues are through scene gravity, ellisoids, or ellisoid offsets. I played around with these options and I must admit, it makes me angry to see people recommend these as solutions. Especially when things like scene gravity effect other aspects of environment (no slide on slope but now character falls down at a speed of 2kmh). Instead, I decided to see if I could try to replicate this exact hit detection, seeing that most of the hit detection in that example lies directly in the source. Here is what I have so far:

I have jumping, walking, and some mild collision detection so far. You can see, if you position yourself on the side of stairs, and jump onto the stairs, the camera actually recognizes the depth change and will allow you to stand on top of the stairs. The hardest part is the walking up the stairs. This should be extremely smooth. It all comes down to the second statement in playerCollisions function:

let rayDir = new BABYLON.Ray(camera.position, playerDirection);
  rayDir.length = 10.0;
  const resultsDir = rayDir.intersectsMeshes(obstacles, false);
  if (resultsDir.length > 0) {
    const intersection = resultsDir[0];
    const normal = intersection.getNormal();

    playerOnFloor = normal.y > 0;
    const newValues = normal.scale(intersection.distance);
    camera.position.addInPlace(newValues);
  }

I have played around with rayDir.length, inverted newValues, and even tried inverting normals on meshes to see if the issue could fix, but no matter what it seems the player will continually fall through the ground instead of traveling up it. I must be forgetting something here, but would like to know if you all have any solutions.

Thank you,
coolcomfort

For stairs, I would do a raycast with origin being the from of the player and a direction going down. if the delta with current ground height is aceptable, then the player can move on and have the new ground height.
I don’t understand why you are doing a raycast in the playerDirection.

You may have a look how all this is done here - GitHub - ssatguru/BabylonJS-CharacterController: A CharacterController for BabylonJS