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

Why is this a function on distance and not angle?

7 posts in this topic

I'm trying to create a billboard that faces the camera, following the Lighthouse3d tutorial and it really isn't working at all.
        glm::vec3 campos = state->getCamera()->getPosition();
        glm::vec3 objToCam = campos - mPosition;
        objToCam = glm::normalize(objToCam);

        glm::vec3 upAux = glm::cross(glm::vec3(0.f, 0.f, 1.f), objToCam);
        float angleCos = glm::dot(glm::vec3(0.f, 0.f, 1.f), objToCam);

        glm::vec3 rot;
        rot.y = glm::degrees(glm::acos(angleCos));
       
        // First translate so we rotate around the center of the billboard
        glm::mat4 rotMatrix = glm::translate(glm::mat4(), glm::vec3(-16.f, 0.f, 0.f));
        rotMatrix = glm::rotate(rotMatrix, rot.y, upAux);

This isn't working as expected.

A) The billboard will follow the camera as long as I am not within a few degrees of 0 on the x axis. As I start to close in to 0 the billboard rotates back to face down the X axis.

b) Rotation of the billboard becomes whacky when I only increase the distance. It starts rotating by large amounts, and faces away from the camera.
0

Share this post


Link to post
Share on other sites

I'll be honest, I have no idea what's wrong with that code. But I'll give you another option, since I don't know why everyone decides you need rotation for this. It's just as simple to build a matrix pointing "at" something. In fact glm::LookAt(pos, target, upGuess) does just that. The math is very similar, so I'll spell that out too:

glm::vec3 up = getCamera()->getUp(); // up guess
glm::vec3 at = glm::normalize( getCamera()->getPosition() - mPosition ); // looking back at the camera
glm::vec3 left = glm::cross(up, at); // figure out what 'left' is
up = glm::cross(at, left); // othognalize our matrix by squaring up our 'up guess' to be otho to the actual at and left

glm::mat4 objectMat(at, up, left, mPosition); // assuming you want Y as "up" otherwise i think the order is 'left, at, up'

Or, just use the built in function that does the same thing:

objectMat = glm::LookAt(objectPos, camPos, camUp);
0

Share this post


Link to post
Share on other sites
It's difficult to tally your description of the problem with the code.

I could say, it looks like you are mixing degrees and radians, but then it shouldn't work at all. And since you normalise the distance vector, a dependence on distance is puzzling.

Overall, it seems like a round-about way of solving the problem. You should know the three components of the rotation matrix directly, since they are the normalised vector to the object, the up vector, and the cross product of those.
0

Share this post


Link to post
Share on other sites
Or, just use the built in function that does the same thing:
objectMat = glm::LookAt(objectPos, camPos, camUp);
 



I've tried that, but still get strange results. If there isn't anything wrong with the method I posted above and using lookAt, then the only other thing it could be is how I'm tracking the position of the camera.
void FreeLookCamera::translateForward(float distance) {
    glm::vec3 forward;

    forward.x = -distance * glm::sin(glm::radians(mRotation.y)) - glm::sin(glm::radians(mRotation.x));
    forward.y = distance * glm::sin(glm::radians(mRotation.x));
    forward.z = distance * glm::cos(glm::radians(mRotation.y)); - glm::sin(glm::radians(mRotation.x)); 

    mPosition += forward;

    mViewMatrix = glm::translate(mViewMatrix, forward);
}
0

Share this post


Link to post
Share on other sites

I'm trying to create a billboard that faces the camera, following the Lighthouse3d tutorial and it really isn't working at all.

        glm::vec3 campos = state->getCamera()->getPosition();
        glm::vec3 objToCam = campos - mPosition;
        objToCam = glm::normalize(objToCam);

        glm::vec3 upAux = glm::cross(glm::vec3(0.f, 0.f, 1.f), objToCam);
        float angleCos = glm::dot(glm::vec3(0.f, 0.f, 1.f), objToCam);

        glm::vec3 rot;
        rot.y = glm::degrees(glm::acos(angleCos));
       
        // First translate so we rotate around the center of the billboard
        glm::mat4 rotMatrix = glm::translate(glm::mat4(), glm::vec3(-16.f, 0.f, 0.f));
        rotMatrix = glm::rotate(rotMatrix, rot.y, upAux);

This isn't working as expected.

A) The billboard will follow the camera as long as I am not within a few degrees of 0 on the x axis. As I start to close in to 0 the billboard rotates back to face down the X axis.

b) Rotation of the billboard becomes whacky when I only increase the distance. It starts rotating by large amounts, and faces away from the camera.

 

That code seems odd... like how you create a vector "rot" and initialize the "y" component, and dont use that vector again, only the y component.

 

Anyway, it's not immediately apparent what's wrong, but if you just want a rotation matrix that looks back at the camera...

 

Why not just take your camera rotation matrix and negate the i and k basis vectors (columns 0 and 2), then set the translation equal to your billboard position.  That should give you a transform looking back at the camera with the same up vector as the cam.  (assumes camera matrix is orthonormal and looks down -k direction)

 

That seems a lot easier and less error-prone than whatever the above is supposed to do.

0

Share this post


Link to post
Share on other sites

[quote name='0r0d' timestamp='1358227306' post='5021681']
Why not just take your camera rotation matrix and negate the i and k basis vectors (columns 0 and 2), then set the translation equal to your billboard position. That should give you a transform looking back at the camera with the same up vector as the cam. (assumes camera matrix is orthonormal and looks down -k direction)
[/quote]

Unless I'm misunderstanding you, the method you suggest creates a billboard that faces with the camera. Yaw/Pitching the camera turns the billboard with it, since it shares the left/up with the camera. The method I listed and the method that lighthouse3d is presenting creates a billboard that faces at the camera. Yaw/Pitching the camera in this case doesn't affect the up/left of the billboard at all.  In both cases however, rolling the camera will result in rolling the billboard since both use the "up" from the camera itself. Therefore it's sometimes useful for your effect to just hint with world up, so rolling the camera doesn't break anything. However you then need to handle the special case of putting the camera too close to straight above/below the billboard by picking a different up.

0

Share this post


Link to post
Share on other sites
 
Yup, looks like you've messed that up. http://en.wikipedia.org/wiki/Spherical_coordinate_system
glm::vec3 dir = glm::vec3( cos(x) * cos(y), sin(x) * cos(y), sin(y) ); // note i swapped cos/sin on Y from what the wiki states so that y=0 is down and y=pi is straight up

forward = dir * speed;
 
 



I have seen that wiki article and have tried implementing it, but it doesn't work properly. It's pretty strange but when I call translateForward(), the camera moves to the left of the direction the camera is facing, and will only move vertically if the camera is facing down the Z axis. What I posted was me fiddling around until it worked as I expected it to :\.

On second thought, the way I keep track of the position is working fine because when I rotate the camera using this method:
 
void FreeLookCamera::rotate(const glm::vec3 &vec) {
    mRotation += vec;

    mViewMatrix = glm::rotate(glm::mat4(1.f), mRotation.z, glm::vec3(0.f, 0.f, 1.f));
    mViewMatrix = glm::rotate(mViewMatrix, mRotation.x, glm::vec3(1.f, 0.f, 0.f));
    mViewMatrix = glm::rotate(mViewMatrix, mRotation.y, glm::vec3(0.f, 1.f, 0.f));
    mViewMatrix = glm::translate(mViewMatrix, mPosition);
}

I start with a blank matrix, and apply the translation at the end based on the position I've accumulated in translateForward and the camera doesn't jump around at all.


I think math just hates my existence lol Edited by CirdanValen
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