Jump to content

Arcade physics velocity y never gets to 0.


lukaMis
 Share

Recommended Posts

Hi

 

I ran into a problem with velocity y with arcade physics and bouncing.

Velocity y never seems to reach 0. It must be some silly thing that i am doing wrong, right?

My question is how to get to velocity 0?

 

I made a small pen to show what i mean:

 

link: http://codepen.io/lukaMis/pen/OyMNYG

 

Any help would be appreciated.

 

Link to comment
Share on other sites

Haven't dug into specifics, but played around with the values and found out that the body's velocity is somehow tied to body.bounce. Set body.bounce to 0 and velocity also falls down to 0 when the body hits world's bounds. Of course that ruins the whole effect, but it's still a starting point, I guess...

Link to comment
Share on other sites

Dont know if this will help, but what i have recently done is to jump up, setting the velocity.y to a negative number (-650), and also have set gravity... so it will fall again... once at the top of the jump, it will fall, causing the velocity to go negative.. I then set the velocity to 0 once it hits the ground (collides)... Or set the velocity.y to a negative amount again for a bouncing simulation...

Link to comment
Share on other sites

I'm sorta the opposite of a Phaser expert, but it appears that the velocity member seems to be more for physics calculations than for the consuming coder, maybe best not to look at it unless you care about how the physics are being calculated "behind the scenes"; instead looks like you either should call DeltaY() on the object's body or subtract the previous position from the current position to calculate it the movement yourself (this is all calling DeltaY() will do)

this.ball.body.position.y - this.ball.body.prev.y; 
Further catches: you have to do this in your update() function, not in render(), you probably want some epsilon on your comparison (ie. check if the value is between -epsilon and +epsilon to treat glacially slow movements as not moving), and depending on what you do with the figure you may (likely?) want to multiply it by the frame time's delta so that the velocity is in seconds and is not frame rate dependent.

 

This might be a weird question... but what do you need a y velocity of 0 for, exactly?

Maybe to unambiguously detect when something is not moving?
Actually, even if velocity were more "friendly", it would surely still be somewhat ambiguous, right? You might have a character jump up, and at the maximum height of the jump the velocity may be zero (or very close). I think drhayes question is quite sensible, in fact there may be cases where an object bouncing might not move (or might not move much) for a frame due to it falling in the previous frame and rebounding in the next frame back to a similar height... if you are going to determine an object is really no moving you might want to check that it hasn't moved for at least a few physics steps.
Link to comment
Share on other sites

Well if you look at my example pen in first post... i thought of using velocity 0 as as sign that ball stopped bouncing so i can re-enable dragging on a ball sprite. 

My example is really simplistic... my real usage is for user to drag and drop sprite to see bounce effect and i disable drag once sprite has been dropped. 

I re-enable drag on sprite after it has stopped bouncing. It would be really simple to just check velocity.y in update and call method when velocity === 0;

Now i am using all sorts of weird hacks... :)

Link to comment
Share on other sites

Well if you look at my example pen in first post... i thought of using velocity 0 as as sign that ball stopped bouncing so i can re-enable dragging on a ball sprite.

My example is really simplistic... my real usage is for user to drag and drop sprite to see bounce effect and i disable drag once sprite has been dropped.

I re-enable drag on sprite after it has stopped bouncing. It would be really simple to just check velocity.y in update and call method when velocity === 0;

Now i am using all sorts of weird hacks... :)

This response makes me regret trying to help; and I did check your code.

I feel I have somewhat covered a valid way to determine the actual change in position between frames and, why that is not what the velocity member does (it is used for dynamics calculations), why you should not use an single frame measure of either to determine if an object has come to rest, and I have given you the further rather important tip (albeit without much explanation) that you should use +/- epsilon comparisons when dealing with physics/dynamics systems (rather than comparing exactly to +0.0 or +/- 0.0)

Link to comment
Share on other sites

Hence my question: you're trying to re-enable dragging when the ball has stopped moving, not *specifically* when its y velocity is 0. It's stopped moving when its position hasn't changed much from frame to frame or to stop jittering. Lots of physics engines use the concept of "sleeping" to combat this problem, for example.

 

chg is right: either check that velocity has been within the same small region for a few frames, check that the position hasn't changed for a few frames, or something similar.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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