Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

castShape sometimes seems to miss objects #258

Open
haynesgt opened this issue Jan 31, 2024 · 1 comment
Open

castShape sometimes seems to miss objects #258

haynesgt opened this issue Jan 31, 2024 · 1 comment

Comments

@haynesgt
Copy link

haynesgt commented Jan 31, 2024

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
Copy link
Author

haynesgt commented Feb 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant