
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
use-cannon
Advanced tools
yarn add use-cannon
Experimental React hooks for cannon. Use this in combination with react-three-fiber.
Ping pong: https://codesandbox.io/s/white-resonance-0mgum
Cube pushing spheres away: https://codesandbox.io/s/r3f-cannon-instanced-physics-devf8
Heap of cubes: https://codesandbox.io/s/r3f-cannon-instanced-physics-g1s88
import { Physics, useBox, ... } from 'use-cannon'
<Physics>{/* Physics related objects in here please */}</Physics>
const [ref, api] = useBox(() => ({ mass: 1 }))
<mesh ref={ref} geometry={...} material={...} />
useFrame(({ clock }) => api.position.set(Math.sin(clock.getElapsedTime()) * 5, 0, 0))
const velocity = useRef([0, 0, 0])
useEffect(() => api.velocity.subscribe((v) => (velocity.current = v)), [])
Let's make a cube falling onto a plane. You can play with a sandbox here.
import { Canvas } from 'react-three-fiber'
import { Physics, usePlane, useBox } from 'use-cannon'
function Plane(props) {
const [ref] = usePlane(() => ({ rotation: [-Math.PI / 2, 0, 0], ...props }))
return (
<mesh ref={ref}>
<planeBufferGeometry attach="geometry" args={[100, 100]} />
</mesh>
)
}
function Cube(props) {
const [ref] = useBox(() => ({ mass: 1, position: [0, 5, 0], ...props }))
return (
<mesh ref={ref}>
<boxBufferGeometry attach="geometry" />
</mesh>
)
}
ReactDOM.render(
<Canvas>
<Physics>
<Plane />
<Cube />
</Physics>
</Canvas>,
document.getElementById('root')
)
function Physics({
children,
step = 1 / 60,
gravity = [0, -10, 0],
tolerance = 0.001,
iterations = 5,
allowSleep = false,
broadphase = 'Naive',
axisIndex = 0,
defaultContactMaterial = {
contactEquationStiffness: 1e6,
},
// Maximum amount of physics objects inside your scene
// Lower this value to save memory, increase if 1000 isn't enough
size = 1000,
}: ProviderProps): JSX.Element
function usePlane(fn: PlaneFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useBox(fn: BoxFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useCylinder(fn: CylinderFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useHeightfield(fn: HeightfieldFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useParticle(fn: ParticleFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useSphere(fn: SphereFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useTrimesh(fn: TrimeshFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useConvexPolyhedron(fn: ConvexPolyhedronFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function useCompoundBody(fn: CompoundBodyFn, ref?: React.MutableRefObject<THREE.Object3D>): Api
function usePointToPointConstraint(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: PointToPointConstraintOpts,
deps: any[] = []
): ConstraintApi
function useConeTwistConstraint(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: ConeTwistConstraintOpts,
deps: any[] = []
): ConstraintApi
function useDistanceConstraint(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: DistanceConstraintOpts,
deps: any[] = []
): ConstraintApi
function useHingeConstraint(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: HingeConstraintOpts,
deps: any[] = []
): ConstraintApi
function useLockConstraint(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: LockConstraintOpts,
deps: any[] = []
): ConstraintApi
function useSpring(
bodyA: React.MutableRefObject<THREE.Object3D>,
bodyB: React.MutableRefObject<THREE.Object3D>,
optns: SpringOptns,
deps: any[] = []
): void
type WorkerApi = WorkerProps<AtomicProps> & {
position: WorkerVec
rotation: WorkerVec
velocity: WorkerVec
angularVelocity: WorkerVec
linearFactor: WorkerVec
angularFactor: WorkerVec
applyForce: (force: number[], worldPoint: number[]) => void
applyImpulse: (impulse: number[], worldPoint: number[]) => void
applyLocalForce: (force: number[], localPoint: number[]) => void
applyLocalImpulse: (impulse: number[], localPoint: number[]) => void
}
type Api = [
React.MutableRefObject<THREE.Object3D | undefined>,
WorkerApi & {
at: (index: number) => WorkerApi
}
]
type ConstraintApi = [
React.MutableRefObject<THREE.Object3D>,
React.MutableRefObject<THREE.Object3D>,
{
enable: () => void
disable: () => void
}
]
type ProviderProps = {
children: React.ReactNode
gravity?: number[]
tolerance?: number
step?: number
iterations?: number
allowSleep?: boolean
broadphase?: 'Naive' | 'SAP'
axisIndex?: number
defaultContactMaterial?: {
friction?: number
restitution?: number
contactEquationStiffness?: number
contactEquationRelaxation?: number
frictionEquationStiffness?: number
frictionEquationRelaxation?: number
}
size?: number
}
type AtomicProps = {
mass?: number
material?: { friction?: number; restitution?: number }
linearDamping?: number
angularDamping?: number
allowSleep?: boolean
sleepSpeedLimit?: number
sleepTimeLimit?: number
collisionFilterGroup?: number
collisionFilterMask?: number
fixedRotation?: boolean
}
type BodyProps = AtomicProps & {
args?: any
position?: number[]
rotation?: number[]
velocity?: number[]
angularVelocity?: number[]
linearFactor?: number[]
angularFactor?: number[]
type?: 'Dynamic' | 'Static' | 'Kinematic'
onCollide?: (e: Event) => void
}
type Event = {
op: string
type: string
body: THREE.Object3D
target: THREE.Object3D
contact: {
ni: number[]
ri: number[]
rj: number[]
impactVelocity: number
}
collisionFilters: {
bodyFilterGroup: number
bodyFilterMask: number
targetFilterGroup: number
targetFilterMask: number
}
}
type PlaneProps = BodyProps & {}
type ParticleProps = BodyProps & {}
type BoxProps = BodyProps & {
args?: number[] // extents: [x, y, z]
}
type CylinderProps = BodyProps & {
args?: [number, number, number, number] // radiusTop, radiusBottom, height, numSegments
}
type SphereProps = BodyProps & {
args?: number // radius
}
type TrimeshProps = BodyProps & {
args?: [number[][], number[][]] // vertices: [[x, y, z], ...], indices: [[a, b, c], ...]
}
type ConvexPolyhedronProps = BodyProps & {
args?:
| THREE.Geometry
// vertices: [[x, y, z], ...], faces: [[a, b, c], ...]
| [(THREE.Vector3 | number[])[], (THREE.Face3 | number[])[]]
}
type HeightfieldProps = BodyProps & {
args?: [
number[], // data
{
minValue?: number
maxValue?: number
elementSize?: number
}
]
}
type CompoundBodyProps = BodyProps & {
shapes: {
type: ShapeType
args?: any
position?: number[]
rotation?: number[]
}[]
}
type PlaneFn = (index: number) => PlaneProps
type BoxFn = (index: number) => BoxProps
type CylinderFn = (index: number) => CylinderProps
type HeightfieldFn = (index: number) => HeightfieldProps
type ParticleFn = (index: number) => ParticleProps
type SphereFn = (index: number) => SphereProps
type TrimeshFn = (index: number) => TrimeshProps
type ConvexPolyhedronFn = (index: number) => ConvexPolyhedronProps
type CompoundBodyFn = (index: number) => CompoundBodyProps
type ConstraintOptns = { maxForce?: number; collideConnected?: boolean; wakeUpBodies?: boolean }
type PointToPointConstraintOpts = ConstraintOptns & {
pivotA: number[]
pivotB: number[]
}
type ConeTwistConstraintOpts = ConstraintOptns & {
pivotA?: number[]
axisA?: number[]
pivotB?: number[]
axisB?: number[]
angle?: number
twistAngle?: number
}
type DistanceConstraintOpts = ConstraintOptns & { distance?: number }
type HingeConstraintOpts = ConstraintOptns & {
pivotA?: number[]
axisA?: number[]
pivotB?: number[]
axisB?: number[]
}
type LockConstraintOpts = ConstraintOptns & {}
type SpringOptns = {
restLength?: number
stiffness?: number
damping?: number
worldAnchorA?: number[]
worldAnchorB?: number[]
localAnchorA?: number[]
localAnchorB?: number[]
}
FAQs
physics based hooks for react-three-fiber
The npm package use-cannon receives a total of 180 weekly downloads. As such, use-cannon popularity was classified as not popular.
We found that use-cannon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.