rapier.js icon indicating copy to clipboard operation
rapier.js copied to clipboard

castShape sometimes seems to miss objects

Open haynesgt opened this issue 6 months ago • 1 comments

I'm seeing some issues with castShape

var RAPIER = require("@dimforge/rapier2d-compat")
var world;
class V {
  constructor(x, y) {this.x = x; this.y = y;}
  sub({x, y}) { return V.of(this.x-x, this.y-y); }
  mul(f) { return V.of(this.x*f, this.y*f); }
  mag() { return Math.sqrt(this.x**2 + this.y**2); }
  norm() { return this.mul(1/this.mag()); }
}
V.of = (x,y) => new V(x, y);

/**/
const myShape = V.of(244.5, 0.5)
const myPos = V.of(884.5, 188.5)
const origin = V.of(380, 241);
const dest = V.of(1004, 137);
/**/

/* another scenario: (though this one works with some scaled values of vel and toi)
const myShape = V.of(335, 0.5);
const myPos = V.of(408, 286.5)
const origin = V.of(894, 328);
const dest = V.of(385, 217);
*/


const vel = dest.sub(origin);

RAPIER.init().then(() => {
const { World, RigidBodyDesc, ColliderDesc, Ray } = RAPIER;


// Initialize the world
world = new World(V.of(0, 0));

// Create a rectangle body
const bodyDesc = RigidBodyDesc.dynamic().setTranslation(myPos.x, myPos.y);
const body = world.createRigidBody(bodyDesc);
const colliderDesc = ColliderDesc.cuboid(myShape.x, myShape.y);
const cuboid = world.createCollider(colliderDesc, body);

world.step();

// Perform ray cast
const ray = new Ray(origin, vel);
const rayCastResult = world.castRay(ray, 1, true);
console.log('Ray Cast Result:', rayCastResult);

// Perform shape cast
const shapeCastResult = world.castShape(origin, 0, vel, new RAPIER.Ball(10), 1, false);
console.log('Shape Cast Result:', shapeCastResult);
});

expected output: Ray cast result: not null Shape cast result: not null

actual output: Ray Cast Result: uA {collider: hI, toi: 0.36936938762664795} Shape Cast Result: null

image

'@dimforge/rapier2d': specifier: ^0.11.2 version: 0.11.2 '@dimforge/rapier2d-compat': specifier: ^0.11.2 version: 0.11.2

haynesgt avatar Jan 31 '24 08:01 haynesgt

For my purposes, I found that creating a box shape along the path and testing if it collided with anything was sufficient.

haynesgt avatar Feb 08 '24 00:02 haynesgt