Jump to content

help - arcade physics with p2 objects


Recommended Posts

Quote: "physic arcade with objects that have physic p2."   sorry.. to me that sounded like you want to combine arcade and p2 physics..


so you want to know how to set something up in p2 that formerly worked in arcade...


i think you'll have to post more code... or even provide a link to the testcase :)

Link to comment
Share on other sites

Piece of my code :)


            this.player = this.game.add.sprite(170, 50, 'player');
            this.game.physics.p2.enable(this.player, true);
            this.player.body.setRectangle(33, 78);
            this.snakes = this.game.add.group();
            this.snakes.create(900, 80, 'snake');
            this.snakes.forEach(enemy.setupSnake, this);


           this.snakes.forEach(enemy.moveSnake, this);




//aux functions

            enemy.setupSnake = function(snake) {
                snake.body.setRectangle(30, 45);
                snake.health = 2;
            enemy.moveSnake = function(snake) {
                  this.game.physics.arcade.accelerateToObject(snake, this.player, utils.rangeRandom(90, 100), utils.rangeRandom(120, 130));
Link to comment
Share on other sites

so you ARE trying to combine p2 with arcade ... thats what i meant..  AFAIK you can not use arcade stuff on p2 bodies..


but have a look here: http://test.xapient.net/phaser/moveto.html


i found your problem interesting and tried to solve it.. i think i found a way to do what you want with p2..  but it's very complicated ^^

i do not fully understand it myself as i just combined different approaches i saw in one of rich's lab examples... 


have a look at this function i wrote:


function moveBullets (bullet) {     var dx = ship.body.x - bullet.x;    var dy = ship.body.y - bullet.y;    bulletRotation= Math.atan2(dy, dx);    bullet.body.rotation = bulletRotation + game.math.degToRad(-90);    var angle = bullet.body.rotation + (Math.PI / 2);    bullet.body.velocity.x = 50 * Math.cos(angle);    bullet.body.velocity.y = 50 * Math.sin(angle);}

it rotates the bullets to the "ship" object and then sets x and y velocity according to the angle..  


you need to call this function on every bullet (or snake) in the update loop (and "ship" needs to be a global)

Link to comment
Share on other sites

p2 is very new... it just got introduced to phaser with 2.0 a few days ago..  while arcade physics was there from the beginning.. 


i guess rich will make sure that p2 will have the same useful and easy to use functions when he finds the time to implement them..


let's just wait and see :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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