`

Defeating Lag With Cubic Splines

阅读更多

This article may not be reprinted commercially without the express permission of the author.
Copyright © 2000 Nicholas Van Caldwell

A common problem when writing networked virtual environments is overcoming the lag inherent to the Internet; players seem to jerk about the field of play as new data packets are incorporated. Common solutions include increasing the frequency of packets sent, reducing packet size through compression, and most importantly, dead reckoning. This article will attempt to explain a technique for eliminating the "jerk" of lag by employing the power of cubic splines in a dead-reckoning algorithm.

What is Dead Reckoning?

Before jumping straight into cubic splines, a brief description of dead reckoning is required. Programmers use this technique to reduce the effects of lag in a game by trying to guess that an object takes. Dead reckoning makes its guess based on the object's characteristics. For example, if an object has a known starting position and velocity then its path can be created using simple physics. The paths created can be applied to the object, creating the illusion of smooth motion. Cubic splines are a kind of dead reckoning that creates a smooth transition between two data points.

Possible Forms of Dead Reckoning

What the programmer wants to do is take a current position for an object and form a smooth path to where the object is supposed to be. Several techniques are available.

The most basic form of dead reckoning is the "point-to-point" method. As its name implies, this method involves only moving a player to a new point when a data-packet arrives. Given an average Internet lag of 200-300 ms this creates a noticeably "jerky" player. This method is by far the worst because unless a remote player is absolutely still, his onscreen representation is completely erroneous. The reason for the error is that data packets in a fast quake-style game arrive only 5-10 times per second, while even a slow game updates at 30 frames per second. The only way to make players move smoothly is to send one packet for every game frame. This is a terrible strain on bandwidth, thus the point-to-point updating method is nearly impossible to effectively implement in a real-time game.


NewPosition = OldPosition

The next level of precision is the "linear" method. This method involves creating a straight-line path to the next position. In terms of physics, this means that the velocity of an object is used to decide where it should next appear. This method reduces jitters caused by lag but has a tragic flaw: it assumes people will only move with a constant velocity. Thus, the generated and actual paths vary noticeably (although the game will be much improved over the "point-to-point" method). When playing a game that uses this method, players will seem to move in straight lines. Further, whenever a player starts a new linear path his velocity could change abruptly. The end result is a thoroughly unrealistic game.


NewPosition = OldPosition + Velocity*Time

A smart programmer might now ask, "why not just add acceleration to your path equations?" Such a method is possible, and will result in even smoother game play. This is called the "quadratic" method because the path created follows a quadratic function. Without detailing the math, this method also fails because even though a player's motion is represented more realistically, his final velocity is likely to be incorrect. This is because quadratic functions do not employ what physicists call "jerk", or the change in acceleration over time. This leads to the final solution: the cubic spline.


NewPosition = OldPosition + Velocity*Time + Acceleration*(time)2

Cubic splines offer one of the most realistic methods for creating a dead reckoning path. This is because they account for the starting position\velocity and the ending position\velocity. As a result, objects following a cubic spline path have no jitters, unless lag is especially severe.

Using Cubic Splines

Using cubic splines to create a path is a matter of simple algebraic equations. The input for these equations are four (x,y) coordinates. The first coordinate represents the object's starting position. Similarly, the fourth coordinate signifies the object's ending position. Usually the end position is a new (x,y) coordinate that has just arrived in a data packet. The most important coordinates are the second and third; they represent the object's velocity. For the second coordinate, calculate where the object will appear after 1 second with its current velocity. For the third coordinate, reverse the object's end velocity, and calculate where it would appear after 1 second. This is the same as traveling back in time for 1 second (assuming constant velocity). In summary:

  1. Coordinate 1 = Starting position
    Coordinate 2 = Position after 1 second using starting velocity
    = Coordinate1 + StartVelocity
    Coordinate 3 = Position after 1 second using reversed ending velocity
    = Coordinate4 – EndVelocity
    Coordinate 4 = Ending position
  2. Here are the parametric equations used to form the spline.

    x = At3 + Bt2 + Ct + D
    y = Et3 + Ft3 + Gt + H

    t is the time variable. It ranges from 0 at the initial point to 1 at the end point.

  3. Formulating the rest of the variables.

    A = x3 – 3x2 +3x1 – x0
    B = 3x2 – 6x1 + 3x0
    C = 3x1 – 3x0
    D = x0

    E = y3 – 3y2 +3y1 – y0
    F = 3y2 – 6y1 + 3y0
    G = 3y1 – 3y0
    H = y0

  4. Once the equation is created, the next step is deciding how to implement it in the game. The following method is simple and effective.
    1. Allow an object to move according physical laws (account for velocity and acceleration).
    2. When a data packet arrives begin creating a spline to the next position.
    3. Coordinates 1 and 2 of the spline can be found using the current position and velocity.

      Coord2 = xold + vold

    4. Coordinates 3 and 4 are more difficult to get. Decide on the number of steps the object will take on the spline before it reaches the final position. Let this number be time T. For coordinate 4, use the new data packet to calculate the final position after t seconds. The same information can be used to calculate the velocity at the new time.

      Coord3 = xpacket + vpacket *t + .5*apacket *t2
      Coord4 = Coord3 – (Vpacket + apacket *t)

      This method combines two forms of dead reckoning: a cubic spline, and quadratic motion. The result is more realistic.

    5. Make the object travel along the spline for T frames.
    6. At the end of the spline resume at step 1.

The result will look something like the following:

Conclusion

This article has covered some of the basics of cubic splines and provided psuedocode for use in any game using 2d Newtonian physics. The provided equations can also be easily extended into use for three-dimensional games. While creating cubic splines is easy, actually implementing them introduces several problems that must be left to the reader to explore on his own.

Happy Coding,
Nick Caldwell
Massachusetts Institute of Technology
Class of '03

分享到:
评论

相关推荐

    藏经阁-Defeating Samsung KNOX with.pdf

    《藏经阁-Defeating Samsung KNOX with》是一篇关于如何突破三星KNOX安全系统的技术性文章,由腾讯Keen Lab的成员Di Shen(Retme)撰写。该文探讨了针对Android设备,尤其是三星Galaxy S7 edge(港版,基于Qualcomm...

    TStarBots Defeating the Cheating Level Builtin AI in StarCraft in the full game

    TStarBots Defeating the Cheating Level Builtin AI in StarCraft in the full game

    Defeating the Stack Based Buffer Overflow

    这篇文章由David Litchfield撰写,他是一位在安全领域享有盛誉的专家。文章主题集中在破解由Microsoft Windows 2003 Server内置的基于栈的缓冲区溢出保护机制。文章首先声明了Microsoft对安全的承诺,并提到了Code ...

    defeating-w2k3-stack-protection.pdf

    《Defeating the Stack Based Buffer Overflow Prevention Mechanism of Microsoft Windows 2003 Server》这篇文章由David Litchfield撰写,深入探讨了如何绕过微软Windows 2003 Server中内置的栈缓冲区溢出防护机制...

    Defeating Kernel Native API Hookers by Direct KiServiceTable Restoration.pdf

    本文档探讨了如何通过直接恢复KiServiceTable来击败内核原生API钩子技术(Defeating Kernel Native API Hookers by Direct KiServiceTable Restoration)。文档由Tan Chew Keong撰写,他是新加坡信息安全组织SIG2的...

    defeating-xpsp2-heap-protection.pdf

    **标题**:“defeating-xpsp2-heap-protection.pdf” 此文档标题表明了其主要内容是关于如何绕过或击败Windows XP SP2中的堆保护机制。 **描述**:文档描述简短且未提供额外信息,但从标题可以推测出其主要探讨的...

    BlackHat-DC-09-Marlinspike-Defeating-SSL.pdf

    ### 黑帽安全大会2009:Marlinspike揭秘SSL漏洞 #### 背景与概述 在2009年的黑帽安全大会上,众多安全专家齐聚美国拉斯维加斯凯撒皇宫酒店,共同探讨了当时互联网安全领域的重要议题之一——SSL加密技术的安全性。...

    2020年高中英语Unit1GreatScientistsSectionⅢLearningaboutLanguage&Using

    - "Although defeating in the match, we didn't lose heart."中,"defeating"应改为过去分词"defeated",与主语构成被动关系。 - "Very few of those interviewed spoke positive about their childhood."中,...

    Reasons and Persons

    that most of us have moral views that are directly self-defeating; and that, when we consider future generations the conclusions will often be disturbing. He concludes that moral non-religious moral ...

    starcraft.part01

    starcraft.part01 starcraft.part01

    Defeating-PHP-GD-imagecreatefromgif:利用PHP-GD内置函数imagecreatefromgif()的缺陷的概念验证

    开发人员使用GD(或Imagemagick)库来通过使用新库重新创建映像来防止执行映像头脚本。 这将擦除图像标题以及所有嵌入的代码。 这是生成有效负载的脚本 <?...$ gif = imagecreatefromgif ( 'poc.gif' );...

    Defeating-PHP-GD-imagecreatefromjpeg:利用PHP-GD内置函数imagecreatefromjpeg()中的缺陷的概念验证

    警告:此POC仅使用libJPEG v8.0进行了测试。... 这是生成有效负载的脚本 <?...imagejpeg ( $ jpg , 'poc.jpg' );imagedestroy ( $ jpg );?... 这是重新创建之前image.jpg的十六进制转储。 这里没什么好看的,只有一些垃圾...

    堆溢出和对抗GS编译的栈溢出经典文献两篇

    文献标题: 1.Defeating Microsoft Windows XP SP2 Heap protection and DEP bypass 2.Defeating the Stack Based Buffer Overflow Prevention Mechanism of Microsoft Windows 2003 Server.

    Unreal Rce / Fixing Armadillo 5.xx Hardware Fingerprint, By NeVaDa

    UnREaL RCE / Fixing Armadillo 5.xx Hardware FingerPrint (with Copy-MemII) <br>This tut is about Fixing Armadillo 5.xx Hardware Fingerprint and defeating Copy-MemII Because something changed in ...

    The Shellcoder's Handbook 2nd Edition

    The Shellcoder's Handbook 2nd Edition About the Authors vii Acknowledgments xi Introduction to the Second Edition xxiii ...Defeating a Non-Executable Stack 35 Return to libc 35 Conclusion 39

    Oracle 数据库攻防 英文原版

    Chapter 8 - Defeating Virtual Private Databases Chapter 9 - Attacking Oracle PL/SQL Web Applications Chapter 10 - Running Operating System Commands Chapter 11 - Accessing the File System ...

    Javelin ATM

    The FGM-148 Javelin is an ... The Javelin's HEAT warhead is capable of defeating modern tanks by attacking them from above, and is also very useful against fortifications in a direct attack flight.

    deoptfuscator:适用于Android应用程序的反混淆器

    该工具被选为 什么是去光散射器 Deoptfuscator是用于对已经使用控制流模糊机制进行了转换的Android应用程序进行模糊处理的工具。 Deoptfuscator可以逆转DexGuard在开源Android应用程序上执行的控制流混淆。...

    信息安全_数据安全_us-18-Gras-TLBleed-When-Protecti.pdf

    文档提到了“Translation Leak-aside Buffer: Defeating Cache Side-channel Protections with TLB Attacks”,这表明文档讨论了TLB(转换后备缓冲区)攻击。TLB是CPU中用于加速虚拟地址到物理地址转换的硬件缓存,...

    The Blood of Olympus.txt

    ">Though the Greek and Roman crewmembers of the <em>Argo II</em> have made progress in their many quests, they still seem no closer to defeating the earth mother, Gaea. Her giants have risen-a

Global site tag (gtag.js) - Google Analytics