how to Rotate only one object by using glrotate in java

233 views Asked by At

I am trying to rotate only one square but it did not work it rotates all of them

in this code when I click N it creates a new square and pushes it in the array I want when I click R to

rotates specific square not all of them

when I search on google I found that glrotate move all the objects created after the calling and I did

know if it is right package assignment;

import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.Random;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;


public class input {

private static final ArrayList<Square> shapes = new ArrayList<Square>();
private static boolean thingselected=false;
private static boolean flag=true;
public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("Input Demo");
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }
   
    
    
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 640, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    
    shapes.add(new Square(20,20));
    
    while (!Display.isCloseRequested()) {

        glClear(GL_COLOR_BUFFER_BIT);
        if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            Display.destroy();
            System.exit(0);
        }
        


 while(Keyboard.next()) {
            if(Keyboard.getEventKey()==Keyboard.KEY_N && Keyboard.getEventKeyState()) {
                shapes.add(new Square(15,15));
            }
   }
    
            for (final Square square:shapes) {
                System.out.println(square.rotated);
                if(Mouse.isButtonDown(0) && square.ismoved(Mouse.getX(),480-`Mouse.getY())&&!thingselected) {`
                    square.selected=true;
                thingselected=true;
            }
            
            if(square.selected) {
                square.update(Mouse.getDX(), -Mouse.getDY());
            }
            
            if(Mouse.isButtonDown(1) ) {
                square.selected=false;
                thingselected=false;
            }
            

            if(Keyboard.isKeyDown(Keyboard.KEY_C)&&square.ismoved(Mouse.getX(),480-Mouse.getY())&&!thingselected ) {
                square.changeColor();
            }
            
              if(Keyboard.getEventKey() == Keyboard.KEY_R && square.ismoved(Mouse.getX(),480-Mouse.getY())){
                        if(flag)
                            square.rotated=true;
                        if(square.rotated)
                        {
                            square.rotate();
                            
                        }
                        flag = false;
              }
              if(Keyboard.getEventKey() == Keyboard.KEY_T) {
                    square.transelate();
                }
            square.draw();
            
        }
        
        
        Display.update();
        Display.sync(60);
    }

    Display.destroy();
    }

    
private static class Square{
    
    public int x,y;
    private float red,green,blue;
    public boolean selected=false;
    public boolean rotated=false;
    
    Square(int x, int y){
        this.x=x;
        this.y=y;
        
        Random random=new Random();
        red=random.nextFloat();
        green=random.nextFloat();
        blue=random.nextFloat();

    }
    
    
    void draw() {
        glColor3f(red, green, blue);
        glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x+50, y);
        glVertex2f(x+50, y+50);
        glVertex2f(x, y+50);

        glEnd();
    }
    
    boolean ismoved(int mousex,int mousey) {
        return mousex > x && mousex < x+50 && mousey > y && mousey < y+50;
    }
    
    void update(double dx,double dy) {
        x+=dx;
        y+=dy;
    }
    
    void changeColor() {
        Random random=new Random();
        red=random.nextFloat();
        green=random.nextFloat();
        blue=random.nextFloat();
    }
    
    
    void transelate() {
        glTranslated(0.1, 0, 0);
    }
    
    void rotate() {
            
            glRotated(0.1, 0, 0, 1);
            
            
    }
}
1

There are 1 answers

2
Rabbid76 On

glRotate and glTranslate manipulate the current matrix. You have to do the rotation and translation before drawing the object. Use glPushMatrix and glPopMatrix (see glPushMatrix / glPopMatrix) to save the current matrix on the matrix stack before changing it and to restore it after the object is drawn.
Add variables for the rotation and translation and change them in translate an roatate. Use the variables in draw:

private static class Square {

    private double translateX, angleZ;

    // [...]

    void draw() {
       
        glPushMatrix(); 
       
        glTranslated(translateX, 0, 0);
        glRotated(angleZ, 0, 0, 1);  

        glColor3f(red, green, blue);
        glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x+50, y);
        glVertex2f(x+50, y+50);
        glVertex2f(x, y+50);
        glEnd();

        glPopMatrix();
    }

    void transelate() {
        translateX += 0.1;
    }
    
    void rotate() {       
        angleZ += 0.1;        
    }
}