# jak6jak

Members

6

• Rank
Newbie

• Gender
Male
• Location
USA
1. ## Phaser2 Pysics p2

Sorry if this is a bit late, Try looking at the p2 "platformer material" example, specifically at this snippet here, I added some of my own comments to explain everything: function checkIfCanJump() { var result = false; //Search through all collisions going on right now for (var i=0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) { var c = game.physics.p2.world.narrowphase.contactEquations[i]; //is this a collision that involves the player? if (c.bodyA === player.body.data || c.bodyB === player.body.data) { //gets the dot product of the yaxis and the normal var d = p2.vec2.dot(c.normalA, yAxis); //if bodyA is the player we need to flip it so it points upward if (c.bodyA === player.body.data) { d *= -1; } //The normal is pointing upward so that means we are on the ground *YAY!* if (d > 0.5) { result = true; } } } return result; } If you don't know what a normal is I can give you a quick explanation. Basically, a normal vector is a vector (a straight line pointing at some angle over a distance) that is perpendicular to the surface. We then take the dot product of the normal vector and the y-Axis. The dot-product basically gives a higher number the closer they are to being perpendicular (Disclaimer: I would look up dot Product online to find a more mathematically correct answer). This allows us to see if we are standing on flat ground.
2. ## Preferred Language for Phaser?

@Mickety Phaser games can also be written in Typescript.
3. ## How to add slopes to tilemap that uses p2 for collision?

So after a long hard look at the source code and the documentation provided I was able to come up with an answer to my own question. First off: convertTilemap only creates rectangle collision boxes and in no way shape or form checks the collisions you have set up in Tiled using the editor. With that in mind let's take a look at what convertCollisionObjects does, covertCollisionObjects takes the shapes you placed in the object layer of your project and creates the physics bodies for them and then returns it in an array. In order to make the ground collide with the player, you need to loop over the array of bodies and set it to collide with the player (or any other object). Here is the code: create: function () { this.game.physics.startSystem(Phaser.Physics.P2JS); //Tile map var level1 = this.game.add.tilemap('level1'); level1.addTilesetImage('spritesheet_ground', 'mapTiles'); //add platforms layer wallsLayer = level1.createLayer("layer1"); wallsLayer.resizeWorld(); //creates the player and ground collisionGroup var groundCollisionGroup = this.game.physics.p2.createCollisionGroup(); var groundMaterial = this.game.physics.p2.createMaterial('groundMaterial'); var playerCollisionGroup = this.game.physics.p2.createCollisionGroup(); var playerMaterial = this.game.physics.p2.createMaterial('playerMaterial'); //returns an array of bodies which we then set the collision group and make it collide with player var tileMapBodiesArray = this.game.physics.p2.convertCollisionObjects(level1, "objectLayer1"); for (var i = 0; i < tileMapBodiesArray.length; i++) { tileMapBodiesArray[i].setCollisionGroup(groundCollisionGroup); tileMapBodiesArray[i].collides(playerCollisionGroup); tileMapBodiesArray[i].setMaterial(groundMaterial); } player = this.game.add.sprite(200, 200, 'player'); this.game.physics.p2.enable(player); player.body.clearShapes(); player.body.addCapsule(40, 40, 0, 0, 1.5708); player.body.setCollisionGroup(playerCollisionGroup); player.body.collides(groundCollisionGroup); player.body.setMaterial(playerMaterial); player.body.data.gravityScale = 20;
4. ## How to add slopes to tilemap that uses p2 for collision?

Hello, I am quite new to phaser and Tiled. I was wondering if anyone could give me some help with understanding on how to create tilemap collisions that are more than just squares with the p2 physics system. Right now every tile has a square collision box, I would like the collision shape to match the tile shape so that I can have slopes and what not. I am a quite a bit confused on the Tiled to phaser workflow especially on what p2.convertTilemap does and how Tiled's collision editor ties into phaser. So far my code looks like this: preload: function () { this.game.load.image('mapTiles', "assets/platformer-pack-redux-360-assets/Spritesheets/spritesheet_ground.png"); this.game.load.tilemap('level1', "assets/testmap2.json", null, Phaser.Tilemap.TILED_JSON); this.game.load.spritesheet('player', "assets/kenney_platformercharacters/PNG/Player/player_tilesheet.png", 80,110, 24); }, create: function () { this.game.physics.startSystem(Phaser.Physics.P2JS); var level1 = this.game.add.tilemap('level1'); level1.addTilesetImage('spritesheet_ground', 'mapTiles'); var wallsLayer = level1.createLayer("layer1"); wallsLayer.resizeWorld(); level1.setCollisionByExclusion([],true,'layer1'); this.game.physics.p2.convertTilemap(level1,wallsLayer); this.game.physics.p2.convertCollisionObjects(level1,"objectLayer1"); player = this.game.add.sprite(110,80, 'player'); var walk = player.animations.add('walk', [0]); player.animations.play('walk', 10, true); this.game.physics.p2.enable(player); this.game.camera.follow(player); player.body.setZeroDamping(); player.body.fixedRotation = true; this.game.physics.p2.setBoundsToWorld(true,true,true,true,false); cursors = this.game.input.keyboard.createCursorKeys(); wallsLayer.debug = true; player.body.debug = true; }, Any help is appreciated!
5. ## How to add velocity to a Group?

ok but now it is giving me this error: Cannot call method 'getFirstDead' of undefined on line 97 what am I doing wrong there?
6. ## How to add velocity to a Group?

http://pastebin.com/1BMsEKQr it keeps giving me this error: TypeError: Cannot read property 'velocity' of null on line 98