Results 1 to 4 of 4

Thread: Ball Game : Project

  1. #1
    Newbie Newbie
    Join Date
    Mar 2015
    Posts
    2

    Ball Game : Project

    Hello everyone. This is my first post. I'm working on a project where a ball will have random motion, the score displayed on top left corner and timer function of 1 min on top right corner. The purpose is that player will click on the ball and score will be implemented by one and the speed of the ball will increase.
    Till now i have made random motion,score displayed but i'm not able to calculate the position of the ball as in when the cursor goes over the ball then the score needs to be displayed otherwise not. So please help me in detecting mouse over the moving ball. Here is my code :

    #include<GL/glut.h>
    #include<math.h>
    #include<stdbool.h>
    #include<stdio.h>
    #define PI 3.14159265f


    GLfloat ballRadius = 0.2;
    GLfloat ballX = 0.0f;
    GLfloat ballY = 0.0f;
    GLfloat ballXMax,ballXMin,ballYMax,ballYMin;
    GLfloat xSpeed = 0.02f;
    GLfloat ySpeed = 0.007f;
    int refreshMills = 30;
    int x1,xa,ya;
    int score = 0;
    int last_mx = 0, last_my = 0, cur_mx = 0, cur_my = 0;
    int arcball_on = false;
    int posx = -1,posy=0,posz=1;
    char *string;
    GLdouble clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipA reaYTop;




    void color()
    {
    if(score<=5)
    glColor3f(1.0,0.0,0.0);
    else
    glColor3ub( rand()%250, rand()%250, rand()%250 );
    }
    void balldisp()
    {
    glTranslatef(ballX,ballY,0.0f);
    glBegin(GL_TRIANGLE_FAN);
    color();
    glVertex2f(0.0f,0.0f);
    int numSegments = 100;

    GLfloat angle;
    int i;
    for(i=0;i<=numSegments;i++)
    {
    angle = i*2.0f*PI/numSegments;
    glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRa dius);
    }
    glEnd();

    ballX += xSpeed;
    ballY += ySpeed;

    if(ballX > ballXMax)
    { xa=ballX;
    ballX = ballXMax;
    xSpeed = -xSpeed;

    }
    else if(ballX < ballXMin)
    { xa=ballX;
    ballX = ballXMin;
    xSpeed = -xSpeed;

    }
    if(ballY > ballYMax)
    { ya=ballY;
    ballY = ballYMax;
    ySpeed = -ySpeed;

    }
    else if(ballY < ballYMin)
    { ya=ballY;
    ballY = ballYMin;
    ySpeed = -ySpeed;

    }
    }

    void scoredisp()
    {
    int z,j=0,k=0;
    z=score;
    glColor3f(1.0,0.0,0.0);
    glLoadIdentity();
    glRasterPos2f(-1,1 );
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,'S');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,'C');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,'O');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,'R');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,'E');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,' ');
    glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24,':');

    while(z > 9)
    {
    k = z % 10;
    glRasterPos2f (-0.58,1);
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,48+ k);
    z /= 10;
    glRasterPos2f(-0.62,1);
    }
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,48+ z);
    }
    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    balldisp();
    scoredisp();
    glFlush();
    }
    void onMouse(int button, int state, int x, int y) /// I want help here to detect mouse over the ball
    {



    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
    arcball_on = true;
    cur_mx = x;
    cur_my = y;

    }
    else
    {
    arcball_on = false;
    if(cur_mx==x && cur_my==y)
    {
    score=score+1;
    }
    printf("%d",score);
    }



    //return score;

    // xSpeed=xSpeed+0.02;
    // ySpeed=ySpeed+0.002;
    }
    void onMotion(int x, int y) {
    if (arcball_on) {
    cur_mx = x;
    cur_my = y;
    printf("%d",cur_mx);
    }
    }


    void reshape(GLsizei width,GLsizei height)
    {
    if(height ==0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(width >=height)
    {
    clipAreaXLeft = -1.0 * aspect;
    clipAreaXRight = 1.0 * aspect;
    clipAreaYBottom = -1.0;
    clipAreaYTop = 1.0;
    }
    else
    {
    clipAreaXLeft = -1.0;
    clipAreaXRight = 1.0 ;
    clipAreaYBottom = -1.0 / aspect;
    clipAreaYTop = 1.0/ aspect;
    }
    gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYB ottom,clipAreaYTop+0.10);
    ballXMin = clipAreaXLeft + ballRadius;
    ballXMax = clipAreaXRight - ballRadius;
    ballYMin = clipAreaYBottom + ballRadius;
    ballYMax = clipAreaYTop - ballRadius;
    }
    void Timer(int value)
    {
    glutPostRedisplay();
    glutTimerFunc(refreshMills,Timer,5);
    }
    int main(int argc,char* argv[])
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Bouncing Ball");
    glutMouseFunc(onMouse);
    glutMotionFunc(onMotion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutPostRedisplay();
    glutTimerFunc(0,Timer,0);
    glutMainLoop();

    }

  2. #2
    Member Newbie
    Join Date
    Nov 2013
    Posts
    48
    Well you need to convert the mouse screen coordinates (x and y) into your world coordinates, i.e. in terms of (clipAreaXleft,...), then compare these transformed coordinates with the area covered by the (ballX,ballY) and ballRadius. Since you used ortho a windowing transformation will do for the transformation.

  3. #3
    Newbie Newbie
    Join Date
    Mar 2015
    Posts
    2
    Quote Originally Posted by sueyllam View Post
    Well you need to convert the mouse screen coordinates (x and y) into your world coordinates, i.e. in terms of (clipAreaXleft,...), then compare these transformed coordinates with the area covered by the (ballX,ballY) and ballRadius. Since you used ortho a windowing transformation will do for the transformation.
    Can u please add in my code that will be very greatful as i'm new to OPENGL. plz

  4. #4
    Member Newbie
    Join Date
    Nov 2013
    Posts
    48
    Well, the only thing related to opengl, is that you need to convert the mouse y to height-y since the origin of the screen coordinate is at the top left and the origin of opengl world coordinate is at the bottom left. The windowing coordinate is just basic computer graphics and I do recommend the following book:
    Peter Shirley and Steve Marschner, Fundamentals of Computer Graphics, CRC Press, Third Edition, 2009

Similar Threads

  1. game project
    By pbivens in forum OpenGL: Basic Coding
    Replies: 14
    Last Post: 05-18-2018, 06:15 PM
  2. 3D bounce ball game
    By baagio in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-07-2012, 08:37 PM
  3. Billiards game centre cue on ball
    By Edney86 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-13-2010, 08:03 AM
  4. game style project
    By dfanuk in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-27-2002, 06:24 AM
  5. 3d & game engine project
    By jide in forum OpenGL: Linux
    Replies: 1
    Last Post: 07-10-2001, 04:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean