Defeating lag with cubic Splines

Source: Internet
Author: User

This article may not be reprinted already cially without the express permission of the author.

Copyright 2000 Nicolas 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 ininitialized ated. Common
Solutions include increasing the frequency of packets sent, switching
Packet size through compression, and most importantly, dead reckoning.
This article will attempt to explain a technique for eliminating
"Jerk" of lag by employing the power of cubic Splines in
Dead-reckoning algorithm.

What is dead reckoning?

Before jumping straight into Cubic splines, a brief description
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 compression sion 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
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
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
When tively 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 shoshould next appear. This method has CES 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 cocould change abruptly. The end result is
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
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
Creating a dead reckoning path. This is because they account for
Starting position/velocity and the ending position/velocity. As
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
Second and third; they represent the object's velocity. For the second
Coordinate, calculate where the object will appear after 1 second
Its current velocity. For the third coordinate, reverse the object's
End velocity, and calculate where it wowould 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
    Valid tive.
    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
      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
      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.
Provided equations can also be easily extended into use
Three-dimen1_games. while creating cubic splines is easy, actually
Implementing them introduces several problems that must be left to
Reader to feature e on his own.

Happy coding,

Nick Caldwell

Mascript usetts Institute of Technology

Class of '03

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.