Jump to content

setting drag & friction values do not decelerate sprite


cdaringe
 Share

Recommended Posts

hi all,

i've found that when setting drag and friction

// in constructor of an extended Phaser.GameObjects.Sprite
this.body.setAllowDrag(true)
this.body.setDrag(100, 0)
this.body.setFriction(0.7, 0)

the above values have no effect in arcade physics once my update loop runs.

// ... e.g.
if (cursors.right!.isDown) this.body.setVelocityX(160)

 

What occurs against expectation instead instead is that my sprites velocity remains consistent until halted by a collision.  In the above code, I can observe the player sprite glide across the screen to the right with unchanging velocity until impact.  here's my sprite's file: https://gist.github.com/cdaringe/76d17baad21f677534f01030fb99d8fe .  it's not my full game, but at least enough to start the conversation.

 

what conditions must be in effect s.t. drag & friction are honored in phaser3?  thank you!

Link to comment
Share on other sites

Drag can't reduce the velocity if you're also setting a (larger) constant velocity at the same time. And acceleration should be 0, or perhaps less than drag.

The basic formula is 

newVelocity = velocity + (acceleration || -drag)

"Friction" in Arcade Physics is a special case of momentum transfer during collisions, unrelated to drag.

Link to comment
Share on other sites

hi @samme,

thanks for writing!

Drag can't reduce the velocity if you're also setting a (larger) constant velocity at the same time

i trust you on this, but it doesn't register as correct internally.  i set dragX only once, and expected it to stick indefinitely for the body.  drag, to me, is a force constantly applying work against a body during motion.  and if that were the case, it would be working against the body's motion regardless of what i set the velocity to.  i read the cross referenced link, but it didn't set me straight.  would you be willing to clarify how my drag interpretation is incorrect?

with respect to friction, I read that the body must be "immovable and in motion".  my character is movable, so i fail this condition.  i'm ok not working on this aspect right now, but is there not a mechanism  to apply friction to say, a sliding body on a platform?

 

my end goal is to have my character to have some momentum that ultimately results him/her stopping after some time.  ideally that stopping time is a factor of his/her current x speed.

i really appreciate the input!

Link to comment
Share on other sites

My formula was wrong: Phaser applies drag only if acceleration is zero. So that may be the problem you're seeing.

Assuming zero acceleration, though — drag is applied constantly to velocity, but if you're setting velocity constantly at the same time, you're just erasing drag's effect.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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