schteppe / p2.js

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

Collisions not working at all #367

Closed ziarmandhost closed 1 year ago

ziarmandhost commented 1 year ago

Hello, @schteppe !

I have multiplayer game and running p2.js from server using nodejs module: v0.7.1. I have 2 entities: player and wall. Also I have this setup:

export const COLLISION_GROUPS = {
  PLAYER: Math.pow(2, 1),
  SOLIDBLOCK: Math.pow(2, 2),

  SPRING: Math.pow(2, 3)
}

Player

    this.body = new p2.Body({
      type: p2.Body.DYNAMIC,
      mass: PLAYER.weight,
      position: [0, 0],

      fixedRotation: true,

      collisionGroup: COLLISION_GROUPS.PLAYER,
      collisionMask: COLLISION_GROUPS.SOLIDBLOCK | COLLISION_GROUPS.SPRING
    });

    this.body.addShape(new p2.Box({
      width: 160,
      height: 249,
      material: MATERIALS.PLAYER
    }));

Wall

    this.body = new p2.Body({
      type: p2.Body.STATIC,
      // mass: 1,
      position: [x, y],

      // fixedRotation: true,

      collisionGroup: COLLISION_GROUPS.SOLIDBLOCK,
      collisionMask: COLLISION_GROUPS.PLAYER,
    });

    this.body.addShape(new p2.Box({
      width,
      height,
      material: MATERIALS.SOLIDBLOCK
    }));

Problem

Player passthough walls. I sending positions to client and it just rendering it.

ziarmandhost commented 1 year ago

Problem was with how I add object to world. Typo in name of property of class that contain world.