schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

how do you make hard collisions with circles work? #310

Closed hayesmaker closed 6 years ago

hayesmaker commented 6 years ago

hi there,

i can't get collisions between circle or line shapes and box/polygon, only box to box and box to polygon. How do i set the circle shape to make physical collision with other shapes? Is that what collisionGroups are for? if that's the case how come polygon to box collides happily with no collisionGroup/mask set?

/*
circle passes through other polygon / box shapes
*/
var circleShape = new p2.Circle({radius: pxmi(50)});
    circleShape.sensor = false;
    this.circleBody = new p2.Body({
      mass: 1,
      position: [
        pxm(200),
        pxm(200)
      ],
      angularVelocity: 1
    });

    this.circleBody.addShape(circleShape);
    this.world.addBody(this.circleBody);
/*
other boxes / polygons cannnot pass through this boxShape/boxBody
*/
let boxShape = new p2.Box({width: pxm(25), height: pxm(25)});
    boxShape.sensor = false;
    this.boxBody = new p2.Body({
      mass: 1,
      position: [
        pxm(200),
        pxm(200)
      ],
      angularVelocity: 1
    });

    this.boxBody.addShape(boxShape);
    this.world.addBody(this.boxBody);
hayesmaker commented 6 years ago

Fixed it... My problem was with my own (copied from phaser) pxmi function producing loopy values and my rendering logic drawing the circle incorrectly