Jump to content

Leadwerks Physics Simulation


theDream
 Share

Recommended Posts

I am working on a server meant for an FPS game. I own the pro version or Leadwerks but do not know C++ at all, so I just use LUA. I am trying to implement a rewind function for delayed or dropped packets. I want to set the objects back to their states 'n' ticks ago and run the physics calculations with the new input back to current tick within 1 rendered frame. I know I need to use Time:Step() which steps through time at 1/60th of a second the same rate as the physics engine. Unfortunately based on tests and the documentation this also calls Time:Delay() to try and maintain 60hz. This makes the max FPS roughly 60 even without context being synced. I figured I would be able to call this multiply times per frame if I could modify the Time:Step() function to get rid of the the Delay. Is this something I could possible modify?

Link to comment
Share on other sites

In Leadwerks, the physics are updated one step in the World::Update method if 16 milliseconds have passed since the last physics step. This works independently from the framerate. Physics objects are smoothly interpolated between the previous two steps in order to provide smooth motion, even if the framerate varies from 60 hz. There is no easy way to modify the physics step time.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thank you for a response, I appreciate it.
I understand I have to call World::Update after Time::Step or Time::Update for the physics to be calculated.

The issue I am having is that Time::Step which is what I need to use to manually 'step' through time with a fixed Delta uses Time::Delay which stops all processing trying to make it match the given framerate.
So if I use Time::Update I get about 300+ FPS.
If I change this to Time::Step and nothing else, because of the call to Time::Delay inside of Time::Step it maxes at 60ish FPS.
So If I call Time::Step twice a frame because of the delay it drops to 30ish FPS do to the 2 calls to Time::Delay.

Without this delay, logically I should be able to calculate Physics using the fixed delta given from Time::Step to calculate physics multiply times a frame.

Here is a basic example:
 

while window:Closed() == false do
   
  	local frameRate = 60
	local n = 10

    for i = 1, n do
        
        Time:Step(frameRate) --Due to the delay in this function call the FPS will drop to frameRate/n
        world:Update() 
        
    end
    
    world:Render()
    
    context:Sync(false)
    
end

 

Link to comment
Share on other sites

22 hours ago, Josh said:

In Leadwerks, the physics are updated one step in the World::Update method if 16 milliseconds have passed since the last physics step. This works independently from the framerate. Physics objects are smoothly interpolated between the previous two steps in order to provide smooth motion, even if the framerate varies from 60 hz. There is no easy way to modify the physics step time.

This is how I believe the Time class and the World class work roughly and obviously in Lua and not C++ like it actually is:

local Time = {}

--[[ 

    All other Time functions

--]]

function Time:Update()

    self.current = self.Millisecs()

    if self.pause then
 
        self.previous = self.current 

    end 

    self.delta = self.current - self.previous

    self.previous = self.current

end

function Time:Step(frameRate)

    local frameRate = frameRate or 60

    local delta = 1000/frameRate

    Time:Delay(delta)
    Time:Update()

end

--------------------------

local World = {}

--Other World Functions

function World:Update()

    self.physicsDelta = self.physicsDelta + Time.delta

    if self.physicsTime >= 1000/60 then

        Physics:Update(self.physicsDelta)
        self.physicsDelta =0

    end

end

I was wondering if Time:Step could act like this:

function Time:Step(frameRate)

     self.delta = 1000/frameRate

     --In case you switch back to using Time:Update()
     self.previous = self:Millisecs()

end

I know this would mean modifying the Time Class. Is this possible?

Link to comment
Share on other sites

1 hour ago, Josh said:

I have attached the timing code for you. Hopefully this will help.

Timing.cpp 3.53 kB · 3 downloads

Thanks, this helps. Is there a way for me to modify this class and rebuild? As I said I am not proficient in C++ at all. I would just have to remove the line 'Delay(delaytime)' from inside Time::Step and it would work how I want it to. 

Link to comment
Share on other sites

On 10/28/2023 at 10:01 PM, theDream said:

Thanks, this helps. Is there a way for me to modify this class and rebuild? As I said I am not proficient in C++ at all. I would just have to remove the line 'Delay(delaytime)' from inside Time::Step and it would work how I want it to. 

I'm pretty sure we don't have engine source code with the install. 

I'm only happy when I'm coding, I'm only coding when I'm happy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...