Jump to content

How to manually stop velocity in Phaser?


cjke
 Share

Recommended Posts

I am trying to manually stop gravity after a manual collision test. I can successfully test the collision, however when I set velocity to zero, the player continues to "sink" under gravity, albeit much slower than just gravity - hence "sinking".



// note that this is babel
update() {
this.player.body.velocity.y = 0;
}

How do you set velocity y to zero within update and actually cease the sprite from continuing to move, albeit slowly, due to gravity?


I am a sneaking suspicion that update is called less frequently than the internal physics update cycle? But I could be wrong.


 


If you are on Stackoverflow, and want that sweet sweet karma - you can answer there too:


http://stackoverflow.com/questions/32577934/how-to-manually-stop-velocity-in-the-phaser-framework


Link to comment
Share on other sites

If you set the Y velocity to 0, it will be 0 until next update : because the engine will apply gravity again to your sprite, thus it will fall. If you want it to be perfectly Y-still, you might have to disable gravity on the sprite as well with sprite.body.allowGravity = false;

 

Phaser example : http://phaser.io/examples/v2/arcade-physics/gravity

Link to comment
Share on other sites

Ah I see, thanks for replying.

 

I see what you are saying, interestingly though if I set sprite.body.allowGravity = false within the update loop, it still slightly sinks. Yet if I put it straight after the creation of the sprite, within the create method, it doesn't sink. 

 

Not OK

update() {    // still sinks    this.player.body.allowGravity = false;}

OK 

 create() {        this.player = new CharacterController(this.game, 100, 50); // returns a sprite    this.game.world.add(this.player);    // ok!    this.player.body.allowGravity = false;}
Link to comment
Share on other sites

You are most likely right. I am trying to come up with a way to manually test collision with abstract bitmap data. So I can get the collisions to occur (in the sense of the collision boolean to return true when the overlap occurs), however having difficulty actually apply the zero velocity. 

 

I feel like the update is the exact location this should be occurring though. Doesn't all collision calculations happen within the update loop? 

 

For example, have a look at this codepen by Rich:

http://codepen.io/photonstorm/pen/VYWZwE?editors=001

 

Namely within the update loop. 

 

I haven't heard of long-term object property as terminology before - forgive my lack of knowledge on the Phaser front, I come from a mix of Flash/Haxe and Unity background. 

Link to comment
Share on other sites

No, I mean checking collisions if perfectly fine in the update() function!

 

Just don't put any properties like gravity, physics mode etc. If you want to switch the gravity to off for your character, do it in a separate function which will be called only once (on user action, for example). No need to redefine the property 60 times per second.

 

But you're right, I think you'll run into trouble trying to check collisions on 0 velocity objects, because I think that velocity is used for collision checks.

Link to comment
Share on other sites

Gotcha. And understanding now what you mean. 

 

I think my sticking point here is I am not sure how to not set gravity/velocity on every update cycle when the collision could happen every cycle. 

 

Looking through Phaser.arcade, they are using a seperateY function to essentially pull two bodies away from each other when overlapping. This also must be happening on every update - I remember reading you can provide a custom callback for seperateY, so I will see if that works. 

Link to comment
Share on other sites

Hi, velocity for next frame is affected with all forces - not only gravity, but also acceleration (see computeVelocity in \src\physics\arcade\World.js)

 

So, to stop object, you will have to reset all these to zero. Maybe, you can use reset(x, y) in src\physics\arcade\Body.js

 

Bam! That was it - nice one  :)

 

Woo woo - Cheers 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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