PDA

View Full Version : Complex paths - a question



Amikam
05-05-2009, 12:50 PM
Hi all,
I wrote a small code for moving a ball in a path of 4 part.
The moving is in a callback function of timer. I used boolean variables to decide when to start the next part.
1. What I can do if I want to create more complex path?
2. What I can do if I want to create a path with randomal directions? Thanks.

The code:

#include <GL/glut.h>
#define _USE_MATH_DEFINES
#include <math.h>

float xb = 0.0;
float yb = -18.0;

bool a = true;
bool b = false;
bool c = false;
bool d = false;

void DrawBall(float x,float y)
{
float ang;
float a,b;

glColor3f(0.0,0.9,0.8);
glBegin(GL_POLYGON);
for (ang = 0; ang < 2*M_PI; ang += (2*M_PI)/360)
{
a = x+2*cos(ang);
b = y+2*sin(ang);
glVertex2f(a,b);
}
glEnd();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

DrawBall(xb,yb);

glFinish();
glEnd();

glutSwapBuffers();
}

void timer(int value)
{
if (a)
{
if (xb < 18.0)
{
xb++;
yb++;
}
if (xb == 18.0)
{
a = false;
b = true;
}
}
if (b)
{
if (yb < 18.0)
{
xb--;
yb++;
}
if (yb == 18.0)
{
b = false;
c = true;
}
}
if (c)
{
if (xb > -18.0)
{
xb--;
yb--;
}
if (xb == -18.0)
{
c = false;
d = true;
}
}
if (d)
{
if (yb > -18.0)
{
xb++;
yb--;
}
if (yb == -18.0)
{
d = false;
a = true;
}
}

display();

glutTimerFunc(40,timer,0);
}

void glInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-20,20,-20,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,400,400);
}

int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutInitWindowPosition(200,200);
glutCreateWindow("ball");
glInit();
glutDisplayFunc(display);
glutTimerFunc(40,timer,0);
glutMainLoop();
return 0;
}