Page 1 of 2
Constant speed in the X axis of a body
Posted: Sat Nov 14, 2015 3:57 am
by Z_ReC
Hello everyone!
Please help request!
As the title says, i need a object having a constant speed in the X axis!
I leave an attachment example.
Constant speed cube would have to be 480, but no.
I hope you help me!
Re: Constant speed in the X axis of a body
Posted: Sat Nov 14, 2015 11:57 am
by Doctory
in love.physics, bodies have velocity which is decreased when they hit another body, this is why the velocity isnt constant
Re: Constant speed in the X axis of a body
Posted: Sat Nov 14, 2015 7:37 pm
by ivan
Yep, Doctory is right.
You can overwrite the linear velocity manually each step, but then the collisions would look weird.
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 1:36 am
by Beelz
Not getting into collisions... This will move you along at a constant rate:
Code: Select all
love.update(dt)
object.x = object.x + distancePerSecond * dt
object.y = 500
end
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 5:32 am
by Z_ReC
Beelz wrote:Not getting into collisions... This will move you along at a constant rate:
Code: Select all
love.update(dt)
object.x = object.x + distancePerSecond * dt
object.y = 500
end
try your code, this is the result.
it can be improved? Thanks!
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 6:35 am
by ivan
If you don't want your body to respond to collisions but move at a constant rate you can make it a "kinematic" body and set its velocity.
Kinematic bodies push other objects but do not respond to collisions themselves.
it can be improved? Thanks!
Code: Select all
love.update(dt)
local lvx, lvy = object:getLinearVelocity()
object:setLinearVelocity(10, lvy)
end
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 8:05 am
by Z_ReC
ivan wrote:If you don't want your body to respond to collisions but move at a constant rate you can make it a "kinematic" body and set its velocity.
Kinematic bodies push other objects but do not respond to collisions themselves.
it can be improved? Thanks!
Code: Select all
love.update(dt)
local lvx, lvy = object:getLinearVelocity()
object:setLinearVelocity(10, lvy)
end
Thanks, but I want to respect collisions!
With "kinematic" this happens:
In the left top corner see that the speed is constant!
but does not respect the collision!
I need collision respect, at the same time, keep constant speed.
-----------------------------------
This respects the collision, but no speed!
also thanks.
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 9:37 am
by Doctory
switch from the love.physics, it is impossible to get a constant velocity in love.physics without screwing something up
Re: Constant speed in the X axis of a body
Posted: Sun Nov 15, 2015 12:35 pm
by s-ol
Doctory wrote:switch from the love.physics, it is impossible to get a constant velocity in love.physics without screwing something up
Not really, he just needs to do a few things every frame:
- get current velocity vector (:getLinearVelocity())
- normalize and scale it by the target velocity
Now you can either just set it directly (setLinearVelocity()), which might look weird, or instead apply a force:
- subtract the actual velocity from the target velocity vector
- apply a force proportional to that difference
Re: Constant speed in the X axis of a body
Posted: Mon Nov 16, 2015 6:51 am
by ivan
Z_ReC wrote:This respects the collision, but no speed!
1.you set the linear velocity
2.you update the simulation
3.the linear velocity changes due to collisions with the floor
So you're going in the update 'step' with the desired velocity
and collisions happen which slow down the body.
There is no simple way to work around this
if you want to have 'realistic'-looking collisions.