• 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
tempvar

Camera rotation - First Person Camera

5 posts in this topic

Hi

I've just switched from deprecated opengl functions to using shaders and GLM math library and i'm having a few problems setting up my camera rotations (first person camera). I'll show what i've got setup so far.

I'm setting up my ViewMatrix using the glm::lookAt function which takes an eye position, target and up vector

[CODE]
// arbitrary pos and target values
pos = glm::vec3(0.0f, 0.0f, 10.0f);
target = glm::vec3(0.0f, 0.0f, 0.0f);
up = glm::vec3(0.0f, 1.0f, 0.0f);

m_view = glm::lookAt(pos, target, up);
[/CODE]

i'm using glm::perspective for my projection and the model matrix is just identity

[CODE]
m_projection = glm::perspective(m_fov, m_aspectRatio, m_near, m_far);

model = glm::mat4(1.0);
[/CODE]

I send the MVP matrix to my shader to multiply the vertex position

[CODE]
glm::mat4 MVP = camera->getProjection() * camera->getView() * model;
// in shader
gl_Position = MVP * vec4(vertexPos, 1.0);
[/CODE]


My camera class has standard rotate and translate functions which call glm::rotate and glm::translate respectively

[CODE]
void camera::rotate(float amount, glm::vec3 axis)
{
m_view = glm::rotate(m_view, amount, axis);
}
void camera::translate(glm::vec3 dir)
{
m_view = glm::translate(m_view, dir);
}
[/CODE]

and i usually just use the mouse delta position as the amount for rotation

Now normally in my previous opengl applications i'd just setup the yaw and pitch angles and have a sin and cos to change the direction vector using (gluLookAt) but i'd like to be able to do this using GLM and matrices.

So at the moment i have my camera set 10 units away from the origin facing that direction. I can see my geometry fine, it renders perfectly. When i use my rotation function...

[CODE]
camera->rotate(mouseDeltaX, glm::vec3(0, 1, 0));
[/CODE]

What i want is for me to look to the right and left (like i would with manipulating the lookAt vector with gluLookAt) but what's happening is
It just rotates the model i'm looking at around the origin, like im just doing a full circle around it. Because i've translated my view matrix, shouldn't i need to translate it to the centre, do the rotation then translate back away for it to be rotating around the origin? Also, i've tried using the rotate function around the x axis to get pitch working, but as soon as i rotate the model about 90 degrees, it starts to roll instead of pitch (gimbal lock?).

Thanks for your help guys,

and if i've not explained it well, basically i'm trying to get a first person camera working with matrix multiplication and rotating my view matrix is just rotating the model around the origin.
0

Share this post


Link to post
Share on other sites
I never really paid much attention to these opengl camera matrices. You might have more help in an opengl forum.
0

Share this post


Link to post
Share on other sites
If I followed your snippets correctly, I think your camera::rotate doesn't look like it produces proper yaw/pitch rotation. I'm not familiar with the GLM api, but I think that rotation function would rotate the camera about the world origin point, and not about the camera point in world space. This however depends on the order glm::rotate concatenates orientations, and I was not immediately able to google the glm specs for it, so try to double-check that. MathGeoLib has a [url="http://clb.demon.fi/MathGeoLib/docs/float3x4_RotateAxisAngle.php"]float3x4::RotateAxisAngle[/url], which takes in a reference point that specifies the off-center axis to rotate about. You can see the source code there, and adapt something similar for GLM.
0

Share this post


Link to post
Share on other sites
I am solving my camera with its own coordinate system in camera space (similar to object space).

My camera has forward, up and right vectors. If I rotate camera, i rotate those 3 vectors around axis by angle.

For example, rotation around Y axis
[CODE]
MyMath::Matrix4x4 rotation = MyMath::Matrix4x4::Rotation(this->up, angle);
this->right = MyMath::Vector3::TransformNormal(this->right, rotation);
this->forward = MyMath::Vector3::TransformNormal(this->forward, rotation);
[/CODE]
TransformNormal takes only 3x3 part of 4x4 matrix. No translation and homogenous part is involved.



Than if I build view matrix
[CODE]
this->lookAt = this->cameraPos + this->forward;
this->viewMatrix = MyMath::Matrix4x4::LookAtLH(this->cameraPos, this->lookAt, MyMath::Vector3::UnitY());
[/CODE]

Final step is to update camera system from view matrix.
[CODE]
this->right.X = this->viewMatrix.M[0][0];
this->right.Y = this->viewMatrix.M[1][0];
this->right.Z = this->viewMatrix.M[2][0];
this->up.X = this->viewMatrix.M[0][1];
this->up.Y = this->viewMatrix.M[1][1];
this->up.Z = this->viewMatrix.M[2][1];
this->forward.X = this->viewMatrix.M[0][2];
this->forward.Y = this->viewMatrix.M[1][2];
this->forward.Z = this->viewMatrix.M[2][2];
[/CODE]

And thats basicly my whole camera. Of course, I have some utils function around it, but idea is to have independent coordinate system and all transformation do on this system.
P.S: Used notification is DirectX (row-base), so for OpenGL matrices should be transposed (col-based) Edited by Martin Perry
0

Share this post


Link to post
Share on other sites
Open has no meaning of row or column major. People needs to stop confusing others with that row/column major nonsense. All Open Gl expects is that the translation values be in the last three elements of the array.
0

Share this post


Link to post
Share on other sites
You're intuition about your current code rotating the camera around the origin is correct. You want to use the inverse of the rotation matrix when you apply it to the view, because the view matrix is a glorified inverse world matrix for the camera. Try swapping the order of the matrices when you multiply them in rotate(...). Remember, the inverse of a rotation matrix is its transpose.

Right now you have:

view = view * rotation.

I believe you want:

view = rotation * view.

The reason this works is that flipping the multiplication is equivalent to transposing the rotation matrix before multiplying like you were before. As it turns out, the transpose of a rotation matrix is its inverse.

EDIT: After thinking about it, I don't think this is correct either. I think the issue is that you you're trying to rotate a matrix that has already been translated. You're probably better off doing what Martin Perry suggested. Edited by ZBethel
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