Jump to content

P2 physics Physics body overlap to another when dragged


Recommended Posts

Hi, I need help to resolve the overlapping of 2 physics object. So I have this ant that I want to drag then it must not overlap with the map(maze)

here's the example:


Edit* as you can see the ant can overlap with the map. Try to use keyboard arrows and it will not. Only when the ant is dragged.

'use strict';

Game.GameArea = {
  player: null,
  maps: null,
  mouseConstraint: null,
  create: function () {

    var level1 = [
      ['layer6', 0, 0, 0],
      ['layer7', 393, 0, 0],
      ['layer8', 361, 239, 0],
      ['layer4', 0, 296, 0],
      ['layer3', 134, 242, 0],
      ['layer2', 296, 110, 0],

    var antCollisionGroup = this.game.physics.p2.createCollisionGroup();
    var mapCollisionGroup = this.game.physics.p2.createCollisionGroup();
    this.maps = [];
    for (var i = level1.length - 1; i >= 0; i--) {

      var xpos = level1[i][1];
      var ypos = level1[i][2]
      var map = this.game.add.sprite(xpos, ypos, level1[i][0]);
      map.body.x += map.width / 2;
      map.body.y += map.height / 2;
      map.body.kinematic = true; //map is static
      map.body.clearShapes(); //Remove standard Bounding Box
      map.body.loadPolygon('collisions', level1[i][0]);
    //, , 
    /*Adding ant*/
    var antInfo = {
      xPos: 117,
      yPos: 163,
      rotation: 119.9998779296875

    this.player = new ant(this.game, antInfo.xPos, antInfo.yPos, antInfo.rotation);

    for(var m = 0; m < this.maps.length; m++){
      var map = this.maps[m];
      //set collision group
      //set collision
      map.body.collides([mapCollisionGroup, antCollisionGroup]);
    this.player.ant.body.collides([antCollisionGroup, mapCollisionGroup]);

    this.mouseBody = new p2.Body();
    this.game.input.onDown.add(this.click, this);
    this.game.input.onUp.add(this.release, this);
    this.game.input.addMoveCallback(this.move, this);

  update: function () {
  click: function (pointer) {
    var bodies = this.game.physics.p2.hitTest(pointer.position, [this.player.ant.body]);

    // p2 uses different coordinate system, so convert the pointer position to p2's coordinate system
    var physicsPos = [this.game.physics.p2.pxmi(pointer.position.x), this.game.physics.p2.pxmi(pointer.position.y)];

    if (bodies.length) {
      var clickedBody = bodies[0];

      var localPointInBody = [0, 0];
      // this function takes physicsPos and coverts it to the body's local coordinate system
      clickedBody.toLocalFrame(localPointInBody, physicsPos);

      // use a revoluteContraint to attach mouseBody to the clicked body
      this.mouseConstraint = this.game.physics.p2.createRevoluteConstraint(this.mouseBody, [0, 0], clickedBody, [this.game.physics.p2.mpxi(localPointInBody[0]), this.game.physics.p2.mpxi(localPointInBody[1])]);
  release: function () {
  move: function (pointer) {
    // p2 uses different coordinate system, so convert the pointer position to p2's coordinate system
    this.mouseBody.position[0] = this.game.physics.p2.pxmi(pointer.position.x);
    this.mouseBody.position[1] = this.game.physics.p2.pxmi(pointer.position.y);


Link to comment
Share on other sites



It looks like (not sure) you are adding velocity with the keyboard, but you are direcly manipulating position (bypassing physics) when using your mouse. Try setting velocity when using your mouse. You should be able to use the standard moveToPointer() function, which works for arcade physics and p2. But you pass the movement speed as a parameter using distanceBetween() the player and mousepointer. An example:


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...