Skip to main content
gamedev.net
🔒 Locked

Euler Camera, World to Camera

Started by CoiN Sep 6, 2003 at 8:17 AM 1 replies 1.3k views
Original Post
CoiN
CoiN
Lo all, I'm stuck again. Basically my world to camera function isn't working as I think it should. The objects don't rotate properly when the camera is away from the world origin (0,0,0). I think I should moving the camera and objects before I rotate everything, but when I try that the objects warp slightly. It is only and extremely basic wireframe 3D app. I'll post the code for the function below:

void TriPoly3D_World_to_Camera(TriPoly3D *poly, EulerCam *cam, Point3Dh *wc)

{
	int count;
	TriPoly3D test;

	Matrix3x3 matx, maty, matz;
	Matrix3x3 temp;
	Matrix3x3 rotation;

	// Initialise the rotation matrix inverse X axis

	Init_Matrix3x3(&matx, 1, 0,  0,
			      0, cosf((PI * cam->Direction.x) / 180), -sinf((PI * cam->Direction.x) / 180),
			      0, sinf((PI * cam->Direction.x) / 180), cosf((PI * cam->Direction.x) / 180));

	// Initialise the rotation matrix inverse Z axis

	Init_Matrix3x3(&matz, cosf((PI * cam->Direction.z) / 180), -sinf((PI * cam->Direction.z) / 180),  0,
			      sinf((PI * cam->Direction.z) / 180), cosf((PI * cam->Direction.z) / 180),  0,
			      0, 0, 1);

	// Initialise the rotation matrix inverse Y axis

	Init_Matrix3x3(&maty, cosf((PI * cam->Direction.y) / 180), 0, sinf((PI * cam->Direction.y) / 180),
			      0, 1, 0,
			      -sinf((PI * cam->Direction.y) / 180), 0, cosf((PI * cam->Direction.y) / 180));

	// Multiply the rotation matrices together

	MMul_3x3_3x3(&matz, &maty, &temp);
	MMul_3x3_3x3(&temp, &matx, &rotation);

	// Transform the vertices

	for(count = 0; count < 3; count++)
	{
		// Rotate the points

		MMul_3x3_Point3Dh(&rotation, &poly->TVList[count], &poly->TVList[count]);

		// Transform the points to camera co-ords

		poly->TVList[count].x -= cam->Position.x;
		poly->TVList[count].y -= cam->Position.y;
		poly->TVList[count].z -= cam->Position.z;
	}

}

The code is a bit wasteful, but it is only until I get everything working properly. A link to a demo of what I mean is below, you rotate the camera using the up, down arrow keys, and move it side to side with the left and right arrow keys. If you move the camera quite far off center and then rotate, you will see what I mean. It is only about 60-70kb. www.chizief.co.uk/test3d.exe Thanks... Coin [edited by - Coin on September 6, 2003 9:19:59 PM]
CodeJunkie
CodeJunkie
The first thing I would try to do is translate the vertices to the camera space before rotating them to camera space.


// Transform the points to camera co-ords
poly->TVList[count].x -= cam->Position.x;
poly->TVList[count].y -= cam->Position.y;
poly->TVList[count].z -= cam->Position.z;
// Rotate the points
MMul_3x3_Point3Dh(&rotation, &poly->TVList[count], &poly->TVList[count]);

[edit]
Second, use Homogenous coordinates.

[x,y,z,1] tack on a 1

X-Axis Rotation matrix 4x4
1 0 0 0
0 cos r sin r 0
0 -sin r cos r 0
0 0 0 1


[edited by - CodeJunkie on September 6, 2003 10:18:25 PM]
CoiN
CoiN
Thanks for the reply,

When I try transforming the points before rotation, when I do actually rotate around the object it starts to elongate the further around I get. This happens until it basically becomes flat.

Homogenous co-ordinates shouldn''t affect the actual math of the rotation so I will leave it like that for now as I know the rotations work.

It is probably something stupid I am doing, I may just try a UVN camera instead.

Cheers,

Coin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.