⚔ Game Maker Tutorials ⚔

Página 2 de 2. Precedente  1, 2

Ver el tema anterior Ver el tema siguiente Ir abajo

What game would you like to create?

⚔ Game Maker Tutorials ⚔ - Página 2 Vote_lcap17%⚔ Game Maker Tutorials ⚔ - Página 2 Vote_rcap 17% 
[ 1 ]
⚔ Game Maker Tutorials ⚔ - Página 2 Vote_lcap17%⚔ Game Maker Tutorials ⚔ - Página 2 Vote_rcap 17% 
[ 1 ]
⚔ Game Maker Tutorials ⚔ - Página 2 Vote_lcap66%⚔ Game Maker Tutorials ⚔ - Página 2 Vote_rcap 66% 
[ 4 ]
 
Votos Totales : 6
 
 

⚔ Game Maker Tutorials ⚔ - Página 2 Empty Re: ⚔ Game Maker Tutorials ⚔

Mensaje  Verax Mar Feb 03, 2015 11:30 pm

⚔ GAME MAKER TUTORIALS ⚔
⚔ Tutorial VIII: Linear Interpolation I: An Explanation ⚔

Just when you thought these tutorials were dead, they got revived. I just found the zelda project lying around today and suddenly wanted to write some more. We are over on the intermediate stuff now ~ congratulations on having passed the basics!

I'm going to cover a fairly confusing topic today. However, this topic is important because you will 120% surely need it while coding games at one point. I'm talking about maths, more specifically Interpolation.


What is interpolation? Why do we use it?:
When you play our game right now, the movement is literally crap. Sure, we do have a cool pixel based movement, however, it is incredible hard to enter doors etc. Now, remember, both our tiles and our player is 16x16. I will be talking a lot about something called the grid later, when we get more in depth with tiling. For now, the only thing you need to know is that we place tiles and objects on an invisible (can be visible in game makers room editor) grid. This means that everything fits well together, and is easy to work with, both for us and the computer essentially.

With the basic knowledge of the grid, we can come up with ideas on how to fix our movement problem. We want to be able to hit one-tile doorways etc. precisely. Instead of moving x pixels per step, what if we could move one tile over, say, 10 steps? Sure, we could just make it so that when the player hits an arrow key he moves one tile and waits for a couple of steps. However, that solution would look extremely awkward and bad. Instead, we use interpolation. We smooth out the movement over x steps, where we at a later point can make it so that the player speed also has an influence on this. (aka. x steps is calculated via player speed). But what is interpolation, really?

"In the mathematical field of numerical analysis, interpolation is a method of constructing new data points within the range of a discrete set of known data points." - Wikipedia
In English, please? We feed our computer with numbers and our computer spits out a new number, based on the number we fed it with. In our situation, we give our computer an (x, y) position, and it returns a new (x, y) position.

So, let's translate this a bit more over to our situation. We want it so that, say, when the player press the right arrow key, we feed our computer with Link's current (x, y) position, and our computer returns a new (x, y) position, which is equal to one tile left of link's current position. However, this is where people really starts to get confused, and I want to clear up one thing; interpolation is not the same as functions. With functions, we feed the computer with data and gets the results at once. With interpolation however, the same thing happens. However, with interpolation, we do something in between. What we choose to do between the data is fed to the computer and before the results are shown, depends on what type of interpolation we want to use. I will only cover Linear Interpolation, the easiest of them in depth for now.


Linear Maths:
For the people who are not familiar with the definition of linear in maths, I want to explain. Linear in maths is basically something that changes in a constant way. Take linear graphs for example. The formula for a linear graph is "ax + b". a is the number the graph increases with for every step on the x axis, x is our variable and b is our constant. Google "Linear Graphs" and have a look at some pictures. You will see a straight line, that increases evenly. Now, look at a picture of "Exponential Graphs", and you will see graphs that increases uneven. (slow at the beginning, fast at the end for example).

The same concept goes for (linear) interpolation. In our case, when we use linear interpolation on link's movement, the idea is that say, we want to move link one tile left within 10 steps. This means that we for example want to move link from the position (16, 16) to a new position (0, 16), in 10 steps. Using linear interpolation, we will want to move Link the same distance every step until the new point at (0, 16) is reached. In this case, it would be -1,6 (16/10 = 1,6 and we are moving to the negative side of the monitor) We cannot control Link until he has reached this new point, but we will see him moving 1,6 pixels every step for the next 10 steps. Which is exactly what we want to do. However, we do not implement this kind of interpolation by calculating 1,6 and adding it every step for the next ten steps.


The 0...1 Range:
When working with interpolation in our case, the idea is to smooth out the movement. We do this by calculating a number within the 0...1 range. Because, you see, we want to use this number in the 0...1 range in order to kinda reach 1, which is our new point. The number within the 0...1 range is calculated by the current step and the number of steps we need to reach the new position (in this case, 10). Then we multiply this value into our old position and our new position, and we will end up with a position somewhere between the old and the new position. How close it is to the new position depends on the current steps we are at. At the first step, we are far away from our new position, because 1/10 will leave us a smaller number within the 0...1 range than say, 9/10 (the 9th step). So the idea is actually to move from 0 to 1 in x, where 0 is our old position, 1 is our new position and x is the number of steps.

However, since linear interpolation (also called lerp) makes our object move at a constant speed every step, it will have a machine-feel over it. To avoid this, we can add something called Smoothstep to our lerp, which makes the object move slowly in the first half of the movement process, and quickly the other half. There are a lot of other ways to do interpolation too; we can use higher powers, we can interpolate along a sin - there are lots of possibilities. However, since we haven't really played with lerp yet, and since this is most likely confusing the heck out of you all already, we won't look at anything else than lerp for now.


In the next part of this massive interpolation tutorial, we will actually implements lerp in our game. I will also try to clear up some confusion on the topic via visual methods, and finally, our controls will be a lot better. Until then, see ya!

Verax

Verax

Messages : 1139
Data de inscrição : 27/06/2014

Volver arriba Ir abajo

⚔ Game Maker Tutorials ⚔ - Página 2 Empty Re: ⚔ Game Maker Tutorials ⚔

Mensaje  xSwimmieX Sáb Feb 14, 2015 5:29 am

Ooh! I like how the tutorials are going, they're pretty interesting, tbh.
xSwimmieX

xSwimmieX

Messages : 4669
Data de inscrição : 03/04/2014

Volver arriba Ir abajo

⚔ Game Maker Tutorials ⚔ - Página 2 Empty Re: ⚔ Game Maker Tutorials ⚔

Mensaje  Verax Sáb Feb 21, 2015 5:43 pm

⚔ GAME MAKER TUTORIALS ⚔
⚔ Tutorial IX: Linear Interpolation II: Advanced Interpolation ⚔

Instead of actually implementing a simple linear interpolation, I wanted to use this tutorial to clear a little more up in interpolation, show some sexy graphs and introduce you to some more advanced interpolation instead. However, the next tutorial will cover implementation of all this; I promise.

Smoothstep:
So, we remember what interpolation is, right? We feed our computer with data sets, and calculate data relative to the data we fed it with at that moment. While doing interpolation in our case, we simply set a target time in which the player is supposed to move from point A to point B (usually Tile A to Tile B), aka. move one tile. When doing linear interpolation, our player will move at a constant speed from Point A to Point B. This constant speed is derived from our target time.

However, as you may or may not understand, depending on your knowledge on linear maths, having a constant speed would feel a little machinistic (yes, we officially create new words on the fly). It would feel unrealistic. However, what if we could start at a slow speed, accelerate until we reach the middle point of our target time, then gradually slow down the speed again? It would certainly feel a lot more realistic because we apply some real world concepts in a pseudo way (to explain it simple; we fake real world concepts in our game).

We can actually apply this quite simple, as long as we already have a working linear interpolation. The formula for a smoothstep would be this:


Código:
for (i = 0; i < N; i++)
{
  v = i / N;
  v = SMOOTHSTEP(v);
  X = (A * v) + (B * (1 - v));
}


However, as you may see, we have to define the function SMOOTHSTEP.
Let's say that Smoothstep(x) = ((x) * (x) * (3 - 2 * (x)))
This function will simply smooth and control our velocity throughout the interpolation. The result, compared with a regular linear interpolation can be shown as a graph like this:

⚔ Game Maker Tutorials ⚔ - Página 2 Smoothstep

The improvement by adding smoothstep looks especially nice in 3D games, due to 3D games being more realistic compared to our own world. However, it also adds nice detail in 2D games.

Advanced Interpolation:
So, what more can we do with interpolation? It can get as advanced as this:


Código:
float catmullrom(float t, float p0, float p1, float p2, float p3)
{
return 0.5f * (
              (2 * p1) +
              (-p0 + p2) * t +
              (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
              (-p0 + 3 * p1 - 3 * p2 + p3) * t * t * t
              );
}

for (i = 0; i < N; i++)
{
  v = i / N;
  v = catmullrom(v, Q, 0, 1, T);
  X = (A * v) + (B * (1 - v));
}


This gives us Catmull-Rom Spline Interpolation for example. The graph for this can look like this:

⚔ Game Maker Tutorials ⚔ - Página 2 Catmullrom

Now, this is advanced interpolation. There are a lot more ways to interpolate, like using higher powers or sins, but the most common thing with all of them is that they can provide us a more specific interpolation for a more specific use. Catmull-Rom Interpolation can be useful for bouncing objects for example. (Just look at CatmullRom(-10, 1) at the graph). Catmull-Rom Interpolation could also be used to add some levitation pseudo physics to objects too. As you can see, interpolation can help us with providing fast and efficient pseudo physics without spending a month on a physics engine!

However, while interpolation is extremely handy, it should be used with care in large scale. If you have a physics engine for example, adding interpolation to fake physics our engine can handle would be a waste of time. If we have one specific case where we want to add some cool movement, or simply want to smooth out movement in general, interpolation is extremely useful.


That's it for today I guess. Next time, we will implement linear interpolation to our character movement, and maybe have a look at adding smoothstep to it. We won't cover anything more advanced when it comes to interpolation, so don't worry; I just wanted to show some cool uses for interpolation. Until then, see ya!


Verax

Verax

Messages : 1139
Data de inscrição : 27/06/2014

Volver arriba Ir abajo

⚔ Game Maker Tutorials ⚔ - Página 2 Empty Re: ⚔ Game Maker Tutorials ⚔

Mensaje  Contenido patrocinado


Contenido patrocinado


Volver arriba Ir abajo

Página 2 de 2. Precedente  1, 2

Ver el tema anterior Ver el tema siguiente Volver arriba

- Temas similares

 
Permisos de este foro:
No puedes responder a temas en este foro.