PDA

View Full Version : how to move object at random direction



flopsy87
04-08-2010, 02:11 AM
i still don't know how to move object randomly.For example, after the object move in x axis,i want to make it to move in y axis or z axis or move backward(random).Where to correct and where to add...

Thanks in advance..

this is my code i'm working right now:

#include <windows.h>

#include "glut.h"
#include <stdlib.h>

char TitleString[] = "animation";

GLfloat eye[]={ 100.0, 60.0, 100.0 };
GLfloat step = 0.00;

//float t = 0.0;
float y = 0.0;
float x = 0.0;
float z = 0.0;


GLfloat gold_ambient[] = {0.24725, 0.1995, 0.0745, 1.0},
gold_diffuse[] = {0.75164, 0.60648, 0.22648, 1.0},
gold_specular[] = {0.628281, 0.555802, 0.366065, 1.0},
gold_shininess[]= {51.2};



GLfloat mat_ambient[]={0.2,0.2,0.2,1},
mat_diffuse[]={0.8,0.8,1.0,1.0},
mat_specular[]={0.8,0.8,1.0,1.0},
mat_shininess[]={100.0};

void myinit(void)
{
GLfloat light_position[] = { 100.0, 100.0, 100.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

//background color
glClearColor(1.0,1.0,1.0,1.0);


glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

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

gluLookAt(eye[0], eye[1], eye[2], 5.0, 0.0, -20.0, 0.0, 1.0, 0.0);

glLightfv(GL_LIGHT0, GL_AMBIENT, gold_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, gold_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, gold_specular);


glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

glutWireCube(9.0);


glTranslatef( 0.0, y, 0.0);
glTranslatef( x, 0.0, 0.0);


glutSolidTeapot(1.0);
glutSwapBuffers( );
}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(10.0, (GLfloat)w/(GLfloat)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void move(void){



{y += step;

if(y > 10.0) y -= 10;}



glutPostRedisplay();


}

void menu(int value){
switch(value) {
case 1:
step = 0.0;
break;
case 2:
step = 0.01;
break;
case 3:
step = 0.04;
break;
case 4:
step = 0.08;
break;
case 5:
exit(0);
break;
}
}

void main(void)
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(330,330);
glutInitWindowSize(800, 450);
glutCreateWindow(TitleString);
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
//glutTimerFunc(10,move,2);
glutIdleFunc(move);

glutCreateMenu(menu);
glutAddMenuEntry("stop", 1);
glutAddMenuEntry("slow", 2);
glutAddMenuEntry("normal", 3);
glutAddMenuEntry("fast", 4);
glutAddMenuEntry("close", 5);
glutAttachMenu(GLUT_LEFT_BUTTON);

glutMainLoop( );
}

dorbie
04-08-2010, 01:54 PM
glTranslate on the modelview matrix moves objects provided you have an intelligible application.

Randomizing the floating point values you use for translation is a mathematical operation outside the scope of OpenGL, but rand() may be useful.