## Constant speed in the X axis of a body

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Z_ReC
Prole
Posts: 7
Joined: Sat Nov 14, 2015 2:48 am

### Constant speed in the X axis of a body

Hello everyone!

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!
Attachments
cube.love

Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Constant speed in the X axis of a body

in love.physics, bodies have velocity which is decreased when they hit another body, this is why the velocity isnt constant
"This is what videogames should be like; they should have meaning and triple machine guns"
- Jan Willem Nijman, 2013﻿
github

ivan
Party member
Posts: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Constant speed in the X axis of a body

Yep, Doctory is right.
You can overwrite the linear velocity manually each step, but then the collisions would look weird.

Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

### Re: Constant speed in the X axis of a body

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


Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end

GitHub -- Website

Z_ReC
Prole
Posts: 7
Joined: Sat Nov 14, 2015 2:48 am

### Re: Constant speed in the X axis of a body

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!

ivan
Party member
Posts: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Constant speed in the X axis of a body

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

Z_ReC
Prole
Posts: 7
Joined: Sat Nov 14, 2015 2:48 am

### Re: Constant speed in the X axis of a body

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.

Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Constant speed in the X axis of a body

switch from the love.physics, it is impossible to get a constant velocity in love.physics without screwing something up
"This is what videogames should be like; they should have meaning and triple machine guns"
- Jan Willem Nijman, 2013﻿
github

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

### Re: Constant speed in the X axis of a body

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end

ivan
Party member
Posts: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Constant speed in the X axis of a body

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.

### Who is online

Users browsing this forum: No registered users and 39 guests