Thread: Question about a game

  1. #1
    TheGr8one
    Guest

    Question about a game

    Hello,
    Im trying to make a top view space ship game in OpenGL that when you press left and right the ship rotates and when you press up the ship goes in the direction that it is pointing. The problem im have is figuring out a way to rotate the ship but also figure out which direction the ship should go when the player presses up to go where its pointing. I was going to just use glRotatef but that just rotates the ship and doesnt keep track of where the ship is pointing. Anyone know a way to do this?

    Thanks
    Chris

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    im not sure this will work but it should.
    I asume a 2D space and rotation is preformed on the y axis.

    rotate the vector and then translate it like the following
    NOTE: this permanently rotates the vector.

    Code:
    #include <math.h>
    #include <string.h>
    // rotation matrix
    roty [4][4];
    // translation matrix
    tran [4][4];
    
    // this will rotate vector and replace the values in vector with the new rotated ones
    
    void rotate(float vector[4], float angle)
    {
        floar rotvect[4] = { 0.0,0.0,0.0,0.0 };
    
        memset(roty[0],0,4);
        memset(roty[1],0,4);
        memset(roty[2],0,4);
        memset(roty[3],0,4);
    
    
    // setup the matrix for y rotation
        roty[0][0] = cos(angle);
        roty[0][2] = sin(angle);
        roty[1][1] = 1;
        roty[2][0] = -sin(angle);
        roty[2][1] = cos(angle);
        roty[3][3] = 1;
    
    
    // rotate the vector
        rotvect[0] = (vector[0] * roty[0][0]) + (vector[1] * roty[0][1]) + (vector[2] * roty[0][2])  + (vector[3] * roty[0][3]);
        rotvect[1] = (vector[0] * roty[1][0]) + (vector[1] * roty[1][1]) + (vector[2] * roty[1][2])  + (vector[3] * roty[1][3]);
        rotvect[2] = (vector[0] * roty[2][0]) + (vector[1] * roty[2][1]) + (vector[2] * roty[2][2])  + (vector[3] * roty[2][3]);
        rotvect[3] = (vector[0] * roty[3][0]) + (vector[1] * roty[3][1]) + (vector[2] * roty[3][2])  + (vector[3] * roty[3][3]);
    
        memcpy(vector,rotvect,4);
    
    }
    
    void translate(float vector[4],float dx,float dy,float dz)
    {
    
        floar tvect[4] = { 0.0,0.0,0.0,0.0 };
    
    
        memset(tran[0],0,4);
        memset(tran[1],0,4);
        memset(tran[2],0,4);
        memset(tran[3],0,4);
    
        tran[0][0] = 1;
        tran[0][3] = dx;
        tran[1][1] = 1;
        tran[1][3] = dy;
        tran[2][2] = 1;
        tran[2][3] = dz;
        tran[3][3] = 1;
    
    // translate the vector
        tvect[0] = (vector[0] * tran[0][0]) + (vector[1] * tran[0][1]) + (vector[2] * tran[0][2])  + (vector[3] * tran[0][3]);
        tvect[1] = (vector[0] * tran[1][0]) + (vector[1] * tran[1][1]) + (vector[2] * tran[1][2])  + (vector[3] * tran[1][3]);
        tvect[2] = (vector[0] * tran[2][0]) + (vector[1] * tran[2][1]) + (vector[2] * tran[2][2])  + (vector[3] * tran[2][3]);
        tvect[3] = (vector[0] * tran[3][0]) + (vector[1] * tran[3][1]) + (vector[2] * tran[3][2])  + (vector[3] * tran[3][3]);
    
        memcpy(vector,tvect,4);
    
    }
    
    draw()
    {
        float vert[4] = { 1.0,0.0,2.0,1.0 };
        // rotate vert 60 degrees
        rotate(vert,60);
        // translate it .1 x and .2 unit z unit
        translate(vert,.1,0.0,.2);
        // draw it.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  3. Question on making game.
    By derrick45123 in forum C++ Programming
    Replies: 10
    Last Post: 06-19-2005, 05:12 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM
  5. Question on sound in a game of mine
    By Vanished in forum Game Programming
    Replies: 2
    Last Post: 12-09-2002, 01:56 PM