Results 1 to 3 of 3

Thread: move (animate) a cube from A to B

  1. #1
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    move (animate) a cube from A to B

    i have a cube on the screen but want to move it (animate it) from the current position to another position. moving the cube will only happen if the '1' key is pressed for example.

    tried youtube but not sure if i'm searching for the right tutorials, any ideas?
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: move (animate) a cube from A to B

    Should be straight forward. Set the current modelview matrix using the glTranslatef call to position you object.
    What have u done so far? We can guide u along but nobody will code it for u.
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie chuck norrrissss's Avatar
    Join Date
    Mar 2011
    Posts
    17

    Re: move (animate) a cube from A to B

    your right, it was fairly easy once i realized how opengl works with the code

    i posted my code below and am wondering if you experts have any hints or suggestions for me? everything works but maybe i didn't code it in the most efficient way?
    Code :
    // ***************************************************
    // ** Creates 3 Boxes                               **
    // ***************************************************
     
    // used later when the FALCON is connected
    //#include <windows.h>
    //#include "glut.h"
    //#include <math.h>
    //#include "haptics.h"
     
    #include <stdlib.h>
    #include <GL/openglut.h>
    #include <math.h>
    //#include <GL/glut.h> 
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>
    #include <cstdlib>
     
    // removes the console window
    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
    using namespace std;
    int w, h;       // width and height of main window
    int pos = 0;    // position of the two moveable gripper fingers
    GLfloat x1, yy1, z1, x2, y2, z2, x3, y3, z3;
     
    // if the window size is changed, the objects inside will be resized as well
    void changeSize(int w, int h) {
     
        // Prevent a divide by zero, when window is too short
    	// (you cant make a window of zero width).
    	if (h == 0)
    		h = 1;
    	double ratio =  w * 1.0 / h;    // changed 'float' to 'double' due to warning
     
    	glMatrixMode(GL_PROJECTION);    // Use the Projection Matrix
    	glLoadIdentity();               // Reset Matrix
    	glViewport(0, 0, w, h);         // Set the viewport to be the entire window
    	gluPerspective(45, ratio, 0.1, 100);    // Set the correct perspective.
    	glMatrixMode(GL_MODELVIEW);     // Get Back to the Modelview
    }
     
     
    // renders any text used
    void renderBitmapString (float x,
                             float y,
                             float z,
                             void *font,
                             char *string) {
        char *c;
        glLoadIdentity();       // reset matrix
        glRasterPos3f(x, y, z);
        for (c = string; *c != '\0'; c++) {
            glutBitmapCharacter(font, *c);
        }
    }
     
     
    void restorePerspectiveProjection() {
     
    	glMatrixMode(GL_PROJECTION);
    	glPopMatrix();                  // restore previous projection matrix
    	glMatrixMode(GL_MODELVIEW);     // get back to modelview mode
    }
     
     
    void setOrthographicProjection() {
     
    	glMatrixMode(GL_PROJECTION);    // switch to projection mode
     
    	                                // save previous matrix which contains the
    	glPushMatrix();                 // settings for the perspective projection.
    	glLoadIdentity();               // reset matrix
    	gluOrtho2D(0, w, h, 0);         // set a 2D orthographic projection
    	glMatrixMode(GL_MODELVIEW);     // switch back to modelview mode
    }
     
     
    void drawGrippers() {
     
        // declares for sphere
        GLdouble radius = 0.2;
        GLint slices = 30;
        GLint stacks = 30;
        GLfloat r = 1.0f;
        GLfloat g = 1.0f;
        GLfloat b = 0.0f;
     
        // draw three spheres
        glLoadIdentity();
        glTranslatef(x1, yy1, z1);
        glColor3f(r, g, b);
        glutSolidSphere(radius, slices, stacks);          // top sphere
     
        glLoadIdentity();
        glTranslatef(x2, y2, z2);
        glColor3f(r, g, b);
        glutSolidSphere(radius, slices, stacks);          // top sphere
     
        glLoadIdentity();
        glTranslatef(x3, y3, z3);
        glColor3f(r, g, b);
        glutSolidSphere(radius, slices, stacks);          // top sphere
        glColor3f(1.0f, 1.0f, 1.0f);
    }
     
     
    // any permanent text or lines are placed on the screen
    void screenLabels() {
     
        GLfloat r = 0.0f;
        GLfloat g = 0.0f;
        GLfloat b = 0.0f;
        GLfloat r1 = 1.0f;
        GLfloat b1 = 1.0f;
     
     
        // draws all 'limit lines'
        glLoadIdentity();
        glBegin(GL_LINES);
            glVertex3f(-0.21f, 1.22f, -5.0f);   // top sphere, iniPOS, TOP line
            glVertex3f(0.21f, 1.22f, -5.0f);
     
            glVertex3f(-0.21f, 1.22f, -5.0f);   // top sphere, iniPOS, LEFT line
            glVertex3f(-0.21f, 0.8f, -5.0f);
     
            glVertex3f(0.21f, 1.22f, -5.0f);    // top sphere, iniPOS, RIGHT line
            glVertex3f(0.21f, 0.8f, -5.0f);
     
            glVertex3f(-1.22f, -0.29f, -5.0f);  // left sphere, iniPOS, TOP line
            glVertex3f(-0.8f, -0.29f, -5.0f);
     
            glVertex3f(-1.22f, -0.29f, -5.0f);  // left sphere, iniPOS, LEFT line
            glVertex3f(-1.22f, -0.7f, -5.0f);
     
            glVertex3f(1.22f, -0.29f, -5.0f);   // right sphere, iniPOS, TOP line
            glVertex3f(0.8f, -0.29f, -5.0f);
     
            glVertex3f(1.22f, -0.29f, -5.0f);   // right sphere, iniPOS, RIGHT line
            glVertex3f(1.22f, -0.7f, -5.0f);
     
            glVertex3f(-0.29f, -1.22f, -5.0f);  // left sphere, altPOS, RIGHT line
            glVertex3f(-0.29f, -0.97f, -5.0f);
     
            glVertex3f(-0.29f, -1.22f, -5.0f);  // left sphere, altPOS, BOTTOM line
            glVertex3f(-0.71f, -1.22f, -5.0f);
     
            glVertex3f(0.29f, -1.22f, -5.0f);   // right sphere, altPOS, LEFT line
            glVertex3f(0.29f, -0.97f, -5.0f);
     
            glVertex3f(0.29f, -1.22f, -5.0f);   // right sphere, altPOS, BOTTOM line
            glVertex3f(0.7f, -1.22f, -5.0f);
     
            glVertex3f(-0.21f, 0.09f, -5.0f);   // top sphere, gripCLOSE, BOTTOM line
            glVertex3f(0.21f, 0.09f, -5.0f);
     
            glVertex3f(-0.3f, -0.2f, -5.0f);    // left sphere, gripCLOSE, DIAGONAL line
            glVertex3f(0.0f, -0.5f, -5.0f);
     
            glVertex3f(0.3f, -0.2f, -5.0f);     // right sphere, gripCLOSE, DIAGONAL line
            glVertex3f(0.0f, -0.5f, -5.0f);
        glEnd();
     
        renderBitmapString(-0.08f, 1.3f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'X'");
        renderBitmapString(-1.5f, -0.5f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'Y'");
        renderBitmapString(1.4f, -0.5f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'Z'");
        renderBitmapString(-2.0f, 1.9f , -5.0f, GLUT_BITMAP_HELVETICA_18, "Simulation of Falcon / Gripper Movements");
     
        if ((pos == 0) || (pos == 1) || (pos == 42)) {
            glColor3f(r1, g, b);
            renderBitmapString(-0.47f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Initial Position!");
        }
        else if ((pos == 2) || (pos == 32)){
            glColor3f(r1, g, b);
            renderBitmapString(-0.55f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Alternate Position!");
        }
        else if (pos == 31) {
            glColor3f(r, g, b1);
            renderBitmapString(-0.9f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Moving to Alternate Position...");
        }
        else if (pos == 41) {
            glColor3f(r, g, b1);
            renderBitmapString(-0.8f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Moving to Initial Position...");
        }
        else if (pos == 51) {
            glColor3f(r, g, b1);
            renderBitmapString(-0.52f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Closing Grippers...");
        }
        else if (pos == 61) {
            glColor3f(r, g, b1);
            renderBitmapString(-0.6f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Opening Grippers...");
        }
        else if (pos == 52) {
            glColor3f(r1, g, b);
            renderBitmapString(-0.6f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Grippers are Closed!");
        }
        else if (pos == 62) {
            glColor3f(r1, g, b);
            renderBitmapString(-0.58f, -0.1f , -5.0f,
            GLUT_BITMAP_TIMES_ROMAN_24, "Grippers are Open!");
        }
        glColor3f(1.0f, 1.0f, 1.0f);
    }
     
     
    // creates three boxes and text
    void renderScene() {
     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // clear screen
        drawGrippers();
        screenLabels();
     
        glutSwapBuffers();
    }
     
     
    void instructions() {
     
        system("cls");
        cout << endl << endl << endl << "  press 'i' at anytime for instructions" << endl;
        cout << "  press 'esc' at anytime to EXIT" << endl << endl;
        cout << "  press '1' = INITIAL position" << endl;
        cout << "  press '2' = ALTERNATE position" << endl;
        cout << "  press '3' = MOVE grippers DOWN" << endl;
        cout << "  press '4' = MOVE grippers UP" << endl;
        cout << "  press '5' = CLOSE grippers" << endl;
        cout << "  press '6' = OPEN grippers" << endl;
    }
     
     
    // checking keyboard for key presses
    void glutKeyboard(unsigned char key, int x, int y) {
     
        GLfloat stepALT = 0.01f;
        GLfloat stepGripper = 0.01f;
        static bool inited = true;
     
        if (key == 27) {    // esc key
            exit(0);
        }
        // the 'y' and 'z' axis of the gripper can move into different
        // positions: INITIAL position, ALTERNATE position, move DOWN
        // & move UP
     
        // INITIAL position
        else if (key == '1') {
            renderBitmapString(-1.0f, -2.0f , -5.0f, GLUT_BITMAP_HELVETICA_18, "TEST");
            pos = 1;
     
            x2 = -1.0f;     // left sphere
            y2 = -0.5f;
     
            x3 = 1.0f;      // right sphere
            y3 = -0.5f;
            glutPostRedisplay();
        }
     
        // ALTERNATE position
        else if (key == '2') {
            pos = 2;
     
            x2 = -0.5f;     // left sphere
            y2 = -1.0f;
     
            x3 = 0.5f;      // right sphere
            y3 = -1.0f;
            glutPostRedisplay();
        }
     
        // DOWN to ALTERNATE position
        else if (key == '3') {
            // left cube
            if ((x2 <= -0.5) &amp;&amp; (y2 >= -1.0)) {
                x2 += stepALT;
                y2 -= stepALT;
                pos = 31;
            }
            else {
                pos = 32;
            }
            // right cube   
            if ((x3 >= 0.5) &amp;&amp; (y3 >= -1.0)) {
                x3 -= stepALT;
                y3 -= stepALT;
            }
            glutPostRedisplay();
        }
     
        // UP to INITIAL position
        else if (key == '4') {
            // left cube
            if (( x2 >= -1.0 &amp;&amp; y2 <= -0.5)) {
                x2 += -stepALT;
                y2 -= -stepALT;
                pos = 41;
            }
            else {
                pos = 42;
            }
            // right cube
            if (( x3 <= 1.0 &amp;&amp; y3 <= -0.5)) {
                x3 += stepALT;
                y3 += stepALT;
            }
        }
     
        // CLOSE grippers
        else if (key == '5') {        
            // top cube
            if (yy1 >= 0.3) {
                //x1 += -stepGripper;
                yy1 -= stepGripper;
                pos = 51;
            }
            else {
                pos = 52;
            }
            // left cube
            if (x2 <= -0.3) {
                x2 += stepGripper;
                //y2 -= -stepGripper;
            }
            // right cube
            if (x3 >= 0.3) {
                x3 -= stepGripper;
                //y3 += stepGripper;
            }
        }
        // OPEN grippers
        else if (key == '6') {
            // top cube
            if (yy1 <= 1.0) {
                //x1 += -stepGripper;
                yy1 += stepGripper;
                pos = 61;
            }
            else {
                pos = 62;
            }
            // left cube
            if (x2 >= -1.0) {
                x2 -= stepGripper;
                //y2 -= -stepGripper;
            }
            // right cube
            if (x3 <= 1.0) {
                x3 += stepGripper;
                //y3 += stepGripper;
            }
        }
        else if (key == 'i') {
            instructions();
        }
            glutPostRedisplay();
    }
     
     
    int main(int argc, char **argv) {
     
        // initialize variables
        x1 = 0.0f;      // top sphere
        yy1 = 1.0f;
        z1 = -5.0f;
     
        x2 = -1.0f;     // left sphere
        y2 = -0.5f;
        z2 = -5.0f;
     
        x3 = 1.0f;      // right sphere
        y3 = -0.5f;
        z3 = -5.0f;
     
    	// init GLUT, create Window and init Keyboard
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    	glutInitWindowPosition(10,10);
    	glutInitWindowSize(800, 650);
    	glutCreateWindow("THREE BOXES DEMO");
        glutKeyboardFunc(glutKeyboard);
     
        instructions();
     
        // register callbacks
    	glutDisplayFunc(renderScene);
    	glutReshapeFunc(changeSize);
     
    	// enter GLUT event processing cycle
    	glutMainLoop();
        return 0;
     
    }

    thanks for making it this far down
    how can i get scroll bars so as to reduce the length of a post?
    am using win7 with vs2005
    am new to openGL, please be gentle [img]<<GRAEMLIN_URL>>/smile.gif[/img]

Similar Threads

  1. Replies: 0
    Last Post: 01-03-2019, 01:10 AM
  2. Replies: 0
    Last Post: 12-27-2016, 05:11 AM
  3. Rotations and Transformations, Move the player or move the world?
    By neurocase in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-14-2014, 12:41 AM
  4. move cube on circle
    By alex2 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-06-2011, 06:20 AM
  5. how 2 animate
    By in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 10-10-2002, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean