Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why is this a function on distance and not angle?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 CirdanValen   Members   -  Reputation: 174

Like
0Likes
Like

Posted 13 January 2013 - 01:12 PM

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.

Sponsor:

#2 KulSeran   Members   -  Reputation: 2154

Like
0Likes
Like

Posted 14 January 2013 - 04:52 AM

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);


#3 EWClay   Members   -  Reputation: 655

Like
0Likes
Like

Posted 14 January 2013 - 05:13 AM

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.

#4 CirdanValen   Members   -  Reputation: 174

Like
0Likes
Like

Posted 14 January 2013 - 03:05 PM

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);
}


#5 KulSeran   Members   -  Reputation: 2154

Like
0Likes
Like

Posted 14 January 2013 - 10:23 PM

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;


#6 0r0d   Members   -  Reputation: 797

Like
0Likes
Like

Posted 14 January 2013 - 11:21 PM

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.



#7 KulSeran   Members   -  Reputation: 2154

Like
0Likes
Like

Posted 15 January 2013 - 11:34 AM

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)

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.



#8 CirdanValen   Members   -  Reputation: 174

Like
0Likes
Like

Posted 21 January 2013 - 08:55 PM

 
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, 21 January 2013 - 08:56 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS