Skip to content

Instantly share code, notes, and snippets.

@dested
Created March 24, 2022 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dested/f981ea63843f6a7817d5950f161f31ba to your computer and use it in GitHub Desktop.
Save dested/f981ea63843f6a7817d5950f161f31ba to your computer and use it in GitHub Desktop.
function test() {
const rapierEventQueue = new EventQueue(true);
let world = new World({x: 0.0, y: 0, z: 0.0});
// create the kinematic controller
const characterController = createObjectFromDesc(
world,
{
status: 2,
translation: {x: 1.4591663825619798, y: 1.375, z: -2.1640342585282166},
rotation: {x: 0, y: 0, z: 0, w: 1},
gravityScale: 1,
linvel: {x: 0, y: 0, z: 0},
mass: 10,
centerOfMass: {x: 0, y: 0, z: 0},
translationsEnabledX: true,
translationsEnabledY: true,
angvel: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
translationsEnabledZ: true,
rotationsEnabledX: true,
rotationsEnabledY: true,
rotationsEnabledZ: true,
linearDamping: 0,
angularDamping: 0,
canSleep: true,
ccdEnabled: false,
dominanceGroup: 0,
},
{
shape: new Cuboid(0.5, 0.5, 0.5),
useMassProps: false,
density: 1,
friction: 0,
restitution: 0,
rotation: {x: 0, y: 0, z: 0, w: 1},
translation: {x: 0, y: 0, z: 0},
isSensor: false,
collisionGroups: 4294967295,
solverGroups: 4294967295,
frictionCombineRule: 0,
restitutionCombineRule: 0,
activeCollisionTypes: 60943,
activeEvents: 1,
activeHooks: 0,
mass: 0,
centerOfMass: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
}
);
let rigidBody = world.getRigidBody(characterController.bodyHandle);
// creat the kinematic pendant we will collide with
const pendant = createObjectFromDesc(
world,
{
status: 2,
translation: {x: 0.6875, y: 1.375, z: 2.875},
rotation: {x: 0, y: 0, z: 0, w: 1},
gravityScale: 1,
linvel: {x: 0, y: 0, z: 0},
mass: 10,
centerOfMass: {x: 0, y: 0, z: 0},
translationsEnabledX: true,
translationsEnabledY: true,
angvel: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
translationsEnabledZ: true,
rotationsEnabledX: true,
rotationsEnabledY: true,
rotationsEnabledZ: true,
linearDamping: 0,
angularDamping: 0,
canSleep: true,
ccdEnabled: false,
dominanceGroup: 0,
},
{
shape: new Cuboid(0.5, 0.5, 0.1875),
useMassProps: false,
density: 1,
friction: 0,
restitution: 0,
rotation: {x: 0, y: 0, z: 0, w: 1},
translation: {x: 0, y: 0, z: 0},
isSensor: true,
collisionGroups: 131071,
solverGroups: 4294967295,
frictionCombineRule: 0,
restitutionCombineRule: 0,
activeCollisionTypes: 60943,
activeEvents: 1,
activeHooks: 0,
mass: 0,
centerOfMass: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
}
);
function moveCharacterController() {
const collider = world.getCollider(characterController.colliderHandle);
let foundCollisionGroup = collider.collisionGroups();
/* THIS IS THE KEY */
// we reset the collision group
const group = foundCollisionGroup;
collider.setCollisionGroups(0b0000_0000_0000_0000_0000_0000_0000_0000);
collider.setCollisionGroups(group);
/* THIS IS THE KEY */
rigidBody = world.getRigidBody(characterController.bodyHandle);
// we move the body forward
rigidBody.setNextKinematicTranslation(
new Vector3(rigidBody.translation().x, rigidBody.translation().y, rigidBody.translation().z + 1 / 16)
);
rigidBody.setNextKinematicRotation(rigidBody.rotation());
}
let hasCollide = false;
while (!hasCollide) {
// move forward until it collides with the pendant
moveCharacterController();
world.step(rapierEventQueue);
rapierEventQueue.drainCollisionEvents((handle1, handle2, started) => {
hasCollide = true;
});
}
// when it does, create a shield
const shield = createObjectFromDesc(
world,
{
status: 2,
translation: {x: 0, y: 0, z: 0},
rotation: {x: 0, y: 0, z: 0, w: 1},
gravityScale: 1,
linvel: {x: 0, y: 0, z: 0},
mass: 10,
centerOfMass: {x: 0, y: 0, z: 0},
translationsEnabledX: true,
translationsEnabledY: true,
angvel: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
translationsEnabledZ: true,
rotationsEnabledX: true,
rotationsEnabledY: true,
rotationsEnabledZ: true,
linearDamping: 0,
angularDamping: 0,
canSleep: true,
ccdEnabled: false,
dominanceGroup: 0,
},
{
shape: new Cuboid(0.5, 0.5, 0.1875),
useMassProps: false,
density: 1,
friction: 0,
restitution: 0,
rotation: {x: 0, y: 0, z: 0, w: 1},
translation: {x: 0, y: 0, z: 0},
isSensor: false,
collisionGroups: 4294967295,
solverGroups: 4294967295,
frictionCombineRule: 0,
restitutionCombineRule: 0,
activeCollisionTypes: 60943,
activeEvents: 1,
activeHooks: 0,
mass: 0,
centerOfMass: {x: 0, y: 0, z: 0},
principalAngularInertia: {x: 0, y: 0, z: 0},
angularInertiaLocalFrame: {x: 0, y: 0, z: 0, w: 1},
}
);
// move again
moveCharacterController();
// move the shield
const shieldBody = world.getRigidBody(shield.bodyHandle);
shieldBody.setNextKinematicTranslation({x: 1.459166407585144, y: 1.375, z: 2.023465633392334});
shieldBody.setNextKinematicRotation(shieldBody.rotation());
// this step will crash
world.step(rapierEventQueue);
}
function createObjectFromDesc(world: World, rigidBodyDesc: any, colliderDesc: any) {
const body = world.createRigidBody(rigidBodyDesc);
const collider = world.createCollider(colliderDesc, body.handle);
return {bodyHandle: body.handle, colliderHandle: collider.handle};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment