Skip to content

Commit 806390f

Browse files
committed
Attempts at reducing input lag
1 parent 9c7da21 commit 806390f

10 files changed

+227
-2
lines changed

engine/source/core/torqueConfig.h

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
// Define me to allow switching physics systems in script
8282
//#define MB_PHYSICS_SWITCHABLE
8383

84+
/// Define me to disable input lag
85+
//#define MB_DISABLE_INPUT_LAG
86+
87+
// Define me to make client physics run every frame
88+
//#define MB_CLIENT_PHYSICS_EVERY_FRAME
89+
8490
// Define me to use MBO physics (does nothing if MB_PHYSICS_SWITCHABLE is defined)
8591
//#define MBO_PHYSICS
8692

engine/source/game/gameBase.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ void GameBase::processTick(const Move* move)
282282
#endif
283283
}
284284

285+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
286+
void GameBase::processPhysicsTick(const Move *move, F32 dt)
287+
{
288+
289+
}
290+
#endif
291+
285292
void GameBase::interpolateTick(F32 delta)
286293
{
287294
}

engine/source/game/gameBase.h

+4
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ class GameBase : public SceneObject, public ProcessObject
295295
/// @param move Move event corresponding to this tick, or NULL.
296296
virtual void processTick(const Move* move);
297297

298+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
299+
virtual void processPhysicsTick(const Move* move, F32 dt);
300+
#endif
301+
298302
/// Interpolates between tick events. This takes place on the CLIENT ONLY.
299303
///
300304
/// @param delta Time since last call to interpolate

engine/source/game/gameConnection.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ GameConnection::GameConnection()
5050
mLastClientMove = 0;
5151
mFirstMoveIndex = 0;
5252
mLastSentMove = 0;
53+
#ifdef MB_DISABLE_INPUT_LAG
54+
mTargetMoveListSize = 0;
55+
mMaxMoveListSize = 1;
56+
#else
5357
mTargetMoveListSize = 3;
5458
mMaxMoveListSize = 5;
59+
#endif
5560

5661
mDataBlockModifiedKey = 0;
5762
mMaxDataBlockModifiedKey = 0;

engine/source/game/gameConnection.h

+4
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ class GameConnection : public NetConnection
290290

291291
void pushMove(const Move& mv);
292292
bool getNextMove(Move& curMove);
293+
294+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
295+
bool getNextMove2(Move& curMove);
296+
#endif
293297
bool isBacklogged();
294298
virtual U32 getMoveList(Move**, U32* numMoves);
295299
MoveList& getMoves() { return mMoveList; }

engine/source/game/main.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
#include "discord/DiscordGame.h"
7171
//#include "../discord/discordGameSDK.h"
7272

73+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
74+
#include "game/gameConnection.h"
75+
#endif
76+
7377
#ifndef BUILD_TOOLS
7478
DemoGame GameObject;
7579
DemoNetInterface GameNetInterface;
@@ -757,6 +761,11 @@ void DemoGame::processConsoleEvent(ConsoleEvent* event)
757761
Sim::postCurrentEvent(Sim::getRootGroup(), new SimConsoleEvent(2, const_cast<const char**>(argv), false));
758762
}
759763

764+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
765+
Move gFirstMove;
766+
Move gNextMove;
767+
#endif
768+
760769
/// Process a time event and update all sub-processes
761770
void DemoGame::processTimeEvent(TimeEvent* event)
762771
{
@@ -779,6 +788,18 @@ void DemoGame::processTimeEvent(TimeEvent* event)
779788
bool tickPass;
780789
if (!gGamePaused)
781790
{
791+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
792+
gFirstMove = gNextMove = NullMove;
793+
if (GameConnection::getConnectionToServer() != NULL)
794+
{
795+
GameConnection* gc = GameConnection::getConnectionToServer();
796+
if (gc)
797+
{
798+
gc->getNextMove2(gFirstMove);
799+
gc->getNextMove2(gNextMove);
800+
}
801+
}
802+
#endif
782803
PROFILE_START(ServerProcess);
783804
tickPass = serverProcess(timeDelta);
784805
PROFILE_END();

engine/source/game/marble/marble.cpp

+110-2
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,10 @@ void Marble::processTick(const Move* move)
20302030
{
20312031
Parent::processTick(move);
20322032

2033+
#ifndef MB_CLIENT_PHYSICS_EVERY_FRAME
20332034
clearMarbleAxis();
2035+
#endif
2036+
20342037
if ((mMode & TimerMode) != 0)
20352038
{
20362039
U32 bonusTime = mMarbleBonusTime;
@@ -2075,17 +2078,20 @@ void Marble::processTick(const Move* move)
20752078
if (move)
20762079
{
20772080
dMemcpy(&delta.move, move, sizeof(delta.move));
2078-
newMove = move;
2081+
newMove = move;
20792082
}
20802083
else
20812084
newMove = &delta.move;
20822085
}
20832086
else
20842087
newMove = &NullMove;
20852088

2089+
#ifndef MB_CLIENT_PHYSICS_EVERY_FRAME
20862090
processMoveTriggers(newMove);
2091+
#endif
20872092
processCameraMove(newMove);
20882093

2094+
#ifndef MB_CLIENT_PHYSICS_EVERY_FRAME
20892095
Point3F startPos(mPosition.x, mPosition.y, mPosition.z);
20902096

20912097
advancePhysics(newMove, TickMs);
@@ -2124,7 +2130,7 @@ void Marble::processTick(const Move* move)
21242130
{
21252131
if (Marble::smEndPadId && Marble::smEndPadId != -1)
21262132
Marble::smEndPad = dynamic_cast<StaticShape*>(Sim::findObject(Marble::smEndPadId));
2127-
2133+
21282134
if (Marble::smEndPad.isNull())
21292135
Marble::smEndPadId = 0;
21302136
}
@@ -2153,10 +2159,112 @@ void Marble::processTick(const Move* move)
21532159
mSinglePrecision.mVelocity = mVelocity;
21542160
mSinglePrecision.mOmega = mOmega;
21552161

2162+
mPosition = mSinglePrecision.mPosition;
2163+
mVelocity = mSinglePrecision.mVelocity;
2164+
mOmega = mSinglePrecision.mOmega;
2165+
#else
2166+
if (isServerObject())
2167+
{
2168+
processPhysicsTick(move, TickSec);
2169+
}
2170+
#endif
2171+
}
2172+
2173+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
2174+
void Marble::processPhysicsTick(const Move *move, F32 dt)
2175+
{
2176+
clearMarbleAxis();
2177+
2178+
const Move* newMove;
2179+
if (mControllable)
2180+
{
2181+
if (move)
2182+
{
2183+
dMemcpy(&delta.move, move, sizeof(delta.move));
2184+
newMove = move;
2185+
}
2186+
else
2187+
newMove = &delta.move;
2188+
}
2189+
else
2190+
newMove = &NullMove;
2191+
2192+
processMoveTriggers(newMove);
2193+
// processCameraMove(newMove);
2194+
2195+
Point3F startPos(mPosition.x, mPosition.y, mPosition.z);
2196+
2197+
//advancePhysics(newMove, TickMs);
2198+
advancePhysics(newMove, (U32)(dt * 1000.0f));
2199+
2200+
Point3F endPos(mPosition.x, mPosition.y, mPosition.z);
2201+
2202+
processItemsAndTriggers(startPos, endPos);
2203+
updatePowerups();
2204+
2205+
if (mPadPtr)
2206+
{
2207+
bool oldOnPad = mOnPad;
2208+
updatePadState();
2209+
#ifdef MB_ULTRA_PREVIEWS
2210+
if (oldOnPad != mOnPad && !(isGhost() || gSPMode))
2211+
#else
2212+
if (oldOnPad != mOnPad && !isGhost())
2213+
#endif
2214+
{
2215+
const char* funcName = "onLeavePad";
2216+
if (!oldOnPad)
2217+
funcName = "onEnterPad";
2218+
Con::executef(mDataBlock, 2, funcName, scriptThis());
2219+
}
2220+
}
2221+
2222+
#ifdef MB_ULTRA_PREVIEWS
2223+
if (isGhost() || gSPMode)
2224+
#else
2225+
if (isGhost())
2226+
#endif
2227+
{
2228+
if (getControllingClient())
2229+
{
2230+
if (Marble::smEndPad.isNull() || Marble::smEndPad->getId() != Marble::smEndPadId)
2231+
{
2232+
if (Marble::smEndPadId && Marble::smEndPadId != -1)
2233+
Marble::smEndPad = dynamic_cast<StaticShape*>(Sim::findObject(Marble::smEndPadId));
2234+
2235+
if (Marble::smEndPad.isNull())
2236+
Marble::smEndPadId = 0;
2237+
}
2238+
}
2239+
}
2240+
2241+
if (mOmega.len() < 0.000001)
2242+
mOmega.set(0, 0, 0);
2243+
2244+
#ifdef MBG_PHYSICS
2245+
#define MB_RESPAWN_TRIGGER_ID 0
2246+
#else
2247+
#define MB_RESPAWN_TRIGGER_ID 2
2248+
#endif
2249+
2250+
#ifdef MB_ULTRA_PREVIEWS
2251+
if (!(isGhost() || gSPMode) && mOOB && newMove->trigger[MB_RESPAWN_TRIGGER_ID])
2252+
#else
2253+
if (!isGhost() && mOOB && newMove->trigger[MB_RESPAWN_TRIGGER_ID])
2254+
#endif
2255+
Con::executef(this, 1, "onOOBClick");
2256+
2257+
notifyCollision();
2258+
2259+
mSinglePrecision.mPosition = mPosition;
2260+
mSinglePrecision.mVelocity = mVelocity;
2261+
mSinglePrecision.mOmega = mOmega;
2262+
21562263
mPosition = mSinglePrecision.mPosition;
21572264
mVelocity = mSinglePrecision.mVelocity;
21582265
mOmega = mSinglePrecision.mOmega;
21592266
}
2267+
#endif
21602268

21612269
//----------------------------------------------------------------------------
21622270

engine/source/game/marble/marble.h

+4
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ class Marble : public ShapeBase
314314
void setPowerUpId(U32 id, bool reset);
315315
virtual void processTick(const Move* move);
316316

317+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
318+
virtual void processPhysicsTick(const Move* move, F32 dt);
319+
#endif
320+
317321
// Marble Physics
318322
Point3D getVelocityD() const;
319323
void setVelocityD(const Point3D& vel);

engine/source/game/moveManager.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,54 @@ bool GameConnection::getNextMove(Move& curMove)
397397
return true;
398398
}
399399

400+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
401+
bool GameConnection::getNextMove2(Move& curMove)
402+
{
403+
if (mMoveList.size() > MaxMoveQueueSize)
404+
return false;
405+
406+
F32 pitchAdd = MoveManager::mPitchUpSpeed - MoveManager::mPitchDownSpeed;
407+
F32 yawAdd = MoveManager::mYawLeftSpeed - MoveManager::mYawRightSpeed;
408+
F32 rollAdd = MoveManager::mRollRightSpeed - MoveManager::mRollLeftSpeed;
409+
410+
curMove.pitch = MoveManager::mPitch + pitchAdd;
411+
curMove.yaw = MoveManager::mYaw + yawAdd;
412+
curMove.roll = MoveManager::mRoll + rollAdd;
413+
414+
//MoveManager::mPitch = 0;
415+
//MoveManager::mYaw = 0;
416+
//MoveManager::mRoll = 0;
417+
418+
419+
curMove.x = MoveManager::mRightAction - MoveManager::mLeftAction + MoveManager::mXAxis_L;
420+
curMove.y = MoveManager::mForwardAction - MoveManager::mBackwardAction + MoveManager::mYAxis_L;
421+
curMove.z = MoveManager::mUpAction - MoveManager::mDownAction;
422+
423+
curMove.freeLook = MoveManager::mFreeLook;
424+
curMove.deviceIsKeyboardMouse = MoveManager::mDeviceIsKeyboardMouse;
425+
curMove.autoCenterCamera = MoveManager::mAutoCenterCamera;
426+
427+
for (U32 i = 0; i < MaxTriggerKeys; i++)
428+
{
429+
curMove.trigger[i] = false;
430+
if (MoveManager::mTriggerCount[i] & 1)
431+
curMove.trigger[i] = true;
432+
else if (!(MoveManager::mPrevTriggerCount[i] & 1) && MoveManager::mPrevTriggerCount[i] != MoveManager::mTriggerCount[i])
433+
curMove.trigger[i] = true;
434+
//MoveManager::mPrevTriggerCount[i] = MoveManager::mTriggerCount[i];
435+
}
436+
437+
curMove.horizontalDeadZone = MoveManager::mHorizontalDeadZone;
438+
curMove.verticalDeadZone = MoveManager::mVerticalDeadZone;
439+
curMove.cameraAccelSpeed = MoveManager::mCameraAccelSpeed;
440+
curMove.cameraSensitivityHorizontal = MoveManager::mCameraSensitivityHorizontal;
441+
curMove.cameraSensitivityVertical = MoveManager::mCameraSensitivityVertical;
442+
443+
curMove.clamp(); // clamp for net traffic
444+
return true;
445+
}
446+
#endif
447+
400448
void GameConnection::pushMove(const Move& mv)
401449
{
402450
U32 id = mFirstMoveIndex + mMoveList.size();

engine/source/sim/processList.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ bool ProcessList::advanceServerTime(SimTime timeDelta)
254254

255255
//----------------------------------------------------------------------------
256256

257+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
258+
extern Move gFirstMove;
259+
extern Move gNextMove;
260+
#endif
261+
257262
bool ProcessList::advanceClientTime(SimTime timeDelta)
258263
{
259264
PROFILE_START(AdvanceClientTime);
@@ -267,6 +272,11 @@ bool ProcessList::advanceClientTime(SimTime timeDelta)
267272

268273
if (mDirty) orderList();
269274

275+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
276+
Move firstmove = gFirstMove;
277+
Move nextmove = gNextMove;
278+
#endif
279+
270280
SimTime targetTime = mLastTime + timeDelta;
271281
SimTime targetTick = targetTime - (targetTime & 0x1F);
272282
SimTime tickCount = (targetTick - mLastTick) >> TickShift;
@@ -332,6 +342,14 @@ bool ProcessList::advanceClientTime(SimTime timeDelta)
332342
GameBase* gb = getGameBase(pobj);
333343
gb->advanceTime(dt);
334344
}
345+
346+
#ifdef MB_CLIENT_PHYSICS_EVERY_FRAME
347+
for (ProcessObject* pobj = mHead.mProcessLink.next; pobj != &mHead; pobj = pobj->mProcessLink.next)
348+
{
349+
GameBase* gb = getGameBase(pobj);
350+
gb->processPhysicsTick(&firstmove, dt);
351+
}
352+
#endif
335353
}
336354
else {
337355
mSkipAdvanceObjectsMs -= timeDelta;

0 commit comments

Comments
 (0)