Jump to content

Como usar a função moveToPointer() com corpo P2JS?

Salomão Neto

Recommended Posts

[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

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


this.game.physics.startSystem(Phaser.Physics.P2JS); //You do it only once, and then use it at will. Not necessary with ARCADE physics.

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

  • 3 months later...

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


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...