Thursday, April 12, 2018

Video Game Development: Relativity

Relativity is an important concept to understand in video game development.  Yes, I am talking about Einstein's relativity.  Don't worry, this will be fairly painless, because we don't need to get into the hard stuff.  Relativity in general is something valuable to understand when developing video games.  In some places it is even critical.

Relativity comes from the word "relative", and the initial assertion made by relativity is that everything on a material level is relative.  More succinctly, there is no universal landmark.  In a programming context, there is no detectable (0, 0) coordinate in real space.  All spacial coordinate systems are relative to some reference frame.  The reference frame is the landmark from which everything else is measured.  In physics, this reference frame may be moving in space relative to something else, but because it is the reference frame, we treat it as stationary.

Copernicus and Galileo were instrumental in Western astronomy in the idea that the Sun is the center of the solar system, instead of the Earth being the center of the universe.  But they were wrong.  Yes, if the Sun is used as the reference frame, we can treat it as the center of the universe.  But previous assumptions that everything orbited the Earth in strange heliocentric orbits were not strictly wrong.  Using the Earth as a reference frame, they were actually just as correct as the Sun-centric models.  The reason the Sun-centric model was superior was efficiency.  For the solar system, the Earth centered model required a lot of excessively complex math to describe the orbits of other planets.  The Sun centered model was far more efficient.  But, when we look at bodies outside of our solar system, the Sun centered model is no better than the Earth centered model for things within our solar system.  For this, the superior model is using the center of the galaxy as the reference frame.  But, increasing scale is not inherently superior.  A galaxy centered model for our solar system has the planets in complex helical orbits again.  The ideal reference frame depends on what you are trying to measure, and it can be vastly different for different things.

This is what relativity is.  It is the idea that there is no universal reference frame, and the ideal reference frame depends on what you are measuring.  This idea is critical in video game development, and every game uses it, whether the developers realize it or not.

The most obvious reference frame for games is the screen or window, where the upper left corner is (0, 0).  This fails, however, when game entities interact with each other.  For example, when we do collision detection, we do not generally compare positions relative to (0, 0) to determine if there is a collision or not.  It is possible, but the math is complex and hard to understand.  Instead, we choose one object as a reference frame, and then we subtract its position from that of the second object.  This subtraction is what makes the first object the reference frame, because the result of that subtraction is the position of the second object relative to the first.  When we subtract like this, we shift the reference frame from the (0, 0) position on the screen or map, to the first object.  From there, all we have to do is some simple size and position calculations to determine if there is a collision or not.

This extends beyond just collision detection.  When we do any kind of physics or vector math to resolve interactions, we start by picking a reference frame object and subtracting it from any other object, to get the positions of each object with reference to the reference frame object.  Again, it is possible in these interactions to use the screen or map as the reference frame, but it makes the math more complex and more difficult to understand.  It can also make it less efficient.  In some cases, using the wrong reference frame can serious impact game performance.

In video game development, relativity is essential.  It is often used without even realizing it, but understanding how and why it works can provide a significant advantage in finding the best solution to difficult problems.

No comments:

Post a Comment