Reputation: 1324
I'm just diving into Cannon.js and was wondering how to achieve this.
My use case is that I have a circular area on the floor where if the player steps inside, they will trigger some interactions. I didn't want to do raycasting for this because I was already using Cannon.js for other collisions and felt that raycasting would add another layer of performance latency.
Right now my player object is a simple sphere shape RigidBody that I move by setting its velocity. I made the interaction area to be a Cylinder shape with a very low height. However, when the player goes over this object, the collision gets registered successfully but the player spins out of control and there's a noticeable bump in the movement.
Is there a standard way to register these kinds of objects for Cannon.js? I would like it so there is no bump, almost as if it's an invisible object that the player can pass through but it still registers collisions.
Upvotes: 1
Views: 461
Reputation: 31076
A typical way of solving this issue in games is the usage of triggers. So physics engines are unrelated to this topic.
A trigger can be implemented as a simple bounding volume like a bounding sphere. In three.js
, it's an instance of THREE.Sphere. Each simulation step, you test if the trigger was activated by the player. You can do this in various ways. For example by just testing if the player's position is inside the bounding sphere via Sphere.containsPoint(). Or you represent the player as another bounding volume like a bounding sphere or AABB (via THREE.Box3) and then perform an intersection test.
Both approaches are very fast and should not noticeable affect the performance of your app. Even if you are doing these tests with more game entities who potentially activate triggers.
Here is a simple example that demonstrates the concept of triggers in Yuka:
https://mugen87.github.io/yuka/examples/entity/trigger/
Upvotes: 2