# Search the Community

Showing results for tags 'p2'.

• ### Search By Tags

Type tags separated by commas.

### Forums

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

• 0 Replies

• 0 Views

### Interests

Found 335 results

1. ## Worms Game P2 physics Question (+ demo)

Hi everyone, I am moving forward on my worms project. I have had a problem with P2 physics for a while that I cannot solve and I need to solve it for impact events. THE DEMO: (Chrome Firefox only) THE REPO: THE PROBLEM: On contact with my level (on a hill) my worm keeps sliding veeeery slowly and annoyingly. If the worm is in contact with the level he should stay still (but he won't). ATTEMPTED: Setting the body of the worm properties: velocity.x =0, velocity.y=0, rotation = 0, angle=0, angularDamping =0, angularVelocity = 0, gravityScale = 0.1 (allowGravityScale = true); Setting the game.physics.p2 properties: friction = 1 (tried 0, 1000, 3000 etc), p2.world.defaultContactMaterial.friction = same thing, restitution = 1; CODE OF INTEREST: WORM CLASS - specifically lines 112-117 GAME STATE Thanks in advance for your help!
2. ## p2 Bug? Sprite falls through world if rotated

(http://g.recordit.co/o5HlkT1d5S.gif) Hi, so my setup involves two hills, where the player is rotated to match the slope angle. The first hill rotates the player to an angle of -45Â° and the second rotates it to 45Â°. The problem is that once the second rotates the player, the player can just fall through the bodies. My code for the hills is almost identical. (have a different triangle and angle). I think this might be a bug in p2. Any thoughts? EDIT: I've added a second example: http://g.recordit.co/FLgBsRxbeI.gif In this example we can see that the sprites will slide down the first hill, but upon touching the second they will fall through the wold.
3. ## Rotating a square on a hill

Hey all I have this square I am rotating in my simple plat former and it moves along the straight ground perfectly. I am using: enemy.body.rotateLeft(450); This rotates the enemy perfectly, and moves it along, but I also have these hills in my game and when the enemy square encounters the hill it has a terrible time trying to get up it. Is there some sort of property that I can set that would help with this perhaps mass? Or any ideas on how to resolve this? Thanks! P.S I've attached a picture of the square on the hill for reference. Thanks!

5. ## Applying Force/ Impulse to P2 Physics Body on collision

I have a p2 Object with the following properties this.game.physics.p2.enable(this.player, true); this.player.body.clearShapes(); this.player.body.loadPolygon('playerPolygon','player'); this.player.body.allowRotation = false; this.player.body.fixedRotation = true; this.player.body.immovable = true; this.player.body.setCollisionGroup(playerCollision); this.player.body.collides(ballCollision); this.player.body.kinematic = true; this.player.body.data.shapes[0].sensor = false; this.player.body.collideWorldBounds = true; The above object can only move along its X axis and I want to trigger push/ hit force to its impacting object on collision this.game.physics.p2.enable(this.ball); this.ball.body.setCircle(28); this.ball.body.collideWorldBounds = true; this.ball.body.allowRotation = true; this.ball.body.fixedRotation = false; this.ball.body.adjustCenterOfMass(); this.ball.body.setCollisionGroup(ballCollision); this.ball.body.collides(playerCollision); The above object is always the impacting object whose movement is controlled by gravity and impulse. I'm trying to figure out how to calculate the following:- The point of collision on the body of "this.player" The impulse to apply to the impacting object after collision to simulate a proper/ accurate deflection setting the projected angle and speed/ velocity what properties of the impacting object do I set? Force? AngularVelocity? Velocity?
6. ## P2 bodies on (rotated) sprites within groups

Hey, I'm building a top-down parking game, for this game we have levels in JSON which (among other things) determines where the player spawns and at what angle. The player consists of three sprites and three bodies (car and two front wheels) that are positioned relatively to each other. Everything goes well, until I try to spawn the car at an angle, because suddenly I no longer know the x and y coordinates of where to spawn the wheels, and it's a bunch of math to figure that out especially as phaser groups fix this problem. Enter example of my problem #1: https://jsfiddle.net/tv4j4tw1/ What I did here is simply create two sprites in a group, positioned them, positioned the group, and then added a body to the sprites. However the bodies spawn completely out of place. If this actually worked properly I'd have no issues. You can ask why do you even need a group here? I do because of the angles mentioned above, if this group would get an angle it'd be a lot harder to figure out the coordinates of the squares. Now we go into where I tried to hack my way out of this problem: https://jsfiddle.net/tv4j4tw1/1/ Ha! This works, I use the group to position the sprites, and then take the sprites out of the group and position them according to their world position while in the group. It's hacky, but I can live with it. Sadly it stops working once I add the angle (which is the whole reason I'm doing this weird stuff): https://jsfiddle.net/tv4j4tw1/2/ The world position is wrong, and the squares still end up in the wrong place. So, I hope I'm doing something fundamentally wrong and getting the hack to work is a silly idea. Halp.
7. ## How to check if on Floor using P2?

Hey, So I need to check whether a sprite is on the floor using P2 physics. Basically something like Arcade.onFloor() but for P2 physics.

9. ## Polygon .json scaling

Hi, I want to scale both sprite and .json physics file, is that possible and if so, how? I ve seen a few posts about that but no answers. Thank you
10. ## Error using render texture with existing p2 bodies

HI, I am enthralled by the pixi render texture tutorial, but I'm getting an error from the update() function: I've changed the variable names, but I've triple checked that they are consistent. Here's some of my create() code to give an idea what I'm doing. I was hoping to use the render-texture tutorial on top of my working code. Is there a way to get the renderTexture working within an existing p2 game? Thank you. //game.stage.backgroundColor = "#f2f2f2"; game.world.setBounds(0, 0, 450, 800); game.physics.startSystem(Phaser.Physics.P2JS); // create two render textures.. // these dynamic textures will be used to draw the scene into itself render_texture1 = game.add.renderTexture(450, 800, 'texture1'); render_texture2 = game.add.renderTexture(450, 800, 'texture2'); current_texture = render_texture; // create a new sprite that uses the render texture we created above output_sprite = game.add.sprite(225, 400, current_texture); // align the sprite output_sprite.anchor.x = 0.5; output_sprite.anchor.y = 0.5; stuff_container = game.add.group(); stuff_container.x = 450/2; stuff_container.y = 800/2; // now create some items and randomly position them in the stuff container for (var i = 0; i < 4; i++) { var item = stuff_container.create( Math.random() * 400 - 200, Math.random() * 400 - 200, game.rnd.pick(game.cache.getKeys(Phaser.Cache.IMAGE)) ); item.anchor.setTo(0.5, 0.5); } // used for spinning! count = 0; // Turn on impact events for the world, // without this we get no collision callbacks game.physics.p2.setImpactEvents(true); game.physics.p2.updateBoundsCollisionGroup(); game.physics.p2.gravity.y = 0; game.physics.p2.restitution = 0.7;

13. ## How to make 2D-trampoline

So I am now making 2d sidescroller, where player can jump with trampoline. But I cannot get it working as I would like to. I made it like this in Physics Editor: But because of using material, which enables bumping, it makes like with arcade physics. (RED IS SHOWING AREA WHERE I WANT IT TO LAND.) So, is there any solution to this? One possible is to cut that jumping area half, and put it together in game with constraints.
14. ## Boomerang effect

Hi, I'm trying to replicate some sort of boomerang effect on an moving object. In short I'm shooting away a sprite but instead of shooting the sprite away in a straight line, I'd like it to move in an 'arc'. Regrettably I have no idea how to achieve this sort of movement. If someone could explain me the theory around such movements, that would be awesome. If you could include a code example, you'd be my hero (of the day). P.S. I'm using P2 physics. Thanks in advance.
15. ## Group Pivot Point with P2 Gravity

Hi all, very new to Phaser and I'm trying to basically make a colour switch kind of game. So I am using p2 physics for the rotating arcs so I can add collision detection later. I had these arcs as graphics in the game but then couldn't add physics to them so they are now sprites in a group call wheel. I have the pivot point of the group set to (85,85). I don't know why but this number works, the sprites are 170x170 but when I try to give the group a pivot point such as (0.5,0.5) it doesn't work I've even tried (0.25,0.25) to try and recreate the (85,85) pivot point. I am trying to do this because when I apply the p2 gravity to the game the wheels pivot point does not move so the wheels rotation gets bigger and bigger. If anyone has a possible solution to this or an alternative method to go about this then please reply! I'm so desperate right now it's not even funny.
16. ## P2 equivalent of Arcade velocityFromRotation

The title is pretty self explanatory. Is there any method, in P2 physics, that has the same effect as the velocityFromRotation method in Arcade physics? If there ain't any equivalent, is there a different way to solve this "problem". Thanks in advance!
17. ## Phaser + P2 + Dynamic Bodies

Greetings! I've been mucking around with this problem for several days, attempting all types of odd solutions and nothing I can come up with works. My problem is this, I spawn a Player object, a sprite with P2 enabled on it. I spawn another Player Object, a sprite with P2 enabled on it. I need both objects to move freely and collide with the world and one another... which they currently do perfectly fine. The issue is when the player objects collide with one another, one always moves the other. I don't wish for either to move the other. Does anyone know of a solution to this using P2? Here's the 'gotcha' .. I need both Player bodies to stay dynamic so they collide with the world objects, and setting them to static or kinematic makes them run through everything which is what I tried. Setting the mass of one to zero or Number.MAX_NUMBER does not work as the game is multiplayer and the server needs to run and have the exact same physics response as the clients, which it doesn't once I do that on the clientâ€™s machine. The server doesn't encounter the same order of collision and one player object might become the one with the super mass and run through the other which is not good! Thank you for your time!

20. ## Lock rotation revolute constraint

How can i lock the angle between two sprites which have a revolute constraint atached to each other? Example i have a arm atached to a body with a revolute constraint and i want to that arm keep up at certain angle, i cant reproduce this : ( 'use strict' game = new Phaser.Game(320, 480, Phaser.AUTO, 'space') mainState = preload: -> game.load.image('body', 'public/images/body.png') game.load.image('arms', 'public/images/arms.png') game.load.image('ground', 'public/images/ground.png') create: -> game.physics.startSystem(Phaser.Physics.P2JS) @chest = game.add.sprite(29,53,'body') @arms = game.add.sprite(8,53,'arms') @ground = game.add.sprite(320,70,'ground') game.physics.p2.enable([@chest, @arms, @ground]) robotCollisionGroup = game.physics.p2.createCollisionGroup() environmentCollisionGroup = game.physics.p2.createCollisionGroup() @ground.body.static = true @ground.body.setCollisionGroup(environmentCollisionGroup) @ground.body.collides(robotCollisionGroup) @chest.body.debug = true @chest.body.setCollisionGroup(robotCollisionGroup) @chest.body.collides(environmentCollisionGroup) @arms.body.debug = true @arms.body.setCollisionGroup(robotCollisionGroup) @arms.body.collides(environmentCollisionGroup) @ground.reset(game.world.centerX, game.world.height) @chest.reset(game.world.centerX, game.world.height - 100) @arms.reset(game.world.centerX, game.world.height - 100) game.physics.p2.gravity.y = 600 @chest.body.collideWorldBounds = true @arms.anchor.setTo(0.5, 0.5) @constraint = game.physics.p2.createRevoluteConstraint(@chest, [ -14, -26.5 ], @arms, [ 0, 28]) @cursors = game.input.keyboard.createCursorKeys() update: -> if @cursors.left.isDown @arms.body.rotateLeft(60) else if @cursors.right.isDown @arms.body.rotateRight(60) else return render: -> game.config.enableDebug = true game.debug.spriteInfo(@arms, 32, 32) # Bootstrap game.state.add('start', mainState) game.state.start('start')
21. ## P2 physics for top-down tank motion

The best example I've found of what I'm trying to achieve is this - http://schteppe.github.io/p2.js/demos/topDownVehicle.html However, instead of a car I have a tank, so the movement should be more... tankish, without this front-wheels-turning effect. But this is not a problem. The problem is that my tank's movement looks more like a boat on a water movement. I press UP for acceleration and when I press LEFT or RIGHT it still moves in the same direction for a while, like drifting. And if I release UP button and press LEFT or RIGHT again it drifts as well, without taking into account, that he has turned 90 degrees already. Looks awful in terms of a tank behaviour. In other words, I want my tank to stay "on track" while turning, without such huge drift (or asteroid in space) effect. I suppose this is something I should do in update section. Here is what I have there for now: if (cursors.left.isDown) {tank.body.angularVelocity = -0.75;} else if (cursors.right.isDown) {tank.body.angularVelocity = 0.75;} else {tank.body.setZeroRotation();} if (cursors.up.isDown) {tank.body.thrust(120);} else if (cursors.down.isDown) {tank.body.reverse(70);} else {tank.body.damping = 0.8;}cursors = game.input.keyboard.createCursorKeys(); And a few more, not related to main topic, questions: What kind of P2 library Phaser is using? Is it the same as in the example I've provided in the beginning? Is it technically possible in Phaser to calculate collision angle between shell and an object, e.g. another tank? Thank you.
22. ## p2 physics with rotation

So im trying to do a space shooter based on p2 physics thrust exemple. in the create method : game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.defaultRestitution = 0.5; this.bullets = game.add.group(); this.bullets.enableBody = true; //this.bullets.physicsBodyType = Phaser.Physics.P2JS; this.bullets.createMultiple(8, 'laser'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 0.5); Then in the update method i call this function fire: function () { if (this.time.now > this.bulletTime) { var bullet = this.bullets.getFirstExists(false); var p1 = new Phaser.Point(this.ship.x, this.ship.y); var p2 = new Phaser.Point(this.ship.x, this.ship.y - 80); p2.rotate(p1.x, p1.y, this.ship.rotation, false); if (bullet) { bullet.reset(p2.x, p2.y); bullet.lifespan = this.bulletLifespan; bullet.rotation = this.ship.rotation; this.bulletTime = this.time.now + 250; } } } As you noticed I commented the line about settings groups physics to P2JS. The rotation of the laser sprite and its relative positioning to the ship is perfect but right after I uncomment the line; the laser sprite no longer rotate. Its positionning is still relative to the ship rotation but the laser sprite stays straight (point to the top of the screen as the inital image is). Is it suggested to choose ARCADE physics for games using rotations? Also, which property of bullet should i call to make it move on the current angle and direction? Thx
23. ## P2 disable body rotation from other forces

Hi there, how can I disable P2 physics rotating specific body while still be able to use body.rotation property ? I tried kinematic but it ruins collision
24. ## How to detect P2 body collisions, while still detecting intersections

Hi, I want to use a P2 body to detect when a player has crossed a certain point.
25. ## Detecting the collision side.

I need to detect the side on which my sprites collide, I'm using P2 physics. I think there should be something in the contact equation array.