Hsoumii

p2 body rotation changes x and y

Recommended Posts

Hello guys I'm making a platformer game, I struggled with making collision between the player and objects created in tiled but after that I got it working with a function I created, however, when I make the rectangle rotated in tiled, in the game it comes out away from its position, take a look:
this is on tiled
r5LTwLY.png

 

in the game it comes out like this

p7zJvra.png

 

here is my code, createCollisionObjects is where the magic happens

var game = new Phaser.Game(70 * 10, 70 * 7, Phaser.AUTO, 'game', {
    preload: preload,
    create: create,
    update: update,
    render: render
});

function preload() {

 
  // tilemap
  game.load.tilemap(
      'map', 
      'assets/tilemaps/maps/lvl-1.json',
      null,
      Phaser.Tilemap.TILED_JSON
  );
  
  // tilemap images
  game.load.image('new', 'assets/tilemaps/tiles/new.png');
  game.load.image('sheet', 'assets/tilemaps/tiles/sheet.png');
  game.load.image('s9af', 'assets/tilemaps/tiles/s9af.png');

  // player spritesheet
  game.load.atlasJSONHash(
    'player', 
    'assets/spritesheets/player_walk.png', 
    'assets/spritesheets/player_walk.json'
  );

}

/*
* GLOBAL VARIABLES
*/
var map;
var player;
var playerSpeed = 400;
var jumpTimer = 0;

function create() {

  
  game.physics.startSystem(Phaser.Physics.P2JS);
  game.physics.p2.setBoundsToWorld(true, true, true, true, false);
  game.physics.p2.gravity.y = 300;
  game.physics.p2.restitution = 0;
  game.stage.backgroundColor = '#ffffff';
  map = createMap().map;

  var start = map.objects.objective[0];
  player = createPlayer({x: start.x, y: start.y});


  cursors = game.input.keyboard.createCursorKeys();
  

}

function update() {

  player.body.setZeroVelocity();


    if (cursors.up.isDown && game.time.now > jumpTimer && checkIfCanJump())
    {
        player.body.velocity.y = 600;
        jumpTimer = game.time.now + 750;
    }


    if(cursors.left.isDown)
    {
        player.body.velocity.x = -playerSpeed;
        player.animations.play('walk');
    }

    else if (cursors.right.isDown)
    {
        player.body.velocity.x = playerSpeed;
        player.animations.play('walk');
         
    } else {
        player.body.velocity.x = 0;
        player.animations.stop();
        player.frame = 0;
    }

    game.camera.follow(player);

}


function render() {

}

function createMap() {
    var m, g, f, b, c;

    m = game.add.tilemap('map');
    var mWidth = m.widthInPixels;
    var mHeight = m.heightInPixels;

    game.world.setBounds(0, 0, mWidth, mHeight);
    m.addTilesetImage('sheet', 'sheet');
    m.addTilesetImage('new', 'new');
    m.addTilesetImage('s9af', 's9af');

    g = m.createLayer('ground');
    f = m.createLayer('fringe');
    b = m.createLayer('background');
    

    g.resizeWorld();
    f.resizeWorld();
    b.resizeWorld();
    


    createCollisionObject(m.objects.objects);

    return {
        map: m,
        ground: g,
        fringe: f
    };

}

function createPlayer(pos) {
    var p = game.add.sprite(pos.x, pos.y, 'player', 'Symbol 2 instance 10000');
    //p.scale.setTo(0.4, 0.4);

    game.physics.p2.enable(p);
    

    p.body.setZeroDamping();
    p.body.fixedRotation = true;
    
    p.body.x+=p.width/2;
    p.body.y+=p.height/2;
    // animation
    p.animations.add(
        'walk', 
        Phaser.Animation.generateFrameNames('Symbol 2 instance 1000', 0, 7, '', 1), 10, true, false);

    //p.animations.play('walk-left');
    return p;
}

function createCollisionObject(objects) {
  if (objects) {
      for(var i=0; i < objects.length; i++) {
          var p2 = game.physics.p2;
          var obj = objects[i];
          var sprite = game.add.sprite(obj.x, obj.y, null);
          sprite.width = obj.width;
          sprite.height = obj.height;
          sprite.angle = obj.rotation;
          game.physics.p2.enable(sprite, false);
          
          sprite.body.x = sprite.x + sprite.width / 2;
          sprite.body.y = sprite.y + sprite.height / 2;
          
          sprite.body.rotation = obj.rotation;

          sprite.body.static = true;
          sprite.body.debug = true;
      }
  }
}

function checkIfCanJump() {

    var yAxis = p2.vec2.fromValues(0, 1);
    var result = false;

    for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++)
    {
        var c = game.physics.p2.world.narrowphase.contactEquations[i];

        if (c.bodyA === player.body.data || c.bodyB === player.body.data)
        {
            var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis
            if (c.bodyA === player.body.data) d *= -1;
            if (d > 0.5) result = true;
        }
    }
    
    return result;

}

 

Share this post


Link to post
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.

Guest
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.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.