Page 1 of 1

Help with my game character's movement

Posted: Fri Nov 21, 2014 2:02 am
by Brainy
I have just started working on my top down game , but I realized that my controls felt too (tight,sharp,)? I set it up so that when I released the button I stopped immediately, this isn't what I want. What I want is for my character to continue in the direction he was going in, kind of like he is on ice, so it doesn't feel so sharp. I am not the best at math so I wasn't able to figure anything out although the answer is probably quite easy , but if anyone could help that would be great! It would also be nice if you explain how your solution worked so I can learn from it. I will have my .love file attached.

Re: Help with my game character's movement

Posted: Fri Nov 21, 2014 4:12 am
by ivan
Depends on what library/module you are using.
If you are programming the physics yourself this is one approach to slow down the player velocity:

Code: Select all

-- apply linear damping (the greater the "damping" variable the quicker the player slows down)
  local d = 1 - dt*damping
  if d < 0 then
    d = 0
  elseif d > 1 then
    d = 1
  end
  -- decrease the player's velocity
  vx, vy = vx*d, vy*d
If you want your player to accelerate gradually, this is another approach:

Code: Select all

-- acceleration
  xaccel = 0
  if move_left then
    xaccel = -10
  elseif move_right then
    xaccel = 10
  end
  -- accelerate
  vx = vx + xaccel*dt

  -- limit the velocity (so we don't accelerate to infinity)
  if vx > 100 then
    vx = 100
  elseif vx < -100 then
    vx = -100
  end
Once you know the player's velocity this is how you would update his position:

Code: Select all

playerx = playerx + vx*dt
playery = playery + vy*dt

Re: Help with my game character's movement

Posted: Fri Nov 21, 2014 2:44 pm
by Brainy
Thanks for the help I finally got the controls how I want them!