0

I'm trying to write a code where my polygon moves from left to right ends of my screen. The main issue I shared is that I'm not able to translate this using fps animator
This is my polygon code:

private void drawface(GL2 gl)
    {
        gl.glBegin(GL2.GL_POLYGON);
        gl.glVertex2f(0, 130);
        gl.glVertex2f(120, 130);
        gl.glVertex2f(120, 140);
        gl.glVertex2f(0, 140);
        gl.glEnd();

    } 


Then this is my display function:

public void display(GLAutoDrawable arg0)
    {
        final GL2 gl = arg0.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();



        gl.glPushMatrix();
        gl.glColor3f(0.5f, 1f, 0.75f);
        gl.glTranslatef(t1x, 1, 1);
        drawface(gl);
        gl.glPopMatrix();
        }


I'm updating the int t1x through a KeyEvent method

public void keyPressed(KeyEvent arg0) 
    {
        int key = arg0.getKeyCode();
        if(key == KeyEvent.VK_RIGHT)
         {
            t1x++;
         }
        else if(key == KeyEvent.VK_LEFT)
            t1x--;

    }


But my polygon isn't translating when I click the right arrow key. What should I do ?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
TheUnluckyVJ
  • 42
  • 1
  • 12
  • Have you debugged this? Are you sure everything is called? – n247s Sep 18 '19 at 16:37
  • @n247s yes I'm sure I have called everything else. Even the polygon is getting displayed. However it's not moving. – TheUnluckyVJ Sep 18 '19 at 16:51
  • I specifically mean uppon key-press. Since that could explain why nothing is updating – n247s Sep 18 '19 at 17:06
  • @n247s Even if I update the values in the display functions without using the KeyPressed it's still not translating. – TheUnluckyVJ Sep 18 '19 at 17:29
  • From the coordinates I assume you have a orthographic projection with screenwidth/screenheight size. In this case, you'll move the object by just one pixel every press. If that's not the case, please show your projection matrix setup. – BDL Sep 19 '19 at 06:50
  • @BDL Yes I move one pixel every time but it has to move right? Or is there something else wrong with my code? `@Override public void init(GLAutoDrawable arg0) { // TODO Auto-generated method stub GL2 gl = arg0.getGL().getGL2(); GLU glu = new GLU(); gl.glClearColor(0.0f,0.0f,0.0f,0.0f); gl.glViewport(-250, -200, 250, 200); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(-250, 250, -200, 200); }` – TheUnluckyVJ Sep 19 '19 at 08:39
  • Negative coordinates in glViewport are definitely wrong and will give an GL_INVALID_VALUE error. Do you check for errors somewhere? – BDL Sep 19 '19 at 08:43
  • @BDL I did check for errors and there was no errors especially no GL_INVALID_VALUE error. By the way, how is the glViewport wrong? – TheUnluckyVJ Sep 19 '19 at 08:59
  • @TheUnluckyVJ: sorry, I have a stupid day today. You are right. Width and height may not be negative, but that's not the case for you. It's nevertheless unusual to have negative coordinates for x,y. For the viewport you define, only a quarter of the visible area lies inside the window, the rest is probably thrown away. – BDL Sep 19 '19 at 09:09

0 Answers0