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
    • Haxe JS
    • Kiwi.js
  • 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. Heyo, I'm currently building a side scrolling space shooter and desired to have pixel perfect collision detection to allow a better experience dodging bullets and correctly colliding with enemies. To do this I used the P2 engine with loadPolygon functionality. However I have run into a problem were I can't stop the physics (velocity) from transferring from my bullet to my enemy and/or rotating either of the objects which becomes more noticeable when re-using pools of objects (although the objects in question retain a angle/rotation of 0). Any solutions I have come across to make the bullet and enemy static has end up with a no collision detection scenario. -Once you set two different body's to static = true they no longer fire collision events. -If body's kinematic = true then the same happens with no fire collision events. I've reviewed the examples page, however the kinematic and/or static demo's only work if your not kill/reset 'ing the objects. Once that happens there is no collision or in my case the player is auto firing currently so it's bullets are kill/reset before enemies are even placed on the screen. Much googling has come across postings of similar issues in the 2014 and 2015 and I was wondering if there is any resolution to this problem yet? Having two static/kinematic P2 bodies collide without transferring any velocity to each other, just need a collusion detection event for firing custom logic (kill bullet, damage enemy); Snippet of working example prier to static/kinematic setting: (similar code is used for enemies / player and enemy bullets and collision work 100% until static/kinematic is turned on to all parties)[this.player], true); this.player.body.clearShapes(); this.player.body.loadPolygon('enemy_physics', 'player'); // this.player.body.static = true; // this.player.body.kinematic = true; this.player.body.setCollisionGroup(this.playerCollisionGroup); this.player.body.collides([this.enemyBulletsCollisionGroup, this.enemiesCollisionGroup], this.killPlayer, this);
  2. Hey, I'm developing a Phaser game and been stuck for way too long in this error in my code. Basically I create the player object and everything else from classes to groups and add them to collision groups. Finally I make them collide with body.collides where the callback should happen. I've tried firing the callbacks with just the function name, this.function, function(this.meteors) but nothing seems to work so that the callback gets fired only on contact. Either it fires with every create or never. Here's the main game.js: BasicGame.Game = function (game) { // When a State is added to Phaser it automatically has the following properties set on it, even if they already exist: var playerSpawnX = 300; var playerSpawnY = 300; = game; // a reference to the currently running game (Phaser.Game) this.player = new BasicGame.Player(,playerSpawnX,playerSpawnY); this.gem = new BasicGame.Gem(; this.meteor = new BasicGame.Meteor(; this.hud = new BasicGame.Hud(,this.player); }; BasicGame.Game.prototype = { create: function () { this.timer; this.timer2; = 0; //this.score = 0; var highscore = localStorage.getItem("highscore");;; this.ships =; this.ships.enableBody = true; this.ships.physicsBodyType = Phaser.Physics.P2JS; this.ground =, 2205, 600, 220, 'layers', 'maa'); this.ship = this.ships.create(this.player.create());, false);, 0, 600, 2400); //this.physics.startSystem(Phaser.Physics.P2JS); //////////////////////////////GLOBAL PHYSICS//////////////////////////////////, false, false, true, false); //GLOBAL PHYSICS GRAVITY this.physics.p2.gravity.y = 250; // Create our collision groups. this.shipCollisionGroup =; this.meteorsCollisionGroup =; this.gemsCollisionGroup =; this.ship.body.setCollisionGroup(this.shipCollisionGroup); // This part is vital if you want the objects with their own collision groups to still collide with the world bounds // (which we do) - what this does is adjust the bounds to use its own collision group. this.shipCollisionGroup.collideWorldBounds = true; this.meteorsCollisionGroup.collideWorldBounds = false; //this.gemsCollisionGroup.collideWorldBounds = false; //to make ship hit the sides of screen; //gems group this.gems =; this.gems.enableBody = true; this.gems.physicsBodyType = Phaser.Physics.P2JS; //meteors group this.meteors =; this.meteors.enableBody = true; this.meteors.physicsBodyType = Phaser.Physics.P2JS; this.meteors.gravity = 0; this.timer =; // Set a TimerEvent to occur for meteors and gems this.timer.loop(3000, updateScoreCounter, this); this.timer.loop(1000, spawnGems, this); this.timer.loop(7000, spawnaaMeteoriitteja, this); this.timer.loop(5000, spawnaaPaikallisiaMeteoriitteja, this); this.timer.start(); function updateScoreCounter() {; this.player.score = this.player.score + * 10; console.log(this.player.score); } function spawnaaMeteoriitteja() { for (var i = 0; i < 1; i++) { var meteor = this.meteors.create(this.meteor.create());; meteor.body.setCollisionGroup(this.meteorsCollisionGroup); meteor.body.collides([this.meteorsCollisionGroup,this.gemsCollisionGroup, this.shipCollisionGroup]); meteor.body.collideWorldBounds=false; } } function spawnaaPaikallisiaMeteoriitteja() { for (var i = 0; i < 1; i++) { var static = "true"; var meteor = this.meteors.create(this.meteor.create(,, static));; meteor.body.setCollisionGroup(this.meteorsCollisionGroup); meteor.body.collides([this.meteorsCollisionGroup, this.gemsCollisionGroup, this.shipCollisionGroup]); meteor.body.collideWorldBounds=false; } } function spawnGems() { for (var i = 0; i < 2; i++) { var static = "false"; var gem = this.gems.create(this.gem.create(,, static));; gem.body.setCollisionGroup(this.gemsCollisionGroup); gem.body.collides([this.gemsCollisionGroup, this.shipCollisionGroup, this.meteorsCollisionGroup ]); gem.body.collideWorldBounds=false; } } this.hud.create(); this.ship.body.collides([this.shipCollisionGroup,this.meteorsCollisionGroup], hitMeteor, this); this.ship.body.collides([this.shipCollisionGroup,this.gemsCollisionGroup], hitGem, this); function hitMeteor(body1, body2) { //console.log(body1); console.log("hit meteor"); } function hitGem(body1, body2) { console.log("hit gem"); } }, update: function () { this.player.update(); }, render: function() { this.hud.render(); this.player.render(); }, quitGame: function (pointer) { } };
  3. Hi, I want to make a game similar to flappy bird but I've got a few problems with it. (Acutally, whole game is a problem :/ ) Would somebody mind helping me on priv? Code here:
  4. I'm trying to make a 2d space game involving a sort of space dogfight between the player and some ai bots. I need help with the enemy ship automatically rotating towards the player, but not being exactly pointed towards the ship 24/7. I want the enemy ship to have some "lag" in its response to the player's movement so it's more realistic. Right now I have a model which is not realistic and would probably be too hard for the player to win: update: function(){ //blah blah var tempAngle = Math.atan2(fightSprites.player.y-fightSprites.enemy[i].y, fightSprites.player.x - fightSprites.enemy[i].x); //gets angle between player and enemy fightSprites.enemy[i].body.rotation = tempAngle + (3.141/2); //too precise }
  5. Good day, I have been struggeling with this issue for a few days, I am also quite new to Phaser, so please forgive me if this is a newbie question. I am currently working on a project regarding a destroyable map with p2 physics. I have been using Physics editor to capture the maps size in a JSON file and all that seems to be working fine, also I am creating the map as BitmapData object which also is working fine. But once a part of the map is being destroyed, it needs to recalculate the physics properties which obviously doesn't happen. The solution for me - I am going to get the boundaries of the map (its a small scrollable map not more than 1.5k pixels in width) and triangulate the smaller objects which create the shape myself, which means that if a part of the map is destroyed it will recalculate the physics properties. The issue here is that I simply can't get the boundary pixel positions of a map this size for the triangulation library, my browser simply crashes, for smaller objects it actually works fine and everything is working smooth! Only if I put larger pictures in the example, it crashes... I am using contour.js marching ants algorithm from D3 an example is demonstrated down below to get the contour. Maybe there is a better solution to detecting contour pixel positions of larger objects in Phaser? Why is this example not working with larger png objects? And yes.. I also need to detect the transparent area
  6. HI everyone, I'm having an issue When the wheel and arrow collision with P2, the wheel can't slow down. How can I make it slow down? Example: please tell me what's wrong with it,thx!
  7. I'm making a game with a lot of asteroids. I noticed that my fps drops the more asteroids I add when running on the 2015 i7 Macbook Pro. That probably means that on less powerful laptops will lag a lot? My asteroids are similar to the ones in the game Asteroids, however I wanted them to have a somewhat realistic collision body shape that represented the sprite shape. On the Phaser p2 examples I noticed that the json for physics bodies' shapes were broken into smaller shapes like this { [1,2 , 3,4 , 5,6], //pretend that this is a triangle [7,8 , 9,10 , 11,12], //pretend this makes another triangle [13,14 , 15,16 , 17,18] //pretend this makes another triangle } //pretend if you put all of the those shapes together it would make another shape as opposed to just having it as one complex shape like { [1,2 , 3,4 , 5,6 , 7,8 , 9,10 , 11,12 , 13,14 , 15,16 , 17,18] //pretend this makes a shape } Which is better for performance? Also I want to have maybe around 200 asteroids in my game at a time, not to mention some other bodies as well. Should I reduce the amount of vertices of the asteroid's collision bodies? Would setting it to be a circle be less power demanding? The game works fine on my desktop, but that has a gtx 970 and I doubt most people who play my game will have that sort of power.
  8. There is a player and some obstacles. The obstacles are some random rectangles. The task is to get the distance between the player and the nearest object in front of him. To do so I create lineOfSight in render method. My idea is to find its intersections with the lines forming the boxes, that float around. The problem is that I can't find the coords of the vertices of the boxes, as long as they are rotated. So there are basically two questions: how to find the vertices of the objects? is it a way to go? I mean, maybe there are some better approaches to this task. Here is my code. You can simply copy it and paste here, to see what it does.
  9. Hey guys, So I am currently working on a project where I have dynamic bodies constrained to static bodies through the use of createRevoluteConstraint so that the dynamic bodies only rotate where the static body is located. That part is working, now the next thing I want to be able to do is click and drag on the static bodies and move them to any point in game world and continue to have the dynamic bodies constrained to them. I have this partially working by calling sprite.body.static = false when the mouse is clicked down and then sprite.body.static = true when the mouse is released. I also have the update function constantly calling sprite.body.static=true when the body is supposed to be static. The issue I am running into is that when I release the mouse button the static body seems to drift in space based off how fast my mouse is moving when I release it. Gravity doesn't seem to have any effect on it though which is good. So, does anyone have some ideas on how to go about fixing this? I want it to be completely static as soon as I release the mouse. Thanks
  10. ok. I have same problem in there: I am digging all documentation and examples for solving that problem. But i have nothing really. I can post my own codes if anyone want. And that example: Its including same problem. If you run it carefully, you can see first place objects colliding world bounds, but after changing state its not colliding.
  11. I have recently attempted to switch several parts of my game from sprites with static p2 bodies attached, to tile sprites with static p2 bodies for texture purposes. However, the tile sprites do not seem to be detected at all with p2 hittest. I have done some research, but the most that I have found is that tile sprites has their own hitarea property, however there is little to no explanation for how to use this. As an additional point, some of the tile sprites are rotated, if that is at all relevant. Is there a simple way to recreate the hittest of a standard sprite on tile sprites(i.e. Adjust the hitarea to cover the sprite)? Any help is appreciated.
  12. I am making a quick demo that is built of off the p2 ragdoll demo. I am not running Phaser, just standalone p2 with p2 renderer. I have a linkage that is anchored to a fixed point that can be articulated with the mouse. The issue is that the mouse can easily... dislocate the linkage due to the mouse having infinite force. I came across an example for Phaser called mouse spring but I was unable to adapt it to standalone p2 with p2rederer. I attempted to hijack the mouse down functions but I'm not really familiar with p2, this was meant to be a slapped together demo. Any help would be greatly appreciated, I have attached what I have so far. ragdoll.html
  13. Kirko Tagline: Kirko is about how the worlds we build fit a specific body we deem 'normal' and if you don't fit in the world, things are a lot more difficult for you. Kirko Controls: Left/Right arrow keys to move Up arrow key to jump I have made some progress on my game/interactive story that has been plaguing my head for about a year now. This is a short preview with some of the elements coming together. I am a one man dev team so forgive the crudeness. It's still a work in progress. For the parts where it's long stretches of land I am envisioning having art to keep you busy, for example Circular houses/trees/ etc. Let me know what you think! It's exciting to start seeing my vision come to life! If you support my cause follow me at : Twitter Thanks for playing! P.S Just so you can see the progress: Original demo here:
  14. Hi, dear game developers. I had seen that link on examples load-polygon-1 or var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('contra2', 'assets/pics/contra2.png'); // Load our physics data exported from PhysicsEditor game.load.physics('physicsData', 'assets/physics/sprites.json'); } var contra; var start = false; function create() { // Enable p2 physics game.physics.startSystem(Phaser.Physics.P2JS); contra = game.add.sprite(400, 300, 'contra2'); // Enable the physics body on this sprite and turn on the visual debugger game.physics.p2.enable(contra, false); // Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache contra.body.clearShapes(); contra.body.loadPolygon('physicsData', 'contra2'); // Just starts it rotating game.input.onDown.add(function() { start = true; }, this); } function update() { if (start) { contra.body.rotateLeft(250); } } function render() { } , and i'm trying create json like that, since i saw that example. I saw some suggestions about physics editor or mapeditor tiled. i installed it, but i have no idea about creating json like that despite researching how its doing on internet. Probably i need some help
  15. Hi Guys, I've come across a problem with p2 body. I have 2 sprite, sprite1 is physics enabled, and sprite2 is create a Phaser.Physics.P2.Body They will be moving using velocity after 2 seconds, but sprite2 not working... I made a JsFiddle to explain my question. Does anyone have an idea what I'm doing wrong? Thanks in advance!
  16. Hello! I just want to make sure there is nothing I am missing before reporting this as a bug. I've created a small test case where a ball go through a wall although it shouldn't because there is the `collide` between the two body materials. I've tried playing with the contactMaterial options without any success. Everything is fine but as a certain velocity where it go through. For instance, if you change the x velocity value to 650, the ball will not go through. Here is the JSFiddle example: I've put a debugger on the "console.log('it overlaps!!!');" on the first occurrence and debugged a bit and the "runNarrowphase" method will return that there is no collision between the ball and the wall. Although, on the second occurrence, the same method returns a collision. Please let me know if I am doing something wrong before I post this as a bug. Thank you very much!
  17. Hello, can you please help me? I require non-rotating crates. Like in this oficial example: , where I add just one line box.body.fixedRotation = true; to the for cycle creating the boxes. Suddenly, they start to shift and slip. I need them to stay still. How can I fix this? Do you have any idea please? I would be very greatful! I have tried using different materials, but notning helps. Friction is just gone
  18. How do I add a terrain that is built from tilemap to a collision group? I couldn't find any examples
  19. I have a sprite sheet of my games playable character. The frames consist of all possible actions the character can do, including two different attacks. I want to change the physics bounds on different animations as my sprites positioning obviously changes with different animations. I got PhysicsEditor expecting it could handle this but as far as I can see, it only works for single images. Is there any solutions for this to help me get near pixel perfect collision? Any help is appreciated as always
  20. Hey, I'm using p2 physics and I have a sprite (pixel line) that is rotating around some point (beginning of the line). For rotation I use angularVelocity. To display my sprite correctly I had to set it's anchor to (0,1) and it works fine, but collision box remained where it was before. Please check out my code and attached image game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setImpactEvents(true); this.obstacle1CollisionGroup =; this.obstacle = game.add.sprite(700, 400, "line"); this.physics.p2.enable(this.obstacle, true); this.obstacle.body.setCollisionGroup(this.obstacle1CollisionGroup); this.obstacle.anchor.setTo(0,1) this.obstacle.body.angularVelocity = 0.3; this.obstacle.body.angularDamping = 0;
  21. Hey, I'm trying to make a basketball game with predicted trajectory and I am having trouble with the trajectory. chargeBall: function(Pointer, x, y) { // If the button is down we charge the ball. if( Pointer.isDown && !isBallMoving ) { // Clear the graphics, set the style and draw the line from the ball to the Pointer. trajectoryGraphics.clear(); trajectoryGraphics.lineStyle( 2, '0xCCCCCC' ); trajectoryGraphics.moveTo( ball.x, ball.y ); trajectoryGraphics.lineTo( x, y ); // Calculate the velocity variables for the ball body to use in launchBall. launchVelocity.x = ( ball.x - x ) * 5; launchVelocity.y = ( ball.y - y ) * 5; // Make the line narrower. trajectoryGraphics.lineStyle( 1, '0xCCCCCC' ); // Lets make 11 segmented trajectory line. For every segment get the point and draw a small square. for( var i = 0; i < 15; i++ ) { var trajectoryPoint = this.getTrajectoryPoint( ball.x, ball.y, launchVelocity.x, launchVelocity.y, i ); trajectoryGraphics.drawRect( trajectoryPoint.x, trajectoryPoint.y, 2, 2 ); } } }, launchBall: function() { if( !isBallMoving ) { // Clear the graphics if the ball has been launched. trajectoryGraphics.clear(); // Set the ball velocity. ball.body.velocity.x = launchVelocity.x; ball.body.velocity.y = launchVelocity.y; // Flag the ball as moving. isBallMoving = true; // Set the ball physics to enabled. = 1; = true; // Create a new ball after 4 seconds. Phaser.Timer.SECOND * 4, this.createNewBall, this ); } }, getTrajectoryPoint: function( startX, startY, velocityX, velocityY, n ) { var t = 1 / 60; var stepVelocityX = t * this.physics.p2.pxm(-velocityX); var stepVelocityY = t * this.physics.p2.pxm(-velocityY); var stepGravityX = t * t * this.physics.p2.pxm(-this.physics.p2.gravity.x); var stepGravityY = t * t * this.physics.p2.pxm(-this.physics.p2.gravity.y); startX = this.physics.p2.pxm(-startX); startY = this.physics.p2.pxm(-startY); var tpx = startX + n * stepVelocityX + 0.05 * (n * n * n) * stepGravityX; var tpy = startY + n * stepVelocityY + 0.05 * (n * n * n) * stepGravityY; tpx = this.physics.p2.mpx(-tpx); tpy = this.physics.p2.mpx(-tpy); return { x: tpx, y: tpy }; } The code is from Phaser Box2D physics example I think, but I am using the P2 physics system. It works. Kind of. The trajectory is wrong. I am absolutely not sure but I think it has got to do something with the ball physics body. I'd love it if you'd take a quick look and tell me if I'm doing something very wrong.
  22. Hi, I've just started porting a bunch of games from version 1.1.6 and I'm struggling to work out how to get the same effects using P2 that were pretty straight forward with the Arcade physics engine. Because setCircle() has been removed from the Arcade engine I need to use P2 to make sure my collisions are correct. Here's a snippet from my update loop previously where I was checking if a radar 'blip' overlapped any of the other blips in the this.blips array: plane.blip, this.blips, function( blip1, blip2 ) { this.onCrash();}, function( blip1, blip2 ) { return &&;}, this ); So I'm looking at using body.onBeginContact.add() but that seems to cause the sprites to start interacting with each other which I don't want. Any suggestion on how to recreate the above? The physics.overlap method before was so simple!
  23. Hello, It's my 2nd attempt to physics in game, first in P2. I want to create four lines in something like X shape and rotate it clockwise. Then I will be shooting some little balls and I would like to check if I hit my X obstacle - I do not want to destroy it, apply some gravity to it or more complex physics. On ball collision with my obstacle I just want to kill/destroy that ball. I don't want to move or destroy my obstacle. I've enabled physics at the beginning and created two collision groups game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setImpactEvents(true); this.ballCollisionGroup = game.physics.p2.createCollisionGroup(); this.obstacleCollisionGroup = game.physics.p2.createCollisionGroup(); I'm spawning my obstacles consisting of 4 lines var dot = game.add.sprite(700, 350, "dot"); dot.anchor.setTo(0.5); this.obstacle.x = 700; this.obstacle.y = 350; var line = game.add.sprite(0, 0, "line"); line.anchor.setTo(0.5, 0); this.obstacle.addChild(line); var line2 = game.add.sprite(0, 0, "line"); line2.anchor.setTo(0.5, 0); line2.angle = 90; this.obstacle.addChild(line2); var line3 = game.add.sprite(0, 0, "line"); line3.anchor.setTo(0.5, 0); line3.angle = -90; this.obstacle.addChild(line3); var line4 = game.add.sprite(0, 0, "line"); line4.anchor.setTo(0.5, 0); line4.angle = 180; this.obstacle.addChild(line4); dot.bringToTop(); this.obstacle.physicsBodyType = Phaser.Physics.P2JS; this.obstacle.enableBody = true; for (var i = 0; i < this.obstacle.children.length; i++) { game.physics.enable(this.obstacle.children[i], Phaser.Physics.P2JS); this.obstacle.children[i].body.angularVelocity = 30; } and my ball this.ball = game.add.sprite(this.activeClock.x, this.activeClock.y, "ball"); this.ball.anchor.set(0.5); game.physics.enable(this.ball, Phaser.Physics.P2JS); this.ball.checkWorldBounds = true;{ game.state.start("Intro"); }); this.ballGroup.add(this.ball); this.ball.body.velocity = game.physics.arcade.velocityFromAngle(shootAngle, ballSpeed); this.ball.body.setCollisionGroup(this.ballCollisionGroup); this.ball.body.collides([this.obstacleCollisionGroup], this.ballCollision, this); Right now I'm stucked at spawning my obstacles, because they are spawned on top of eachother and they got hit at spawning and all fall down my game. How to make my obstacles not collide with themselves and apply no gravity to them? Previously I've tried using ARCADE physics but it didn't track the motion of my obstacles in collisions, so I had to change to P2. Thanks in advance for help
  24. I have a simple space ship shooter game and I have a question about collisions and grouping them. I'm familiar with the collisions groups, impact events and postbroadphase callback P2 examples. Setting collision groups to have ships bounce off walls and each other is like the examples. When I add bullets to a group they bounce around too; off walls and push ships around. The result of the impact happens before the bullet sprite is killed. Bullets act as expected using a postbroadphase callback, but that callback fires anytime anything collides with anything. Is this the correct way to handle this? Is there a way to stop impact events using collision groups? I've googled the hell out of this, but maybe some keyword to point me in the right direction eludes me. Thanks for any help.
  25. Hi! I've been trying to make a moving platform where the player (and other sprites) can ride upon. I tried using something similar to this example ( to no avail. I've also tried using a contact material and messed with the various friction properties to no avail. I've seen a gravity based solution that ramps up gravity 100x, but I can't use that because of how my game works. Thanks in advance for your help!