PDA

View Full Version : bouncing ball



iscsi
03-15-2009, 03:18 AM
Hi all,

I've followed zeus tutorials and i have some questions.
I've tried to make a bouncing ball, it works but :
1: the balls has "shadow" (the shadow following the ball) i don't know why
2:where i have to put redisplay?
3: if i change program to fullscreen the ball became elipse

My Code:



#pragma comment(linker, "/subsystem:\"windows\" \
/entry:\"mainCRTStartup\"")

#include <gl/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


bool fullscreen = false;
float posx,posy,movespeedx,movespeedy;


bool init()
{
glClearColor(0.93f, 0.93f, 0.93f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);

return true;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int PI=3.14159265;
float radius=0.03f;

if (posx+radius>1.0f || posx-radius<-1.0f)
{
movespeedx*=-1;
}
if (posy+radius>1.0f || posy-radius<-1.0f)
{
movespeedy*=-1;
}
posx+=movespeedx;
posy+=movespeedy;

glBegin(GL_POLYGON);
for (float angle = 0;angle<360;angle+=5)
{
float xc=sin(angle*PI/180) * radius;
float yc=cos(angle*PI/180) * radius;
glVertex3f( xc+posx, yc+posy ,0.0f);
}
glEnd();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
if (key == 27)
exit(1);
}

void specialKeyboard(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_F1:
fullscreen = !fullscreen;
if (fullscreen)
glutFullScreen();
else
{
glutReshapeWindow(500, 500);
glutPositionWindow(50, 50);
}
break;
}
}

int main(int argc, char *argv[])
{
srand ( time(NULL) );
int step=10;
posx=(rand()%step)/step-0.5f;
posy=(rand()%step)/step-0.5f;
movespeedx=0.02f;
movespeedy=0.03f;

glutInit(&amp;argc, argv);

glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutCreateWindow("05 - Primitives");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
if (!init())
return 1;

glutMainLoop();

return 0;
}




Thanks Istvan

Brolingstanz
03-15-2009, 10:20 AM
Since when is pi an integer?

P.S. Pi = ln(-1) / sqrt(-1), and who said pi isn't rational? ;-)

iscsi
03-15-2009, 10:37 AM
Thank you, i misspelled that.
But this is don't solve my problems and questions, but improve my code :) thank you for your answer.

iscsi
04-05-2009, 08:58 AM
Hi

i made a simple bouncing ball model program (with starting y speed, elacticity and gravity).
I'm calculating the speed and the position from elapsed time using Gettickcount function.
But i realize the ball when the speed is high is not continous i mean between two draws too much time elapsed so the ball movement isn't smooth.
(This code i'm using rectangle instead of ball)
Could you help me how can i made the ball, or rectangle movement more smoothly



http://adatrosta.hu/~iscsi/Lesson6.cpp

iscsi
04-06-2009, 09:07 AM
http://adatrosta.hu/~iscsi/Lesson6.cpp

i change the code.

I think my problem is shorty is the program fps is few, Please help somebody where i do it wrong,
Thanks