# Search the Community

Showing results for tags 'p2'.

• ### Search By Tags

Type tags separated by commas.

### Forums

• HTML5 Game Coding
• News
• Game Showcase
• Coding and Game Design
• Frameworks
• Phaser 3
• Phaser 2
• Pixi.js
• Babylon.js
• Panda 2
• melonJS
• Haxe JS
• Kiwi.js
• General
• General Talk
• Collaborations (un-paid)
• Jobs (Hiring and Freelance)
• Services Offered

• 0 Replies

• 0 Views

### Interests

Found 334 results

1. ## Enemy rotation AI degrees problem

Hi, yesterday i was struggling with rotation of an enemy. In my game you drive a spaceship and the enemies should rotate to your location in the nearest way (left or right), so they can follow you. This seems to be simple, but i find some problems in the rotation degrees. First i have to find the angle that the enemy should have, to point to my spaceship. I find two ways to do it. The first doesn't work: angle = game.physics.arcade.angleBetween( this , player ); // returns angles between something like 3.1 and -3.1// ( this = Enemy class )I don't know why. Maybe because i am using p2 physics and not arcade? The second works: angleRadians = Math.atan2(this.position.y - player.position.y, this.position.x - player.position.x);angle = Math.atan2(this.position.y - player.position.y, this.position.x - player.position.x) * 180 / Math.PI;// returns 180 to -180So i use that instead. Here I find something strange, the 0° angle in the Enemy is on Top. But, the 0° angle returned in the var angle is on the left. ( both ways in clockwise ) Finally i try doing something tricky like this, but doesn't work properly: if( this.body.angle+180 - angle-90 <= 180 && this.body.angle+180 - angle-90 >= 0 ){ this.body.angle += .6;}else{ this.body.angle -= .6;} May if I figure out why the degrees are different, i can get it work. Thanks!
2. ## How to detect collision between P2 and Arcade bodies

Hey there, my game consists of a player implemented with P2 and enemies implemented with Arcade physics (for better performance, since the AABB is sufficient for them). How can I detect collision between the Arcade bodies and the P2 body/bodies ? (The point is: I want the enemies to bounce away) OR: I've read in some other thread, that P2 offers AABB. I've searched the Phaser docs, but I didn't find anything helpful. Can you recommend a way to implement the described above only with P2, without affecting performance? Thank you guys in advance!
3. ## Box2D vs P2

Hi, I've a mobile game developed using phaser, P2 and CocoonJS Canvas+, but on an iphone4s, the framerate is around 20fps, no problem with iphone 5 and higher or any GalaxyS (from 40 to 70). I have more or less sixties sprites with physic. So my question is, would Box2d help me make it more performant ? Thanks
4. ## Best way to get started with p2?

What's the best way to get started with the p2 physics system (other than the examples)? For someone who knows the Arcade system pretty well, what are the main differences and things to consider when working with p2? Also, do you guys use any external tool to create polygons or work with this library? Thanks!
5. ## P2 anchor.set Issue in Sandbox!

https://phaser.io/sandbox/edit/tpauMNLC When i try to enable p2 physics for a sprite i get Uncaught TypeError: Cannot read property 'set' of undefined. for the object.anchor.set(0.5); of @method Phaser.Physics.P2#enableBody. Please check the link above. Also how do i go about debugging in Sandbox? I have tried putting breakpoints in VM#### but a new one starts everytime i refresh or change something so that didn't work.
6. ## Struggling with layering in Phaser

I'm creating a game with many types of tiles. Each tile type has different properties that I'm customizing: it's texture, layering, and collision. These are all determined separately. In the interest of making my P2 life easy, I have two tile groups, collision and no-collision. The tiles that collide with the player are placed in one and the others go into the other. The problem is, as I mentioned before, my game is pretty layering-intensive. As of right now I haven't found a way to have individuals tiles within a group have an index independent of their parent group's. This makes sense of course, especially when you're familiar with the Pixi renderer. That doesn't really solve my problem however. Does anyone know of a way in which I can keep my collision groups separate from my layering? I would love, love, love if Phaser 3 had some sort of Z-index setter as well- manipulating everything through bringToTop() is pretty difficult.
7. ## P2 collision groups - have to set 'collides' twice?

Hi there I'm learning up on how P2 collision groups work and I have a question. It seems that if I have two collision groups, one for the player and one for platforms, to get the two to interact I have to write: this.ground.body.collides(playerCollisionGroup);this.player.body.collides(groundCollisionGroup);If I just use one of those statements the collisions don't work, I have to use both. This seems a little strange - if ground is set to collide with player, why do I also have to set the player to collide with ground? It seems a little redundant. Is there a reason it has to be bound twice? Rich is a heck of a lot smarter than me so I assume I'm just missing a very good reason for this Cheers!
8. ## p2 tiled no collison with black background on tiles

Hi, I have some strange behavior, I made 2 collision groups in p2, first have one member - player character, the second one have tiles loaded from json made by tiled. everything seems to work fine, tiles are 15x15 blocks with black background and on borders i have dotted and dashed blue lines. my character can walk on this tiles so collision works but when he is moving camera is shaking and if space between border dots is wider than my character can fall inside title. so I made a little experiment and add some black solid tiles and my character can pass them like it was background ar nothing was there. So I have no idea what is going one. please help
9. ## p2 detect collision from one side

Hi, Im trying to detect collision in p2 from one side (like jumping on top of something) its easy with arcade phisics, but p2 don't have such option, i think. So is there any way to implement this? if not than I was thinking about sticking another invisible body to a side of object and detect collision with that second object instead. But I dont know how to stick other object to first one and I've experimented with RevoluteConstraint to do it but it;s not what I intended. my code: var constraint = game.physics.p2.createRevoluteConstraint(arrow, [20, 5.5 ], arrowTop, [5.5,5.5]); constraint.collideConnected=false; constraint.setStiffness(600); constraint.update();but it behave like two objects on elastic rope so sometimes arrowTop if closer to arrow and sometimes its further . even if I set more stiffness it's bouncing. Is there way to stick two bodies like on glue instead of rope ?
10. ## can't destroy p2 bodies created by 'convertCollisionObjects' from Tiled

this is how the array that contains all the physicsbodies is created polyline_bodies = game.physics.p2.convertCollisionObjects(supermap,"polygons");i can then cycle over all bodies and flawlessly set collisiongroups, materials and so on.. but if i want to cycle through this array and kill the physics bodies in it for (i=0; i<polyline_bodies.length; i++){ polyline_bodies[i].destroy()}i get the following weird error: Uncaught TypeError: Cannot set property 'body' of nullc.Physics.P2.Body.destroy @ phaser.min.js:23level0.update @ game.js:616how can i destroy the bodies in there.. thank you !
11. ## Jumpthrough platforms causing bouncing

I'm trying to create a game that involves platforms I can jump through and land on top of. However, when I jump through a platform and land on it, I bounce on top of the platform. The bouncing has to do with the height I dropped from onto the platform. In order to do the jumpthrough platforms, I used the ideas from this thread, and modified them to work with my game. The actual code I'm using in the onPreSolve method is below: world.on('preSolve', function(presolve) { for (var i = 0; i < presolve.contactEquations.length; i++) { var c = presolve.contactEquations[i] var f = presolve.frictionEquations[i]; if (c.bodyA.shapes[0].collisionGroup == GROUND || c.bodyA.shapes[0].collisionGroup == PLATFORM) { var yAxis = p2.vec2.fromValues(0, 1); var y = p2.vec2.dot(c.normalA, yAxis); if (y >= 0){ // check for jumpthrough object // check if moving upwards c.enabled = false //disable contactEquation if (f) { f.enabled = false //disable frictionEquation (solves the stuckInPlatform problem) } if (c.bodyA.velocity[1] < 15 ){ // velocity < 15 - still inside the platform c.bodyA.velocity[1] -= 5; // course correction! } } } } });The only thing that seems like it might be causing the issue would be the course correction, though I wouldn't expect that code to be executed when I am falling and land on top of a platform, since this should check if it is moving upwards on contact. If this is a common problem, please point me to the thread where I can read more about this, because I wasn't able to find it through searches. I've attached a video showing the bouncing. It's more slight in the video than usual, but sometimes it gets to the point that the character is bouncing higher than he is tall. (I apologize for the poor video quality, don't have any screen recording software installed on this computer.) IMG_1733.MOV
12. ## Turning P2 Physics On and Off

In the game that I am currently making using Phaser and P2 Physics I am looking to turn off the collisions between 3 objects when the game is paused or when the level has ended and the scoreboard shows so that unnecessary collisions are not being checked and dropping the frame rate (game is intended to run on mobile devices). I am using the following two functions at the moment TurnOffCollisions: function() { console.log("TurnOffCollisions") SavedBallVelX = Ball.body.velocity.x; SavedBallVelY = Ball.body.velocity.y; Ball.body.velocity.x = 0; Ball.body.velocity.y = 0; this.game.physics.p2.gravity.y = 0; FairwayHole.body.clearCollision(); Ball.body.clearCollision(); FairwayHole.body.clearShapes(); Ball.body.clearShapes(); Block.body.clearCollision(); Block.body.clearShapes(); },And to turn on the Collisions again when the game is unpaused or the next level starts: TurnOnCollisions: function() { console.log("TurnOnCollisions"); this.game.physics.p2.enable(FairwayHole); this.game.physics.p2.enable(Ball); this.game.physics.p2.enable(Block); FairwayHole.body.loadPolygon("Physics", "Level1-Hole"); FairwayHole.kinematic = true; Ball.body.loadPolygon("Physics", "Ball2"); Ball.body.velocity.x = SavedBallVelX; Ball.body.velocity.y = SavedBallVelY; Block.body.static = true; this.game.physics.p2.gravity.y = 1400; ballMaterial = this.game.physics.p2.createMaterial("ballMaterial", Ball.body); groundMaterial = this.game.physics.p2.createMaterial("groundMaterial", FairwayHole.body); this.game.physics.p2.setWorldMaterial(groundMaterial, true, true, true, true); contactMaterial = this.game.physics.p2.createContactMaterial(ballMaterial, groundMaterial); contactMaterial.friction = 0.5; contactMaterial.restitution = 0.5; }However when I do this it seems to work OK, until the Level Finishes and the Ball object and FairwayHole object are still touching, even though I think I have turned off the Physics I get the following error: Uncaught TypeError: Cannot read property 'collisionMask' of undefined54.c.runNarrowphasephaser.js:76867 54.c.internalStepphaser.js:76685 54.c.stepphaser.js:78255 c.Physics.P2.updatephaser.js:61785 c.Physics.updatephaser.js:26388 c.Game.updateLogicphaser.js:26327 c.Game.updatephaser.js:44881 c.RequestAnimationFrame.updateRAFphaser.js:44865 c.RequestAnimationFrame.start.window.requestAnimationFrame.forceSetTimeOut._onLoopIs there a better way of turning on and off collisions using P2 Physics? Or Am I doing something wrong with the way I am doing it that is causing the game to crash?
13. ## Get rid of small bounce-back effect on collision with P2 physics

When implementing collision with P2, I can't seem to get rid of a small bounce-back effect. Example: http://test.xapient.net/phaser/tilemapexample/index-p2.html In this example, when Mario jumps on the ground, it gets a little bit below the ground and then smoothly moves up quickly to stand on it. This doesn't happen with the Arcade physics and I can't seem to get rid of it with P2. Also every other example I've looked at has this effect and I tried several parameters. Since I only need AABB collisions, another approach I tried is implementing the collision myself in the update() routine. A very simple example for just a general ground would be:if (player.body.y >= 500) { player.body.y = 500; player.body.velocity.y = 0;}In this case, for one frame, the player appears below the ground. I assume this is, because this calculation would need to be done between the P2 physics calculations and the rendering, but the update() happens before the P2 physics calculations. Does anyone know how to around this bounce-back effect I'm describing? Cheers!
14. ## is anyone using a p2 capsule successfully?

it seems i cant even get an error message or a debug body.. it stays a rectangle.. trying with sprite.body.addCapsule(length,radius); thx for any tips!
15. ## Sprite Body with P2

Hi everyone, I've got a problem about P2 body. I've got a group who contains sprites with body enabled. /*...*/this.game.physics.startSystem(Phaser.Physics.P2JS);/*...*/this.myGroup = new Phaser.Group(this.game);item = new Phaser.Sprite(this.game, x, y, 50, 'sprite');this.game.physics.p2.enable(item, true);this.myGroup.add(item);When I move the group like bellow this.myGroup.x = 100;this.myGroup.y = 100;Sprite move the right way, but not his body ! .. and that is my problem :/ what is the right way to make the body following the sprite ?
16. ## Different shapes in a single body with different collision mask

I am trying to make a combat game. I want players to collide with the ground like normal, but to interact differently when they collide with other players. To do this, I've created a body with two shapes, one that is not a sensor, and collides with the ground, and one that is a sensor, and collides with other players. When I try to have both of the shapes active on the body, however, I fall through the ground. When only the ground colliding shape is active, the ground collision happens, but the other collision (obviously) does not happen. Is this a limitation in the p2 physics engine that doesn't allow a body to have shapes with different collision masks, or or do you think there is something I am doing wrong to mess this up? Below is the code I'm using to add the shapes to the body. I set the collision mask when I load the shapes earlier in the code. for (var i = 0; i < groundCollisionShapes.length; i++) { this.body.addShape(groundCollisionShapes[i], groundCollisionShapes[i].offset);}for (var i = 0; i < playerCollisionShapes.length; i++) { this.body.addShape(playerCollisionShapes[i], playerCollisionShapes[i].offset);}
17. ## game.physics.p2.getConstraints() produces an array with [undefined] entries

i'm (again) not able to remove ALL constraints in my game state by cycling through the array getConstraints should deliver because all entries are "undefined" am i doing it wrong? has something changed in 2.2.2 ??? thx for the help!
18. ## How to get the collision force on a p2 contactEquation

The p2 docs are not of much help to me as I am not a physics academic. on the contactEquation I have this: function _onEnter(bodyB, shapeA, shapeB, contactEquations) { for(var i = 0; i < contactEquations.length; i++) { if(contactEquations[i].firstImpact === true) { window.console.log(contactEquations[i].relativeVelocity); } } } spr.body.onBeginContact.add(_onEnter, this);however, the contactEquations.relativeVelocity always returns 0, so I am looking for something else I believe. I need to play a sound when two objects collide, the volume will be dependant on either the relative velocity between these two objects, or even better, the total force they collide with, but I can calculate this myself with the velocities times the two bodies masses. The docs I am looking at: http://schteppe.github.io/p2.js/docs/classes/ContactEquation.html#property_contactPointA It seems like I may have to run some function to calculate the relativeVelocities, but I can't figure out which one as the docs only says something like: "Computes G*inv(M)*f, where M is the mass matrix with diagonal blocks for each body, and f are the forces on the bodies.", which does not make much sense to me. I really hope someone can help me out here I can't seem to find more documentation of p2 on my own.
19. ## how to calculate object position for different anchor point?

Hi! I want to import an object layer from Tiled map editor into a physics engine ( p2.js ). My problem is that p2.js uses 0.5,0.5 as anchor points, but tiled uses 0,0 (top-left). How can i recalculate it's position so i can give it the proper coordinated for a different anchor point? Any help is very much appreciated! Edit : This is easy with rectangles, but my problem is with rotated polygons.
20. ## obj1 should NOT collide with obj2 but still trigger onBeginContact and collide with obj3

is there a way to disable collisions for a certain object but still get the onBeginContact event when they collide? activating sensor is no option because i need the object to still collide with everything else.. using collision groups seems not to work either because if i configure obj1 to not collide with obj2 the onBeginContact event is not fired anymore. for now i use a hacky workaround in the onPresolve Event to cancel the collsion response.. but i have the feeling that there is an easier way for that. thx in advance