Physics broken before the first world:update

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.
Post Reply
tsugiru
Prole
Posts: 2
Joined: Wed Oct 12, 2022 6:38 pm

Physics broken before the first world:update

Post 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.
User avatar
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Physics broken before the first world:update

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Physics broken before the first world:update

Post 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.
obey
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Physics broken before the first world:update

Post 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.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Physics broken before the first world:update

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Physics broken before the first world:update

Post 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:
obey
tsugiru
Prole
Posts: 2
Joined: Wed Oct 12, 2022 6:38 pm

Re: Physics broken before the first world:update

Post 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?
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Physics broken before the first world:update

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest