0

I am having trouble in implementing a robotic arm which can pick objects, because of the glTranslate(), glRotate() calls I have in my implementation each and every part of the robot is dependent on the previous part.

Example:

     void drawRobo()
         {

              draw a Cylinder as Base;

              glTranslate(); 
              drawArmpart1();
              glTranslate();  
              drawJoint1();
              glRotate();  
              drawArmpart2();
              glTranslate();
              drawGrabbers(); // The claw or the endeffector   


         }
    void drawObjects()
          {
           glTranslate();
           drawCube() // Object
           glTranslate();
           drawSphere() // Object 2

          }

    void display()
      {
       drawRobo();
       drawObjects();
       glPostredisplay();
      }   

Now the problem is when i rotate the the endeffector or the grabber using glRotate(); my objects rotate as well, I don't want that. I want to be able to rotate the joints and the arm such that it comes closer to objects and then i want to pick them using the grabber.

How do i deal with this? I have the glPushMAtrix() and glPopMatrix() commands at all places where i need them.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Bounty Collector
  • 615
  • 7
  • 19

1 Answers1

4

Since you have this problem, it's obvious that you don't have appropriate push and pop matrix operations. Your pseudocode should be:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// setup view matrix

glPushMatrix();
// setup robot's world matrix
drawRobo();
glPopMatrix();

// setup object's world matrix
drawObjects();

glutPostRedisplay();

Notice that last draw call don't use push/pop - just because it is last so you don't need to recover matrix after it.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • Thanks, my objects don't rotate anymore.. last glPopMatrix(); was misplaced.Also, how do i know that my arm has reached closer to an object, so that i can pick it up? The objects are translated and rotated from the endeffector and then draw... Is there an easy way to get the reverse transformation so that everything is in the same coordinate space? – Bounty Collector Oct 24 '13 at 12:06
  • There is. You can calculate robot's world matrix (with the same values you passing to GL) and multiply it by arm coordinates. As a result, you'll get your arm position in world (i.e. global) space. Then `length(arm_position - object_position)` will give you distance. This will require some knowledge of linear and vector algebra, tho - but honestly, this is not GL-related at all - just math. – keltar Oct 24 '13 at 12:44