wojownik266 Temat założony przez niniejszego użytkownika |
[OpenGL] Kolizja kwadratu z kwadratem » 2011-10-18 14:05:18 Poniżej prezentuję kolizję kwadratu z kwadratem, która to kolizja działa bardzo dobrze. Problem w tym że jak włacze ten kod do gry 2D którą tworze pod opengl to kod odmawia posłuszeństwa (osobno działa ok). Czy ktoś mógłby rzucić okiem na ten kod i powiedzieć co jest źle? Dodam jeszcze że w ostateczny programie oba kwadraty są w ruchu, może dlatego program nie działa.Hm!?
#include <windows.h>
#include <gl\gl.h> #include <gl\glu.h>
char className[] = { "rectangle_class" }; char wndCaption[] = { "rectangle" };
LRESULT CALLBACK WndMsgProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
BOOL keys[ 256 ];
struct _player { float x, y; float width, height; } player;
struct _rect { float x, y; float width, height; } rect;
void ProcessKeys(); void StartEngine(); void DoEngine();
int screen_width = 800; int screen_height = 600; int screen_bits = 16; int screen_frequency = 75; BOOL full_screen = FALSE;
HGLRC hRC; HDC hDC;
void SetupPixelFormat( HDC hDC ); void StartWGL( HWND hWnd ); void StopWGL(); void SetFullScreen(); void SetupRC(); void ChangeSize( int width, int height ); void Render();
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { WNDCLASSEX wc; HWND hWnd; MSG Msg; int style; ZeroMemory( & wc, sizeof( WNDCLASSEX ) ); wc.cbSize = sizeof( WNDCLASSEX ); wc.style = CS_HREDRAW | CS_VREDRAW; wc.hInstance = hInstance; wc.lpfnWndProc = WndMsgProc; wc.hbrBackground =( HBRUSH )( COLOR_WINDOW ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hIconSm = wc.hIcon; wc.lpszClassName = className; wc.lpszMenuName = NULL; wc.cbClsExtra = 0; wc.cbWndExtra = 0; RegisterClassEx( & wc ); if( full_screen ) { SetFullScreen(); style = WS_POPUP; } else { style = WS_OVERLAPPEDWINDOW; } hWnd = CreateWindowEx( 0, className, wndCaption, style, 0, 0, screen_width, screen_height, NULL, NULL, hInstance, NULL ); ShowWindow( hWnd, nShowCmd ); while( 1 ) { if( PeekMessage( & Msg, 0, 0, 0, PM_REMOVE ) ) { if( Msg.message == WM_QUIT ) { break; } TranslateMessage( & Msg ); DispatchMessage( & Msg ); } else { ProcessKeys(); DoEngine(); Render(); SwapBuffers( hDC ); } } return 0; }
LRESULT CALLBACK WndMsgProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) { switch( Msg ) { case WM_CREATE: StartWGL( hWnd ); SetupRC(); StartEngine(); break; case WM_DESTROY: StopWGL(); PostQuitMessage( 0 ); break; case WM_SIZE: { int width = LOWORD( lParam ); int height = HIWORD( lParam ); ChangeSize( width, height ); } break; case WM_KEYDOWN: { keys[ wParam ] = TRUE; } break; case WM_KEYUP: { keys[ wParam ] = FALSE; } break; } return DefWindowProc( hWnd, Msg, wParam, lParam ); }
void SetupPixelFormat( HDC hDC ) { int pixelFormat; static PIXELFORMATDESCRIPTOR pfd = { sizeof( PIXELFORMATDESCRIPTOR ), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; pixelFormat = ChoosePixelFormat( hDC, & pfd ); SetPixelFormat( hDC, pixelFormat, & pfd ); }
void StartWGL( HWND hWnd ) { hDC = GetDC( hWnd ); SetupPixelFormat( hDC ); hRC = wglCreateContext( hDC ); wglMakeCurrent( hDC, hRC ); }
void StopWGL() { wglMakeCurrent( hDC, NULL ); wglDeleteContext( hRC ); }
void SetFullScreen() { DEVMODE screen = { 0 }; screen.dmSize = sizeof( DEVMODE ); screen.dmPelsWidth = screen_width; screen.dmPelsHeight = screen_height; screen.dmBitsPerPel = screen_bits; screen.dmDisplayFrequency = screen_frequency; screen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY; ChangeDisplaySettings( & screen, CDS_FULLSCREEN ); }
void ProcessKeys() { if( keys[ VK_LEFT ] || keys[ 'A' ] ) player.x -= 0.1f; if( keys[ VK_RIGHT ] || keys[ 'D' ] ) player.x += 0.1f; if( keys[ VK_UP ] || keys[ 'W' ] ) player.y += 0.1f; if( keys[ VK_DOWN ] || keys[ 'S' ] ) player.y -= 0.1f; }
void StartEngine() { player.x = 0.0f; player.y = 0.0f; player.width = 3.0; player.height = 3.0; rect.x = 0.0f; rect.y = 0.0f; rect.width = 3.0f; rect.height = 3.0f; }
void DoEngine() { if(( player.x < rect.x + rect.width / 2 + 0.1f ) &&( player.x > rect.x ) &&( player.y < rect.y + rect.height / 2 ) &&( player.y > rect.y - rect.height / 2 ) ) player.x = rect.x + rect.width / 2 + 0.1f; if(( player.x > rect.x - rect.width / 2 - 0.1f ) &&( player.x < rect.x ) &&( player.y < rect.y + rect.height / 2 ) &&( player.y > rect.y - rect.height / 2 ) ) player.x = rect.x - rect.width / 2 - 0.1f; if(( player.y < rect.y + rect.height / 2 + 0.1f ) &&( player.y > rect.y ) &&( player.x < rect.x + rect.width / 2 ) &&( player.x > rect.x - rect.width / 2 ) ) player.y = rect.y + rect.height / 2 + 0.1f; if(( player.y > rect.y - rect.height / 2 - 0.1f ) &&( player.y < rect.y ) &&( player.x < rect.x + rect.width / 2 ) &&( player.x > rect.x - rect.width / 2 ) ) player.y = rect.y - rect.height / 2 - 0.1f; }
void SetupRC() { glEnable( GL_DEPTH_TEST ); }
void ChangeSize( int width, int height ) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60.0f,( float ) width /( float ) height, 1.0f, 1000.0f ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); }
void renderPlayer() { glPushMatrix(); glTranslatef( player.x, player.y, 0.0 ); glBegin( GL_QUADS ); glVertex3f( player.x - player.width / 2, player.y - player.height / 2, 0.0f ); glVertex3f( player.x + player.width / 2, player.y - player.height / 2, 0.0f ); glVertex3f( player.x + player.width / 2, player.y + player.height / 2, 0.0f ); glVertex3f( player.x - player.width / 2, player.y + player.height / 2, 0.0f ); glEnd(); glPopMatrix(); }
void renderRectangle() { glPushMatrix(); glBegin( GL_QUADS ); glVertex3f( rect.x - rect.width / 2, rect.y - rect.height / 2, 0.0f ); glVertex3f( rect.x + rect.width / 2, rect.y - rect.height / 2, 0.0f ); glVertex3f( rect.x + rect.width / 2, rect.y + rect.height / 2, 0.0f ); glVertex3f( rect.x - rect.width / 2, rect.y + rect.height / 2, 0.0f ); glEnd(); glPopMatrix(); }
void Render() { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glLoadIdentity(); gluLookAt( 0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f ); glColor3f( 1.0f, 0.0f, 0.0f ); renderPlayer(); glColor3f( 1.0f, 1.0f, 1.0f ); renderRectangle(); } |