kaplayjs / kaplay

🦖 A JavaScript/TypeScript Game Library that feels like a game. Make games fast, fun and fanstastic.
https://kaplayjs.com
MIT License
394 stars 31 forks source link

update velocity when moving if available #395

Open dragoncoder047 opened 1 week ago

dragoncoder047 commented 1 week ago

I was trying to write some code that used the player's velocity to make footstep noises when the player moves, but the pos component's move function just updates the position, not the velocity. I had some horribly ugly hacks in place to get it working.

Why not add the move()'s "simulated velocity" to the real velocity to make it work instead? Then you can check the velocity to get the the actual motion of the object.

(I will test this and close the PR if it obviously doesn't work.)

pkg-pr-new[bot] commented 1 week ago

Open in Stackblitz

pnpm add https://pkg.pr.new/kaplayjs/kaplay@395

commit: 9ba6e16

dragoncoder047 commented 1 week ago

OK, that's very weird. I tried it and it COMPLETELY breaks movement; and additionally all of the fixed bodies fail to render at all (Why?)

imaginarny commented 1 week ago

Shouldn't you then just use the addForce() or applyImpulse() available on BodyComp for movement?

dragoncoder047 commented 1 week ago

Shouldn't you then just use the addForce() or applyImpulse() available on BodyComp for movement?

It's not exactly equivalent, since if I use move() the body stops moving instantly when I stop calling move(), but the if I use addForce(), it will continue to slide until it hits something or slows down enough.

imaginarny commented 1 week ago

Would it be possible to reset the velocity in that case to stop?

mflerackers commented 1 week ago

Shouldn't you then just use the addForce() or applyImpulse() available on BodyComp for movement?

It's not exactly equivalent, since if I use move() the body stops moving instantly when I stop calling move(), but the if I use addForce(), it will continue to slide until it hits something or slows down enough.

You need to take into account the current velocity when calculating your force. Like the surface effector does for example.

mflerackers commented 1 week ago

BTW, the vector rejection of a vector on the same vector is 0 because there is no orthogonal component.