Wall Collision Detection

Hello,

I have entity with collision and kinematic rigid body and movement based on mouse input, with ray casting. Iโ€™m trying to add a collision against walls (the wall entity has a static rigid body and mesh collider). My logic should not let the item through walls, snap against the wall but still be able to follow the mouse in any direction. Can anybody help me with this feature.

Thanks. :smiley:

Hi @Lukas_31 and welcome! I would use a short forward raycast from the player and if this raycast hit something you block the movement. When the player rotates, the player is be able to move again.

2 Likes

Thank you! I was thinking to have raycast from aabb corners because every item is different in scale.

Not sure there is an easy way, considering all your objects are either static or kinematic. You will have a placed chair intersect another placed chair, for example.

If I would do it, I would probably go for a vertical shape cast, where a shape would match the top profile of an object you are placing. Like these chairs would be spheres, table would be a box, etc. Then a pseudo code:

onMouseMove() {
    // e.g. you have a 3D cursor in world, where an 
    // object is placed on its position. We can cast from floor to
    // ceiling on its position:
    var start = cursor.floorPosition;
    var end = cursor.ceilingPosition;

    var result = shapecast(from,  to, sphereShape);
    if (result.entity.name !== "Wall") {
        chair.setPosition(cursor.position);
    } else {
        // calculate closest position to cursor, based on collision result
       var pos = getClosestPosition();
       chair.setPosition(pos);
    }
}

Edit:
Thinking a bit more over it. I think Ammo allows to do a query if a shape intersects another shape. If yes, you could use that then, instead of a shapecast.

Edit2:
Another option, if you donโ€™t want to do sweep tests, you could create a ghost object (btPairCachingGhostObject). It holds all objects that intersect it, so if you create a sphere shaped ghost object and place it at the position of the chair - it will show all objects that intersect it and you can then update the position, based on what it collided with.

1 Like

Sorry, I thought you wanted to move in the room, but now I understand you want to place objects in the room. You can ignore my suggestion.