Search code examples
delphi3drotationcube

delphi cube rotation


I want to rotate a cube on my screen around its axis.

I found this example in javascript which uses the 3D rotation matrix. I tried to make something similar in my application but the cube doesn't want to rotate and I don't know where's the problem.

The cube is moving in different positions (like viewing from a perfect angle) but it does not show the motion until that view. It's something like view from top, bottom, front and side.

Here's my code:

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;

Where's the problem?


Solution

  • You are using integer numbers to represent angles. cos and sin functions are using floating numbers (radian). So in your case angles of rotation are changing by ~57 degrees descrete.