Page 1 of 1

Physics broken before the first world:update

Posted: Thu Oct 13, 2022 9:33 pm
by tsugiru
Hello,

I'm learning love2d and I've been tinkering with some things. There is one thing that confuses me, namely, it seems like physics properties like velocity (but also forces and impulses) have different effects when applied to entities before after the first `world:update`. Is there a reason for this? This makes it hard to set proper "initial state" for the game, as one has to wait until the world executes a single update, and then apply physics properties to objects.

Consider this example:

Code: Select all

-- main.lua
local world = love.physics.newWorld(0, 0)

local entity = {}
entity.body = love.physics.newBody(world, 200, 200, 'dynamic')
entity.body:setMass(32)
-- entity.body:setLinearVelocity(500, 0)
entity.shape = love.physics.newCircleShape(0, 0, 10)
entity.fixture = love.physics.newFixture(entity.body, entity.shape)

love.draw = function()
  print(entity.body:getLinearVelocity())
  local ball_x, ball_y = entity.body:getWorldCenter() 
  love.graphics.circle('fill', ball_x, ball_y, entity.shape:getRadius())
end

local applied = false

love.update = function(dt)
  world:update(dt)
--  if not applied then 
--    entity.body:setLinearVelocity(500, 0)
--    applied = true
--  end
end
If I run this main.lua locally, applying the velocity on the entity in the global scope prints 188.86 (rounded) in the love.draw function, however, applying it in the love.update function after world:update prints the correct velocity, 500.

Re: Physics broken before the first world:update

Posted: Fri Oct 14, 2022 6:28 am
by togFox
You have an interesting way of invoking love.draw and love.update.

Most ppl just do

Function love.draw()
...
End

Function love.update(dt)
...
End

I'm not saying you're doing it wrong - just different, and I don't know enough to say that is causing your physics update problem.

Re: Physics broken before the first world:update

Posted: Fri Oct 14, 2022 6:51 am
by BrotSagtMist
Neither of these methods give 500 or 188 for me, its booth 499.99996.
You better not rely on the physics stuff being predictable.
Mainly cause its not really setting the speed of the object but applying a force over a time given by the world update which itself takes a varying number.
You best bet is calling world:update(0) after you applied values.
You can also see the varying effect if you set a bigger float than 0 there.

I write like that too @togfox.

Re: Physics broken before the first world:update

Posted: Fri Oct 14, 2022 7:15 am
by ReFreezed
Like BrotSagtMist, uncommenting any of the two places where setLinearVelocity is called produces the same value to be printed for me (499.99996948242).
BrotSagtMist wrote: Fri Oct 14, 2022 6:51 am You better not rely on the physics stuff being predictable.
But the physics are predictable as long as you provide the same input (like a fixed time step).
togFox wrote: Fri Oct 14, 2022 6:28 am You have an interesting way of invoking love.draw and love.update.
`function love.draw()` is just syntactic sugar for `love.draw=function()`. The latter is arguably showing more clearly what's happening. (Also, it's assignments - not invocations.)

Re: Physics broken before the first world:update

Posted: Fri Oct 14, 2022 7:16 am
by zorg
togFox wrote: Fri Oct 14, 2022 6:28 am I'm not saying you're doing it wrong - just different, and I don't know enough to say that is causing your physics update problem.
function blah(params) end is literally just syntax sugar for blah = function(params) end; it's definitely not the issue. :3

Re: Physics broken before the first world:update

Posted: Fri Oct 14, 2022 8:04 am
by BrotSagtMist
ReFreezed wrote: Fri Oct 14, 2022 7:15 am But the physics are predictable as long as you provide the same input (like a fixed time step).
Of course it is, but i would not rely on it :cool:

Re: Physics broken before the first world:update

Posted: Sun Oct 16, 2022 10:43 am
by tsugiru
Thanks for answering everyone!
BrotSagtMist wrote: Fri Oct 14, 2022 6:51 am You better not rely on the physics stuff being predictable.
What do you mean? If I'm applying certain velocities to game objects, I'd expect them to be deterministic, how else would I make the game feel/play a certain way?
BrotSagtMist wrote: Fri Oct 14, 2022 6:51 am You best bet is calling world:update(0) after you applied values.
This brings the velocity up very slightly for the case where we apply the velocity in the global scope, but it's still nowhere close to 500.
BrotSagtMist wrote: Fri Oct 14, 2022 6:51 am Neither of these methods give 500 or 188 for me, its booth 499.99996.
ReFreezed wrote: Fri Oct 14, 2022 7:15 am Like BrotSagtMist, uncommenting any of the two places where setLinearVelocity is called produces the same value to be printed for me (499.99996948242).
What platforms are you on? I'm on MacOS (arm chip). Maybe that's what causing the difference?

Re: Physics broken before the first world:update

Posted: Sun Oct 16, 2022 2:20 pm
by ReFreezed
tsugiru wrote: Sun Oct 16, 2022 10:43 am What platforms are you on? I'm on MacOS (arm chip). Maybe that's what causing the difference?
I'm on Windows with x86. I suppose that could be the issue. Try different LÖVE versions to see if the same thing happens. Maybe report the issue to the issue tracker.