• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Matrices general questions

3 posts in this topic

I've a very old project in openGL that used glRotateglTranslate, and other glMatrix functions, and I'm updating it to the more modern approach of using matrices for calculations.

 

But I'm having some problems because I have very little to no experience with matrices (I always avoided it in my older project, I preferred using basic trigonometry because I found it to be easier), and I'd like to ask these questions:

 

I'm calculating, on each frame, this matrix:





mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
    glm::vec3(_camera.posX, _camera.posY, _camera.posZ), //Camera Position		
    glm::vec3(_camera.posX + _camera.directionX, _camera.posY - _camera.directionY, _camera.posZ - _camera.directionZ), //Eye Position
    glm::vec3(0, 1, 0) //Head Up
);
mat4 mModel = glm::mat4(1.0f);
mat4 mMVP = mProjection * mView * mModel;	

I'd like to change my object's position, so I tried this:





mat4 mModel = glm::mat4(1, 0, 0, object->posX, 
                        0, 1, 0, object->posY, 
		        0, 0, 1, object->posZ,
		        0, 0, 0, 1);

Because I read that the last column's represents the x, y, z translating positions of the plane.

But I guess I'm mistaken, because with this my object seems "stuck" and doesn't move at all (and my navigation stops).

 

I thought of multiplying it by a vec3(posX, posY, posZ), but then wouldn't the result be a another vec3? I read that when multiplying matrices such as 4x4 and a 4x2, the result is a 4x2, so I don't know how I can calculate this. I guess rotations will be even more complicated...

 

Another question I have is, I had a targetX, targetY, targetZ for my camera object, and I did this:





glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //go to the target
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //rotateX
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f); //rotateY
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //to go camera position

//Loop to draw all objects, assuming "camera position" as the new identity

And this way I could control where I was looking to, and change the zoom or switch to first person by making the target = position.

With the matrices' calculations I posted above, I don't know how I can achieve this, since the camera's position I send to the mView should change according to where I'm looking, and the center target as well right?

Any help on how I can achieve this?

 

Any info on how can I get to understand matrices and it's relations to the world space is really appreciated, I'm reading some tutorials about it but it still feels very overwhelming, too much information that I can't relate to the world's coordinates.

 

Thanks!

Edited by Danicco
0

Share this post


Link to post
Share on other sites

I managed to get translation with a glm function, glm::translate(modelMatrix, positionVector), although I'm clueless what it is doing...

 

I got another question too, I added rotation with:

mat4 mModel = glm::mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);	
mat4 mModelRotateX = rotate(mModel, object->RotationX(), vec3(1.0, 0.0, 0.0));
mat4 mModelRotateY = rotate(mModel, object->RotationY(), vec3(0.0, 1.0, 0.0));
mat4 mModelRotateZ = rotate(mModel, object->RotationZ(), vec3(0.0, 0.0, 1.0));
mat4 mModelTranslate = translate(mModel, vec3(object->posX, object->posY, object->posZ));
mat4 mMVP = mProjection * mView * mModelTranslate * mModelRotateZ * mModelRotateY * mModelRotateX;

I think I could reduce the rotation matrices down to 1, but I don't know how, tips?

 

And for some reason, my object isn't rotating around it's center anymore (from my previous immediate drawing example), it's rotating around one of it's edges it seems. Why?

 

How can I control the "center" of the object like this, where I begin drawing? The vertexShader code is this:

#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;
void main()
{	
	gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
}

With MVP being the same as the mMVP in the main drawing loop.

I just noticed that ever since I switched from immediate drawing to VBOs, I'm not using any of the "object positions" values, so I don't know where it is assuming the object's center...

0

Share this post


Link to post
Share on other sites
Okay, so matrices. If you really want to know the deep and dark secrets that they contain, take a course in linear algebra and you will probably know more than you ever need to know about them. Anyways, I'll just be giving basic examples so that you can more easily understand what you read on the internet.

Okay, so a matrix looks something like
[eqn]
\mathbf{M} = \left(
\begin{array}{ccc}
a_0 & a_1 & a_2 \\
b_0 & b_1 & b_2 \\
c_0 & c_1 & c_2
\end{array}
\right)
[/eqn]
or [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]] because I can't use two sets of eqn tags for some reason.

Notice how it has 3 rows and 3 columns, so we say it is a 3x3 matrix. Thus, a 2x3 matrix would have 2 rows and 3 columns. This can be represented in code as a 2 dimensional array

We can also add two matrices by adding the elements in row i and column j of matrix 1 to row i and column j of matrix two. In essence, a[i][j] + b[i][j]. For example, [[1, 2, 3], [4, 5, 6]] + [[2, 3, 4], [3, 4, 5]] = [[3, 5, 7], [7, 9, 11]]. Adding is then fundamental to translations. If we want to translate a point 3 units in the x direction, we can just compute [[x], [y]] + [[3], [0]]. We can also scale matrices by a number, by scaling every element by that same number. This is useful for dilating an object with respect to its local origin.

Multiplying is a bit more complicated, but it can be done. If you think of row i of matrix 1 as some vector v1, and you think of column j of matrix 2 as some vector v2, then the entry in row i and column j of the resultant matrix is v1.v2 (add the products of the first elements, second elements, and so on). So a simple example would be [[1, 2]] * [[2], [1]] = [[4]]. We then arrive at an important conclusion about matrix multiplication. It is that the number of rows in the first matrix must be equivalent to the number of columns in the second matrix. Then, it is not hard to see that A * B is not necessarily the same as B * A, and both may not necessarily be defined for matrices A and B. Multiplying becomes the core of rotation, and transforming one system of coordinates to another (i.e. world space to screen space).

Due to 3D matrices being hard to do, I will simplify a general rotation example into 2D. If you actually get out some pencil and paper, and try to rotate a point (x, y) about the origin some angle t degrees about the origin, you can figure out by simple application of similar triangles that x' = x cos t + y sin t and y' = -x sin t + y cos t. Applying our knowledge of matrix multiplication, it is easily verifiable that [[cos t, -sin t], [sin t, cos t]] * [[x], [y]] = [[x'], [y']] is a valid way to compute the rotation of a point without hard coding a bunch of equations that are hard to maintain.

Hopefully this is decent enough as a brief introduction to matrices.
0

Share this post


Link to post
Share on other sites





mat4 mModel = glm::mat4(1, 0, 0, object->posX, 
                        0, 1, 0, object->posY, 
		        0, 0, 1, object->posZ,
		        0, 0, 0, 1);

 

Transpose that matrix. Translation should be at the bottom, not the right.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0