Search the Community

Showing results for tags 'p2'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

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

Found 287 results

  1. How to use P2 Physics with each layer object? Code this.saws = this.game.add.group(); this.saws.enableBody = true; this.map.createFromObjects('saws', 4, 'saw', 0, true, false, this.saws); this.saws.forEach(function(saw) { saw.body.clearShapes(); saw.body.loadPolygon('physicsData', 'saw'); });
  2. Check out our new tutorial, Phaser Cars & Trucks with P2: https://loonride.com/learn/phaser/p2-truck We will cover constraining wheels to the truck body, increasing wheel grip, and creating a bouncy chassis effect that is seen in many popular games today. See what you will learn to create: https://loonride.com/examples/bouncy-truck
  3. Our new tool, Loon Physics, has just been released to open beta for free! Try it now to export P2 Physics body polygons for Phaser. Use a URL to test live without exporting files. Use Loon Physics here: https://loonride.com/physics Learn how to make a game with P2 Physics using this brand new tutorial: https://loonride.com/learn/phaser/p2-physics-bodies
  4. Hello, In the tiled map editor is a tilecollision editor i was just wondering if there is any way to get the stuff u do in there to work in phaser, or any other way to set a specific collision polygon to a tile.
  5. I'm working on a Pachinko-like game and have a question about physics. I'm new to Phaser, so if this is an obvious answer, sorry! I have a star-shaped pin wheel thing. I need it to be stationary but rotate around its anchor point when hit by an object (based on physics). How would I go about making it stationary, but allowing it to be rotated based on objects colliding with it? (I know how to get the polygon collision using the Physics Editor, I'm just not sure about the rotation part). Any help will be greatly appreciated! Thanks.
  6. Loon Ride, the epic loon game, has received major updates on all platforms! Collect loons, shoot cacti, and soar through endless mode. This game was made using Phaser. Read more about the game here: https://loonride.com/about/loonride Play the game in your browser here: https://loonride.com/games/loonride Get the game on Google Play here: https://play.google.com/store/apps/details?id=com.loonride Promo video:
  7. We recently released a tool for editing P2 Phyics body polygons called Loon Physics. Check it out here: https://loonride.com/physics Read a tutorial about P2 Physics bodies and Loon Physics here: https://loonride.com/learn/phaser/p2-physics-bodies Do you find Loon Physics useful? Do you encounter any bugs when using the tool? Any suggestions?
  8. hi, i'm coding a pinball game in js with phaser and p2physics. I search how i can do rotation with a anchor point. I want create the rotation for the flipper but method rotateLeft or rotateRight do the rotation by the center's sprites. I want create a rotation around topleft and top right point of the sprite to do a flipper movement. orry for my bad english, thx.
  9. I made an example of how you can mimic a skeleton/bodyparts using LockConstraints in P2 physics. I thought it might be useful to share it/ add to the Phaser examples , for people looking for ways to use joints/ lock constraints in their game. here is the example: http://54.87.189.59/example3/ The sprites I used are from my own game, but you could very well replace it with any other sprites... In the code I also explained how it works.
  10. Hello guys I'm making a platformer game, I struggled with making collision between the player and objects created in tiled but after that I got it working with a function I created, however, when I make the rectangle rotated in tiled, in the game it comes out away from its position, take a look: this is on tiled in the game it comes out like this here is my code, createCollisionObjects is where the magic happens var game = new Phaser.Game(70 * 10, 70 * 7, Phaser.AUTO, 'game', { preload: preload, create: create, update: update, render: render }); function preload() { // tilemap game.load.tilemap( 'map', 'assets/tilemaps/maps/lvl-1.json', null, Phaser.Tilemap.TILED_JSON ); // tilemap images game.load.image('new', 'assets/tilemaps/tiles/new.png'); game.load.image('sheet', 'assets/tilemaps/tiles/sheet.png'); game.load.image('s9af', 'assets/tilemaps/tiles/s9af.png'); // player spritesheet game.load.atlasJSONHash( 'player', 'assets/spritesheets/player_walk.png', 'assets/spritesheets/player_walk.json' ); } /* * GLOBAL VARIABLES */ var map; var player; var playerSpeed = 400; var jumpTimer = 0; function create() { game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setBoundsToWorld(true, true, true, true, false); game.physics.p2.gravity.y = 300; game.physics.p2.restitution = 0; game.stage.backgroundColor = '#ffffff'; map = createMap().map; var start = map.objects.objective[0]; player = createPlayer({x: start.x, y: start.y}); cursors = game.input.keyboard.createCursorKeys(); } function update() { player.body.setZeroVelocity(); if (cursors.up.isDown && game.time.now > jumpTimer && checkIfCanJump()) { player.body.velocity.y = 600; jumpTimer = game.time.now + 750; } if(cursors.left.isDown) { player.body.velocity.x = -playerSpeed; player.animations.play('walk'); } else if (cursors.right.isDown) { player.body.velocity.x = playerSpeed; player.animations.play('walk'); } else { player.body.velocity.x = 0; player.animations.stop(); player.frame = 0; } game.camera.follow(player); } function render() { } function createMap() { var m, g, f, b, c; m = game.add.tilemap('map'); var mWidth = m.widthInPixels; var mHeight = m.heightInPixels; game.world.setBounds(0, 0, mWidth, mHeight); m.addTilesetImage('sheet', 'sheet'); m.addTilesetImage('new', 'new'); m.addTilesetImage('s9af', 's9af'); g = m.createLayer('ground'); f = m.createLayer('fringe'); b = m.createLayer('background'); g.resizeWorld(); f.resizeWorld(); b.resizeWorld(); createCollisionObject(m.objects.objects); return { map: m, ground: g, fringe: f }; } function createPlayer(pos) { var p = game.add.sprite(pos.x, pos.y, 'player', 'Symbol 2 instance 10000'); //p.scale.setTo(0.4, 0.4); game.physics.p2.enable(p); p.body.setZeroDamping(); p.body.fixedRotation = true; p.body.x+=p.width/2; p.body.y+=p.height/2; // animation p.animations.add( 'walk', Phaser.Animation.generateFrameNames('Symbol 2 instance 1000', 0, 7, '', 1), 10, true, false); //p.animations.play('walk-left'); return p; } function createCollisionObject(objects) { if (objects) { for(var i=0; i < objects.length; i++) { var p2 = game.physics.p2; var obj = objects[i]; var sprite = game.add.sprite(obj.x, obj.y, null); sprite.width = obj.width; sprite.height = obj.height; sprite.angle = obj.rotation; game.physics.p2.enable(sprite, false); sprite.body.x = sprite.x + sprite.width / 2; sprite.body.y = sprite.y + sprite.height / 2; sprite.body.rotation = obj.rotation; sprite.body.static = true; sprite.body.debug = true; } } } function checkIfCanJump() { var yAxis = p2.vec2.fromValues(0, 1); var result = false; for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) { var c = game.physics.p2.world.narrowphase.contactEquations[i]; if (c.bodyA === player.body.data || c.bodyB === player.body.data) { var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis if (c.bodyA === player.body.data) d *= -1; if (d > 0.5) result = true; } } return result; }
  11. Hi, I am getting some unwanted behaviour with groups and p2 physics. When the sprites is leaving the screen, I want to kill them and reset new ones. But when I kill the first one, and reset a new one, the physics goes haywire. Anyone got a suggestion on how to fix this? Check this pen for an example:
  12. I need to check if one object (Sprite/p2 body/shape) contains another object. In p2 documentation I found only Contact event, but I need to know when body inside sensor or other area. In Phaser I found Phaser.rectangle.containsRect, this is what I need but Rectangle do not support rotation as I understand. So fail (( With AS3 and Nape I did it this way: if(Geom.contains(outShape, inShape)){.....} How I can do it in Phaser and/or p2 physics ?
  13. Hi to you all. Hi have a question. I'm making a platformer game using phaser and , for now, arcade physics. I make my level in Tiled and export them in JSON. I noticed that since Tiled work with png tileset and that tiles are square. My character do not detect a slope or slante corner. It stands on a invisible cube border. What i was wondering is, is it possible for phaser to detect slanted slope using arcade or should i move to P2JS. Also how do I tell phaser to ignore the transparent part and collide only with the image? I tried making object layer coutouring all the platform for a more precise collision but it seems i'm not able to create a collision layer with a Tiled object layer. or maybe i'm doing it wrong. function create() { map = game.add.tilemap('niveau2'); map.addTilesetImage('tileset','background'); map.addTilesetImage('hints','hints'); backgroundLayer = map.createLayer('background'); collisionLayer = map.objects.evenement.find( o => o.name == 'platform'); collisionLayer.visible = false; console.log(collisionLayer); map.setCollisionByExclusion([],true,collisionLayer); collisionLayer.resizeWorld(); //map.setCollision(1) player = game.add.sprite(32, game.world.height - 150, 'star'); game.physics.arcade.enable(player); player.body.collideWorldBounds = true; player.body.gravity.y=10050; // Our controls. cursors = game.input.keyboard.createCursorKeys(); } function update() { game.physics.arcade.collide(player, collisionLayer); //more code }
  14. Hi, 1 month ago, I started looking into physics coz I've wanted to make a pinball game (I own 5 real games and am a bit of Pinball freak). So I started looking at physics, which I never had used before (I made some other Phaser games before) Anyway, P2 seemed good enough and I also found this demo by forum member george : http://georgiee.github.io/phaserpinball/ So I worked from that and I now have this : http://n-vision.nl/games/pinball/ Still a work in progress, but It plays quite nicely I think.. What I would like to add is *active* elements that not just bounce the ball back, but kick it back when hit. I can register a hit and then need the bumpers and slingshots to kick the ball away (on top of the bounce) What would be the best way to achieve this ? I tried using body,reverse but that is producing strange effects.. I can think of these options : always apply the same restitution value for bumpers and slingshots (so not dependent on speed of ball) - but that's probably not possible create a virtual kicker using P2 that kicks the ball (seems pretty hard to do for a bumper because it's a circle that can be hit from any direction) create a reverse force for the pinball Anyone has any ideas or pointers ? Besides this - would box2d be better for pinball then P2 ? thanks for any input or feedback.
  15. Hi guys! I'd like to make a computer object in my game, such that when player approches to the computer,it triggers onEnter callback ( where I can for example show a message on screen to press certain key ) and when player goes away it triggers onLeave callback ( where I can hide the message ). In order to achieve that I need two colliders, one for regular collision ( red one ), and the other to detect if the player entered computer's zone ( green one, this is the one that I want to trigger callback ). this.computer = this.game.add.sprite( 9 * TILE_WIDTH + COMPUTER_WIDTH / 2, TILE_HEIGHT + COMPUTER_HEIGHT / 2, 'computer' ); this.game.physics.p2.enable( this.computer ); this.computer.body.static = true; this.computer.body.addRectangle( TILE_WIDTH, TILE_HEIGHT, COMPUTER_WIDTH / 2, COMPUTER_HEIGHT / 2 ); this.computersCollisionGroup = this.game.physics.p2.createCollisionGroup( this.computer ); this.computer.body.collides( this.playerCollisionGroup, () => console.log( 'player in range' ) ); this.player.body.collides( this.computersCollisionGroup ); My question is: How do I turn off physical collision in green collider and what to do to make green collider trigger onEnter and onLeave callbacks ?
  16. A mobile only game(Chrome only), and the second that I made so far (mostly it's just me experimenting stuff xD ). It uses javascript api to detect motion and move a ball through a maze. The maze is re-generated on every run. I use p2js for physics and on the devices that I tried out works pretty well. https://play.google.com/store/apps/details?id=com.cadsotic.aMazeBallfree
  17. 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.
  18. Hi! If I use p2 physics what is the most effective way to add slopes to my tilemap? I use tiled. I saw a solution here (below) where you make the tile layer with the slope tiles non-collidable, and you make an object layer on top of that where you draw polylines, and make the object layer collidable, but thats not that efficient if you design a level and want to try out a lot of variations. So my question is, is there a more efficient way of doing this? For example like in ninja physics where you can associate the tiles with different shapes.
  19. Hi all, I'm trying to have my bullets group that already collides with the enemies, both of them are a group, but now that I want to add a static block or obstacle, it simply wont collide or overlap or nothing, what I want is that when the bullet collides or overlaps it gets destroyed or at least bounce into the obstacle, see code below: ////////////////My Bullets//////////These work fine and collide correctly with my enemies.////// createBullets: function() { this.bullets = this.game.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.P2JS; this.bullets.createMultiple(100, 'bulletSprite', 0, false); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 0.5); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); }, fireBullet: function(){ if (this.bulletTimer < this.game.time.time) { this.bulletTimer = this.game.time.time + 500; this.bullet = this.bullets.getFirstExists(false); if (this.bullet) { this.bullet.reset(this.tanke.x, this.tanke.y - 20); this.bullet.body.setCollisionGroup(this.bulletCG); this.bullet.body.collides([this.figuraCG]); this.bullet.body.velocity.y = -3500; } } }, //////This is the block or obstacle this just wont work no matter what I try/////////// makeOneBloque: function(){ this.bloque = this.game.add.sprite(500, 950, 'Blokes'); this.bloque.enableBody = true; this.game.physics.p2.enable(this.bloque, true); this.bloque.body.kinematic = true; this.bloque.body.collides(this.bullets, this.collisionBulletBloque, this); //////I tried overlaps and it just crashes the lvl }, collisionBulletBloque: function(bullet) { bullet.sprite.destroy(); }, I would greatly appreciate any help or suggestions.
  20. Trying to create a group, but keeps saying it is undefined got any suggestions? Please see code below. this is defined in the create function: this.bloqueCG = this.game.physics.p2.createCollisionGroup(); makeOneBloque: function(){ this.bloque = this.game.add.group(); this.bloque.enableBody = true; this.bloque.physicsBodyType = Phaser.Physics.P2JS; this.bloque.create(200, 950, 'Blokes'); this.bloque.create(550, 950, 'Blokes'); this.bloque.create(900, 950, 'Blokes'); this.bloque.body.setCollisionGroup(this.bloqueCG); <<<<----I get error here :( this.bloque.body.collides(this.bulletCG, this.hitBloke, this); this.bloque.body.kinematic = true; this.bloque.setAll('anchor.x', 0.5); this.bloque.setAll('anchor.y', 0.5); this.bloque.setAll('outOfBoundsKill', true); this.bloque.setAll('checkWorldBounds', true); },
  21. Runnable code here: https://github.com/darkf/grr 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?
  22. 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, this.game.world.height); 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; enemy.body.data.shapes[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?
  23. How to detect the object from group when collided to player in p2. like: Arcade child from group ex:
  24. 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(game.world.centerX-10, game.world.centerY-20, 'world'); world.anchor.setTo(0.5, 0.5); world.scale.setTo(scaleRatio, scaleRatio); platforms = game.add.group(); platforms .enableBody = true; platforms.physicsBodyType = Phaser.Physics.P2JS; for (var i = 0; i < 10; i++) { asteroid = platforms.create(game.world.height , game.world.width , '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(game.world.centerX, game.world.centerY, '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 = game.world.centerX+ Math.cos(rocketangel) * rocketradius ; player.y = game.world.centerY+Math.sin(rocketangel) * 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() { this.game.state.start("gameover"); }; </script> </body> </html>
  25. 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.