Search the Community

Showing results for tags 'p2'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • HTML5 Game Coding
    • News
    • Game Showcase
    • Facebook Instant Games
    • Web Gaming Platform
    • Coding and Game Design
  • Frameworks
    • Phaser 3
    • Phaser 2
    • Pixi.js
    • Babylon.js
    • Panda 2
    • melonJS
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start



Website URL





Found 335 results

  1. Hi All I have spend all night trying to figure this out. I changed my game from Arcade to P2 physics and can not for the life of me get collisions to work. I changed my body types to Kinematic since I don't need any physics applied to my object. Below is my code : createEnemies: function() { var enemy = enemies.getFirstExists(false); if (enemy) { enemy.reset(500,; enemy.body.fixedRotation=true; enemy.body.kinematic = true; enemy.body.velocity.y = -100; enemy.anchor.setTo(0.5, 0.5); enemy.body.moves = true; enemy.body.debug = true;[0].sensor = true; enemy.scale.set(scaleRate); enemy.body.onBeginContact.add(blockHit, this); // DOES NOT TRIGGER //enemy.body.collides(bulletsCollisionGroup, hitEnemy, this); // DOES NOT TRIGGER } If I change my body from Kinematic = true, to Kinematic = false, then all the events trigger. Any ideas?
  2. How to detect the object from group when collided to player in p2. like: Arcade child from group ex:
  3. in this game i need physics for multiple objects for a group its fine to enable p2 bt whenever i tried to enable p2 for player with this line game.physics.p2.enable(player, false); my screen goes black why is this happening ??? var game = new Phaser.Game(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio, Phaser.AUTO, '', { preload: preload, create: create, update: update}); var player; var world; var blink; var rocketangel; var rocketdirection; var rocketSpeedDelta; var rocketradius; var platforms; var scoreText; var playerangle; var scaleRatio; var score; var asteroid; scaleRatio = window.devicePixelRatio / 2; //load images or files before game starts function preload() { game.load.image('background', 'assets/game/background.png'); game.load.image('world', 'assets/game/world.png'); game.load.image('player', 'assets/game/player.png'); game.load.image('blink', 'assets/game/blink.png'); game.load.image('asteroid', 'assets/game/asteroid.png'); } //create all the images ans things on the screen function create() { score=0; rocketangel = 0; rocketdirection = 1; rocketSpeedDelta = 0.002; rocketradius = 170; playerangle=60; // A simple background for our game game.add.sprite(0, 0, 'background'); // We're going to be using physics, so enable the Arcade Physics system game.physics.startSystem(Phaser.Physics.P2JS); // Turn on impact events for the world, without this we get no collision callbacks game.physics.p2.setImpactEvents(true); game.physics.p2.restitution = 0.8; // Create our collision groups. One for the player, one for the pandas var playerCollisionGroup = game.physics.p2.createCollisionGroup(); var asteroidCollisionGroup = game.physics.p2.createCollisionGroup(); world=game.add.sprite(,, 'world'); world.anchor.setTo(0.5, 0.5); world.scale.setTo(scaleRatio, scaleRatio); platforms =; platforms .enableBody = true; platforms.physicsBodyType = Phaser.Physics.P2JS; for (var i = 0; i < 10; i++) { asteroid = platforms.create( , , 'asteroid'); asteroid.anchor.setTo(0.5, 0.5); asteroid.scale.setTo(scaleRatio, scaleRatio); game.physics.p2.enable(asteroid, false); asteroid.body.setRectangle(40, 40); // Tell the panda to use the pandaCollisionGroup asteroid.body.setCollisionGroup(asteroidCollisionGroup); // Pandas will collide against themselves and the player // If you don't set this they'll not collide with anything. // The first parameter is either an array or a single collision group. asteroid.body.collides([asteroidCollisionGroup, playerCollisionGroup]); } //adding player player = game.add.sprite(,, 'player'); player.anchor.setTo(0.5, 0.5); player.scale.setTo(scaleRatio, scaleRatio); //game.physics.p2.enable(player, false); //player.body.setCircle(28); // Set the ships collision group //player.body.setCollisionGroup(playerCollisionGroup); // The ship will collide with the pandas, and when it strikes one the hitPanda callback will fire, causing it to alpha out a bit // When pandas collide with each other, nothing happens to them. //player.body.collides( asteroidCollisionGroup, hitPanda, this); // The platforms group contains the ground and the 2 ledges we can jump on //asteroid.body.collideWorldBounds = true; // The score scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#FFF' }); scoreText.scale.setTo(scaleRatio, scaleRatio); cursors = game.input.keyboard.createCursorKeys(); } function hitPanda(body1, body2) { // body1 is the space ship (as it's the body that owns the callback) // body2 is the body it impacted with, in this case our panda // As body2 is a Phaser.Physics.P2.Body object, you access its own (the sprite) via the sprite property: body2.sprite.alpha -= 0.1; } function update() { var playerangelchange; platforms.forEachAlive(moveBullets,this); platforms.forEach(function(asteroid) { game.physics.arcade.collide(player,asteroid,deathHandler, null, this); }, this); //keypress if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { //it will speed up the rocket } else if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) { //it will cause the blink blink= game.add.sprite(player.x, player.y, 'blink'); blink.anchor.setTo(0.5, 0.5); blink.scale.setTo(scaleRatio, scaleRatio); game.add.tween(blink).to( { alpha: 0 }, 20, Phaser.Easing.Linear.None, true); //blink.destroy(true); } else if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { rocketdirection=-1; //change the direction } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { rocketdirection=1; //change the direction } //speed of current rocket movement if (rocketdirection == 1) { rocketSpeedDelta = 1.14; playerangelchange=1.1; } else if (rocketdirection == -1) { rocketSpeedDelta = -1.14; playerangelchange=-1.1; } //for moving on a circular path scoreText.text = 'Score: ' + score; rocketangel += this.time.physicsElapsed * rocketSpeedDelta ; player.x = Math.cos(rocketangel) * rocketradius ; player.y = * rocketradius ; playerangle+=playerangelchange; player.angle=playerangle; } function moveBullets (asteroid) { setTimeout(accelerateToObject(asteroid,world,30),50000); //start accelerateToObject on every bullet } function accelerateToObject(obj1, obj2, speed) { if (typeof speed === 'undefined') { speed = 60; } var angle = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x); //obj1.body.rotation = angle + game.math.degToRad(90); // correct angle of angry bullets (depends on the sprite used) obj1.body.force.x = Math.cos(angle) * speed; // accelerateToObject obj1.body.force.y = Math.sin(angle) * speed; } function deathHandler() {"gameover"); }; </script> </body> </html>
  4. Hi, I´ve been using Phaser version 2.4.2 and everything has been working fine expect for triangle collision shapes made in TIled. They trigger collisions with setPostBroadphaseCallback even if a sprite is not colliding with the shape. Oddly it only affects slopes. So i´ve tried updating Phaser to the ce edition if that would remove the bug, but only got a new one. My players sprite is spawned on the right edge of the screen. Changing world.setBounds doesnt make a difference. Also im unable to create the players sprite anywhere else in its create function. The x and y coords have no effect. What changed in Phaser version 2.4.7 that broke my game? 2.4.7 Is the version after the bounds bug appears. Any help would be appreciated.
  5. My problem is the position of the sprite is not correct when using accelerateToObject function. Running accelerateToObject function in sprite: Without accelerateToObject function in sprite: Of course I need accelerateToObject function to follow the player. Thanks in advance
  6. I don't know why this error is happening: This code has a problem and I don't know why: this.enemy.body.collides(objectsCollisionGroup, function(){enemies[].jump();}); My full code: Enemy_1 = function (index, game, player) { var x =; var y =; = game; = Math.floor((Math.random() * 20) + 5);; this.player = player; this.alive = true; this.punchedCount = 0; this.enemy = game.add.sprite(x, y, 'stickman_2'); = index.toString(); this.enemy.punching = false; game.physics.p2.enable(this.enemy); this.enemy.animations.add('left', Phaser.Animation.generateFrameNames('walk/left_', 1, 8), 10, true); this.enemy.animations.add('right', Phaser.Animation.generateFrameNames('walk/right_', 1, 8), 10, true); this.enemy.animations.add('sprint_left', Phaser.Animation.generateFrameNames('walk/left_', 1, 8), 20, true); this.enemy.animations.add('sprint_right', Phaser.Animation.generateFrameNames('walk/right_', 1, 8), 20, true); this.enemy.animations.add('punch_1_left', Phaser.Animation.generateFrameNames('punch_1/left_', 1, 3), 20, true); this.enemy.animations.add('punch_1_right', Phaser.Animation.generateFrameNames('punch_1/right_', 1, 3), 20, true); this.enemy.checkWorldBounds = true; this.enemy.body.collideWorldBounds = true; this.enemy.body.fixedRotation = true; this.enemy.body.damping = 0.5; this.enemy.body.setCollisionGroup(enemies_1ColisionGroup); this.enemy.body.collides(blockCollisionGroup); this.enemy.body.collides(objectsCollisionGroup, function(){enemies[].jump();}); this.enemy.healthBar = game.add.text(0, 0, 'HP: ' + + '%', {font: "10px Arial", fill: "#ffffff"}); this.enemy.addChild(this.enemy.healthBar); this.enemy.healthBar.position.y = this.enemy.healthBar.position.y-80; }; Enemy_1.prototype.damage = function() { -= 1; if ( <= 0) { this.alive = false; this.enemy.kill(); return true; } return false; }; Enemy_1.prototype.punch = function () { if(this.enemy.facing == 'left') {'punch_1_left'); this.enemy.punching = true; if(this.player.facing == 'left') { this.player.body.x += 1; } else { this.player.body.x -= 1; } } else {'punch_1_right'); this.enemy.punching = true; } }; Enemy_1.prototype.jump = function () { if(checkIfCanJump(this.enemy)) { this.enemy.body.moveUp(300); } }; Enemy_1.prototype.update = function() { this.enemyMoves = false; //<Enemy_Punch> if(checkOverlap(this.enemy, player)) { if(playerPunching == true){ if(this.enemy.body.velocity.x < 0 && this.enemy.body.velocity.y < 150 && this.enemy.body.velocity.y > 0) { this.enemy.body.x += 30; } else if(this.enemy.body.velocity.x > 0 && this.enemy.body.velocity.y < 150 && this.enemy.body.velocity.y > 0) { this.enemy.body.x -= 30; } enemies[].damage(); } else { playerPunching = false; } * 0.5, function(){enemies[].punch();}, this); } else { this.enemy.punching = false; this.enemyMoves = true; if(this.enemyMoves != false) { accelerateToObject(this.enemy, this.player, 200); } if(this.enemy.body.velocity.x < 0 && this.enemy.body.velocity.y < 150 && this.enemy.body.velocity.y > 0) {'left'); this.enemy.facing = 'left'; } else if(this.enemy.body.velocity.x > 0 && this.enemy.body.velocity.y < 150 && this.enemy.body.velocity.y > 0) {'right'); this.enemy.facing = 'right'; } } //</Enemy_Punch> if(this.enemy.punching != false) { playerSubtractHealthBar(0.5); } this.enemy.healthBar.setText('HP: ' + + '%'); }; Add the enemies in the game: Thanks in advance
  7. I'm trying to make a prototype for a RPG game, I managed to make the drive, but I can't do the collision, can help? I have the ' background ' layer, that would be my floor, the 'colisao', where I keep all objects that the player will collide, as walls, and finally the 'coletaveis', where the items that the player can interact. Follow my code level 1: Not even the collision with the edge of the screen is working, can anyone help me?
  8. Is there a way of finding the point of contact between 2 bodies? In some of the p2 demos it draws a line from body centre to contact, so clearly the data is in there somwhere, but I can't seem to find a way of extract it...
  9. crffty


    Hi. I'm trying to make my first game with Phaser and I'm trying to get some physics working - global gravity. I'm not sure whats going on but somethings not working: = { create : function() { this.background =, 0,, 360, 'background'); this.background.autoScroll(-100, 0); this.background =, 290,, 8, 'ground'); this.background.autoScroll(-180, 0); this.player = this.add.sprite(30, 230, 'player'); this.player.animations.add('run');'run', 10, true); // physics engine; // gravity = 400; }, based on a couple tutorials I've been following I was expecting for the 'ground' to fall, but it does not. I'm still really new to Phaser and kinda new to JS, so i figure I'm overlooking something simple. I actually only need gravity on the player sprite so if you could suggest how to go about that I would appreciate in but would be happy just to get some gravity of any kind to start with. Thank you in advance for your help.
  10. Hello everyone, this is my first time posting on this forum, which means I'm pretty desperate at this point haha! I'm trying to make a simple game where a character is sliding down a hill, this is what I got so far: I want to make the hill as slippery as ice, just like some ice parts in this game: I learned a lot from this game btw. I tried to copy how the user made his ice material, but it doesn't seem to work in my game! // Create ramp and player from tiles sharp =,"points"); player =,10, 'player'); // Define materials groundMaterial =; playerMaterial ='player'); // Give the ramp material for (i=0; i<sharp.length; i++){ sharp[i].setMaterial(groundMaterial); } // define what happens when one material contacts the other, groundMaterial, { friction: 0 , restitution: 0 }); What am I doing wrong? Please help me
  11. I'm using P2 physics, and I'm creating a relatively intricate border around the screen that I want balls to bounce against. I am scaling all assets on the screen for 3 different aspect ratios, depending on the user's screen size. I create the border like this: bgOverlay = otherSpriteGroup.create(gameWidth / 2, gameHeight / 2, "bgOverlay"); bgOverlay.anchor.setTo(0.5, 0.5); bgOverlay.scale.setTo(scale.x, scale.y); bgOverlay.body.clearShapes(); bgOverlay.body.loadPolygon("physicsData", "bgOverlay"); bgOverlay.body.setCollisionGroup(otherCollisionGroup); bgOverlay.body.collides(bubbleCollisionGroup, otherHitBubble, this); bgOverlay.body.static = true; The trick is that I scale all assets using a Phaser Point, scale.x and scale.y. But when I load in the polygon data using bgOverlay.body.loadPolygon, it takes in actual pixel values, and I can't scale bgOverlay.body. I am not sure how to scale the numbers in the physics data. Any ideas?
  12. Hi guys, I'm very new to this so I'd really appreciate if someone would be able to point me in the right direction. I have a 2d top-down car game where the track boundaries is a polygon made with physics editor. I need to get the distances from the car at various angles to the boundaries of the track. I've looked for line polygon intersection but I haven't been able to find anything. Any idea where I should start here?
  13. Using P2 Physics, what's the best way to move a sprite, similar to sprite.body.moveRight, sprite.body.moveLeft but in parallel with another sprite that has a rotation (which constantly changes). I want to be able to move the purple line up and down along the yellow line, in parallel and at a fixed distance while they both rotate. Anya ideas? Thanks
  14. hi guys, I wonder is there any way to check whether 2 sprite in different groups are overlap, in the p2 system, currently I am using game.physics.p2.setPostBroadphaseCallback(this.checkOverlap, this); to do it, however I think it maybe expensive because I need to check whether the 2 sprites are from same group (the same group should not do anything). and I do not need collide when 2 sprite impact, I just need them to trigger my function. so I wonder is there any way to do thing like body.collide in arcade system, thanks.
  15. Hi, I'm just working with P2 and have hit a bit of a wall. I need to check whether all the bodies in the world are at rest (i.e. no longer moving)... in Box2D (as far as I recall) there was either a world.sleeping property (or perhaps so you could check when/if this had happened. There doesn't seem to be an obvious way of doing this in P2. One solution would be to loop thru all the bodies checking for velocity == 0, but this seems a little cumbersome. Have I missed an obvious way to do this?
  16. Hi friends, I'm very new to Phaser (but I have a long programming background) and right now I'm trying to work on a new game concept of mine. Basically, it is a regular Pong but with few twists. I have attached an image below of the main game for further clarifications. gifs upload 1) As you can see, the shape of the paddles is a polygon. As such, I have to use P2 physics for them as the simple arcade won't suffice because of their peculiar shape. 2) Now since the paddles are using P2, I'm forced to use P2 for the ball as well because they both need to collide and I can't collide a P2 and Arcade sprite 3) Now the problem is, when I apply a velocity to the ball and make it move at a specific direction, I want it to collide with the body ahead (either one of the paddle or wall) and continue on it's path with the SAME velocity infinitely . Right now, it slows down momentarily till it comes to a full stop. I can achieve this 'continuous velocity' for the ball when it is in arcade but I can't use arcade since my paddles are P2 and so I"m forced to find a method to achieve this in P2 for the ball. My Ball sprite code is : this.Ball = this.add.sprite( / 2, / 2, 'Ball') this.physics.p2.enable(this.Ball); this.Ball.body.clearShapes(); this.Ball.body.loadPolygon('physicsData', 'Ball'); this.Ball.anchor.setTo(0.5, 0.5) this.Ball.checkWorldBounds = true; this.Ball.body.collideWorldBounds = true; this.Ball.body.mass = 0.01 this.Ball.body.restitution = 1 this.Ball.body.damping = 0 this.Ball.body.velocity.x = 400*(Math.cos(0)); this.Ball.body.velocity.y = 400*(Math.sin(0)); Thank you and warm regards
  17. Hi all I have a small problem, I'm trying to determine my score by the type of sprite that you hit with your bullets, this type is defined to the player by a small sprite on top of the screen, kind of like if you hit matching one you get 100 points if not 50 points see my code below. Can someone correct me in what I'm doing wrong? ///////I load my atlas this.load.atlas('Monsters', 'asset/game1/Monstruos/monstruos.png', 'asset/game1/Monstruos/monstruos.json'); //////This is my sprite that determines the type createFiguraCompare: function(){ // create a sprite, random frame 0..4 this.figuritaspega =, 0, 'Monsters'); this.figuritaspega.frame = this.rnd.integerInRange(0,4); // create a group this.figuritaspegaG =; // <- function call this.figuritaspegaG.add(this.figuritaspega); // scale entire group and reposition group this.figuritaspegaG.scale.set(0.5 , 0.5 ); // notice that the sprite position is relative to the group position this.figuritaspegaG.x = 800; this.figuritaspegaG.y = 140; }, ////////this is my moving sprites that you must hit with the bullets makeOneFigura: function() { this.figura =; this.figura.enableBody = true; this.figura.physicsBodyType = Phaser.Physics.P2JS; // for (var i = 0; i < 5; i++){ this.figura.createMultiple(100, 'Monsters', 0, false); // } this.figura.setAll('anchor.x', 0.7); this.figura.setAll('anchor.y', 0.7); this.figura.setAll('outOfBoundsKill', true); this.figura.setAll('checkWorldBounds', true); }, makeFiguras: function(x, y){ if (this.timerFiguras) { this.figuras = this.figura.getFirstExists(false); if (this.figuras) { this.figuras.reset(500, 0); this.figuras.frame =,4); this.figuras.body.setCollisionGroup(this.figuraCG); this.figuras.body.collides(this.bulletCG); this.figuras.body.velocity.y = 1400; } }; }, ////and my collision handler My problem is mainly here collisionBulletFigura: function(bullet, figura, score, scoreText, figuritaspegaG) { if (this.figura.currentFrame === this.figuritaspegaG.currentFrame){ figura.sprite.kill(); bullet.sprite.kill(); this.score += 100; this.scoreText.text = this.score; }else { figura.sprite.kill(); bullet.sprite.kill() this.score += 50; this.scoreText.text = this.score; } },
  18. Hi everyone, I'm new to Phaser, and I'm using P2 for my collision events. Now I'm justing doing a test in which a group of objects colliding with the world boundary. It works fine but the objects lose a lot of energy and velocity when collides with the world boundary. I just want the objects to bounce back with same speed, like 'setBounce' in arcade. I've tried the following : 1. Set the restitution, but I can just see little difference between different restitution values. 2. Contact Material. I referenced to this page physics&f=contact+material.js&t=contact material It looks fine but it seems that one contact material contains only one sprite? Is there any way to add a collision group to one contact material? Or are there other ways to do this?
  19. Hi everyone, Anyone knows how i could convert a object layer from tiled(with rectangle, circle...) into a P2 object/sprite or whatever ? I would like that my player can check if there's an overlap with theses objects :/
  20. GameDemon

    p2 body sleep

    How do you tell if a P2 body is sleeping? I cant see any property for that on Body, only `allowSleep` and `sleepSpeedLimit` (both of those undocumented btw). I just have some simple circle bodies with gravity atm, I wanted to colorise the sleeping bodies for debug purposes. I tried turning p2 sleepmode on by declaring `game.physics.p2.sleepMode = 2;` I'm not sure how to set this properly, I saw World.BODY_SLEEPING=2 in the Phaser source, with standalone p2 its set via `p2.World.BODY_SLEEPING;`
  21. I'm having a bit of trouble with tilemap collisions. I'm using P2 and a json tiled tilemap. Strangely, it seems to work with small, simple tilemaps but as soon as I boost the complexity it seems to stop working. I've got a simple car being driven by physics that turns one way when you touch and the other when you don't. Help? Here's the create code: create: function(){ game.physics.startSystem(Phaser.Physics.P2JS); //!THIS STUFF WORKS WITH TEST MAP = this.add.tilemap('city');'roguecity'); this.decorationLayer ='decoration'); this.collisionLayer ='collision'); this.collisionLayer.resizeWorld();, 12); this.physics.p2.convertTilemap(, this.collisionLayer); this.stage.backgroundColor = "#d3d3ff"; this.cursors = game.input.keyboard.createCursorKeys(); game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]); //map stuff //create the layer //this.backgroundLayer ="background"); //car stuff = game.add.sprite(60,35,"car"); game.physics.p2.enable(;,0.5); = true; }, Thanks!
  22. Hi, my problem is collision's callback works only on "bounce" or on the first touch of every collision, idk. My code checks, when player collides platform, then (in other function) if arrow is down it sets sprite's velocity x to 200. This sprite changes velocity only if it "bounces", but doesn't when it's standing on this platform. I set console's log in my function and it also appears, when player is bouncing, not standing . I'm switching from arcade to P2 right now and I'm such a newbie. Could anyone help? (Sorry for my english) code here: demo here:
  23. Hey My question is pretty straigh forward, is there any alternative to PhysicsEditor, that is at least Free if not OpenSource. Thanks in advance!
  24. Hey there So, this is my first post in here, i hope everybody is good out there, let's go straight into the issue. I'm new to, and I'm intending to use it to create a hill climber clone. I have successfully create car like shapes (following a couple of online tutos) but still very confused about how to create the land on which the car will run. To make it clearer, here's an illustration of what i exactly want: So basically, the original game introduces 2+ types of ground, that are the bridges, land, water, ... I need to implement only two types. I know how to "materialize" the body and everything that should go after i declare them, which is my problem. On examples page there's quiet half the solution to my problem but uses Box2D which is paid (and i can afford it right now, else, i would love to get it!)
  25. If I apply an impulse/force on a body, I see that the body is moving as expected, but is stopping (velocity=0) very, very slowly. I can still see the velocity be something like 0.00003 and moving down to zero. Applying damping does help, but it does affect my gameplay, since it will stop quickly only if I set it at a very high damping value, and I would like it to simply be stable after applying a force instead of having to wait a long time till the velocity = 0, with minimal damping. How can I fix this? Edit: Solved Applying damping did help, but I just had to lower the mass to get the effect I was looking for. Thanks!