Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


#ActualBornToCode

Posted 23 June 2013 - 10:51 AM

You know the object position  and you know the camera position. So what you need to do is recompute the right up and look vector of the object based on the camera position relative to the object. That way the object will always look at the camera no matter what the camera position is. You do not need to mock around with the vertices position at all. I have attached a piece of code from my engine that will do just that. If you have any question, please feel free to ask. Another thing to know is that my matrix is row major. So your basic Axis's are row based not column based.

 

  //**************************************************
  //Build an billboard matrix.
  //Using your own up vector.
  //**************************************************
  Matrix4x4 Matrix4x4::BillboardLookAt(const Vector3& objectPosition,const Vector3& target,const Vector3& up)
  {
    Vector3 lForward = (target-objectPosition).normalized();
    Vector3 lRight = Vector3::Cross(up,lForward).normalized();
    Vector3 lUp = Vector3::Cross(lForward,lRight).normalized();
    return Matrix4x4::From(lRight,lUp,lForward,Vector3::one,objectPosition);
  }

 

  //**************************************************
  //Build an matrix from a given set of vectors.
  //**************************************************
  Matrix4x4 Matrix4x4::From(const Vector3& right,const Vector3& up,const Vector3& forward,const Vector3& scale,const Vector3& position)
  {
    Matrix4x4 m = Matrix4x4::identity;
    m.Array[0]=right.x*scale.x;
    m.Array[1]=right.y*scale.x;
    m.Array[2]=right.z*scale.x;
    m.Array[4]=up.x*scale.y;
    m.Array[5]=up.y*scale.y;
    m.Array[6]=up.z*scale.y;
    m.Array[8]=forward.x*scale.z;
    m.Array[9]=forward.y*scale.z;
    m.Array[10]=forward.z*scale.z;
    m.Array[12]=position.x;
    m.Array[13]=position.y;
    m.Array[14]=position.z;
    return m;
  }


#3BornToCode

Posted 23 June 2013 - 10:48 AM

You know the object position  and you know the camera position. So what you need to do is recompute the right up and look vector of the object based on the camera position relative to the object. That way the object will always look at the camera no matter what the camera position is. You do not need to mock around with the vertices position at all. I have attached a piece of code from my engine that will do just that. If you have any question, please feel free to ask.

 

  //**************************************************
  //Build an billboard matrix.
  //Using your own up vector.
  //**************************************************
  Matrix4x4 Matrix4x4::BillboardLookAt(const Vector3& objectPosition,const Vector3& target,const Vector3& up)
  {
    Vector3 lForward = (target-objectPosition).normalized();
    Vector3 lRight = Vector3::Cross(up,lForward).normalized();
    Vector3 lUp = Vector3::Cross(lForward,lRight).normalized();
    return Matrix4x4::From(lRight,lUp,lForward,Vector3::one,objectPosition);
  }

 

  //**************************************************
  //Build an matrix from a given set of vectors.
  //**************************************************
  Matrix4x4 Matrix4x4::From(const Vector3& right,const Vector3& up,const Vector3& forward,const Vector3& scale,const Vector3& position)
  {
    Matrix4x4 m = Matrix4x4::identity;
    m.Array[0]=right.x*scale.x;
    m.Array[1]=right.y*scale.x;
    m.Array[2]=right.z*scale.x;
    m.Array[4]=up.x*scale.y;
    m.Array[5]=up.y*scale.y;
    m.Array[6]=up.z*scale.y;
    m.Array[8]=forward.x*scale.z;
    m.Array[9]=forward.y*scale.z;
    m.Array[10]=forward.z*scale.z;
    m.Array[12]=position.x;
    m.Array[13]=position.y;
    m.Array[14]=position.z;
    return m;
  }


#2BornToCode

Posted 23 June 2013 - 10:47 AM

You know the object position  and you know the camera position. So what you need to do is recompute the right up and look vector of the object based on the camera position relative to the object. That way the object will always look at the camera no matter what the camera position is. You do not need to mock around with the vertices position at all. I have attached a piece of code from my engine that will do just that. It will return to you an matrix that contains the new right up and look vector. Just ignore the translation part of it. Since all you care about is the relation based on the orientation.

 

  //**************************************************
  //Build an billboard matrix.
  //Using your own up vector.
  //**************************************************
  Matrix4x4 Matrix4x4::BillboardLookAt(const Vector3& objectPosition,const Vector3& target,const Vector3& up)
  {
    Vector3 lForward = (target-objectPosition).normalized();
    Vector3 lRight = Vector3::Cross(up,lForward).normalized();
    Vector3 lUp = Vector3::Cross(lForward,lRight).normalized();
    return Matrix4x4::From(lRight,lUp,lForward,Vector3::one,objectPosition);
  }

 

  //**************************************************
  //Build an matrix from a given set of vectors.
  //**************************************************
  Matrix4x4 Matrix4x4::From(const Vector3& right,const Vector3& up,const Vector3& forward,const Vector3& scale,const Vector3& position)
  {
    Matrix4x4 m = Matrix4x4::identity;
    m.Array[0]=right.x*scale.x;
    m.Array[1]=right.y*scale.x;
    m.Array[2]=right.z*scale.x;
    m.Array[4]=up.x*scale.y;
    m.Array[5]=up.y*scale.y;
    m.Array[6]=up.z*scale.y;
    m.Array[8]=forward.x*scale.z;
    m.Array[9]=forward.y*scale.z;
    m.Array[10]=forward.z*scale.z;
    m.Array[12]=position.x;
    m.Array[13]=position.y;
    m.Array[14]=position.z;
    return m;
  }


#1BornToCode

Posted 23 June 2013 - 10:45 AM

You know the object position  and you know the camera position. So what you need to do is recompute the right up and look vector of the object based on the camera position relative to the object. That way the object will always look at the camera no matter what the camera position is. You do not need to mock around with the vertices position at all.

I have attached a peace of code from my math library that will automatically compute an Quaternion that gives you the orientation the object needs to be in to face another object. If you have any other question, please feel free to ask.

 

  //**************************************************
  //Build an billboard matrix.
  //Using your own up vector.
  //**************************************************
  Matrix4x4 Matrix4x4::BillboardLookAt(const Vector3& objectPosition,const Vector3& target,const Vector3& up)
  {
    Vector3 lForward = (target-objectPosition).normalized();
    Vector3 lRight = Vector3::Cross(up,lForward).normalized();
    Vector3 lUp = Vector3::Cross(lForward,lRight).normalized();
    return Matrix4x4::From(lRight,lUp,lForward,Vector3::one,objectPosition);
  }

 

  //**************************************************
  //Build an matrix from a given set of vectors.
  //**************************************************
  Matrix4x4 Matrix4x4::From(const Vector3& right,const Vector3& up,const Vector3& forward,const Vector3& scale,const Vector3& position)
  {
    Matrix4x4 m = Matrix4x4::identity;
    m.Array[0]=right.x*scale.x;
    m.Array[1]=right.y*scale.x;
    m.Array[2]=right.z*scale.x;
    m.Array[4]=up.x*scale.y;
    m.Array[5]=up.y*scale.y;
    m.Array[6]=up.z*scale.y;
    m.Array[8]=forward.x*scale.z;
    m.Array[9]=forward.y*scale.z;
    m.Array[10]=forward.z*scale.z;
    m.Array[12]=position.x;
    m.Array[13]=position.y;
    m.Array[14]=position.z;
    return m;
  }


PARTNERS