## Making a character walk with physics

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
ColourTheory
Prole
Posts: 11
Joined: Tue Jun 05, 2012 8:27 pm

### Making a character walk with physics

What method is the best to make a character, and have a camera?

I tried to use applyForce to the character body, but I need a way to make it come to a stop immediately after he stops hitting the arrow keys.

I also need a way to stop it from gaining speed as you hold it.

I tried using setPosition but then the physics are like canceled out on that block.

What should I do?
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: Making a character walk with physics

It depends, do you really need realistic physics? If the answer is "Yes, I absolutely need realistic physics" then you can use Body:setLinearVelocity; if you're just making a platformer, Box2d might be too much (too heavy, too complicated) and coding simple platformer physics isn't too hard
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics
ColourTheory
Prole
Posts: 11
Joined: Tue Jun 05, 2012 8:27 pm

### Re: Making a character walk with physics

Yes, I kind of do because the player can dig, and would have to fall and stuff to get deeper.
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: Making a character walk with physics

Well, that still might not be too hard to code; furthermore, digging requires destroying and rebuilding shapes each frame during which you dig (if you dig continuously), it may actually be harder than coding your simplified physics system that only has the features you need
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: Making a character walk with physics

I would like to mention here an interesting experiment I've been working on in the past, about simulating character motion and jump using a custom made time-integration physics engine. Find the source code on github, and the forum thread here.
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

### Re: Making a character walk with physics

I tried to use applyForce to the character body, but I need a way to make it come to a stop immediately after he stops hitting the arrow keys.
If your character needs to stop moving immediately, you're probably not using realistic physics which means you probably don't need love.physics for it. I think you'd indeed be better off programming your own simple physics system.
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Making a character walk with physics

If you want, you can just use getX() and setX().

Code: Select all

local x = body:getX()
if love.keyboard.isDown('left') then
body:setX(x - 400 * dt)
elseif love.keyboard.isDown('right') then
body:setX(x + 400 * dt)
end

As for everyone else in the thread who decided not to directly answer the question; I've been in this situation before, where I needed to use love.physics for accurate physics that could not be achieved through a library, but I needed player movement that did not feel like I was on ice.

You could also use the combination of :applyForce() and :setLinearDamping(), where higher linear damping values will make the player stop quicker after the force is applied.
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: Making a character walk with physics

Fisrt, he said he already tried setPosition; second, I addressed the question directly telling him that he can use setLinearVelocity on the body.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Making a character walk with physics

Admittedly, I missed the part about him already trying position setting, but linear damping and applyForce is a valid solution. With the right numbers, it provides much smoother movement than setting linear velocity, and is much easier to implement.

When I referred to people who did not answer the question, that was not directed at you. Sorry for any confusion.

### Who is online

Users browsing this forum: Bing [Bot] and 10 guests