Skip to content

Commit

Permalink
Merge pull request #83 from pmndrs/triggers
Browse files Browse the repository at this point in the history
feat(body): add isTrigger option
  • Loading branch information
marcofugaro authored Mar 6, 2021
2 parents db058cb + de322f9 commit e7c57b6
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
8 changes: 4 additions & 4 deletions examples/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ class Demo extends CANNON.EventTarget {
this.currentMaterial = this.solidMaterial
const contactDotMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff })
this.particleMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 })
this.triggerMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true })

const contactPointGeometry = new THREE.SphereGeometry(0.1, 6, 6)
this.contactMeshCache = new GeometryCache(this.scene, () => {
Expand Down Expand Up @@ -894,10 +895,9 @@ class Demo extends CANNON.EventTarget {
throw new Error('The argument passed to addVisual() is not a body')
}

// if it's a particle paint it red, otherwise just gray
const material = body.shapes.every((s) => s instanceof CANNON.Particle)
? this.particleMaterial
: this.currentMaterial
// if it's a particle paint it red, if it's a trigger paint it as green, otherwise just gray
const isParticle = body.shapes.every((s) => s instanceof CANNON.Particle)
const material = isParticle ? this.particleMaterial : body.isTrigger ? this.triggerMaterial : this.currentMaterial

// get the correspondant three.js mesh
const mesh = bodyToMesh(body, material)
Expand Down
74 changes: 74 additions & 0 deletions examples/trigger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>cannon.js - trigger demo</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
</head>
<body>
<script type="module">
import * as CANNON from '../dist/cannon-es.js'
import { Demo } from './js/Demo.js'

/**
* Demonstrates the use of trigger bodies, bodies which don't
* collide with other bodies and allow you to run callbacks
* when another body enters/exits them.
*/
const demo = new Demo()

demo.addScene('Trigger', () => {
const world = setupWorld(demo)

const radius = 1

// Sphere moving towards right
const sphereShape = new CANNON.Sphere(radius)
const sphereBody = new CANNON.Body({ mass: 1 })
sphereBody.addShape(sphereShape)
sphereBody.position.set(-5, 0, 0)
const impulse = new CANNON.Vec3(5.5, 0, 0)
const topPoint = new CANNON.Vec3(0, radius, 0)
sphereBody.applyImpulse(impulse, topPoint)
sphereBody.linearDamping = 0.3
sphereBody.angularDamping = 0.3
world.addBody(sphereBody)
demo.addVisual(sphereBody)

// Trigger body
const boxShape = new CANNON.Box(new CANNON.Vec3(2, 2, 5))
const triggerBody = new CANNON.Body({ isTrigger: true })
triggerBody.addShape(boxShape)
triggerBody.position.set(5, radius, 0)
world.addBody(triggerBody)
demo.addVisual(triggerBody)

// It is possible to run code on the exit/enter
// of the trigger.
triggerBody.addEventListener('collide', (event) => {
if (event.body === sphereBody) {
console.log('The sphere entered the trigger!', event)
}
})
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)
}
})
})

demo.start()

function setupWorld(demo) {
const world = demo.getWorld()
world.gravity.set(0, 0, 0) // no gravity

return world
}
</script>
</body>
</html>
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
'spring',
'stacks',
'tear',
'trigger',
'trimesh',
'tween',
]
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ It's a type-safe flatbundle (esm and cjs) which allows for **tree shaking** and

These minor changes and improvements were also made:

- These PRs from the original repo were merged: [schteppe/cannon.js#433](https://github.com/schteppe/cannon.js/pull/433), [schteppe/cannon.js#430](https://github.com/schteppe/cannon.js/pull/430), [schteppe/cannon.js#418](https://github.com/schteppe/cannon.js/pull/418), [schteppe/cannon.js#360](https://github.com/schteppe/cannon.js/pull/360), [schteppe/cannon.js#265](https://github.com/schteppe/cannon.js/pull/265), [schteppe/cannon.js#392](https://github.com/schteppe/cannon.js/pull/392)
- These PRs from the original repo were merged: [schteppe/cannon.js#433](https://github.com/schteppe/cannon.js/pull/433), [schteppe/cannon.js#430](https://github.com/schteppe/cannon.js/pull/430), [schteppe/cannon.js#418](https://github.com/schteppe/cannon.js/pull/418), [schteppe/cannon.js#360](https://github.com/schteppe/cannon.js/pull/360), [schteppe/cannon.js#265](https://github.com/schteppe/cannon.js/pull/265), [schteppe/cannon.js#392](https://github.com/schteppe/cannon.js/pull/392), [schteppe/cannon.js#424](https://github.com/schteppe/cannon.js/pull/424)
- The `ConvexPolyhedron` constructor now accepts an object instead of a list of arguments. [#6](https://github.com/pmndrs/cannon-es/pull/6)
- The `Cylinder` is now oriented on the Y axis. [#30](https://github.com/pmndrs/cannon-es/pull/30)
- The `type` property of the `Cylinder` is now equal to `Shape.types.CYLINDER`. [#59](https://github.com/pmndrs/cannon-es/pull/59)
- `Body.applyImpulse()` and `Body.applyForce()` are now relative to the center of the body instead of the center of the world [86b0444](https://github.com/schteppe/cannon.js/commit/86b0444c93356aeaa25dd1af795fa162574c6f4b)
- Sleeping bodies now wake up if a force or an impulse is applied to them [#61](https://github.com/pmndrs/cannon-es/pull/61)
- Added a property `World.hasActiveBodies: boolean` which will be false when all physics bodies are sleeping. This allows for invalidating frames when physics aren't active for increased performance.
- Add support for [Trigger bodies](https://pmndrs.github.io/cannon-es/examples/trigger). [#83](https://github.com/pmndrs/cannon-es/pull/83)
- Deprecated properties and methods have been removed.
- The [original cannon.js debugger](https://github.com/schteppe/cannon.js/blob/master/tools/threejs/CannonDebugRenderer.js), which shows the wireframes of each body, has been moved to its own repo [cannon-es-debugger](https://github.com/pmndrs/cannon-es-debugger).

Expand Down
Binary file added screenshots/trigger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/objects/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type BodyOptions = {
linearFactor?: Vec3
angularFactor?: Vec3
shape?: Shape
isTrigger?: boolean
}

/**
Expand Down Expand Up @@ -130,6 +131,12 @@ export class Body extends EventTarget {
aabbNeedsUpdate: boolean // Indicates if the AABB needs to be updated before use.
boundingRadius: number // Total bounding radius of the Body including its shapes, relative to body.position.
wlambda: Vec3
/**
* When true the body behaves like a trigger. It does not collide
* with other bodies but collision events are still triggered.
* @default false
*/
isTrigger: boolean

static idCounter: number
static COLLIDE_EVENT_NAME: 'collide'
Expand Down Expand Up @@ -246,6 +253,7 @@ export class Body extends EventTarget {
this.aabbNeedsUpdate = true
this.boundingRadius = 0
this.wlambda = new Vec3()
this.isTrigger = Boolean(options.isTrigger)

if (options.shape) {
this.addShape(options.shape)
Expand Down
2 changes: 1 addition & 1 deletion src/solver/Solver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Solver {
* @param {Equation} eq
*/
addEquation(eq: Equation): void {
if (eq.enabled) {
if (eq.enabled && !eq.bi.isTrigger && !eq.bj.isTrigger) {
this.equations.push(eq)
}
}
Expand Down

0 comments on commit e7c57b6

Please sign in to comment.