Basics of Games Production Mechanism of Game Contents 5 (Game Engine, View Space)
GCC Academy
Hello, everyone
This is Lee Deukwoo of Game Math
During this time, I'm going to explain about the principle of
game structure
We'll talk about local space and world space
and about how game rendering proceeds
Next, we'll learn about how the resources made for game production
are managed
I'll explain about its process
Next, I'll talk about view space, used for expressing
camera on the game
I'm going to explain about camera on games
and a new space called view space
Finally, we'll learn about how we need to construct a matrix
in order to calculate the view space
Structure and Principle of Game Engine
First, we'll talk about
local space and world space
Earlier, when forming an object
multiple points came together
to create a single object
And in order to draw that object, we had an origin
In terms of a two-dimensional plane
(0, 0, 1) was the standard
and relative locations were chosen
to express the form of an object
When drawing a single object like this
this space proceeding with (0, 0, 1) as the standard
is called local space
But when we make a game
we don't need a single object
We need multiple objects mixed together in a space
These come together to create the stage
And the main character goes into this space
and has to explore around it
Therefore, we need a separate space
where these objects are gathered together
This space used for organizing the game stage
is called world space
So game stage is
where local spaces forming up objects have gathered up
It's a form of central space where they have gathered up
To explain this mathematically
the local spaces get linearly transformed
with the world space as the standard
And with a value for translation, rotation, and scale
they are arranged
Like the picture
when an object has local space
this local space sets the world space as the standard
and places its own local space on top of it
That's how you can view it
In order to place local space on the world space
there would be unique properties of the object
There would be properties such as how far it is
away from the origin of the world
There are various information to distinguish this
But normally, we use 3 types of information
And those are scale, rotation, and location
These were mentioned earlier
when we were talking about matrices related to affine transformation
These, together
are called transform
We'll try inducing matrices
related to this transform
For these three affine transformations
scale transformation is S, rotation is R
and translation is T
The result of applying these three transformations in order
has the following possible number of cases
For matrix, commutative property isn't valid
Therefore, all of these
The result of multiplying these would all be different
Let's say that we apply the translation T
in the middle, as second
If we apply it that way
I'll use the picture to explain the problem that occurs
Let's say that we translated up
by (0, 1)
If we move up (0, 1) after rotating
then the final location would be (0, 1)
But if we translate by (0, 1) first
and then rotate
the final location of the object isn't (0, 1)
Then when I make the command of moving up one unit
an unexpected result comes out
Then in the perspective of the user who commands to make the game
a disharmony would occur
I commanded it to go up
but it seems like it's going the wrong direction
So for the transformation itself, we can apply
both cases, but
when we actually make or play the game
we need to use the form
that performs the set command
in the perspective of a user who doesn't know math
Then this translation part
has to come last
So for the 6 cases above
we would choose the cases where translation occurs last
I've said that matrix
application occurs from right to left
Therefore, T·S·R or T·R·S
are the two cases that we get
Then which one should we choose out of the two transformations?
Let's say that we're increasing the scale for one side
If we increase the scale for one side
and then rotate it
First, when we rotate it first and then scale it
I scaled the original object sideways
then the rectangle has to be stretched out
But here, we get the result of a rhombus
getting stretched out
But if you look at the bottom result
we stretch the object horizontally first
and then the result of rotation
Therefore, among the final 6 cases
the transformation
that we need to choose for the game producer
or the ones playing the game
is TRS, applying the scale first, then applying the rotation
and then applying the translation
That's the order we need to proceed with
Therefore, the following form
is the set order of transformation
If we link TRS together and multiply them together
we would get the same square matrix
This is called a model matrix
When managing these transforms, we
use it in the form of matrix
But in the perspective of actual game producers or game players
even if they don't know what matrix is
they just want to give commands in the form of
go up one unit
or rotate 30 degrees
Therefore, when related to production
the matrix itself isn't used
We just pull out the essential elements
needed for matrix production, and manage them
And those are scale, rotation, and translation
Data related to scale is Vector2
We just need to know the scale of the x axis
and the y axis, just these two things
Therefore, we set it as Vector2
For rotation, since it's a rotation on a plane
we just need the information on angle
For translation as well, Vector2
We just need the information on
how much translation we did on x axis
and translation on y axis
Just by changing these information
they should be able to produce or play the game
That's the environment that should be made
So when we usually work on the game engine
you probably have done this before but
we set these transform settings
These values for the objects and space transformation
can be set up on the interface they provide
For Unity or Unreal Engine as well
transform is provided for all objects
Then how will these inputted information
be used internally?
These are used for creating
the model matrix we mentioned earlier
The scale transformation of affine transformation
proceeds like this
Next, rotation proceeds like this
And translation proceeds like this
Taking each of these s_x, s_y, and θ
and t_x and t_y
When we combine these 3 information and multiply them
we get the following matrix
So we don't perform each multiplication
individually
We just need to insert the data
into this final product of matrix
Then, the final transform gets completed
This is the advantage of
matrix multiplication
Then this time, with this transform as the foundation
we'll look at how game rendering proceeds
For a game to work smoothly
the multiple objects arranged in world space
need a systematic method
to manage them
efficiently
The system that can manage the overall game stage is called
at the developing level, and this is a development term but
it's called a scene
It's also called a level
Objects belonging to this scene
always have the transform information
containing information about
where it's located on the world space
This transform information is managed by the unit of object
And the object having transformation
would also have to be managed separately
These are usually managed as
game objects or actors
Expressing this structure as a picture
can be shown as the following
There's the overall scene
A scene, in a way
is a collection of game objects
This game object must
have a transform
That's the structure it has
Now, a game is ultimately
exported images of videos
In the monitor, different images are
flipped through very quickly
Therefore, it proceeds quickly in the unit of frames
When we input through the keyboard
and move an object
it's not like the object is actually moving
It catches the input value in a very short time frame
and draws out a new frame
That's the way it proceeds
Therefore, we confirm the transform information of game objects belonging to the scene
for each frame
Then when the transform for each object has been decided
there's the mesh information of the object
The mesh is composed of vertices
and these vertices actually compose triangles
The triangle information from the mesh
gets matched to the model matrix
and other matrices that we'll learn about
And then each triangle
gets drawn one by one
Changing the transform of game objects
through input
actually has no relationship to drawing the triangle of the mesh
It simply shows the changed information
about how the game object has moved
Only after these transform information has been confirmed
we can safely transform the vertices
of triangles composing up the mesh
Therefore, it proceeds with two
overall processes
These are called game logic
and rendering logic
I wrote it down here
A game logic is
the process of confirming the transform of the game object
that is not related to rendering
Rendering logic is when this confirmed transform is used as the foundation
to transform the vertices composing the mesh
through matrix multiplication
These changed vertices ultimately make up the triangle
And coloring each of the triangles
is the process of rendering logic
Showing this as a picture
There's an object that has the transform
and the mesh information
But we don't always use these two information the same
First, in game logic, we only use the information
about transform
On top of this confirmed transform
we render the mesh information of the object
The two steps proceed separately
as you should know
Next, we'll learn about game resources
A game composes objects and the stage
Composing the objects for each frame
and rendering them is a core process, but
managing the data used in the game
is also one of its important roles
Therefore, the data used in the game
needs to be managed systematically and efficiently
That way, when users download this later
they will be able to play pleasantly, with small capacity
These data used in games
are called game resources, or game assets
Game resources are a distinct concept from
scene and game object that we talked about earlier
When making a 2D game
let's say that we arrange 100 of the
same tiles, or same pictures
Then if the game object possesses this asset
then the 100 objects
holding the same texture, or same image
all have the asset
Then the size of the scene will greatly increase
Therefore, I wrote this down here too but
when 100 identical monsters are placed on the map
having duplicates in all the game objects
increases the capacity of the game exponentially
Therefore, it's more preferred to manage
identical assets in a single data
That's more efficient
So normally, when managing
the resources of game engines internally
a unique key is given
An identifier is given
The game object only holds the identifier information
Then the game object
doesn't have to possess the asset anymore
Then, the game object capacity will decrease
This monster data that gives the unique key
or the tile data that makes up the background
is managed separately, through database
And these are called resource repository
Inside the resource repository
various types of data get included
Because as mentioned earlier
the model information may go in
Or the image information may go in
The music information may go in
All the shared data used in the game
go into the resource repository
In this resource repository
primary classification occurs based on the type
Next, it gives a unique key for each
Then the game objects
can use this and share various resources
to use them efficiently
So the scene and the resource repository is separated
Only the key information is provided, in order to minimize the size of the scene
and manage the data effectively
That's what you should know
View Space
I'll first explain about the camera
The world space that we looked at earlier
is actually a space constructed to place objects
In the perspective of the one making the game
it's a needed, essential space
Even when composing the game
it's an important space
For the content of games
we're not making the stage
Instead, an avatar that represents the person playing the game
explores the game space
And therefore
everything has to be expressed
in the perspective of the game avatar
Therefore, the camera
The view of the avatar
is the imaginary camera
Everything gets reinterpreted with this view as the standard
and they have to be rendered that way
Therefore, after placing the objects on the world space
the entire world has to be reinterpreted with camera as the standard
If that is not the case
as you can see in the picture
on the left, everything can only be expressed
with the origin as the standard
within this world space
But when the world space is very big
it's impossible to express everything in one screen
But if we use something called a camera
and use this view space, as you can see here
we can freely show a section of the world space
that we want on the screen
That's the advantage that we get
In order to create space like this
we need to know the concept of view space
And we need to check how to
calculate this
View space is ultimately rearranging the world space
with the camera as the origin
A camera is, in a way
an object placed in the world space
We reinterpret the world space with the placed object as the origin
In other words, objects placed in the world space
have to be calculated again
with the camera as the origin
Let's look at an example
Let's say that the camera is located at (-10, -10)
If it's located at (-10, -10)
and if the location of the object with the camera as the origin
is at (10, 10)
Then the location of the object becomes (20, 20)
What that means is that, the camera is right here
It's at (-10, -10)
If we reinterpret the object location in the perspective of camera
we have to set this as the origin again
Then, we're reinterpreting that the object is
(20, 20) away from here
Let's analyze the mechanism why it becomes (20, 20)
The location of the camera in the original world space
was (-10, -10)
And the location of the object was (10, 10)
But this one calculates the location of the camera
in the reverse direction, or reverse matrix
Then the inverse matrix of (-10, -10)
becomes a negative value
and becomes (10, 10)
There, we add the original location of the object
when the world was the standard
Then we add (10, 10)
and it becomes (20, 20)
This is composed of
two steps
First, there's the location of the camera
and the location of the camera becomes the standard
and we set the inverse matrix
Then we can look at the relative distance
between the world origin and the camera origin
If we set the camera as the origin
and ask what the origin of the world is
then it is (10, 10)
And since the other coordinates
are all calculated with the world origin
we just need to add to that
Then we can calculate how far the object is
with the camera as the standard
Those are the two steps it proceeds in
So if we express the above expression
in the perspective of matrix
then we're obtaining the inverse matrix of translation
It's the inverse matrix of the translation value
of the camera transform
And we're adding the value of the object
But ultimately, since that's also a form of translation
this can also be expressed
as multiplication of translation matrix
Therefore, just like the following
we can express it in a two-step matrix
If we just want to calculate the location value away from the camera
then we take the original transform of the object
and add the relative distance
of the camera from the world origin
Once we calculate that through inverse matrix
then we can ultimately calculate how far the object is
from the origin of the camera
That's what this expression is showing
A matrix calculated this way is called a view matrix
Now let's take a look at how we should calculate
the view matrix
The model matrix of game object from earlier
proceeds with the multiplied form of
three affine transformations, TRS
So when we actually render a triangle
one point, vertex, of a mesh
is composed with the local coordinate system as the standard
On this vertex, we multiply TRS
And the final result from multiplying TRS
is the location when
the world is the origin
So we multiply v_world and v_local to TRS matrix
We multiply the model matrix, TRS matrix, to
go through the transformation process, as we've learned earlier
Then if we are to reinterpret
with the camera as the standard
we have to rearrange the ones
that have been arranged with world as the standard
Therefore, we multiply a specific matrix to v_world
and create a new point called v_view
That's the concept
Therefore, with the transform of camera object
Since we place the camera ourselves
we can know where the camera is placed
with the world as the standard
So based on the transform of camera object
we transform the point of the world like the following
When we just look at translation
we multiply the inverse matrix of the transform of camera
to the point that's based on the world
And we create a point on the view space
When this is expanded out
it gives us the result of
multiplying the inverse matrix of camera transform and TRS matrix
in order
to the local space
Matrix like this isn't called the inverse matrix of translation
We just combine it to call it the view matrix
We can expand out a view matrix
in the following way
Up until now, I only explained
about translation, but
functions other than translation can go in
Rotation is
a main example
So although the camera is two-dimensional
it can rotate like this
But we don't need to care about the scale
Just because the camera becomes bigger
doesn't mean the screen gets bigger
They aren't related at all
Although we set the transform on camera
we don't use the information on its scale
We proceed with a fixed scale
If the camera rotates, how will the screen look?
If I rotate the camera like this
then in the perspective of expression
in the perspective of the world, it turns the other way
That's how it's seen to the users
Therefore, same thing for this
when we rotate the camera
we can interpret the screen
to rotate the other way
This is also an inverse matrix
I've said that going the opposite direction is inverse matrix
Therefore, when there's a transform
that sets the camera
reinterpreting the world
with the transform as the standard
is in a way, applying inverse matrix
So we apply the inverse matrix
of TRS
We've said that scale doesn't matter
Then it would be the inverse matrix of TR
In the definition of inverse matrix
we've seen that the inverse matrix of TR
is the same thing as flipping
the inverse matrix of each
This is the same result as
the inverse function of composite function
So when we consider the rotation as well
the view matrix, ultimately
is the result of multiplying
the inverse matrix of rotation and inverse matrix of translation
Inverse matrix of rotation
is a transpose matrix, as we learned before
Since inverse matrix of rotation is a transpose matrix
the sine parts flip over
For the inverse matrix of translation, we add
a negative sign on the translation value
Therefore, multiplying these two matrices
gives us the following result
If you look at this expression, the last third column
has the form of dot product
If we think of each row of rotation matrix as row vectors
then the first row vector becomes cosθ, sinθ, 0
And the second row becomes -sinθ, cosθ, 0
If we think of translation as the column vector
it becomes -t_x, -t_y, 1
Then the third column
where the two have been multiplied together
is the dot product of the first row vector, cosθ, sinθ, 0
and the third column vector
So cosθ × –t_x + sinθ × –t_y
And since this is 0
we get an expression like this
The same goes for the second value
Matrix composed this way
is called a view matrix
I've organized it again in the following part
When we place an object
and in order for the avatar acting as the player
to explore the game world
we need the following
additional processes
First, we create the view matrix from the camera and calculate beforehand
When we design, the camera location would be decided
It could be the location of the player
or it could be ahead of the player
It will vary based on the game plan
Anyways, if camera transform exists on the world
we can obtain θ and translation value
or location value from that transform
Therefore, we can calculate the view matrix beforehand
So we obtain the value beforehand
And then we draw the game objects
belonging to the scene, one by one
If there are 100 game objects in the scene
then we draw out 100, one by one
We've said before that each game object
has its unique transform
Then a unique TRS, or model matrix
will be made for each game object
On that model matrix, we multiply the view matrix we obtained earlier
and create the final matrix
That becomes the final matrix
Then when we multiply this final matrix
with each vertex of the mesh of game objects
then we get the coordinates of a new point
that has been interpreted with view space as the origin
If we use this as the basis
to draw triangles and show them on the screen
then as we mentioned earlier
we can create the form of content
where we can explore the world
With that, we learned about how game contents are made
for two-dimensional
graphics
That's all for today's lecture
Great job for listening to this lesson
Thank you
Game Engine Local space and world space Local Space: Space with (0, 0, 1) as the standard to draw single objects World Space: Space for game stage
Transform: Unique value of object, provides transform information to all objects Managing transform: Scale (Vector2), Rotation (Angle), Translation...
Process of game rendering Scene or level: System that manages the overall game stage Game object or actor: Object that gets the transform
Game logic: Confirming the transform of game object Rendering logic: Rearranging mesh vertices from local space standard to world space standard...
Managing game resources Data used in games need to be managed efficiently Game resource or game asset: Data used in games
Identical asset is managed with one data Resource repository: Storage of game asset information Resource repository only provides key information...
View space Camera Set the view of avatar as an imaginary camera and need to reinterpret the world based on the camera
View space Rearranged world space with camera as the origin Step 1: Calculate the inverse matrix of camera location Step 2: Add the object location...
Calculation of view matrix V = R^(-1) T^(-1) =
[ cosθ sinθ -(t_x cosθ + t_y sinθ) ] [ -sinθ cosθ -(t_x sinθ - t_y cosθ) ] [ 0 0 1]
Logic that renders the final game object of scene Calculate the view matrix beforehand, using location value of θ of the camera transform Go through..