Thread: 3D rotation problem, help please!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I just think you should have done some more thinking about glPushMatrix(), glPopMatrix(), and glGetFloatv() -- which with the last one you can extract the current values of the matrix -- before some hoodwinking hooligan got you onto this 19th century math voyage. Which I'm sure it is interesting, but I will not be riding in your spaceship even if you let me (no offense).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay I got it to work. The idea here is to use a 6 element float array to represent the current state of an object, which I'm using a cube (eight sides!), not a spaceship:
    Code:
    typedef struct {
            float x;
            float y;
            float z;
            float xR;
            float yR;
            float zR;
    } Thing;
    
    Thing Cube={0.0f}  /* we begin at the origin */
    I have two functions to deal with Cube, both of which get called in my glutDisplayFunc() if you want to learn with glut. Anyway, the second one builds a cube, bells, whistles, textures, rocket launchers, what have you, using the Cube coordinates. The first one is what's important here, because it controls the position of the cube:
    Code:
    void movecube () {      /* think borg ;) */
            float trix[16];
            glPushMatrix(); /* reserve current state */
    
            glTranslatef(Cube.x,Cube.y,Cube.z);      /* move to   */
            glRotatef(Cube.xR, 1.0f, 0.0f, 0.0f);    /* where we  */
            glRotatef(Cube.yR, 0.0f, 1.0f, 0.0f);    /* should be */
            glRotatef(Cube.zR, 0.0f, 0.0f, 1.0f);      /* INCLUDE ROTATION */
    
            glTranslatef(0.0f,0.0f,0.5f);   /* now move FORWARD slowly... */
    
            glGetFloatv(GL_MODELVIEW_MATRIX,trix);
            Cube.x=trix[12];        /* this saves the new position of the Cube */
            Cube.y=trix[13];
            Cube.z=trix[14];
    
            glPopMatrix();  /* restore state (IMPORTANT) */
    }
    Like I said, this gets called before the routine which uses the Cube.elements to draw the cube in the right place.

    You control the xR, yR, zR with arrow keys + pgup/pgdown (glut handles this mostly). You have no choice about the acceleration, it is constant (nice simple demo), but it is relative to the cube; ie, it always moves front face forward, so you can steer it around all three dimensions (and notice, the forward is always because of a Z translation, just what you wanted!!).

    So it works fine. I can keep it spiralling around the viewport perpetually, but of course steering an object you are watching from the ground is tricky (get the camera to follow: next step). Sorry there are no quaternions.

    ps. That is some crazy C++ tish found by the OP! I thot it was supposed to be better! "Object oriented" AHAHAHA!!
    Last edited by MK27; 03-08-2009 at 08:50 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. rotation in 3d
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 11-04-2003, 10:06 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 07:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM