← back to project logs

fancy movement

Initially, the player movement was: if the button to move left is being pressed, the player is moved left by a number of pixels. This is snappy and feels easy to control but I want this project to be like a physics simulation with friction and forces, so I have changed the player movement and the stick throwing. Pressing the button to move left now initiates a force to the left that acts against friction to give a total force and then an acceleration, used to update the player’s velocity. This also gives a natural max speed of the player and plenty of variables I can tweak to make movement feel different in different areas or when carrying heavy objects. The player has a mass and an output force and the surface has a friction value. The values I have picked are just numbers that looked and felt OK and there are plenty of tweaks to make to make this more physically accurate but it is a start. Here is a snippet of the code to move the player:

var mass = 60 const FORCE = 18000 const SURFACE_FRICTION = 1.5 const FRICTION = 350 * SURFACE_FRICTION var input_vector = Vector2( Input.get_action_strength("right") - Input.get_action_strength("left"), Input.get_action_strength("down") - Input.get_action_strength("up") ) var input_force = input_vector.normalized() * FORCE * SURFACE_FRICTION var friction_force = -velocity * FRICTION var acceleration = (input_force + friction_force) / mass velocity += acceleration * delta move_and_slide()

I also added sprinting which neatly lent itself to increasing the player’s force in this more physics-based movement:

const SPRINT_FORCE_MULTIPLIER = 1.4 if Input.is_action_pressed("sprint"): input_force *= SPRINT_FORCE_MULTIPLIER

Here is a gif of the player moving with SURFACE_FRICTION set to 0.1:

I also added a shadow to the stick that shows its actual 2D location when it is being thrown, not shown in the clip.