Jump to content
• Advertisement

# glitch in the matrix

This topic is 3992 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

##### Share on other sites
Advertisement
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

##### 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

##### Share on other sites
so would i only have to multiply the vector by one matrix to get the 2d screen coordinate?

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12
• Advertisement

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013222
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!