Facepunch / garrysmod-issues

Garry's Mod issue tracker
147 stars 56 forks source link

Think hook physics jitter in multiplayer #3199

Open thegrb93 opened 7 years ago

thegrb93 commented 7 years ago

For some reason, things like ApplyForceCenter are not consistent in the Think hook. I've made a plot of d/dt-CurTime vs SysTime and also a video showing this periodic jitter.

Pretty much all E2 creations are affected by this. If we could get it consistent then things would be much better.

The spikes here seem to be periodic every 0.5 sec. JitterPlot

The physics jitters seem to be periodic every 1sec. (Yes I'm certain it's not the dynamics of it oscillating. It jitters then stabilizes then jitters then stabilizes etc...) https://www.youtube.com/watch?v=W0OYG6K0jjs

thegrb93 commented 7 years ago

Here's another simple test. The entity should hover without moving at all, but it slowly falls due to this.

local e = Entity(301):GetPhysicsObject()
hook.Add("Think","T",function()
    e:ApplyForceCenter(Vector(0, 0, e:GetMass() * 600 * engine.TickInterval()))
end)

Plot

thegrb93 commented 7 years ago

^ I know that the downward descent can't be prevented due to physics iterations in between Thinks, but the net velocity should remain stable at 0 I would think. I plotted the velocity of the prop at each think.

thegrb93 commented 7 years ago

Okay, I was able to make sense of that plot.

The ramp indicates that ApplyForceCenter is putting slightly too much force on the prop. Seems to be trying to compensate for the spike. The spike indicates a frame where no force at all is applied to the prop.

The spike is exactly gravity*engine.TickInterval()

This is happens every 99 frames.

danielga commented 7 years ago

Physics simulation is not synchronized to the Think hook.

thegrb93 commented 7 years ago

Yeah I know, but is there a way to make it more uniform? If so, then I think that should be the default setting.

thegrb93 commented 7 years ago

I just found out this only happens in multiplayer. In singleplayer it's fine.

danielga commented 7 years ago

Use the Physics* hooks, this requires you to use scripted entities though.

thegrb93 commented 7 years ago

Addons such as E2 and Starfall which use Think to simulate physics on prop_physics can't do that.

G-Force007 commented 7 years ago

https://github.com/Facepunch/garrysmod-issues/issues/2502

Kefta commented 7 years ago

@G-Force007 This isn't related to CurTime imprecision. The jitter is an interval.

ZehMatt commented 7 years ago

Do not use Think for this, use PhysicsSimulate instead and use its specified delta time.

Also keep in mind theres interpolation involved aswell, since theres no clientside physics object and I'm guessing no clientside logic to move the object you can only snap to the last server value.

thegrb93 commented 7 years ago

The anomolies were measured in serverside.