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 5 of 5

Thread: Translation

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6

    Translation

    I have a problem with translation of sphere. I make a game-Frozen Bubbles. The goal is to move the middle sphere in the bottom of the screen to the last row spheres from up to down. But the sphere doesn't move. I tried different variants-with translation of every point of the sphere, translation of the sphere, change of the coordinates of every point ot the sphere. This must happen, when the left button of mouse is down. That`s why this code is in mouse() function. The thing, that happen is that the arrow, which define the direction of the sphere stop moving.
    This is my code:
    void mouse(int button, int state, int x, int y)
    {
    int t=132, s=8;
    if (button==GLUT_LEFT_BUTTON) {
    if (state==GLUT_DOWN) {
    if (((248*x-32736-16*x*l+2112*l)/(y-8)>=0) && ((248*x-32736-16*x*l+2112*l)/(y-8)<=264)) {
    for (int p=0; p<=304; p++)
    {
    for (int v=0; v<=272; v++)
    {
    if ((p-132)*(p-132) + (v-8)*(v-8)<=64)
    {
    glPushMatrix();
    p+=(248*x-16*x*l+2112*l-132*y-31680)/(y-8);
    v+=240-16*l;
    glTranslatef((248*x-16*x*l+2112*l-132*y-31680)/(y-8), 240-16*l, 0);
    glVertex2f(p, v);
    glPopMatrix();
    }
    }
    }
    }
    else if ((248*x-32736-16*x*l+2112*l)/(y-8)<0)
    {
    int f=0;
    if (l%2)
    {
    for (int p=0; p<=304; p++)
    {
    for (int v=0; v<=272; v++)
    {
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
    {
    glPushMatrix();
    p-=124;
    v+=240-16*l;
    glTranslatef(-124, 240-16*l, 0);
    glVertex2f(p, v);
    glPopMatrix();
    }
    }
    }
    }
    else
    {
    for (int p=0; p<=304; p++)
    {
    for (int v=0; v<=272; v++)
    {
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
    {
    glPushMatrix();
    p-=116;
    v+=240-16*l;
    glTranslatef(-116, 240-16*l, 0);
    glVertex2f(p, v);
    glPopMatrix();
    }
    }
    }
    }
    }
    else
    {
    int b=15;
    if (l%2)
    {
    for (int p=0; p<=304; p++)
    {
    for (int v=0; v<=272; v++)
    {
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
    {
    glPushMatrix();
    p+=116;
    v+=240-16*l;
    glTranslatef(116, 240-16*l, 0);
    glVertex2f(p, v);
    glPopMatrix();
    }
    }
    }
    }
    else
    {
    for (int p=0; p<=304; p++)
    {
    for (int v=0; v<=272; v++)
    {
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
    {
    glPushMatrix();
    p+=124;
    v+=240-16*l;
    glTranslatef(124, 240-16*l, 0);
    glVertex2f(p, v);
    glPopMatrix();
    }
    }
    }
    }
    }

    }
    }
    }
    And this is the screen:
    Click image for larger version. 

Name:	Screen.jpg 
Views:	46 
Size:	87.9 KB 
ID:	816

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    Please put your code in '[code'] ... '[/code'] tags (without the quotes of course). You make people's eyes hurt.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    I have a problem with translation of sphere. I make a game-Frozen Bubbles. The goal is to move the middle sphere in the bottom of the screen to the last row spheres from up to down. But the sphere doesn't move. I tried different variants-with translation of every point of the sphere, translation of the sphere, change of the coordinates of every point ot the sphere. This must happen, when the left button of mouse is down. That`s why this code is in mouse() function. The thing, that happen is that the arrow, which define the direction of the sphere stop moving.
    This is my code:

    Code :
    void mouse(int button, int state, int x, int y)
     {
     int t=132, s=8;
     if (button==GLUT_LEFT_BUTTON) {
     if (state==GLUT_DOWN) {
     if (((248*x-32736-16*x*l+2112*l)/(y-8)>=0) && ((248*x-32736-16*x*l+2112*l)/(y-8)<=264)) {
     for (int p=0; p<=304; p++)
     {
     for (int v=0; v<=272; v++)
     {
     if ((p-132)*(p-132) + (v-8)*(v-8)<=64)
     {
     glPushMatrix();
     p+=(248*x-16*x*l+2112*l-132*y-31680)/(y-8);
     v+=240-16*l;
     glTranslatef((248*x-16*x*l+2112*l-132*y-31680)/(y-8), 240-16*l, 0);
     glVertex2f(p, v);
     glPopMatrix();
     }
     }
     }
     }
     else if ((248*x-32736-16*x*l+2112*l)/(y-8)<0)
     {
     int f=0;
     if (l%2)
     {
     for (int p=0; p<=304; p++)
     {
     for (int v=0; v<=272; v++)
     {
     if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
     {
     glPushMatrix();
     p-=124;
     v+=240-16*l;
     glTranslatef(-124, 240-16*l, 0);
     glVertex2f(p, v);
     glPopMatrix();
     }
     }
     }
     }
     else
     {
     for (int p=0; p<=304; p++)
     {
     for (int v=0; v<=272; v++)
     {
     if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
     {
     glPushMatrix();
     p-=116;
     v+=240-16*l;
     glTranslatef(-116, 240-16*l, 0);
     glVertex2f(p, v); 
    glPopMatrix();
     }
     }
     }
     }
     }
     else
     {
     int b=15;
     if (l%2)
     {
     for (int p=0; p<=304; p++)
     {
     for (int v=0; v<=272; v++)
     {
     if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
     {
     glPushMatrix();
     p+=116;
     v+=240-16*l;
     glTranslatef(116, 240-16*l, 0);
     glVertex2f(p, v);
     glPopMatrix();
     }
     }
     }
     }
     else
     {
     for (int p=0; p<=304; p++)
     {
     for (int v=0; v<=272; v++)
     {
     if ((p-132)*(p-132)+(v-8)*(v-8)<=64)
     {
     glPushMatrix();
     p+=124;
     v+=240-16*l;
     glTranslatef(124, 240-16*l, 0);
     glVertex2f(p, v);
     glPopMatrix();
     }
     }
     }
     }
     }
     
     }
     }
     }

    And this is the screen:

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    Ok, it doesn't help at all of if you put your code in code tags but copy the unindented code into the section - that completely defeats the purpose.


    This code (behold the indentation) will absolutely not do what you expect:

    Code :
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)  
    {  
        glPushMatrix();  
        p+=124;  
        v+=240-16*l;  
        glTranslatef(124, 240-16*l, 0);  
        glVertex2f(p, v);  
        glPopMatrix();
    }

    Since you're using immediate mode, what is this supposed to do? First, you push (hopefully) the MODELVIEW stack. Then you do some magic number acrobatics with completely unintuitively named variabled. Then you invoke glVertex*f() - it looks like this is happening outside of a glBegin/glEnd block - which simply results in undefined behavior. And even if you do start the glBegin/glEnd block outside the function it still would't behave correctly because calling glPushMatrix/glPopMatrix is illegal before calling glEnd. So is glTranslatef.

    This is what a correct batch launch looks like:
    Code :
    {
        glPushMatrix();  
        glTranslatef(someX, someY, someZ);  
        glBegin(GL_WHATEVER_PRIMITIVE_TYPE);
        glVertex2f(p, v);  
        // possibly more vertices or other attributes.
        //...
        glEnd();
        glPopMatrix();
    }

    BTW, please stop using magic numbers and names that have no meaning to anyone else currently working on the piece of code - i.e. you. No one will ever be able to really know what your code does except they sit down, stare at your code and your screen shot and apply some ninja coding wisdom to deduce what your code is supposed to mean.

    Code :
    if ((p-132)*(p-132)+(v-8)*(v-8)<=64)  
    ...
    v+=240-16*l;

    The above is simply bad code. Hands down. You might think to yourself "I know what I write and I know what I was thinking when I wrote it" but you will get stuck if you let it rest for a while and look at this atom bomb of code later.

    In general, I suggest you work through a tutorial or book on OpenGL, in your case sadly legacy OpenGL, and a likewise for C/C++ programming.
    Last edited by thokra; 07-19-2012 at 09:04 AM. Reason: Mistakenly posted too early.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    I did this, that you say. In the beginning the application run, but now it doesn't run. I changed the names of variable to be more meaningful.
    Here is the code:
    Code :
    void mouse(int button, int state, int x, int y)
    {
    	int cx=132, cy=8;
    	if (button==GLUT_LEFT_BUTTON) {
    		if (state==GLUT_DOWN) {
    				if (((248*x-32736-16*x*row_number+2112*row_number)/(y-8)>=0) && ((248*x-32736-16*x*row_number+2112*row_number)/(y-8)<=264)) {
    for (int X=0; X<=304; X++)
    {
    						for (int Y=0; Y<=272; Y++)
    						{
    if ((X-132)*(X-132) + (Y-8)*(Y-8)<=64)
    							{
    glPushMatrix();
    glTranslatef((248*x-16*x*row_number+2112*row_number-132*y-31680)/(y-8), 240-16*row_number, 0);
    								glBegin(GL_POINTS);
    								glVertex2f(X, Y);
    								glEnd();
    glPopMatrix();
     }
    						}
    }
    }
                else if ((248*x-32736-16*x*row_number+2112*row_number)/(y-8)<0)
    							{
    								int f=0;
    								if (row_number%2)
    								{
    									for (int X=0; X<=304; X++)
    									{
    										for (int Y=0; Y<=272; Y++)
    										{
    if ((X-132)*(X-132)+(Y-8)*(Y-8)<=64)
    											{
    glPushMatrix();
    glTranslatef(-124, 240-16*row_number, 0);
    									glBegin(GL_POINTS);
    									glVertex2f(X, Y);
    												glEnd();
    glPopMatrix();
    }
    										}
    }
    								}
    								else
    								{
    									for (int X=0; X<=304; X++)
    									{
    										for (int Y=0; Y<=272; Y++)
    										{
    if ((X-132)*(X-132)+(Y-8)*(Y-8)<=64)
    											{
    glPushMatrix();
    glTranslatef(-116, 240-16*row_number, 0);
    											    glBegin(GL_POINTS);
    												glVertex2f(X, Y);
    												glEnd();
    glPopMatrix();
    }
    									}
    									}
    }
    								}
    							else
    							{
    								int b=15;
    								if (row_number%2)
    								{
    									for (int X=0; X<=304; X++)
    									{
    										for (int Y=0; Y<=272; Y++)
    										{
    if ((X-132)*(X-132)+(Y-8)*(Y-8)<=64)
    											{
    glPushMatrix();
    glTranslatef(116, 240-16*row_number, 0);
    												glBegin(GL_POINTS);
    												glVertex2f(X, Y);
    glPopMatrix();
    }
    }
    								}
    								else
    								{
    									for (int X=0; x<=304; X++)
    									{
    										for (int Y=0; Y<=272; Y++)
    										{
     
    if ((X-132)*(X-132)+(Y-8)*(Y-8)<=64)
    {
    glPushMatrix();
    glTranslatef(124, 240-16*row_number, 0);
    												glBegin(GL_POINTS);
    												glVertex2f(X, Y);
    												glEnd();
    glPopMatrix();
    }
    }
    									}
    }
     
    										}
    							}
    							}

Posting Permissions

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