Thread: Problem multiplying rotation matrices together

  1. #16
    Registered User rahaydenuk's Avatar
    Join Date
    Jul 2002
    Posts
    69
    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,
    Richard Hayden. ([email protected])
    Webmaster: http://www.dx-dev.com
    DXOS (My Operating System): http://www.dx-dev.com/dxos

    PGP: 0x779D0625

  2. #17
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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);
    Last edited by Silvercord; 03-01-2003 at 06:27 PM.

  3. #18
    Registered User rahaydenuk's Avatar
    Join Date
    Jul 2002
    Posts
    69
    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,
    Richard Hayden. ([email protected])
    Webmaster: http://www.dx-dev.com
    DXOS (My Operating System): http://www.dx-dev.com/dxos

    PGP: 0x779D0625

  4. #19
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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. #20
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    bitmaps

  6. #21
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    and finally the model

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  2. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  3. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  4. Quaternion Rotation
    By psychopath in forum Game Programming
    Replies: 16
    Last Post: 07-23-2006, 03:28 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM