[Solved] Speed increasing - love.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
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

[Solved] Speed increasing - love.physics

Post by NowakFul »

Hello,
My problem is that I use love.physics with body etc.. for my character. But when I press the key to move the character...

Code: Select all

self.collision.body:applyForce(-400, 0)
When I hold the key, I would like to have the same speed but the player speed continues to increases more and more..

Code: Select all

if love.keyboard.isDown("a") then
walk_left:update(dt)   
self.collision.body:applyForce(-400, 0)
self.direction = "left"
self.move = "true"

elseif love.keyboard.isDown("d") then
walk_right:update(dt)   
self.collision.body:applyForce(400, 0)
self.direction = "right"
self.move = "true"
else
self.move = "false"
end
Thank you in advance! :awesome:

With the .love
Attachments
game.love
(644.99 KiB) Downloaded 95 times
Last edited by NowakFul on Sat May 09, 2015 11:44 am, edited 1 time in total.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Speed increasing - love.physics

Post by bobbyjones »

That is physics lol. You have to play with density and your force to get a nice feel. Also damping. But depending on your game love.physics can be too much. It would be better to use a solution you write up yourself for learning and all that jazz. And p.s force isn't speed. Force is the equivalent of pushing an object. If you push with the same force repeatedly then the object will speed up. But if you stop it will slow down do to gravity if you have it. And friction if you have it. Possibly you could remove gravity from your world and use set velocity for everything. But that's more complicated. What kind of game is it?
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

Re: Speed increasing - love.physics

Post by NowakFul »

bobbyjones wrote:That is physics lol. You have to play with density and your force to get a nice feel. Also damping. But depending on your game love.physics can be too much. It would be better to use a solution you write up yourself for learning and all that jazz. And p.s force isn't speed. Force is the equivalent of pushing an object. If you push with the same force repeatedly then the object will speed up. But if you stop it will slow down do to gravity if you have it. And friction if you have it. Possibly you could remove gravity from your world and use set velocity for everything. But that's more complicated. What kind of game is it?
Thank you for the answer, a problem also is that I don't know a lot of things in physics. I try to play with velocity, density but I got nothing.. I'll maybe try to remove gravity, but it seems strange to me? For the kind of game, I am not sure how to say it... A stupid adventure game? Like Jazzpunk, this kind of game. I would need the collision for not penetrate walls, furniture and other.. Thank you again for the answer, I hope to find a solution.

edit : PS: Sorry for my bad English, I am French.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Speed increasing - love.physics

Post by ivan »

bobbyjones wrote:That is physics lol. You have to play with density and your force to get a nice feel. Also damping.
I agree except for the "density" part.
Density affects collisions response with the other bodies in the simulation so I wouldn't change it for this purpose.

If you want your object to always move at a constant speed you can use
"body:setLinearVelocity"
but then your body won't respond to collisions correctly.

A better approach could be to manually "clamp" the velocity if it's above a certain threshold:

Code: Select all

vx, vy = body:getVelocity()
v = math.sqrt(vx^2 + vy^2)
if v > maxVel then
  local ratio = 1/v*maxvel
  vx, vy = vx*ratio, vy*ratio
  body:setVelocity(vx, vy)
end
But it would look bad if something hits your body really hard:
the body won't move as fast as it should (since you're clamping the velocity).
Generally, if you want 'realistic' physics you want to stay away from setLinearVelocity.

Damping would probably work well in this case.
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

Re: Speed increasing - love.physics

Post by NowakFul »

ivan wrote:Damping would probably work well in this case.
Thank you, that's what I used and it works perfectly! Thank you again to both of you! I just have one last problem, the box of the player rotate, I know this is physics but there is a way to stop the rotation?

Image
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Speed increasing - love.physics

Post by ivan »

Sure, there is: body:setFixedRotation(true)
NowakFul
Prole
Posts: 24
Joined: Mon Jul 28, 2014 7:13 pm

Re: Speed increasing - love.physics

Post by NowakFul »

Everything works! Thank you again,see you soon! :awesome:

Edit : Solved
Post Reply

Who is online

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