Help with my game character's movement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Brainy
Prole
Posts: 2
Joined: Fri Nov 21, 2014 1:40 am

Help with my game character's movement

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help with my game character's movement

Post 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
Brainy
Prole
Posts: 2
Joined: Fri Nov 21, 2014 1:40 am

Re: Help with my game character's movement

Post by Brainy »

Thanks for the help I finally got the controls how I want them!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 8 guests