Salomão Neto Posted February 27, 2018 Share Posted February 27, 2018 [Português] Estou começando a estudar o PhaserJS e estou criando um clone do Agar.io. Preciso mover o player de acordo com a posição do cursor, porém, não obtive sucesso ao utilizar a função moveToPointer usando um corpo P2JS. Veja no codepen a minha tentativa: [English] I'm starting to study PhaserJS and I'm creating a clone of Agar.io. I need to move the player according to the position of the cursor, however, I did not succeed when using the moveToPointer function using a P2JS body. See my codepen for my attempt: Link to comment Share on other sites More sharing options...
DanielKlava Posted February 27, 2018 Share Posted February 27, 2018 Hello @Salomão Neto! If you are using P2 physics (or any other system other than P2), you have to explicitly activate it before using. The default Physics system is ARCADE, so that's why you didn't have to start it when you were using it. Please, try updating the line: game.physics.enable(this.player, Phaser.Physics.P2JS) // Error To: this.game.physics.startSystem(Phaser.Physics.P2JS); //You do it only once, and then use it at will. Not necessary with ARCADE physics. this.game.physics.p2.enable(this.player); Also, regarding the "moveToPointer()" function, it's not available in P2 Physics. You will have to calculate the mouse pointer location on screen, and the angle and positions relatives to your player object and move it accordingly. Take a look at this topic, where a function "moveBullets" was posted with a simple calculation: Let me know if it works for you! Link to comment Share on other sites More sharing options...
samid737 Posted March 2, 2018 Share Posted March 2, 2018 under the hood, moveToPointer works the same way for P2 bodies. Link to comment Share on other sites More sharing options...
Kamran Posted June 25, 2018 Share Posted June 25, 2018 I have faced the same problem when I was developing my game. Here's the solution: As there is no such function in P2JS you will need to calculate all the stuff by yourself. I though up 3 functions which thoroughly replace the moveToPointer(): function moveToPointer(displayObject, speed, pointer, maxTime) { pointer = pointer; if (maxTime === undefined) { maxTime = 0; } var angle = angleToPointer(displayObject, pointer); if (maxTime > 0) { speed = distanceToPointer(displayObject, pointer) / (maxTime / 1000); } displayObject.body.velocity.x = Math.cos(angle) * speed; displayObject.body.velocity.y = Math.sin(angle) * speed; return angle; } function distanceToPointer(displayObject, pointer, world) { if (world === undefined) { world = false; } var dx = (world) ? displayObject.world.x - pointer.worldX : displayObject.body.x - pointer.worldX; var dy = (world) ? displayObject.world.y - pointer.worldY : displayObject.body.y - pointer.worldY; return Math.sqrt(dx * dx + dy * dy); } function angleToPointer(displayObject, pointer, world) { if (world === undefined) { world = false; } if (world) { return Math.atan2(pointer.worldY - displayObject.world.y, pointer.worldX - displayObject.world.x); } else { return Math.atan2(pointer.worldY - displayObject.body.y, pointer.worldX - displayObject.body.x); } } Still, you will need to activate P2JS to calculate body positions and velocity. Link to comment Share on other sites More sharing options...
Recommended Posts