Question

I've been screwing around with this collision response far to long now. I thought i'd ask you guys for some guidance.

http://jsbin.com/qoyuciti/1 Edit link: http://jsbin.com/qoyuciti/1/edit?html (just know that you can't use the movement keys in jsbin edit mode (as far as i know))

This JSBin shows what i have at the moment. I can move around and when i hit the box i won't go trough and i glide of the box. There are two problems:

  1. Gliding left, all goes well, gliding right it starts "hopping";
  2. Sometimes after colliding for like 20 seconds the sphere goes trough the box.. I think this might have to do something with the "hopping" i'm experiencing when gliding right.

A quick explanation of my approach

User starts walking, as soon as i intersect with the box i start testing for intersects in a 180 degree cone in front of the sphere (the direction the user is heading). As soon as it finds an empty spot it will put the player there.

If anyone has a better approach please let me know. As i'm explaining my code it seems like this could go more efficient but let me know :)

Thanks in advance!

Was it helpful?

Solution 2

I've fixed the problem i was experiencing. Here's the code for anyone who needs it :)

var intersect = box.intersectsPoint(player.position);
if (intersect) {
    var x = prevX,
        z = prevZ,
        slideSpeed = speed * 0.7;


    for (var angle = 0; angle > -90; angle -= 1) {

        x = prevX - Math.sin((direction - angle) * Math.PI / 180) * slideSpeed;
        z = prevZ - Math.cos((direction - angle) * Math.PI / 180) * slideSpeed;

        var intersect = box.intersectsPoint(new BABYLON.Vector3(x, player.position.y, z), true);
        if (!intersect) {
            break;
        }

        x = prevX - Math.sin((direction + angle) * Math.PI / 180) * slideSpeed;
        z = prevZ - Math.cos((direction + angle) * Math.PI / 180) * slideSpeed;

        var intersect = box.intersectsPoint(new BABYLON.Vector3(x, player.position.y, z), true);
        if (!intersect) {
            break;
        }

    }

    player.position.x = x;
    player.position.z = z;

}

OTHER TIPS

You can perhaps use the internal collision system but creating a fake freecamera and add your sphere as child of the camera. Then you can move the camera using keys and automatically babylonjs will compute collisions for you

Else you can also use the private function scene._getNewPosition to use the collision engine directly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top