Making a character walk with physics

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
ColourTheory
Prole
Posts: 11
Joined: Tue Jun 05, 2012 8:27 pm

Making a character walk with physics

Post 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?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Making a character walk with physics

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
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

Post by ColourTheory »

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

Re: Making a character walk with physics

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Making a character walk with physics

Post 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.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Making a character walk with physics

Post 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.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Making a character walk with physics

Post 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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Making a character walk with physics

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Making a character walk with physics

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests