德尔福立方体旋转

时间:2012-12-29 17:25:37

标签: delphi 3d rotation cube

我想围绕轴旋转我的屏幕上的立方体。

我在javascript中找到了这个使用3D旋转矩阵的例子。我尝试在我的应用程序中制作类似的东西但是立方体不想旋转,我不知道问题出在哪里。

立方体在不同的位置移动(例如从完美的角度观看),但直到该视图才显示运动。从顶部,底部,正面和侧面看都是如此。

这是我的代码:

dimension: Integer = 1;
a: Integer = 0;
b: Integer = 0;
ite: Integer = 27;
ite1: Integer;
ite2: Integer;
ite3: Integer;
x: Integer;
y: Integer;
z: Integer;
u: Integer = 0;
v: Integer = 0;
w: Integer = 0;
u2: Integer = 0;
v2: Integer = 0;
w2: Integer = 0;
c: Integer = 0;
pts: TpointArray;

ite := 0;
a := round(Mouse.CursorPos.X/99);
b := round(Mouse.CursorPos.Y/99);
for x := -dimension to dimension do
begin
  for y := -dimension to dimension do
  begin
    for z := -dimension to dimension do
    begin
      u := x;
      v := y;
      w := z;
      u2 := round(u*cos(a)-v*sin(a));
      v2 := round(u*sin(a)+v*cos(a));
      w2 := w;
      u := u2; v := v2; w := w2;
      u2 := u;
      v2 := round(v*cos(b)-w*sin(b));
      w2 := round(v*sin(b)+w*cos(b));
      u := u2; v := v2; w := w2;
      pts[ite].X := 200+u*(w+2)*30;
      pts[ite].Y := 200+v*(w+2)*30;
      Ellipse(pts[ite].X,pts[ite].Y,pts[ite].X+10,pts[ite].Y+10);
      ite := ite + 1;
    end;
  end;
end;

for ite1 := 0 to 25 do
begin
  for ite2 := 1 to 26 do
  begin
    MoveTo(pts[ite1].X,pts[ite1].Y);
    LineTo(pts[ite2].X,pts[ite2].Y);
  end;
end;

问题出在哪里?

2 个答案:

答案 0 :(得分:7)

您使用整数表示角度。 cos和sin函数使用浮点数(弧度)。所以在你的情况下,旋转角度会变化〜57度离散。

答案 1 :(得分:2)

解决方案如下所示:

a: Double = 0;
b: Double = 0;
ite: Integer = 27;
ite1: Integer;
ite2: Integer;
ite3: Integer;
x: Integer;
y: Integer;
z: Integer;
u: Double = 0;
v: Double = 0;
w: Double = 0;
u2: Double = 0;
v2: Double = 0;
w2: Double = 0;
c: Integer = 0;
pts: TpointArray;

ite := 0;
      a := Mouse.CursorPos.X/99;
      b := Mouse.CursorPos.Y/99;
      for x := -dimension to dimension do
      begin
        for y := -dimension to dimension do
        begin
          for z := -dimension to dimension do
          begin
            u := x;
            v := y;
            w := z;
            u2 := u*cos(a)-v*sin(a);
            v2 := u*sin(a)+v*cos(a);
            w2 := w;
            u := u2; v := v2; w := w2;
            u2 := u;
            v2 := v*cos(b)-w*sin(b);
            w2 := v*sin(b)+w*cos(b);
            u := u2; v := v2; w := w2;
            pts[ite].X := round(200+u*(w+5)*30);
            pts[ite].Y := round(200+v*(w+5)*30);
            Ellipse(pts[ite].X,pts[ite].Y,pts[ite].X+10,pts[ite].Y+10);
            ite := ite + 1;
          end;
        end;
      end;

      for ite1 := 0 to 25 do
      begin
        for ite2 := 1 to 26 do
        begin
           MoveTo(pts[ite1].X,pts[ite1].Y);
           LineTo(pts[ite2].X,pts[ite2].Y);
        end;
      end;