Page 1 of 1

Making a character walk with physics

Posted: Tue May 21, 2013 7:29 pm
by ColourTheory
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?

Re: Making a character walk with physics

Posted: Tue May 21, 2013 7:46 pm
by Nixola
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

Re: Making a character walk with physics

Posted: Tue May 21, 2013 7:48 pm
by ColourTheory
Yes, I kind of do because the player can dig, and would have to fall and stuff to get deeper.

Re: Making a character walk with physics

Posted: Tue May 21, 2013 7:51 pm
by Nixola
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

Re: Making a character walk with physics

Posted: Tue May 21, 2013 8:33 pm
by Roland_Yonaba
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.

Re: Making a character walk with physics

Posted: Wed May 22, 2013 10:06 am
by Plu
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.

Re: Making a character walk with physics

Posted: Wed May 22, 2013 10:16 am
by Kingdaro
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.

Re: Making a character walk with physics

Posted: Wed May 22, 2013 10:22 am
by Nixola
Fisrt, he said he already tried setPosition; second, I addressed the question directly telling him that he can use setLinearVelocity on the body.

Re: Making a character walk with physics

Posted: Wed May 22, 2013 1:51 pm
by Kingdaro
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.