Page 1 of 1
[Solved] Speed increasing - love.physics
Posted: Fri May 08, 2015 12:58 pm
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!
With the .love
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 5:46 am
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?
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 8:59 am
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.
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 9:40 am
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.
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 10:30 am
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?
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 11:08 am
by ivan
Sure, there is: body:setFixedRotation(true)
Re: Speed increasing - love.physics
Posted: Sat May 09, 2015 11:44 am
by NowakFul
Everything works! Thank you again,see you soon!
Edit : Solved