Rotate 3d Cube Java

0

Good, this is my first question and I do not know if this goes here but I will try to be as clear as possible, I have the following:

    private final int B_WIDTH = 850;
    private final int B_HEIGHT = 850;  
    private final int DELAY = 25;
    private Thread animator;
    private Punto[] p = new Punto[8];
    private int x = 50, y=150;
    float YP[] = new float[2];
    float XP[] = new float[2];

    private int[][] matrizRotacion = new int[3][3];
        public Line(){
            initLine();
        }

        private void initLine(){
            setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
            setDoubleBuffered(true);

            p[0] = new Punto(x,x,y);
            p[1] = new Punto(y,x,y);
            p[2] = new Punto(y,x,x);
            p[3] = new Punto(x,x,x);
            p[4] = new Punto(x,y,x);
            p[5] = new Punto(y,y,x);
            p[6] = new Punto(y,y,y);
            p[7] = new Punto(x,y,y);
        }

        @Override
        public void addNotify() {
            super.addNotify();

            animator = new Thread(this);
            animator.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.translate(200, 10);
            pintar(g);

        }

        public void pintar(Graphics g){
            g.drawLine(p[0].XP, p[0].YP, p[1].XP, p[1].YP);
            g.drawLine(p[1].XP, p[1].YP, p[2].XP, p[2].YP);
            g.drawLine(p[2].XP, p[2].YP, p[3].XP, p[3].YP);
            g.drawLine(p[3].XP, p[3].YP, p[0].XP, p[0].YP);

            g.drawLine(p[6].XP, p[6].YP, p[5].XP, p[5].YP);        
            g.drawLine(p[5].XP, p[5].YP, p[4].XP, p[4].YP);
            g.drawLine(p[4].XP, p[4].YP, p[7].XP, p[7].YP);
            g.drawLine(p[7].XP, p[7].YP, p[6].XP, p[6].YP);

            g.drawLine(p[6].XP, p[6].YP, p[1].XP, p[1].YP);
            g.drawLine(p[0].XP, p[0].YP, p[7].XP, p[7].YP);
            g.drawLine(p[3].XP, p[3].YP, p[4].XP, p[4].YP);
            g.drawLine(p[2].XP, p[2].YP, p[5].XP, p[5].YP);
            Toolkit.getDefaultToolkit().sync();
        }



        private void cycle() {
         // x +=1;
         // y += 1;
            for (int i = 0; i < 100; i++) {
                initLine();
            }


        }

        @Override
        public void run() {
        long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();

        while (true) {

            cycle();

            repaint();

            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;

            if (sleep < 0) {
                sleep = 2;
            }

            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                System.out.println("Interrupted: " + e.getMessage());
            }

            beforeTime = System.currentTimeMillis();
        }
    }   
} 

I also have a Point class and a class that draws the panel where that is:

public class Punto {
        double x,y,z;
        int XP;
        int YP;
        public Punto(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;

            get2DX();
            get2DY();

        }

        private int get2DX() { 
            XP = (int) (x+z*(Math.cos(90)));
            return XP;
        }

        private int get2DY() { 
            YP = (int) (y+z*(Math.sin(90)));
            return YP;
        }
    } 

This draws a 3d cube, but I want to make this cube rotate with some axis and I honestly do not understand how to do the rotation matrix

| cos, -sen 0 | | x |

| sin cos 0 | * | y | something like that ..

| 0 0 1 | | z |

So my question is if someone can explain or show me an example of how I can do this? or if what I have is fine (I know there are methods to make this easy but the point is to understand how it works).

thanks.

    
asked by Andres M 31.05.2017 в 01:11
source

0 answers