# Thread: Problem multiplying rotation matrices together

1. Originally posted by Silvercord
MiddleX and MiddleY are the screen width and height divided by two. it is the center of the screen in screen coords. note that when using getcursorpos and setcursorpos those screen coordinates are setup like a regular cartesian system. the point 0,0 is actually in the middle of the screen. for everything else I think the positive y goes down the screen and 0, 0 is the top left corner.
SetCursorPos(MiddleX, MiddleY); //sets cursor to exact middle of screen
Code:
```	static float MiddleX = SCREEN_WIDTH / 2;
static float MiddleY = SCREEN_HEIGHT / 2;```
SCREEN_WIDTH and SCREEN_HEIGHT are #define'd compiler directives passed into the function that sets up the resolution.

If there is anything else that I didn't explain good enough please ask me.

Code:
```#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768```
I'm not sure exactly what your code is trying to do, so I've written a quick snippet showing how I'd do it (this would be repeated each time the scene is rendered).

I'm assuming as you said that the mouse cursor co-ordinate system is cartesian in nature with the +y-axis running up the screen, and the +x-axis running to the right.

Code:
```float yaw = 0.0f, pitch = 0.0f;
float oldxpos = 0.0f, oldypos = 0.0f;
float xdisp, ydisp;

//all we are concerned with is the mouse's displacement since last render
xdisp = MousePos.x - oldxpos;
ydisp = MousePos.y - oldypos;

yaw += -(xdisp * c); //use -ve as rotations are anti-clockwise
pitch += -(ydisp * c);

//you may want to add a couple of simple loops
//here to make the yaw and the pitch modulus 2*pi.
//This would just prevent the values of yaw and pitch from
//getting too large (or too small) for the float type.

//store positions of mouse for next render
oldxpos = MousePos.x;
oldypos = MousePos.y;

//You then need to transform the position of all objects (rotate them) in your scene through the following:
object.x = object.x * cos(yaw) + object.y * sin(yaw) * sin(pitch) + object.z * sin(yaw) * cos(pitch);
object.y = object.y * cos(pitch) - object.z * sin(pitch);
object.z = -object.x * sin(yaw) + object.y * cos(yaw) * sin(pitch) + object.z * cos(yaw) * cos(pitch);```
The value 'c' in the above code is a constant, which you will need to experiment with. It depends on many things such as screen size and your personal feeling as to how fast the rotation should be. Try something like 0.003 to start. You should then maybe form a formula for 'c', which uses the screen size, so as to account for running this application on different size screens. This should all be possible by trial and error.

The last part of the code, which refers to some structure 'object' should naturally be repeated for all objects in your scene.

You must also remember that you need to implement a projection transformation to map the 3D scene on to your 2D screen, and you would also need another translation transform if you intend to move the camera.

Good luck,

Regards,

2. I am getting frustrated, everything I do doesn't actually work. I'm plugging into an application that previously utilized rotation about the Y axis. When I try plugging in the equations for combining the X and Y rotation matrices, it does not work as it should. Can you look at this code and tell me if anything does not look right? If there is nothing wrong here, then I will simply look elsewhere in the code for the source of the problem.

Code:
```	RotDir.x = (Dir.x * cosYTheta) + (Dir.z * cosXTheta) + (Dir.y * sinYTheta * sinXTheta);
RotDir.y = (Dir.y * cosXTheta) - (Dir.z * sinXTheta);
RotDir.z = (Dir.z * cosYTheta * cosXTheta) - (Dir.x * sinYTheta * sinXTheta);```

3. Originally posted by Silvercord
I am getting frustrated, everything I do doesn't actually work. I'm plugging into an application that previously utilized rotation about the Y axis. When I try plugging in the equations for combining the X and Y rotation matrices, it does not work as it should. Can you look at this code and tell me if anything does not look right? If there is nothing wrong here, then I will simply look elsewhere in the code for the source of the problem.

Code:
```	RotDir.x = (Dir.x * cosYTheta) + (Dir.z * cosXTheta) + (Dir.y * sinYTheta * sinXTheta);
RotDir.y = (Dir.y * cosXTheta) - (Dir.z * sinXTheta);
RotDir.z = (Dir.z * cosYTheta * cosXTheta) - (Dir.x * sinYTheta * sinXTheta);```
No, it should be:

Code:
```object.x = object.x * cos(y) + object.y * sin(y) * sin(x) + object.z * sin(y) * cos(x);
object.y = object.y * cos(x) - object.z * sin(x);
object.z = -object.x * sin(y) + object.y * cos(y) * sin(x) + object.z * cos(y) * cos(x);```
Where y is the angle about the y-axis and x is the angle about the x-axis.

Regards,

4. I have finally semi-gotten this to work. I am sorry to say that I did not get it working with combining the rotations about the X and Y axes. Instead I rotated the view about the current strafe vector then added the rotation about the Y axis. This works when everything is in the positive direction, but as soon as I turn around and face the camera everything gets all screwed up. Do I need to cap the amount that I can rotate in a single scene or something, or is there just something flawed with my application that I am un aware of? I guess I need to just keep experimenting with this until it's perfected.

I'm including the binary with all of the necessary data files (I have to break it up across multiple zip files). Just make sure you put everything in the same folder before you run the executable. I'm not including the source right now (too much stuff at once).

5. bitmaps

6. and finally the model