[SOLVED] Angular Dashing Mechanism (p2.js integration)

I have been trying recently to add a feature to my 2D game where I can make the player dash forwards when they press SPACE. Does anyone know how I would make angular force such as:
apply force “10” at “facing angle of player”
The above situation in Italics is an example of my end goal. Does anyone know what I would need to do (IN p2.js) in order to apply a force at the facing angle of my 2D player

Thanks,
Chhes

[SOLUTION]:
In order to solve this I used some of the code below, but instead of using this.entity.forward I used this.entity.up only for the x and y axis.

Hi @Chhes ,

It’s been a while since I’ve used p2.js in PlayCanvas. I think though that wouldn’t be much different than doing it in 3D space with Ammo.

The steps would involve:

  1. Find the forward direction of your player entity in 2D space (e.g. in XY if that’s your p2 setup).

  2. Calculate the angular force on the body based on that forward direction:

const dashPower = 100.0;

body.angularForce[0] = forward.x * dashPower;
body.angularForce[1] = forward.y * dashPower;

Hope that is of help!

2 Likes

oh wow, I did not know angular force was an array, thanks for your help I will try this and get back to you!

1 Like

Hello again @Leonidas it seems that the angular force I implemented did not work. I do not think that body.angularForce is actually an array so it did not work. Here is what I put:

this.entity.script.p2Body.body.angularForce = [100, 100];

When I logged the values for angularForce[0] and [1] they would show as 100 since I am testing with that first. The player does not move whatsoever when I pressed or even held down space. Any other ideas?

Thanks for your help!

For dashing, would applying an impulse be more applicable? Body - p2.js

1 Like

honestly, once ANY form of launching my character works (e.g. impulse, force) then I will be able to customize it, I am just not sure how to get the player to launch at its facing angle since none of the methods I have tried are actually… launching it

Example of dashing forward: https://playcanvas.com/editor/scene/1186906

See: https://playcanvas.com/editor/code/808993?tabs=50796454&line=20

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        var forward = this.entity.forward;
        this.entity.script.p2Body.body.applyImpulse([forward.x * 10, -forward.z * 10]);
    }
1 Like

Hey Thanks for the help so far, but I am having trouble with applyImpulse() since it is not actually sending my player in any distance at any angle anywhere. I have been trying to figure this out but cannot seem to get it right. Do you happen to know why the applyImpulse() may not be working correctly?

if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.entity.script.p2Body.body.applyImpulse([10, 10]);
    }

Another thing is that my this.entity.forward.x or .y was equal 0 and the .z was equal to -1, so I removed it for now in order to test this current issue.

Difficult to tell without a project that showcases your issue as it works in the example project that I posted.

For example, is there other code that is moving your player that is resetting the position?

oh alright, here is the project:
https://playcanvas.com/editor/scene/1088199
The information will be under Movement.js in the scripts folder
At lines 54 - 56

oh wait, there was a velocity thing resetting the position!
Thanks, that part works now so I will try to get the angle for this impulse now

alright, I do not know why the:

this.entity.forward;

is still showing 0 for x and y, so if you happen to know why this happened, then I think that will solve the rest of this issue!

Also, sorry for the three messages in a row, next time I will condense it

Looking at the project, the way that your project is laid out, ‘forward’ for your player is not the forward vector for Entity, it’s the up vector.

The following code fixes that issue:

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        var forward = this.entity.up;
        this.entity.script.p2Body.body.applyImpulse([up.x * 50, up.y * 50]);
        dash = true;
    }

The other issue on why it’s not dashing is because in the next frame, the velocity is set to 0. In your current code:

    if (!dash && (x !== 0 || y !== 0)) {
        x *= dt * this.playerSpeed;
        y *= dt * this.playerSpeed;

        this.entity.script.p2Body.body.velocity = [x, y];
    }
    else if(!dash)
    {
        this.entity.script.p2Body.body.velocity = [0, 0];
    }
    
    dash = false;

Every frame, dash is set to false so on the next update frame, the code sets the velocity 0.

1 Like