Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: how to move object at random direction

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2010
    Posts
    1

    how to move object at random direction

    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( );
    }

  2. #2
    Super Moderator OpenGL Guru dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    4,388

    Re: how to move object at random direction

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •