Rubiks Cube segments rotation methods

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Rubiks Cube segments rotation methods

Jakub_Klopotek
Hi, I've been trying to make a Rubiks cube in java3d. Right now I'm stuck at rotation problem. How to rotate segments of the cube by the least effort? I feel like I have to paste all my code in.

```
package rubikscubegeneral;

import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;

import com.sun.j3d.exp.swing.JCanvas3D;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.swing.*;
import java.awt.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.Transform3D;
import javax.vecmath.*;


import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.ViewingPlatform;
import sun.java2d.pipe.SpanShapeRenderer;

import java.awt.event.*;


public class RubiksCubeGeneral extends JFrame implements ActionListener, KeyListener {

    private Button reset = new Button("reset kamery");  // cam reset
    SimpleUniverse simpleU;
    Vector3f[] pozycja = new Vector3f[8];       // vectors for cubes
    TransformGroup[] tg = new TransformGroup[8];   // transform for cubes
    public RubiksCubeGeneral() {


        super("Kostka Rubika");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);


        GraphicsConfiguration config =
                SimpleUniverse.getPreferredConfiguration();


        Canvas3D canvas3D = new Canvas3D(config);
        canvas3D.setPreferredSize(new Dimension(800, 600));

        add(canvas3D);
        pack();
        setVisible(true);

        BranchGroup scena = utworzScene();
        scena.compile();
         canvas3D.addKeyListener(this);        // key listener for canvas
        simpleU = new SimpleUniverse(canvas3D);
        //obsever placing
        Transform3D przesuniecie_obserwatora = new Transform3D();
        przesuniecie_obserwatora.set(new Vector3f(0, 0, 20.0f));

        simpleU.getViewingPlatform().getViewPlatformTransform().setTransform(przesuniecie_obserwatora);
        ViewingPlatform viewingPlatform = simpleU.getViewingPlatform();

        //orbital camera
        OrbitBehavior orbit = new OrbitBehavior(canvas3D, OrbitBehavior.REVERSE_ALL);
        BoundingSphere sceneBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
        orbit.setSchedulingBounds(sceneBounds);
        viewingPlatform.setViewPlatformBehavior(orbit);

        simpleU.addBranchGraph(scena);
        // reset camera button
        setLayout(new BorderLayout());
        Panel bottom = new Panel();
        bottom.add(reset);
        reset.addActionListener(this);
        add(BorderLayout.CENTER, canvas3D);
        add(BorderLayout.SOUTH, bottom);
        setVisible(true);
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == reset) {
            Transform3D przesuniecie_obserwatora = new Transform3D();
            przesuniecie_obserwatora.set(new Vector3f(0, 0, 20.0f));

            simpleU.getViewingPlatform().getViewPlatformTransform().setTransform(przesuniecie_obserwatora);
        }


    }


    BranchGroup utworzScene() {
        BranchGroup wezel_scena = new BranchGroup();
        BoundingSphere bounds = new BoundingSphere(new Point3d(0, 0, 0), 10);  
        //lights
        AmbientLight lightA = new AmbientLight();
        lightA.setInfluencingBounds(bounds);
        wezel_scena.addChild(lightA);


        // cubes declaration
        ColorCube[] szescian = new ColorCube[8];

        Transform3D[] transform = new Transform3D[8];

        // vectors to move cubes by ( at the start)
        pozycja[0] = new Vector3f(-1.025f, 1.025f, 1.025f);
        pozycja[1] = new Vector3f(1.025f, 1.025f, 1.025f);
        pozycja[2] = new Vector3f(-1.025f, -1.025f, 1.025f);
        pozycja[3] = new Vector3f(1.025f, -1.025f, 1.025f);
        pozycja[4] = new Vector3f(-1.025f, 1.025f, -1.025f);
        pozycja[5] = new Vector3f(1.025f, 1.025f, -1.0025f);
        pozycja[6] = new Vector3f(-1.025f, -1.025f, -1.025f);
        pozycja[7] = new Vector3f(1.025f, -1.025f, -1.025f);
        // placing the cubes in their spots
        for (int i = 0; i < 8; i++) {
            szescian[i] = new ColorCube();
            tg[i] = new TransformGroup();
            tg[i].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            tg[i].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            transform[i] = new Transform3D();


            transform[i].setTranslation(pozycja[i]);

            tg[i].setTransform(transform[i]);

            tg[i].addChild(szescian[i]);

            wezel_scena.addChild(tg[i]);
        }


        return wezel_scena;
    }


    public static void main(String args[]) {
        new RubiksCubeGeneral();

    }
   // key listener for rotations ( not done)
    @Override
    public void keyTyped(KeyEvent e) {
        switch (e.getKeyChar()) {
            case 'q': { }
            break;
            case 'w': {}
            break;
            case 'a':{} break;
            case 's':{}break;
            case 'r':{} break;
            case 't':{} break;
            case 'f':{} break;
            case 'g':{} break;
        }
    }
    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
   

}

```

Im new to java so sorry if this question does not makes sense.
Reply | Threaded
Open this post in threaded view
|

Re: Rubiks Cube segments rotation methods

gouessej
Administrator
Hello

You can use this project as a source of inspiration (I thank DuckDuckGo by the way):
https://github.com/MSaito/RubikCubeAnimationJava3d
Julien Gouesse | Personal blog | Website