Jump to content

help - arcade physics with p2 objects


Recommended Posts

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

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