我想出了一个使用TriangleMesh
构建的2x2魔方的模型,并使用不同的变换对它们进行转换。现在我想为立方体添加动画。我不知道一个很好的算法,但我自己尝试了一些东西。我制作了四个Group
个物体并添加了立方体,这些立方体形成了它的正面,背面,左面和右面。我还使用类型Node
的数组以顺序方式存储所有立方体,以便跟踪立方体的每个移动将代表前,后,左和右面的立方体。
Here's a snapshot of the problem.
正面和背面旋转精细,如果没有左右两面。一旦我移动左脸,立方体重叠。我犯了什么错误?有更好的方法来执行动画吗?
这是该计划的动画部分。
//ADDING ANIMATIONS TO THE CUBE
Button F = new Button("F");
Button F_ = new Button("F_");
Button B = new Button("B");
Button B_ = new Button("B_");
Button L = new Button("L");
Button L_ = new Button("L_");
Button R = new Button("R");
Button R_ = new Button("R_");
Button X = new Button("X");
Button X_ = new Button("X_");
Button Y = new Button("Y");
Button Y_ = new Button("Y_");
Button Z = new Button("Z");
Button Z_ = new Button("Z_");
Group front=new Group(c1,c2,c5,c6);
Group back=new Group(c3,c4,c7,c8);
Group right=new Group(c1,c4,c5,c8);
Group left=new Group(c2,c3,c6,c7);
PerspectiveCamera camera2 = new PerspectiveCamera(true);
//moves- normal letters represent clockwise and underscored ones represent anticlockwise rotations.
F.setTranslateY(200);
F_.setTranslateY(170);
B.setTranslateY(140);
B_.setTranslateY(110);
L.setTranslateY(80);
L_.setTranslateY(50);
R.setTranslateY(20);
R_.setTranslateY(-10);
X.setTranslateY(-40);
X_.setTranslateY(-70);
Y.setTranslateY(-100);
Y_.setTranslateY(-130);
Z.setTranslateY(-160);
Z_.setTranslateY(-190);
Node ar[]={/*front*/c1,c5,c6,c2,/*back*/c4,c8,c7,c3,/*right*/c1,c5,c8,c4,/*left*/c2,c6,c7,c3};
//Groups of rectangles with each 4 of them taken together in sequence represent the
//initial front, back, right, and left faces respectively. The array is to updated with each
//movement of the cube to represent the faces correctly.
Node ar_prev[]={/*front*/c1,c5,c6,c2,/*back*/c4,c8,c7,c3,/*right*/c1,c5,c8,c4,/*left*/c2,c6,c7,c3};
Group r=new Group(F,F_,B,B_,L,L_,R,R_);
Scene s=new Scene(r,600,600,true);
camera2.setNearClip(0.1);
camera2.setFarClip(10000.0);
F.setOnAction(e -> {
front.getChildren().removeAll(ar_prev[0],ar_prev[1],ar_prev[2],ar_prev[3]);
front.getChildren().addAll(ar[0],ar[1],ar[2],ar[3]);
int ctr=0;
for(Node i:ar)
{
ar_prev[ctr++]=i;
}
front.setRotationAxis(Rotate.Z_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), front);
rt.setByAngle(90);
rt.play();
Node temp=ar[3];
ar[3]=ar[2];
ar[12]=ar[2];//for left
ar[2]=ar[1];
ar[13]=ar[1];//for left
ar[1]=ar[0];
ar[9]=ar[0];//for right
ar[0]=temp;
ar[8]=temp;//for right
});
F_.setOnAction(e -> {
front.getChildren().removeAll(ar_prev[0],ar_prev[1],ar_prev[2],ar_prev[3]);
front.getChildren().addAll(ar[0],ar[1],ar[2],ar[3]);
int ctr=0;
for(Node i:ar)
{
ar_prev[ctr++]=i;
}
front.setRotationAxis(Rotate.Z_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), front);
rt.setByAngle(-90);
rt.play();
Node temp=ar[0];
ar[0]=ar[1];
ar[8]=ar[1];//right
ar[1]=ar[2];
ar[9]=ar[2];//right
ar[2]=ar[3];
ar[13]=ar[3];//left
ar[3]=temp;
ar[12]=temp;//left
});
root.getChildren().addAll(front);
B.setOnAction(e -> {
back.getChildren().removeAll(ar_prev[4],ar_prev[5],ar_prev[6],ar_prev[7]);
back.getChildren().addAll(ar[4],ar[5],ar[6],ar[7]);
int ctr=0;
for(Node i:ar)
{
ar_prev[ctr++]=i;
}
back.setRotationAxis(Rotate.Z_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), back);
rt.setByAngle(90);
rt.play();
Node temp=ar[7];
ar[7]=ar[6];
ar[15]=ar[6];//for left
ar[6]=ar[5];
ar[14]=ar[5];//for left
ar[5]=ar[4];
ar[10]=ar[4];//for right
ar[4]=temp;
ar[11]=temp;//for right
});
B_.setOnAction(e -> {
back.getChildren().removeAll(ar_prev[4],ar_prev[5],ar_prev[6],ar_prev[7]);
back.getChildren().addAll(ar[4],ar[5],ar[6],ar[7]);
int ctr=0;
for(Node i:ar)
{
ar_prev[ctr++]=i;
}
back.setRotationAxis(Rotate.Z_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), back);
System.out.println("hhhh");
rt.setByAngle(-90);
rt.play();
Node temp=ar[4];
ar[4]=ar[5];
ar[11]=ar[5];//right
ar[5]=ar[6];
ar[10]=ar[6];//right
ar[6]=ar[7];
ar[14]=ar[7];//left
ar[7]=temp;
ar[15]=temp;//left
});
root.getChildren().addAll(back);
L.setOnAction(e -> {
left.getChildren().removeAll(ar_prev[12],ar_prev[13],ar_prev[14],ar_prev[15]);
left.getChildren().addAll(ar[12],ar[13],ar[14],ar[15]);
int ctr=0;
for(Node i:ar)
{
ar_prev[ctr++]=i;
}
left.setRotationAxis(Rotate.X_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), left);
rt.setByAngle(90);
rt.play();
Node temp=ar[15];
ar[15]=ar[14];
ar[7]=ar[14];//for back
ar[14]=ar[13];
ar[6]=ar[13];//for back
ar[13]=ar[12];
ar[2]=ar[12];//for front
ar[12]=temp;
ar[3]=temp;//for front
});
L_.setOnAction(e -> {
left.setRotationAxis(Rotate.X_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), left);
rt.setByAngle(-90);
rt.play();
});
root.getChildren().addAll(left);
R.setOnAction(e -> {
right.setRotationAxis(Rotate.X_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), right);
rt.setByAngle(90);
rt.play();
});
R_.setOnAction(e -> {
right.setRotationAxis(Rotate.X_AXIS);
RotateTransition rt = new RotateTransition(Duration.millis(2000), right);
rt.setByAngle(-90);
rt.play();
});
root.getChildren().addAll(right);
camera2.getTransforms().addAll (new Translate(0, 0, -1000));
s.setCamera(camera2);
r.getChildren().addAll(scene);
primaryStage.setScene(s);
primaryStage.show();