rapier.js
rapier.js copied to clipboard
Raycasting before step returns undefined
I'm struggling with kind of an insane bug right now, or perhaps it's just undefined behavior, but i am not receiving any results of a raycast if step has not been called at least once.
The use case is I am restoring a snapshot and immediately casting and getting null.
const world = new World({x: 0.0, y: -9.81, z: 0.0});
const colliderDesc = ColliderDesc.cuboid(10, 10, 10);
const body = world.createRigidBody(RigidBodyDesc.newStatic());
const collider = world.createCollider(colliderDesc, body.handle);
world.step();
const result = world.castRay(new Ray(new Vector3(0, 20, 0), new Vector3(0, -1, 0)), 100, true, 0xfffffffff);
console.log(!!result); /*true, but would be false if I commented out step*/
const world2 = World.restoreSnapshot(world.takeSnapshot());
const result2 = world2.castRay(new Ray(new Vector3(0, 20, 0), new Vector3(0, -1, 0)), 100, true, 0xfffffffff);
console.log(!!result2); /*false*/
If this is the intended behavior, is there any thoughts on a way to "pump the gas" without explicitly calling step and pushing the simulation forward?
Hi! This looks like an oversight. The acceleration structure for scene queries like ray-casting or shape-casting isn’t included in the serialized/deserialized data. So your call to .step() triggers the re-computation of that acceleration structure.
A workaround right now is to update that acceleration structure individually, right after you restore the snapshot by calling world2.queryPipeline.update(world2.islands, world2.bodies, world2.colliders).
Perfect, thank you!