Skip to main content

Ricochet2DBehavior

@zylem/game-lib


@zylem/game-lib / behavior / Ricochet2DBehavior

Variable: Ricochet2DBehavior

const Ricochet2DBehavior: BehaviorDescriptor<Ricochet2DOptions, Ricochet2DHandle, unknown>

Defined in: node_modules/.pnpm/@zylem+behaviors@0.8.9_@types+react@19.3.0_react@19.3.0_three@0.185.1/node_modules/@zylem/behaviors/dist/ricochet-2d.d.ts:272

Ricochet2DBehavior

Example​

import { Ricochet2DBehavior } from "@zylem/game-lib/behavior";

const ball = createSphere({ ... });
const ricochet = ball.use(Ricochet2DBehavior, {
minSpeed: 3,
maxSpeed: 15,
reflectionMode: 'angled',
});

ball.onCollision(({ entity, other }) => {
const velocity = entity.body.linvel();
const result = ricochet.getRicochet({
selfVelocity: velocity,
contact: { normal: { x: 1, y: 0 } }, // from collision data
});

if (result) {
entity.body.setLinvel(result.velocity, true);
}
});