PDA

View Full Version : Ball Game : Project



deep2sunny
03-28-2015, 09:36 AM
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();

}

sueyllam
03-28-2015, 11:00 PM
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.

deep2sunny
03-29-2015, 12:04 AM
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

sueyllam
03-30-2015, 09:14 PM
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