Weired results

This is a discussion on Weired results within the Game Programming forums, part of the General Programming Boards category; Hi, I've been looking through stuff on OpenGL for a little while now, and am trying to create my first ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    Weired results

    Hi, I've been looking through stuff on OpenGL for a little while now, and am trying to create my first game, old school PONG. I have gotten everything done with it, collision detection and all, but then I found a flaw that has rendered my project useless.

    I made the ball go on a random trajectory by getting a random number between 0 and 359 degrees. No problem there, except the ball doesn't take the given course.

    I did some experimenting, and found out that all the degree system I was using was lopsided, literally. I set the ball angle manually to 0 degrees, and it went off to the right. Well, I ended up scrapping the game and starting fresh, but not before I find something to correct the problem. Here is the source for a small program I made from a template that is still having the same problem, it draws a line from the center, to supposidly 270 degrees.

    I have no clue what it is, but if I were to put any money on it, it would be in the trig I did to find the ending point of the line.

    Any help is appreciated

    Code:
    // Include headers
    #include <windows.h>
    #include <gl/gl.h>
    #include <math.h>
    
    #define PI 3.14159265
    
    
    // Declare functions
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,		// Windows procedure
    WPARAM wParam, LPARAM lParam);							// Windows procedure contd..
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);	// Initialize OpenGL
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);		// DisableOpenGL
    void draw_scene();										// Draw OpenGL Scene
    
    // Declare global variables
    char class_name[] = "OpenGL 2D Pong";					// Windows class name
    char window_name[] = "Pong";							// Window title name
    int window_width = 500;									// Window width
    int window_height = 500;								// Window height
    
    WNDCLASS wc;											// Windows class handle
    HWND hWnd;												// Window Handle
    HDC hDC;												// Device context handle
    HGLRC hRC;												// Rendering context handle        
    MSG msg;												// Message handle
    bool bQuit = FALSE;										// Quit the loop
    bool keys[256];											// Array for the keypress routine
    
    // Windows Main function
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int iCmdShow)
    {
        // register window class
        wc.style = CS_OWNDC;										// Window style - own decive context
        wc.lpfnWndProc = WndProc;									// Which windows procedure to use
        wc.cbClsExtra = 0;											// Extra parameters, 0
        wc.cbWndExtra = 0;											// Extra parameters, 0
        wc.hInstance = hInstance;									// Program instance handle
        wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);				// Program icon
        wc.hCursor = LoadCursor (NULL, IDC_ARROW);					// Mouse cursor
        wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);		// Background color of our screen
        wc.lpszMenuName = NULL;										// Menu Name
        wc.lpszClassName = class_name;								// Name of our class
        RegisterClass (&wc);										// Register the class
    
        // Create the window
        hWnd = CreateWindow (
          class_name, window_name, 
          WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
          0, 0, window_width, window_height,
          NULL, NULL, hInstance, NULL);
    
        // Enable OpenGL
        EnableOpenGL(hWnd, &hDC, &hRC);
    
        // Main Program Loop
        while (!bQuit)
        {
            // Check for messages in the queue
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                // Handle or dispatch messages
                if (msg.message == WM_QUIT)
                {
                    bQuit = TRUE;
                }
                else
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
            else
            {
    		 	// Draw the OpenGL Scene
    			draw_scene();
            }
        }
    
        // Shutdown OpenGL
        DisableOpenGL(hWnd, hDC, hRC);
    
        // Destroy the window
        DestroyWindow(hWnd);
    
        return msg.wParam;
    }
    
    // Windows Procedure
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
                             WPARAM wParam, LPARAM lParam)
    {
    
        switch (message)
        {
        case WM_CREATE:
            return 0;
    	break;
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
    	break;
    
        case WM_DESTROY:
            return 0;
    	break;
        case WM_KEYDOWN:
    		keys[wParam] = true;
            switch (wParam)
            {
             	case VK_ESCAPE:
                	 PostQuitMessage(0);
                	 return 0;
    			break;
            }
            return 0;
    	break;
    	case WM_KEYUP:
    		 keys[wParam] = false;
    	break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    
    // Enable OpenGL
    void EnableOpenGL(HWND hWnd, HDC *hDC, HGLRC *hRC)
    {
        PIXELFORMATDESCRIPTOR pfd;
        int iFormat;
    
        // Get the device context for the window
        *hDC = GetDC(hWnd);
    
        // Set PFD
        ZeroMemory (&pfd, sizeof(pfd));
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
          PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
        iFormat = ChoosePixelFormat(*hDC, &pfd);
        SetPixelFormat (*hDC, iFormat, &pfd);
    
        // Create the RC and make it current
        *hRC = wglCreateContext(*hDC);
        wglMakeCurrent(*hDC, *hRC);
    
    }
    
    
    // Disable OpenGL
    void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hRC);
        ReleaseDC(hWnd, hDC);
    }
    
    // Draw OpenGL Scene
    void draw_scene()
    {
     	 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	 glClear(GL_COLOR_BUFFER_BIT);
         
    
    	 glPushMatrix();
    	 	glColor3f(0.2f, 1.0f, 0.2f);
    	 	glBegin(GL_LINES);
                //for (int i = 0; i < 360; i++)
                //{
                    glVertex2f(0.0f, 0.0f);
                    glVertex2f(cos((270 * (PI / 180)) * 0.9f), sin((270 * (PI / 180)) * 0.9f));
                //}
    		glEnd();	 			  
    	 glPopMatrix();
    
    	 SwapBuffers(hDC);
    }

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    From what I'm taking...you're trying to draw the line to be 0.9 units long?

    I think you have to move the 0.9f outside of the brackets it is in, i.e.:
    Code:
    glVertex2f(cos((270 * (PI / 180)) * 0.9f), sin((270 * (PI / 180)) * 0.9f));
    becomes
    Code:
    glVertex2f(cos((270 * (PI / 180))) * 0.9f, sin((270 * (PI / 180))) * 0.9f);
    Right now you're multiplying the rotation angle by 0.9, which would be throwing your degrees off....but that change will take the sin/cos of your angle, and then size it appropriately by your 0.9 value.
    Last edited by Epo; 06-07-2005 at 09:27 PM. Reason: It wasn't pretty enough
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    7

    re:

    Wow, what a way to screw up. It works perfectly, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 10:12 AM
  2. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 10:46 AM
  3. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2004, 12:46 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  5. show all compiler results?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2002, 11:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21