OpenGl problem

Hey guys im kind of a beginner on openGL, im trying to make this ball bounce using this code but it runs in super speed, any idea what im doing wrong??
im using Xcode on mac btw..


#include <stdio.h>
#include <GLUT/glut.h>
#include <math.h>

GLfloat Xpos = 0.40, Ypos = 1.0;
GLdouble Xvel = 0.2, Yvel = 0.1;
GLfloat Xmin = -3.7, Xmax = 3.7;
GLfloat Ymin = -3.7, Ymax = 3.7;
GLfloat G = -0.9;

void
idle(void)
{
//static int count=0;
static float vel0 = -0.00000001;
//count++;
//printf("%d", count);

Ypos -= Yvel;
Yvel -= G;
if (Ypos <= Ymin) {
Ypos = Ymin;
if (vel0 == -0.0000000001)
vel0 = fabs(Yvel);
Yvel = vel0;
}

glutPostRedisplay();
}


void drawBall(){

glPushMatrix ();
glColor3f(0.9, 0.9, 0.0);
glTranslatef (Xpos, Ypos, 0.0);
//glRotatef (30.0, 1.0, 0.0, 0.0);
glScalef(0.3, 0.3, 0.3);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
glutSolidSphere (1.0, 30, 100);
glPopMatrix ();
}


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawBall();

glutSwapBuffers();
}

static GLint axis = 2;
void mouse(int btn, int state, int x, int y)
{

/* mouse callback, selects an axis about which to rotate */

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/*
gluPerspective(80.0, 1.00, 1.0, 10.0);
gluLookAt(1.0, 0.50, 2.00,
1.0, 0.00, 0.00,
0.00, 1.00, 0.00);
*/

glOrtho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);

glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char **argv)
{
glutInit(&argc, argv);

/* need both double buffering and z buffer*/

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("House 3D");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glutMainLoop();

}
Such are your numbers. Xpos starts from 0.4 and Xvel is 0.2, so after 2 frames Xpos will be 0 and after 1 second (let's pretend that fps = 60, though glut doesn't force it to be 60, so it might be much more) Xpos = -11.6.
any idea on how i would make the ball bounce on y axis only? the x is not important for now
Try initializing Ypos = 1.0, Yvel = 0, G = 0.01; (I'm just guessing. If you need to increase height, increase Ypos. If you and to make it slower, decrease G)
And then in idle(), Ypos += Yvel; Yvel-=G; if( Ypos <= 0 ) Yvel = - Yvel;
Ok i did it, thanks for all the help hamsterman, here is the code:


#include <stdio.h>
#include <GLUT/glut.h>
#include <math.h>
GLfloat Xpos = 2.0, Ypos = 3.0, P = -0.88;
GLfloat Xvel = 0.002, Yvel = 0.005, YvelU = 0.005;
GLfloat Xmin = 0.2, Xmax = 3.7;
GLfloat Ymin = 0.2, Ymax = 3.8;
GLfloat G = -0.0000099;
GLfloat Acc = -0.00009
;

void
idle(void)
{


float oldPos;
//printf("%f" " Accel: ", P);
//printf("%f" " Velocity: ", Yvel);
Yvel += G;
Ypos += Yvel;

if (Ypos<=Ymin) {
Ypos = Ymin;
Yvel *= -0.88;
}

Xpos += Xvel;
Xpos += G;
if (Xpos >= Xmax) {
Xpos = Xmax;
Xvel *= -0.88;
//Zstep = -Zstep;
}
if (Xpos <= Xmin) {
Xpos = Xmin;
Xvel *= -0.88;
//Zstep = -Zstep;
}

glutPostRedisplay();
}


void drawBall(){

glPushMatrix ();
glColor3f(0.5, 0.3, 0.2);
glTranslatef (Xpos, Ypos, 0.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glScalef(0.2, 0.2, 0.2);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
glutSolidSphere (1.0, 30, 100);
glPopMatrix ();

Topic archived. No new replies allowed.