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
Before you make a thread asking for help, read this.
Z_ReC
Prole
Posts: 7
Joined: Sat Nov 14, 2015 2:48 am

Constant speed in the X axis of a body

Post by Z_ReC »

Hello everyone!

Please help request! :3
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! :awesome:
Attachments
cube.love
(635 Bytes) Downloaded 182 times
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Constant speed in the X axis of a body

Post by Doctory »

in love.physics, bodies have velocity which is decreased when they hit another body, this is why the velocity isnt constant
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Constant speed in the X axis of a body

Post by ivan »

Yep, Doctory is right.
You can overwrite the linear velocity manually each step, but then the collisions would look weird.
User avatar
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

Post 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

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

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

Image

it can be improved? Thanks! :crazy:
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Constant speed in the X axis of a body

Post 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! :crazy:

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

Post 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! :crazy:

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:

Image

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! :x
Image

also thanks.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Constant speed in the X axis of a body

Post by Doctory »

switch from the love.physics, it is impossible to get a constant velocity in love.physics without screwing something up
User avatar
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

Post 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

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
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Constant speed in the X axis of a body

Post by ivan »

Z_ReC wrote:This respects the collision, but no speed! :x
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.
Post Reply

Who is online

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