can nay on etell me tht why if i am moving a ball thn my hurdl also moving ..
here is the code
#include <glut.h> // Also included gl.h, glu.h
#include <Math.h>
#define PI 3.14159265f
// Global variables
char title[] = "Bouncing Ball (2D)";
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
GLfloat ballRadius = 0.05f; // Radius of the bouncing ball
GLfloat xPos = -1.2; // Ball's (x, y) position
GLfloat yPos = -0.8;
GLfloat h=0.0;
GLfloat h1=0.0;
GLfloat xPosMax, xPosMin, yPosMax, yPosMin; // Ball's (x, y) bounds
GLdouble xLeft, xRight, yBottom, yTop; // Projection clipping area
GLfloat xSpeed = 0.02f; // Ball's speed in x and y directions
GLfloat ySpeed = 0.007f;
int refreshMillis = 30; // Refresh period in milliseconds
void hurdles(){
glColor3f(0.0,0.0,1.0);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glVertex2f(-0.5,0.5);
glVertex2f(-0.2,0.5);
glVertex2f(-0.2,-0.9);
glVertex2f(-0.5,-0.9);
glEnd();
glFlush();
// }
}
void ball(){
glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
// Reset model-view matrix
glTranslatef(xPos, yPos, 0.0f); // Translate to (xPos, yPos)
// Use triangular segments to form a circle
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 0.0f); // Blue
glVertex2f(0.0f, 0.0f); // Center of circle
int numSegments = 20;
GLfloat angle;
for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
angle = i * 2.0f * PI / numSegments; // 360 deg for all segments
glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
}
glEnd();
glutSwapBuffers(); // Swap front and back buffers (of double buffered mode)
}
// Initialize OpenGL Graphics
void initGL() {
glClearColor(1.0, 1.0, 0.0, 1.0); // Set background (clear) color to black
glEnable(GL_POLYGON);
}
// Callback handler for window re-paint event
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
hurdles();
ball();
// Animation Control - compute the location for the next refresh
}
// Call back when the windows is re-sized.
void reshape(GLsizei weight, GLsizei height) {
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)weight / height; // Get aspect ratio
// Set the viewport to cover the entire window
glViewport(0, 0, weight, height);
// Adjust the aspect ratio of clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // Select the Projection matrix
glLoadIdentity(); // Reset
if (weight <= height) {
xLeft = -1.0;
xRight = 1.0;
yBottom = -1.0 / aspect;
yTop = 1.0 / aspect;
} else {
xLeft = -1.0 * aspect;
xRight = 1.0 * aspect;
yBottom = -1.0;
yTop = 1.0;
}
gluOrtho2D(xLeft, xRight, yBottom, yTop);
xPosMin = xLeft + ballRadius;
xPosMax = xRight - ballRadius;
yPosMin = yBottom + ballRadius;
yPosMax = yTop - ballRadius;
// Reset the model-view matrix
glMatrixMode(GL_MODELVIEW); // Select the model-view matrix
glLoadIdentity(); // Reset
}
// Animation timer function
/*void Timer(int value) {
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, Timer, 0); // subsequent timer call at milliseconds
}*/
// Callback handler for special-key event
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_F1: // F1: Toggle between full-screen and windowed mode
break;
case GLUT_KEY_RIGHT: // Right: increase x speed
xPos += 0.05;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
xPos -=0.05;// Left: decrease x speed
glutPostRedisplay();
break;
case GLUT_KEY_UP: // Up: increase y speed
yPos+=0.045;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN: // Down: decrease y speed
yPos -=0.045;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_UP: // Page-Up: increase ball's radius
ballRadius *= 1.05f;
xPosMin = xLeft + ballRadius;
xPosMax = xRight - ballRadius;
yPosMin = yBottom + ballRadius;
yPosMax = yTop - ballRadius;
break;
case GLUT_KEY_PAGE_DOWN: // Page-Down: decrease ball's radius
ballRadius *= 0.95f;
xPosMin = xLeft + ballRadius;
xPosMax = xRight - ballRadius;
yPosMin = yBottom + ballRadius;
yPosMax = yTop - ballRadius;
break;
}
}
// main function - GLUT run as a Console Application
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(windowWidth, windowHeight); // Initial window width and height
glutInitWindowPosition(windowPosX, windowPosY); // Initial window top-left corner (x, y)
glutCreateWindow(title); // Create window with given title
glutDisplayFunc(display); // Register callback handler for window re-paint
glutReshapeFunc(reshape);
glutSpecialFunc(specialKeys); // Register callback handler for window re-shape
/// glutTimerFunc(0, Timer, 0); // First timer call immediately
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter event-processing loop
return 0;
}