Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
Comment on lines +54 to +61
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

})

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