tzortzik
tzortzik

Reputation: 5133

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?

Upvotes: 2

Views: 1551

Answers (2)

tzortzik
tzortzik

Reputation: 5133

The solution looks like this:

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;

Upvotes: 2

Dave
Dave

Reputation: 356

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.

Upvotes: 7

Related Questions