Sign in to follow this  

glitch in the matrix

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just implemented a function(in java) which converts a point in 3d space into a 2d point to be projected on screen the problem is that every time the camera location goes beyond a point it displays a sort of mirror image of this point. heres the code I use:
   public static Point convert(Point point, Camera camera) {

      ////////////////////////////////////////////////////////////////////////////
      double pointMatrixD[][] = { { point.x },
                                  { point.y },
                                  { point.z } };
      Matrix pointMatrix = new Matrix(pointMatrixD);

      ////////////////////////////////////////////////////////////////////////////
      double camPointMatrixD[][] = { { camera.location.x }, 
                                     { camera.location.y },
                                     { camera.location.z } };
      Matrix camPointMatrix = new Matrix(camPointMatrixD);

      //////////////////////////////////////////////////////////////////////////// the x rotation matrix
      double xRotationMatrixD[][] = { { 1,                            0,                           0 },
                                      { 0,  Ops.cos(-camera.rotation.x), Ops.sin(-camera.rotation.x) },
                                      { 0, -Ops.sin(-camera.rotation.x), Ops.cos(-camera.rotation.x) } };
      Matrix xRotationMatrix = new Matrix(xRotationMatrixD);
      //////////////////////////////////////////////////////////////////////////// the y rotation matrix
      double yRotationMatrixD[][] = { { Ops.cos(-camera.rotation.y), 0, -Ops.sin(-camera.rotation.y) },
                                      {                           0, 1,                            0 },
                                      { Ops.sin(-camera.rotation.y), 0,  Ops.cos(-camera.rotation.y) } };
      Matrix yRotationMatrix = new Matrix(yRotationMatrixD);
      //////////////////////////////////////////////////////////////////////////// the z rotation matrix
      double zRotationMatrixD[][] = { {  Ops.cos(-camera.rotation.z), Ops.sin(-camera.rotation.z), 0 },
                                      { -Ops.sin(-camera.rotation.z), Ops.cos(-camera.rotation.z), 0 },
                                      {                            0,                           0, 1 } };
      Matrix zRotationMatrix = new Matrix(zRotationMatrixD);
      ////////////////////////////////////////////////////////////////////////////

      Matrix translatedPointMatrix = null;
      try {
         /* translates the point from its coordinate system into the cameras coordinate system */
         translatedPointMatrix = xRotationMatrix.multiply(
                                    yRotationMatrix.multiply(
                                       zRotationMatrix.multiply(
                                          pointMatrix.subtract(camPointMatrix)
                                       )
                                    )
                                 );
      } catch(Exception e) {
       //  System.out.println(e.getMessage());
      }

      /* simply translates the matrix to a point */
      Point translatedPoint = new Point(translatedPointMatrix.get(0, 0),
                                        translatedPointMatrix.get(1, 0),
                                        translatedPointMatrix.get(2, 0));


      /* calculates and returns the final projected point knowing the distance from the camera(camera.viewerPosition) */
      return new Point( (translatedPoint.x - camera.viewerPosition.x) * (camera.viewerPosition.z / translatedPoint.z),
                        (translatedPoint.y - camera.viewerPosition.y) * (camera.viewerPosition.z / translatedPoint.z));

   }

the camera class just defines the location of the camera(x, y, z), the rotation(x, y, z) and the field of view(viewerPosition)(x, y, z) which is usally somthing like(0, 0, -1000). I know this probaly isn't the most efficient way of converting a point but it does work. any ideas why its displaying the inverted point if the camera is pointed in the opposite direction?

Share this post


Link to post
Share on other sites
You probably get the inversion when the point is actually behind the camera from the cameras point of view. In that case the point should actually not be visible at all.

The normal way to transform a point by a camera or any other transformation is by a simple matrix multiplication.

p' = TransformationMatrix * p

where TransformationMatrix is a 4x4 matrix and p is a 4-vector with (x, y, z, w) w normally beeing 1. The resulting point lies in clip space where you would do the clipping to the view frustum by comparing x, y, z to w. The final step is the perspective division x/w, y/w, z/w. Then you only need to scale and offset the values to fit into your viewport.

Share this post


Link to post
Share on other sites
I think the transformation matrix you are taking about is just the result of:

translatedPointMatrix = xRotationMatrix.multiply(
yRotationMatrix.multiply(
zRotationMatrix.multiply(
pointMatrix.subtract(camPointMatrix)
)
)
);


but this is the first time i've worked with matrices/3d graphics programming so im not sure.

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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