/* Copyright (c) 2011 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener adiener@sacredsoftware.net */ #include #include #include #if defined(__APPLE__) #include #include #else #include #endif #if defined(__APPLE__) #include #include #elif defined(__linux) #include #elif defined(WIN32) #include #endif #include "CollisionSystem.h" #include "FixedIntervalRunLoop.h" #define NUM_MOVING_OBJECTS 2 #define NUM_STATIC_OBJECTS 9 static FixedIntervalRunLoop runLoop; static unsigned int windowWidth = 800, windowHeight = 600; static struct movingObject movingObjects[NUM_MOVING_OBJECTS]; static struct staticObject staticObjects[NUM_STATIC_OBJECTS]; double getCurrentTime() { #if defined(__APPLE__) static mach_timebase_info_data_t timebaseInfo; if (timebaseInfo.denom == 0) { mach_timebase_info(&timebaseInfo); } return mach_absolute_time() * (double) timebaseInfo.numer / timebaseInfo.denom * 0.000000001; #elif defined(WIN32) static LARGE_INTEGER frequency; LARGE_INTEGER currentTime; if (frequency.QuadPart == 0) { QueryPerformanceFrequency(&frequency); } QueryPerformanceCounter(¤tTime); return (double) currentTime.QuadPart / frequency.QuadPart; #elif defined(__linux) struct timespec currentTime; clock_gettime(CLOCK_MONOTONIC, ¤tTime); return currentTime.tv_sec + currentTime.tv_nsec * 0.000000001; #endif } #define ACCELERATION 0.0078125f #define DECELERATION 0.015625f #define VELOCITY_MAX 0.125f static void run() { size_t movingObjectIndex; struct movingObject * object; for (movingObjectIndex = 0; movingObjectIndex < NUM_MOVING_OBJECTS; movingObjectIndex++) { object = &movingObjects[movingObjectIndex]; if (object->moveX == 0) { if (object->velocity.x < 0.0f) { object->velocity.x += DECELERATION; if (object->velocity.x > 0.0f) { object->velocity.x = 0.0f; } } else if (object->velocity.x > 0.0f) { object->velocity.x -= DECELERATION; if (object->velocity.x < 0.0f) { object->velocity.x = 0.0f; } } } else { if (object->velocity.x * object->moveX < 0) { object->velocity.x += DECELERATION * object->moveX; } else { object->velocity.x += ACCELERATION * object->moveX; } if (object->velocity.x < -VELOCITY_MAX) { object->velocity.x = -VELOCITY_MAX; } else if (object->velocity.x > VELOCITY_MAX) { object->velocity.x = VELOCITY_MAX; } } if (object->moveY == 0) { if (object->velocity.y < 0.0f) { object->velocity.y += DECELERATION; if (object->velocity.y > 0.0f) { object->velocity.y = 0.0f; } } else if (object->velocity.y > 0.0f) { object->velocity.y -= DECELERATION; if (object->velocity.y < 0.0f) { object->velocity.y = 0.0f; } } } else { if (object->velocity.y * object->moveY < 0) { object->velocity.y += DECELERATION * object->moveY; } else { object->velocity.y += ACCELERATION * object->moveY; } if (object->velocity.y < -VELOCITY_MAX) { object->velocity.y = -VELOCITY_MAX; } else if (object->velocity.y > VELOCITY_MAX) { object->velocity.y = VELOCITY_MAX; } } object->lastPosition = object->position; object->position.x += object->velocity.x; object->position.y += object->velocity.y; } resolveCollisions(movingObjects, NUM_MOVING_OBJECTS, staticObjects, NUM_STATIC_OBJECTS); } static void initObjects() { // Player movingObjects[0].moveX = 0; movingObjects[0].moveY = 0; movingObjects[0].position.x = -0.5f; movingObjects[0].position.y = -0.5f; movingObjects[0].velocity.x = 0.0f; movingObjects[0].velocity.y = 0.0f; movingObjects[0].width = 1.0f; movingObjects[0].height = 1.0f; // Pushable blocks movingObjects[1].moveX = 0; movingObjects[1].moveY = 0; movingObjects[1].position.x = 5.0f; movingObjects[1].position.y = 5.0f; movingObjects[1].velocity.x = 0.0f; movingObjects[1].velocity.y = 0.0f; movingObjects[1].width = 1.5f; movingObjects[1].height = 1.5f; // Outer walls staticObjects[0].position.x = -16.0f; staticObjects[0].position.y = -12.0f; staticObjects[0].width = 0.5f; staticObjects[0].height = 24.0f; staticObjects[1].position.x = 15.5f; staticObjects[1].position.y = -12.0f; staticObjects[1].width = 0.5f; staticObjects[1].height = 24.0f; staticObjects[2].position.x = -16.0f; staticObjects[2].position.y = -12.0f; staticObjects[2].width = 32.0f; staticObjects[2].height = 0.5f; staticObjects[3].position.x = -16.0f; staticObjects[3].position.y = 11.5f; staticObjects[3].width = 32.0f; staticObjects[3].height = 0.5f; // Solitary block staticObjects[4].position.x = 4.0f; staticObjects[4].position.y = -3.0f; staticObjects[4].width = 4.0f; staticObjects[4].height = 3.0f; // Vertical pair with small gap staticObjects[5].position.x = -10.0f; staticObjects[5].position.y = -1.75f; staticObjects[5].width = 1.75f; staticObjects[5].height = 1.75f; staticObjects[6].position.x = -10.0f; staticObjects[6].position.y = 0.0f; staticObjects[6].width = 1.75f; staticObjects[6].height = 1.75f; // Horizontal pair with small gap staticObjects[7].position.x = -1.75f; staticObjects[7].position.y = -8.0f; staticObjects[7].width = 1.75f; staticObjects[7].height = 1.75f; staticObjects[8].position.x = 0.0f; staticObjects[8].position.y = -8.0f; staticObjects[8].width = 1.75f; staticObjects[8].height = 1.75f; } #define SCALE 12.0f #define STATIC_OUTLINE_RADIUS 0.0625f static void displayFunc() { float ratio; size_t objectIndex; FixedIntervalRunLoop_run(&runLoop); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); ratio = (float) windowWidth / (float) windowHeight; glOrtho(-ratio * SCALE, ratio * SCALE, -SCALE, SCALE, -1.0f, 1.0f); glBegin(GL_QUADS); for (objectIndex = 0; objectIndex < NUM_STATIC_OBJECTS; objectIndex++) { glColor4f(1.0f, 0.675f, 0.25f, 1.0f); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width - STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width - STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y + STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y + STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height - STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height - STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glVertex2f(staticObjects[objectIndex].position.x, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height); glColor4f(1.0f, 0.5f, 0.0f, 1.0f); glVertex2f(staticObjects[objectIndex].position.x + STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width - STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x + staticObjects[objectIndex].width - STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height - STATIC_OUTLINE_RADIUS); glVertex2f(staticObjects[objectIndex].position.x + STATIC_OUTLINE_RADIUS, staticObjects[objectIndex].position.y + staticObjects[objectIndex].height - STATIC_OUTLINE_RADIUS); } glColor4f(0.5f, 1.0f, 0.5f, 1.0f); glVertex2f(movingObjects[0].position.x, movingObjects[0].position.y); glVertex2f(movingObjects[0].position.x + movingObjects[0].width, movingObjects[0].position.y); glVertex2f(movingObjects[0].position.x + movingObjects[0].width, movingObjects[0].position.y + movingObjects[0].height); glVertex2f(movingObjects[0].position.x, movingObjects[0].position.y + movingObjects[0].height); glColor4f(0.5f, 0.5f, 1.0f, 1.0f); for (objectIndex = 1; objectIndex < NUM_MOVING_OBJECTS; objectIndex++) { glVertex2f(movingObjects[objectIndex].position.x, movingObjects[objectIndex].position.y); glVertex2f(movingObjects[objectIndex].position.x + movingObjects[objectIndex].width, movingObjects[objectIndex].position.y); glVertex2f(movingObjects[objectIndex].position.x + movingObjects[objectIndex].width, movingObjects[objectIndex].position.y + movingObjects[objectIndex].height); glVertex2f(movingObjects[objectIndex].position.x, movingObjects[objectIndex].position.y + movingObjects[objectIndex].height); } glEnd(); glutSwapBuffers(); glutPostRedisplay(); } static void reshapeFunc(int newWidth, int newHeight) { windowWidth = newWidth; windowHeight = newHeight; glViewport(0, 0, newWidth, newHeight); } static void keyDownFunc(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: movingObjects[0].moveX = -1; break; case GLUT_KEY_RIGHT: movingObjects[0].moveX = 1; break; case GLUT_KEY_DOWN: movingObjects[0].moveY = -1; break; case GLUT_KEY_UP: movingObjects[0].moveY = 1; break; } } static void keyUpFunc(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: if (movingObjects[0].moveX == -1) { movingObjects[0].moveX = 0; } break; case GLUT_KEY_RIGHT: if (movingObjects[0].moveX == 1) { movingObjects[0].moveX = 0; } break; case GLUT_KEY_DOWN: if (movingObjects[0].moveY == -1) { movingObjects[0].moveY = 0; } break; case GLUT_KEY_UP: if (movingObjects[0].moveY == 1) { movingObjects[0].moveY = 0; } break; } } int main(int argc, char ** argv) { #ifdef __APPLE__ GLint VBL = 1; #endif glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition(0, 0); glutInitWindowSize(windowWidth, windowHeight); glutCreateWindow("Collision Sample"); glutReshapeFunc(reshapeFunc); glutDisplayFunc(displayFunc); glutSpecialFunc(keyDownFunc); glutSpecialUpFunc(keyUpFunc); #ifdef __APPLE__ CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL); #endif FixedIntervalRunLoop_init(&runLoop, getCurrentTime, 1.0f / 120.0f, run); initObjects(); glutMainLoop(); return EXIT_SUCCESS; }