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 » Sat Nov 14, 2015 3:57 am

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 70 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 » Sat Nov 14, 2015 11:57 am

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

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

Re: Constant speed in the X axis of a body

Post by ivan » Sat Nov 14, 2015 7:37 pm

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 » Sun Nov 15, 2015 1:36 am

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 » Sun Nov 15, 2015 5:32 am

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: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Constant speed in the X axis of a body

Post by ivan » Sun Nov 15, 2015 6:35 am

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 » Sun Nov 15, 2015 8:05 am

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 » Sun Nov 15, 2015 9:37 am

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

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 » Sun Nov 15, 2015 12:35 pm

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: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Constant speed in the X axis of a body

Post by ivan » Mon Nov 16, 2015 6:51 am

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: No registered users and 39 guests