Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h> //Use glut or freeglut library
- int keyDown[256]; //Array of key codes
- void keyPressed(unsigned char key, int x, int y){keyDown[key]=1;} //If key pressed its down
- void keyUp (unsigned char key, int x, int y){keyDown[key]=0;} //Else key is up
- const int WIN_X = 640, WIN_Y = 480; //Window size
- const int FPS = 60; //Frames per second
- int timeFrame = 0; //TimeFrame
- float delta = 0.0f; //Jump physics
- float x_Pos = 0, y_Pos = 0; //Sprite coordinates
- float velocity = 0.01f; //Sprite velocity
- float gravity = 0.0f; //Gravity force
- float jumpHeight = 0.5f; //Jump height limit
- float jumpLength = 5.0f; //Length of natural jump
- struct RECT{float x, y, w, h;}; //Construct Rectangle
- RECT sprite = { 0, 0, 10, 10}; //Attributes for Sprite
- RECT platform = { 50, 20, 120, 10}; //Attributes for Platform
- RECT platform2 = {150, 50, 50, 10}; //Attributes for Platform2
- //RECT platform3 = { 50, 30, 100, 10};
- //RECT platform4 = { 50, 40, 80, 10};
- //RECT platform5 = { 50, 50, 60, 10};
- void init(){ //Initialise
- glViewport(0.f, 0.f, WIN_X, WIN_Y ); //Set the viewport
- glEnable(GL_TEXTURE_2D); //Enable textures for mapping
- glMatrixMode( GL_PROJECTION ); //Initialise Projection Matrix
- glLoadIdentity(); //Load current
- gluOrtho2D(0.0, 200.0, 0.0, 150.0); //Set 2D Ortho view
- glMatrixMode( GL_MODELVIEW ); //Initialise Modelview Matrix
- glClearColor( 0.f, 0.f, 0.f, 1.f ); //Initialise clear color
- }
- bool check_collision( RECT sprite, RECT platform ){ //Function to check collisions
- float sprite_Left, plat_Left; //Left sides of the rectangles
- float sprite_Right, plat_Right; //Right sides of the rectangles
- float sprite_Top, plat_Top; //Top sides of the rectangles
- float sprite_Bottom, plat_Bottom; //Bottom sides of the rectangles
- sprite_Left = sprite.x; //Calculate the left side of first object
- sprite_Right = sprite.x + sprite.w; //Calculate the right side of first object
- sprite_Top = sprite.y; //Calculate the top side of first object
- sprite_Bottom = sprite.y + sprite.h; //Calculate the bottom side of first object
- plat_Left = platform.x; //Calculate the left side of second object
- plat_Right = platform.x + platform.w; //Calculate the right side of second object
- plat_Top = platform.y; //Calculate the top side of second object
- plat_Bottom = platform.y + platform.h; //Calculate the bottom side of second object
- if(sprite_Bottom +y_Pos <= plat_Top ){return false;} //If sprites bottom is on or inside platforms top, colliding
- if(sprite_Top +y_Pos >= plat_Bottom ){return false;} //If sprites top is on or inside platforms bottom, colliding
- if(sprite_Right +x_Pos <= plat_Left ){return false;} //If sprites right is on or inside platforms left, colliding
- if(sprite_Left +x_Pos >= plat_Right ){return false;} //If sprites left is on or inside platforms right, colliding
- return true; //If no side(s) are touching, objects not colliding
- }
- void move(){ //Function to control moving sprite
- if(check_collision(sprite,platform) //If colliding...
- || check_collision(sprite,platform2)){
- if(keyDown[115]){ y_Pos += (velocity*2);} //IF key is still down, return to previous velocity
- if(keyDown[119]){ y_Pos -= (velocity*2);} //IF key is still up, return to previous velocity
- if(keyDown[100]){ x_Pos -= (velocity*2);} //IF key is still right, return to previous velocity
- if(keyDown[97] ){ x_Pos += (velocity*2);} //IF key is still left, return to previous velocity
- }
- glTranslatef(sprite.x+x_Pos, sprite.y+y_Pos, 0.0); //Move sprite to calculated coordinates in x/y position
- }
- void drawSprite (RECT rect){ //Function to draw sprite
- glPushMatrix(); //Push matrix
- move(); //Check collision and move appropriately
- glBegin(GL_QUADS); //Begin drawing rectangle
- glColor3f(0.0f, 0.0f, 1.0f); //Set colour(s) of rectangle
- glVertex3f(rect.x, rect.y, 0.0); //First point of quadrilateral or polygon
- glVertex3f(rect.x, rect.y+rect.h, 0.0); //Second point of quadrilateral or polygon
- glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0); //Third point of quadrilateral or polygon
- glVertex3f(rect.x+rect.w, rect.y, 0.0); //Final point of quadrilateral or polygon
- glEnd(); //Finish drawing rectangle
- glPopMatrix(); //Pop current matrix
- }
- void drawPlatform (RECT rect){ //Function to draw platform
- glBegin(GL_QUADS); //Begin drawing rectangle
- glColor3f(1.0f,0.0f,0.0f); //Set colour(s) of rectangle
- glVertex3f(rect.x, rect.y, 0.0); //First point of quadrilateral or polygon
- glVertex3f(rect.x, rect.y+rect.h, 0.0); //Second point of quadrilateral or polygon
- glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0); //Third point of quadrilateral or polygon
- glVertex3f(rect.x+rect.w, rect.y, 0.0); //Final point of quadrilateral or polygon
- glEnd(); //Finish drawing rectangle
- }
- void controls(void){
- if(!timeFrame){timeFrame = glutGet(GLUT_ELAPSED_TIME);} //Assign elapsed time
- int now = glutGet(GLUT_ELAPSED_TIME); //Current time
- int elapsedMilliseconds = now - timeFrame; //Calculate milliseconds
- float delta = elapsedMilliseconds/1000.0f; //Calculate delta
- timeFrame = now; //Assign time frame
- if(keyDown[119]){ //While 'w' key is pressed and not colliding
- if(!check_collision(sprite,platform) //If not colliding...
- && !check_collision(sprite,platform2)){
- y_Pos += jumpHeight; //Jump up
- if(keyDown[97] ){x_Pos -= velocity*jumpLength;} //While 'w' and 'a' are pressed, simulate arc of jump/gravity
- if(keyDown[100]){x_Pos += velocity*jumpLength;} //While 'w' and 'd' are pressed, simulate arc of jump/gravity
- }
- else //Else you must be colliding, bounce off
- y_Pos -= jumpHeight;
- }
- if(keyDown[115]){y_Pos -= velocity;} //While 's' key is pressed
- if(keyDown[97] ){x_Pos -= velocity;} //While 'a' key is pressed
- if(keyDown[100]){x_Pos += velocity;} //While 'd' key is pressed
- //gravity
- if(y_Pos>0){ //If above ground
- if(!check_collision(sprite,platform) //And not colliding
- && !check_collision(sprite,platform2)){
- gravity-=5*delta; //Calculate gravity force
- y_Pos+=gravity; //Descend to ground
- }
- if(keyDown[97] ){x_Pos -= velocity*2;} //While off ground, whilst 'a' is pressed, move left
- if(keyDown[100]){x_Pos += velocity*2;} //While off ground, whilst 'd' is pressed, move right
- }
- else{gravity=0;} //Else, on ground, so gravity force is zero
- }
- void display(void){ //Render and display
- glClear(GL_COLOR_BUFFER_BIT); //Clear buffer
- controls(); //User input to control sprite
- drawSprite(sprite); //Render sprite
- drawPlatform(platform); //Render platform
- drawPlatform(platform2); //Render platform2
- //drawPlatform(platform3);
- //drawPlatform(platform4);
- //drawPlatform(platform5);
- glutSwapBuffers(); //Swap buffers
- }
- void update(){glutPostRedisplay();} //Refresh display
- void runMainLoop(int val){
- update();
- glutTimerFunc(1000/FPS, runMainLoop, val);
- }
- void main (int argc, char** argv){
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(400, 200);
- glutInitWindowSize(WIN_X, WIN_Y);
- glutCreateWindow("Basic Collision");
- glutTimerFunc(1000/FPS, runMainLoop, 0);
- init();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyPressed);
- glutKeyboardUpFunc(keyUp);
- glutIdleFunc(update);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment