Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(body): add isTrigger option #83

Merged
merged 6 commits into from
Mar 6, 2021
Merged

feat(body): add isTrigger option #83

merged 6 commits into from
Mar 6, 2021

Conversation

marcofugaro
Copy link
Member

Originally proposed here schteppe#146 and coded in this PR schteppe#424.

Trigger allow you to detect if a body has entered a certain zone. They can let you run code when the body either entered or exited the trigger. They exist in other game engines such as Unity or Unreal.

You define a trigger by passing using the isTrigger option.

const boxShape = new CANNON.Box(new CANNON.Vec3(2, 2, 5))
const triggerBody = new CANNON.Body({ isTrigger: true })
triggerBody.addShape(boxShape)

Comment on lines +54 to +61
world.addEventListener('endContact', (event) => {
if (
(event.bodyA === sphereBody && event.bodyB === triggerBody) ||
(event.bodyB === sphereBody && event.bodyA === triggerBody)
) {
console.log('The sphere exited the trigger!', event)
}
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably emit endContact on the body as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants