Results 1 to 2 of 2

Thread: Billboard facing the camera has wrong rotation near 180 degrees

  1. #1
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    4

    Billboard facing the camera has wrong rotation near 180 degrees

    I've implemented a particle system. I'm drawing their textures on billboards that should be rotated towards the camera.

    This works fine except for the case when the angle between particle->camera and the normal comes near to 180 degrees. Then the particle starts rotating around itself many times.

    The angle is calculated using cos(angle) = dot(a, b) / (length(a) * length(b), the length are both 1 cause the Vectors are normalized. The axis is calculated using the cross product of those two vectors.




    glDisable(GL_CULL_FACE);

    //calculate rotation
    Vector3f normal =newVector3f(0,0,1);
    Vector3f dir =Vector3f.sub(newVector3f(GraphicsData.camera.x,GraphicsData.camera.y,GraphicsData.camera.z),newVector3f(x, y, z),null);
    if(dir.length()==0)
    {
    glEnable
    (GL_CULL_FACE);
    return;
    }
    dir
    =(Vector3f) dir.normalise();
    float angle =(float)Math.toDegrees(Math.acos(Vector3f.dot(normal, dir)));
    Vector3f rotationAxis =Vector3f.cross(normal, dir,null);
    rotationAxis
    =(Vector3f) rotationAxis.normalise();
    System.out.println("Angle: + "+ angle +" Axis: "+ rotationAxis);


    glBindTexture
    (GL_TEXTURE_2D,ParticleEngine.particleTextures.get(typeId).texture.getTextureID());


    glColor4f
    (1f,1f,1f, time >= lifeTime - decayTime ?((float)lifeTime -(float)time)/((float)lifeTime -(float)decayTime):1f);
    shaderEngine
    .createModelMatrix(newVector3f(x, y, z),newVector3f(angle * rotationAxis.x, angle * rotationAxis.y, angle * rotationAxis.z),newVector3f(sx, sy, sz));
    shaderEngine
    .loadModelMatrix(shaderEngine.particle);
    glCallList
    (ParticleEngine.particleTextures.get(typeId).displayListId + textureIndex);

    glEnable
    (GL_CULL_FACE);





    What am i doing wrong calculating the rotation?




    publicstaticvoid createModelMatrix(Vector3f pos,Vector3f rot,Vector3f scale)
    {
    GraphicsData.camera.modelMatrix =newMatrix4f();
    GraphicsData.camera.modelMatrix.setIdentity();
    GraphicsData.camera.modelMatrix.translate(pos);
    GraphicsData.camera.modelMatrix.rotate((float)Math.toRadians(rot.x),newVector3f(1,0,0));
    GraphicsData.camera.modelMatrix.rotate((float)Math.toRadians(rot.y),newVector3f(0,1,0));
    GraphicsData.camera.modelMatrix.rotate((float)Math.toRadians(rot.z),newVector3f(0,0,1));
    GraphicsData.camera.modelMatrix.scale(scale);
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,111
    Quote Originally Posted by Geosearchef View Post
    The angle is calculated using cos(angle) = dot(a, b) / (length(a) * length(b), the length are both 1 cause the Vectors are normalized. The axis is calculated using the cross product of those two vectors.
    You shouldn't be trying to calculate the angle. Just generate the matrix directly from the vectors. Better still, just render the particles in eye-space rather than trying to generate a model matrix which, aside from the translation, is the inverse of the view matrix.

    More generally, if you ever find yourself wanting to use acos(), look for another solution, preferably without involving trigonometry. E.g. If you're going to pass that angle (or something derived from it) to sin/cos/tan, use trigonometric identities to eliminate both the inverse and forward trigonometric functions.

Similar Threads

  1. Camera turning starting from 90 degrees
    By tati987 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-06-2017, 12:37 PM
  2. How to locate camera facing down with gluLookAt equivalent?
    By theateist in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 08-01-2016, 05:12 PM
  3. Can a quaternion represent more than 360 degrees of rotation?
    By openlearner in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-21-2013, 08:58 AM
  4. Move view or camera in 6 degrees of freedom
    By FoX_*D i E* in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-12-2010, 01:43 AM
  5. newbie question: camera isn't facing correctly?
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-26-2003, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean