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(colliders): added the ability to use sensors on colliders #90

Closed
wants to merge 2 commits into from

Conversation

micmania1
Copy link
Contributor

@micmania1 micmania1 commented Sep 17, 2022

I've added the ability to use sensor colliders: https://rapier.rs/docs/user_guides/javascript/colliders#collider-type

You can see it in action in the Collider Sensor demo.

Use Case

When a player throws a ball into a bucket, I need to figure out whether they've got it in the bucket or missed the shot.

Usage

RigidBody

<RigidBody colliders="cuboid" colliderType="sensor" onCollisionEnter={e => console.log('collision enter')} onCollisionExit={e => console.log('collision exit')}> 
  <Box />
</RigidBody>

Collider

<SomeCollider type="sensor" onCollisionEnter={e => console.log('collision enter')} onCollisionExit={e => console.log('collision exit')}> 
  <Box />
</SomeCollider>

PR Side effects

The collisition event signatures have changes - target (rigid body) may now be undefined in the case of colliders (these currently don't work standalone). manifold and flipped aren't passed through as part of the intersection version of the events.

@changeset-bot
Copy link

changeset-bot bot commented Sep 17, 2022

⚠️ No Changeset found

Latest commit: 5855483

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Sep 17, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
react-three-rapier ✅ Ready (Inspect) Visit Preview Sep 17, 2022 at 11:19PM (UTC)


/* Collider events */
collider1Events?.onCollisionEnter?.({
target: rigidBody2,
Copy link
Contributor Author

@micmania1 micmania1 Sep 17, 2022

Choose a reason for hiding this comment

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

Note that there's a subtle difference here. rigidBody2 could be undefined. Same goes for the other events.

const rigidBodyHandle2 = collider2.parent()?.handle;

if (!collider1 || !collider2 || rigidBodyHandle1 === undefined || rigidBodyHandle2 === undefined) {
if (!collider1 || !collider2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found that I couldn't trigger collision events on colliders which didn't have rigid bodies. The only reason I could see for this was that the event below requires a rigid body in the payload.

If we remove that I think we can enable collision events on standalone colliders.

} else {
world.contactPair(collider1, collider2, (manifold, flipped) => {
/* RigidBody events */
rigidBody1Events?.onCollisionEnter?.({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've re-used the existing events onCollisionEnter and onCollisionExit. The exit event is identical and even triggered in the same way below, but the enter event is quite different.

There's currently no way to differentiate on the exit event apart from checking whether sensor is set to true. We could try that if we wanted to differentiate between contact collision and intersection collision.

@wiledal
Copy link
Member

wiledal commented Sep 23, 2022

Hey!
I appreciate the effort, but the planned API for this is slightly different!

Essentially:

<SomeCollider sensor />
<RigidBody sensor />

Lots of parts have undergone a complete rebuild behind the scenes, and adding props like this will be streamlined when that's finished.

Your points regarding the collision events for standalone colliders are valid, and I'll come back to them once I'm done refactoring, thanks for that! I'll keep this here for now as a reminder, and will lift some parts of it with you in the credits :)

@wiledal
Copy link
Member

wiledal commented Sep 27, 2022

Sensors are added with a slightly different api in #100!
See demo/src/sensors/SensorsExample.tsx

@wiledal wiledal closed this Sep 27, 2022
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.

None yet

2 participants