5 Replies - 5265 Views - Last Post: 23 March 2012 - 11:57 AM Rate Topic: ****- 1 Votes

#1 kapil_dd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 08

Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 20 March 2012 - 08:12 AM

Hi guys,
This is one of the two mini-projects for Computer Graphics. It is a simple game that me and my 2 friends have tried to do.

Concept of game: Try to dodge other balls as long as possible by moving your ball with mouse.

Till now we are done with the core part of the game. We have completed the controlling of the balls and collision handling with the help of various tutorials and books.

Now we are facing a problem.
Problem:
We want to display
1) The time for which player managed to dodge the balls.
2) "Game Over" at the end.

We also want to show the replay option if possible.

We have tried much about displaying text. But were not successfull. We read some tutorials at NeHe.GameDev.net. But those tutorials were for Windows. We are doing this project in UBUNTU desktop using CodeBlocks IDE.

(By the way, the Game starts by pressing 's' ;))

#include <GL/glut.h>
#include <cstdio>
#include <stdlib.h>
#include <math.h>
#include "vmath.h"

static double wWidth=800,wHeight=600;
static void resize(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-wWidth/2,wWidth/2, -wHeight/2,wHeight/2, -50.0, 50.0);
    //glOrtho(-wWidth,wWidth, -wHeight,wHeight, -250.0, 250.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}
double distance(double x1, double y1, double x2, double y2)
{
    return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}


//Radius and Centers of balls................also the direction (sign) of velocity and magnitude of velocity
static double rad=25.0,x[4],y[4],sx[4],sy[4],vx[4],vy[4];
int startTime,endTime;  //calculating the time
bool start=false;   //true when game starts
bool hitWall=false,checkBound=true; 
//hitWall for boundary checking and reflection along wall.............and checkBound becomes false when game is over
void collision2Ds(double& xx1, double& yy1, double& xx2, double& yy2,
                 double& vxx1, double& vyy1, double& vxx2, double& vyy2,
                 double& sxx1, double& syy1, double& sxx2, double& syy2)     
{
       Vector2<double> p1(xx1,yy1);//centre of ball 1
       Vector2<double> p2(xx2,yy2);//circle of ball 2
       Vector2<double> v1(sxx1*vxx1,syy1*vyy1);//velocity of ball 1
       Vector2<double> v2(sxx2*vxx2,syy2*vyy2);//velocity of ball 2
        Vector2<double> posDiff = (p2-p1);//collision line vector
        Vector2<double> velDiff = (v2-v1);

        Vector2<double> posDiffNorm=posDiff;
        posDiffNorm.normalize();//normalized collision line or unit normal line
        Vector2<double> tangent;//unit tangent between two balls
        tangent.x=-posDiffNorm.y;
        tangent.y=posDiffNorm.x;
        //redundant code for understanding the process
        Vector2<double> v1n=v1*posDiffNorm;//components of velocity of balls along normal
        Vector2<double> v2n=v2*posDiffNorm;
        Vector2<double> v1t=v1*tangent;//components of velocity of balls along tangent
        Vector2<double> v2t=v2*tangent;
        //swapping the normal components and keeping the tangential components same
        Vector2<double> newv1n=v2n*posDiffNorm;
        Vector2<double> newv2n=v1n*posDiffNorm;
        Vector2<double> newv1t=v1t*tangent;
        Vector2<double> newv2t=v2t*tangent;

        //assign new velocities
            v1=newv1n+newv1t;
            v2=newv2n+newv2t;


        //adjust the ball position to avoid sticking of balls in case of intersection of ball boundaries
            Vector2<double> adjust=posDiffNorm*(2*rad-distance(xx1,yy1,xx2,yy2))/2;
            if(newv1n<0)p1+=adjust;
            else p1-=adjust;
            if(newv2n<0)
            p2-=adjust;
            else p2+=adjust;
        //assign the changed positions
        xx1=p1.x;
        xx2=p2.x;
        yy1=p1.y;
        yy2=p2.y;
        //change the sign of velocity accordingly
        if(sxx1*v1.x*vxx1<0)
        sxx1=-sxx1;
        if(syy1*v1.y*vyy1<0)
        syy1=-syy1;
        if(sxx2*v2.x*vxx2<0)
        sxx2=-sxx2;
        if(syy2*v2.y*vyy2<0)
        syy2=-syy2;

        //keep the absolute values in velocities
        vxx1=fabs(v1.x);
        vxx2=fabs(v2.x);
        vyy1=fabs(v1.y);
        vyy2=fabs(v2.y);

       return;
}
//unsatisfactory code to maintain velocity above a certain desired value
void checkVel(double &vx,double &vy)
{
    if(checkBound==true)
    {
    if(vx<1.5)
    vx=1.5;
    if(vy<1.5)
    vy=1.5;
    }
}
//code for checking boundary collisions
void checkBoundary()
{
    if(checkBound==true)
    {
     for(int i=1;i<4;i++)
     {
        if(x[i]>=wWidth/2-rad)
        {
                x[i]=wWidth/2-rad;
                hitWall=true;
        }if(x[i]<=rad-wWidth/2)
        {
                x[i]=rad-wWidth/2;
                hitWall=true;
        }


        if(y[i]>=wHeight/2-rad)
        {
            y[i]=wHeight/2-rad;
                hitWall=true;
        }
        if(y[i]<=rad-wHeight/2)
        {y[i]=rad-wHeight/2;
            hitWall=true;
        }
    }

    }
}


//When game is over drop all the balls
static void dropAll()
{
        checkBound=false;
        endTime=glutGet(GLUT_ELAPSED_TIME);
            vx[1]=0;vy[1]=-1.5*sy[1];
            vx[2]=0;vy[2]=-1.5*sy[2];
            vx[3]=0;vy[3]=-1.5*sy[3];
            glutPostRedisplay();

}
//randomize the initial positions.............unsatisfactory code
void randomInit()
{
        sx[1]=sy[1]=1;
        sx[2]=sy[2]=-1;
        sy[3]=1;sx[3]=1;
        x[1]=random()%800-400;
        x[2]=random()%800-400;
        x[3]=random()%800-400;
        y[3]=random()%600-300;
        y[1]=random()%600-300;
        y[2]=random()%600-300;

        vx[1]=(double)(rand()%25)/5;
        vx[2]=(double)(rand()%25)/5;
        vx[3]=(double)(rand()%25)/5;
        vy[1]=(double)(rand()%25)/5;
        vy[2]=(double)(rand()%25)/5;
        vy[3]=(double)(rand()%25)/5;

}
//check the collisions for all combinations of balls
void checkCollision()
{
        if(distance(x[1],y[1],x[2],y[2])<=2*rad)
        {
                collision2Ds(x[1],y[1],x[2],y[2],vx[1],vy[1],vx[2],vy[2],sx[1],sy[1],sx[2],sy[2]);

        }
        if(distance(x[2],y[2],x[3],y[3])<=2*rad)
        {
                collision2Ds(x[2],y[2],x[3],y[3],vx[2],vy[2],vx[3],vy[3],sx[2],sy[2],sx[3],sy[3]);

        }
        if(distance(x[1],y[1],x[3],y[3])<=2*rad)
        {
                collision2Ds(x[1],y[1],x[3],y[3],vx[1],vy[1],vx[3],vy[3],sx[1],sy[1],sx[3],sy[3]);

        }
        //if collision is with ball controlled by mouse then game is over
        if(distance(x[1],y[1],x[0],-y[0])<=2*rad||
            distance(x[2],y[2],x[0],-y[0])<=2*rad ||
            distance(x[3],y[3],x[0],-y[0])<=2*rad
        )
        {
                dropAll();
        }
        glutPostRedisplay();

}

static void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(1,0,0);
    //Your ball...............the one controlled with mouse
    glPushMatrix();
        glTranslated(x[0],-y[0],0.0);
        glutSolidSphere(rad,30,30);
    glPopMatrix();
    //Other balls
    glColor3f(0.0,1.0,0.0);
    glPushMatrix();
        glTranslated(x[1],y[1],0.0);
        glutSolidSphere(rad,30,30);
    glPopMatrix();

    glColor3f(1.0,1.0,0.0);
    glPushMatrix();
        glTranslated(x[2],y[2],0.0);
        glutSolidSphere(rad,30,30);
    glPopMatrix();

    glPushMatrix();
        glTranslated(x[3],y[3],0.0);
        glutSolidSphere(rad,30,30);
    glPopMatrix();
    //Move the random balls to next position by checking collisions with other balls and wall
    checkCollision();
    checkBoundary();
    //try to maintain velocity of balls
    for(int i=1;i<4;i++)
    checkVel(vx[i],vy[i]);
    //if ball was hit to the boundary...........change the velocity direction of corresponding component
    if(hitWall==true)
    {
            if(x[1]>=wWidth/2-rad||x[1]<=rad-wWidth/2)
            sx[1]=-sx[1];
            if(y[1]>=wHeight/2-rad||y[1]<=rad-wHeight/2)
            sy[1]=-sy[1];
            if(x[2]>=wWidth/2-rad||x[2]<=rad-wWidth/2)
            sx[2]=-sx[2];
            if(y[2]>=wHeight/2-rad||y[2]<=rad-wHeight/2)
            sy[2]=-sy[2];
            if(x[3]>=wWidth/2-rad||x[3]<=rad-wWidth/2)
            sx[3]=-sx[3];
            if(y[3]>=wHeight/2-rad||y[3]<=rad-wHeight/2)
            sy[3]=-sy[3];
            hitWall=false;
    }
    //if game was started the change the position
    if(start){
    x[1]=x[1]+vx[1]*sx[1];y[1]=y[1]+vy[1]*sy[1];
    x[2]=x[2]+vx[2]*sx[2];y[2]=y[2]+vy[2]*sy[2];
    x[3]=x[3]+vx[3]*sx[3];y[3]=y[3]+vy[3]*sy[3];
    }
    glFlush();
    glutSwapBuffers();
    glGetError();
}
//To quit the game press q........to start press s
void keyboard (unsigned char key, int x, int y)
{
        switch(key)
        {
                case 'q':
                    exit(0);
                    break;
                case 's':
                    start=true;
                    if(start==false)
                    startTime=glutGet(GLUT_ELAPSED_TIME);//note down the initial time
                    break;
                default:
                    break;
        }
}
//Function to control your ball with mouse
void mouse (int x2,int y2)
{

        x[0]=x2-wWidth/2;
        y[0]=y2-wHeight/2;
        glutPostRedisplay();


}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{

    glutInit(&argc, argv);
    glutInitWindowSize(wWidth,wHeight);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

    glutCreateWindow("Dodge The Balls");
    randomInit();

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutPassiveMotionFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0,0,0,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glDisable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);


    glutMainLoop();

    return EXIT_SUCCESS;
}




The main file and vmath.h (for vector calculations) are attached.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

#2 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 20 March 2012 - 03:11 PM

Some comments:

1. Why have you declared these 'static'? Why are they 'double' instead of an integral type? Why are they not 'const' - especially since they are global?

static double wWidth=800,wHeight=600;



You are encouraging a lot of implicit casting by doing this which is not safe.

2. Why are the static functions static? Looking at your project this is the only source file anyway, so it has no effect.

Plus many other things in there that are too many to cover.

Regarding your problem, what you need is a timer function that takes the current time once the level (but not the game) starts and another function that stops at the appropriate time. See this code for a clue.

This post has been edited by ButchDean: 20 March 2012 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 kapil_dd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 08

Re: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 21 March 2012 - 07:00 AM

View PostButchDean, on 20 March 2012 - 03:11 PM, said:

Some comments:

1. Why have you declared these 'static'? Why are they 'double' instead of an integral type? Why are they not 'const' - especially since they are global?

static double wWidth=800,wHeight=600;



You are encouraging a lot of implicit casting by doing this which is not safe.

2. Why are the static functions static? Looking at your project this is the only source file anyway, so it has no effect.

Plus many other things in there that are too many to cover.

Regarding your problem, what you need is a timer function that takes the current time once the level (but not the game) starts and another function that stops at the appropriate time. See this code for a clue.



Thank you, ButchDean for the reply.

I will change the code according to your suggestions in point "1" and "2". I thank you for pointing it out.

Here I must tell you that, I know how to get the time. But problem is in showing the TEXT; or for that matter the calculated TIME; in that window. I am unable to show any kind of Text inside my window. If you could tell me how to print some TEXT inside of window of FONT size at least 72, it would be great!
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 21 March 2012 - 03:12 PM

Well, I just Googled 'OpenGL Font' and it returned this!

As they say, "Google is your friend".
Was This Post Helpful? 0
  • +
  • -

#5 kapil_dd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 08

Re: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 23 March 2012 - 03:20 AM

View PostButchDean, on 21 March 2012 - 03:12 PM, said:

Well, I just Googled 'OpenGL Font' and it returned this!

As they say, "Google is your friend".


Thanks ButchDean!

Now I am pretty sure that Google is our friend. I searched through google and found another site http://www.lighthous...al-projections/

I have successfully done the required part using the tutorial. Thanks once again.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Showing the Time and Game Over in the game done in OpenGL(GLUT) &

Posted 23 March 2012 - 11:57 AM

No problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1