fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

Movement Issues #147

Closed sourcerose closed 3 years ago

sourcerose commented 3 years ago

If you walk forward while jumping, then release the forward key, you continue moving forwards. Not sure if this is a bug or expected but I was wondering how I could remove it.

fenomas commented 3 years ago

Hi, so there are a couple of factors here:

  1. The physics engine behaves like regular physics - bodies in motion stay in motion unless a force acts on them. So in general, if an entity (including the player) is moving in the air, it keeps moving since there's no friction from the ground to slow it down.
  2. The code that exerts move forces on the player is in the movement component. The default implementation is, when you're pressing a key then a force is exerted (the force is reduced if you're not on the ground). If no move key is being pressed, no force is exerted.

So the result is, when a player is moving, and releases the move key, if they're on the ground they will be stopped by friction, and if they're in the air they'll keep moving since no forces are applied.

If you want to change this, you can remove the default movement component from the player entity and add your own custom one. I.e.:

noa.entities.removeComponent(noa.playerEntity, noa.entities.names.movement)
noa.entities.addComponent(noa.playerEntity, yourMovementComponent)

I put the move code in a component like that to make it easy to override, since I assume most games will want to customize it.

sourcerose commented 3 years ago

Ah I see, so when the player is jumping up and down, they don't stick to the ground, so there is less friction

fenomas commented 3 years ago

Yes, that's the idea. The logic for move forces is a little more complicated, here is where it's implemented. But when no movement key is pressed then all the code does is set the entity's friction to a given value, and assume that ground friction will naturally stop the entity.

sourcerose commented 3 years ago

Thanks, got it fixed!