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. 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.
  2. 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
  3. 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.
  4. 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.
  5. 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
  6. 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
  7. 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!
  8. So I have been searching a solution for this for 2 days now. I have a character with physics body. This character has sprites attached to it with addChild(). Arms, sword, cloak, range indicator. Range indicator also has a body (only used as sensor/trigger ). However as I found out addChild doesn't work on physics bodies. Sprites move together but I can get Range Indicator's body to move with them. It pretty much sits in the corner. Since addChild() is no go for physics bodies, is there anything else I can use with this? Really trying to avoid writing a custom function to make the body follow me but running out of options here. EDIT: Found a workaround for weird child behaviour with bodies. Added another invisible sprite and made it child to my body. this.rangePositioner = game.add.sprite(0, 0, 'rangeBlack'); this.rangePositioner.anchor.set(0.5); this.rangePositioner.pivot.set(0, 40); this.addChild(this.rangePositioner); Take its world position with ( rangePositioner.position returns 0 ) then set it to my child body.position. I had to dig for some time before I realized returns a point. I would expect it to be named something like worldPoint. Anyways I hope this helps someone.
  9. How do I add a terrain that is built from tilemap to a collision group? I couldn't find any examples
  10. 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
  11. 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;
  12. Runnable code here: I'm using P2 physics and I'm trying to raycast to the mouse position. I've got a tilemap with physics bodies set up, and they show fine with debug draw on. However, the ray never hits any of the tile bodies, nor the player body. It only hits the top and left side of the world bounds. Screenshots attached. Is there anything obvious I'm doing wrong, or is this a bug?
  13. 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!
  14. 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.
  15. 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:
  16. 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
  17. 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.
  18. ( 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: 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.
  19. 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!
  20. Hey guys, I am developing this draft game demo and I need some advice/ feedback on some tips to make the collision a bit better. I'm using loadPolygon to load a custom body shapes for this sprite object and responding to collisions using onBeginContact to applyImpulse to the ball on a SPACEKEY.onDown The following is the code player.body.collides(ballCollisonGroup, this.headerBall, player) headerBall(bodyA, bodyB, shapeA, shapeB, equation) { let duration = 10; let localX = equation[0].contactPointA[0]; let localY = equation[0].contactPointA[1]; let forceX = 0.12 * this.headMass; let forceY = 0.8 * this.headMass; if(this.headerKey.isDown) // only if see is down apply additional force { duration = Math.min(this.headerKey.duration, 500); // apply time based on how long key pressed forceY = (1 * this.headMass).toFixed(2); // apply force of 1 velocity x mass of head (.53) forceX = (1 * this.headMass).toFixed(2); let impulse = [(forceX * duration), (forceY * duration )]; // calculate impulse bodyA.applyImpulse(impulse, localX, localY ); // apply it to ball } } I'm not sure if I am doing it right as I am not verse in Math Physics but i would like some feedback on the following: - Is my formula accuarate? Can I set varying impulses based on the contact shape? There is a sort of glitch if the ball comes into contact with his shoes and the ball ends up in the middle of his body which gets stuck. How can I avoid this? The key down condition seems to be glitchy is there anyway to make this a bit more "safe" to detect and capture? I need to apply some force to the ball ONLY when the space key is pressed. Thank you in advance for your feedback.
  21. 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!
  22. 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), = same thing, restitution = 1; CODE OF INTEREST: WORM CLASS - specifically lines 112-117 GAME STATE Thanks in advance for your help!
  23. 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: 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: 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): 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.
  24. I have a p2 Object with the following properties, 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;[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.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?
  25. 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.