Are you interested in games? Want to create a game but do not know where to start? Then you here. In this article I will consider the simplest physics engine, with the construction of which you can begin your journey in GameDev'e. And yes, the engine will write from scratch.
Several times my friends were interested in how I write games / game engines. After another such question and answer, I decided to make an article, since this topic is so interesting.
Javascript was chosen as the programming language, because the
experimental friend was not able to download the IDE and compiler. We will draw on canvas.
Formulation of the problem
It is necessary for several objects on the plane to realize the interaction using the fundamental force of gravity.
Those. do something like the attraction of stars in space.
')
Algorithm
First you need to understand the difference between computer physics and real. Real physics acts continuously (in any case, the opposite cannot be proved at the current moment). Computer physics, like computer act discretely, i.e. we cannot calculate it continuously, so we divide its calculation into steps with a certain interval (I prefer an interval of 25 ms). The coordinates of the objects change after each step and the objects are displayed on the screen.
Now let's get to gravity itself.
The law of the world (Newton's gravity) reads:
F = G * m1 * m2 / R^2 (1)
Where:
F []- G = 6.67*10^-11 [^3/( * ^2)]- m1, m2 [] - 1 2 R [] -
How does this help us in determining the new coordinates? And we will apply this force to these objects using Newton's second law:
F = m * a (2)
Where:
F [] - , m [] - a [/^2] -
Let us forget for a while that in (1) force is a scalar, and in (2) force is a vector. And in 2 cases we will consider force and acceleration by scalars.
That got the change in acceleration:
a = F / m (3)
Changing the speed and coordinates follows from the following:
a = v' → a = dv / dt → dv = a * dt v = s' → v = ds / dt → ds = v * dt v += dv Pos += ds
Where:
d - () v - s - Pos - ,
moving from vectors to scalars:
ax = a * cos(α) ay = a * sin(α) dv.x = ax * dt dv.y = ay * dt vx += dv.x vy += dv.y ds.x = vx * dt ds.y = vy * dt Pos.x += ds.x Pos.y += ds.y
Where:
cos(α) = dx / R sin(α) = dy / R dx = Pos2.x - Pos.x dy = Pos2.y - Pos.y R^2 = dx^2 + dy^2
Since there is no other kind of force in the project, we use (1) in this form and slightly simplify the calculations:
F = G * m * m2 / R^2 a = G * m2 / R^2
Code
Launch the
index.html page immediately and connect the code:
you can not watch <!DOCTYPE html> <html> <head> <title>Physics</title> <script type="text/javascript" src="script.js"></script> </head> <body></body> </html>
The focus will be on the file with the program code
script.js . The code for drawing is commented out enough and it does not concern the topic:
let's see and forget for a while var canvas, context; var HEIGHT = window.innerHeight, WIDTH = window.innerWidth; document.addEventListener("DOMContentLoaded", main, true); function main(){
Now the
most delicious : the code that calculates physics.
For each object, we will only store mass, coordinates and speed. Oh yes, I also need a radius - we will need it to calculate the collisions, but this is in the next article.
So, the “class” of the object will be:
function Star(){ this.x = 0; this.y = 0; this.vx = 0; this.vy = 0; this.r = 2;
var star = new Array();
Generating random objects at the very beginning:
var aStar; for(var i = 0; i < count; i++){ aStar = new Star(); aStar.x = Math.random() * WIDTH; aStar.y = Math.random() * HEIGHT; star.push(aStar); }
The step will be calculated in the following function:
function Step(){ var a, ax, ay, dx, dy, r;
Well, the long-awaited start of the timer:
var dt = 0.02;
You can see the work
here , and the code
here .
You may have noticed that objects fly through each other. It still lacks handling of collisions and from the physical point of view each object is a material point. In the next article I will look at collision handling.
Minuses
The complexity of the algorithm grows exponentially, so the increase in objects leads to a noticeable subsidence of FPS. The solution using Quad tree or other algorithms does not help, but in real games objects do not interact on the principle of each with each.
Testing was performed on a machine with an Intel Pentium processor with a frequency of 2.4 GHz. With 1000 objects, the calculation interval already exceeded 20 ms.
Using
As a force, you can use the superposition of different forces in (3). For example, engine thrust, resistance to ground and air, as well as collisions with other objects. The algorithm can be easily expanded to three dimensions; it is enough to enter
z in the same way as
x and
y .
This algorithm was written by me as far back as grade 9 in Pascal, and until now it has been translated into all languages that I know
simply because I can as a personal Hello World'a. Even in the terminal.
Also, this algorithm can be used for another fundamental interaction - electromagnetic (G → k, m → q). I used this algorithm to build the magnetic induction lines of the charge system, but about this in another article.
Thank you all for reading. I hope this article will help you a little in creating your own games.
The course we keep approximate on
this - a spiral galaxy.