2

我想在我的屏幕上围绕它的轴旋转一个立方体。

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

立方体在不同的位置移动(比如从一个完美的角度观察),但直到那个视图它才显示运动。这有点像从顶部、底部、正面和侧面的视图。

这是我的代码:

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;

问题出在哪里?

4

2 回答 2

7

您正在使用整数来表示角度。cos 和 sin 函数使用浮点数(弧度)。因此,在您的情况下,旋转角度的变化幅度约为 57 度。

于 2012-12-29T18:35:10.977 回答
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;
于 2012-12-30T01:26:28.093 回答