SRL Archive - RS3 Outdated / Broken Scripts

3d Engine


Killerdou

2008-01-02 07:17:34

Well, maybe not quite an 3D engine, but it draws a cube and knows what sides not to draw:), enough of this talking, here is the script:



type
MyPoint = record
x,y,z : Integer;
end;

Type
TPoly = Array[1..8] of MyPoint;
Type
T_2D = Array[1..8] of TPoint;

var
YRot, XRot : Extended;
DebugCanv, CubeCanv : TCanvas;
TempBmp : integer;

Function SetupBox(Size : integer) : TPoly;
Var
Poly : TPoly;
begin
Poly[1].x := 1 * size;
Poly[1].y := 1 * size;
Poly[1].z := 1 * size;
Poly[2].x := -1 * size;
Poly[2].y := 1 * size;
Poly[2].z := 1 * size;
Poly[3].x := -1 * size;
Poly[3].y := 1 * size;
Poly[3].z := -1 * size;
Poly[4].x := 1 * size;
Poly[4].y := 1 * size;
Poly[4].z := -1 * size;
Poly[5].x := 1 * size;
Poly[5].y := -1 * size;
Poly[5].z := 1 * size;
Poly[6].x := -1 * size;
Poly[6].y := -1 * size;
Poly[6].z := 1 * size;
Poly[7].x := -1 * size;
Poly[7].y := -1 * size;
Poly[7].z := -1 * size;
Poly[8].x := 1 * size;
Poly[8].y := -1 * size;
Poly[8].z := -1 * size;
Result := Poly;
end;

Function _3DTo2D(X,Y,Z: integer; RotX, RotY, RotZ : extended) : TPoint;
begin
Result.x := Round((X*Cos(RotY)-Z*Sin(RotY)));
Result.y := -Round((Y*Cos(RotX)+(-X*Sin(RotY)-Z*Cos(RotY))*Sin(RotX)));
end;



Function CheckSide(Vertices : array of integer; _2D : T_2D) : boolean;
var
I : integer;
Middle : TPoint;
PreviousAngle : Extended;
CurrentAngle : Extended;
BiggerCount : integer;
begin
Middle.x := 0;
Middle.y := 0;
BiggerCount := 0;
For I := 0 to 3 do
begin
Middle.X := Middle.X + _2D[Vertices[I]].x;
Middle.Y := Middle.Y + _2D[Vertices[I]].y;
end;
Middle.x := Round(Middle.x/4);
Middle.y := Round(Middle.y/4);
PreviousAngle := ArcTan2(_2D[Vertices[3]].x - Middle.x, _2d[Vertices[3]].y - Middle.y);
For I := 0 to 3 do
begin
CurrentAngle := ArcTan2(_2D[Vertices[I]].x - Middle.x, _2d[Vertices[I]].y - Middle.y);
If CurrentAngle > PreviousAngle then Inc(BiggerCount);
PreviousAngle := CurrentAngle;
end;
If (BiggerCount >= 3) then Result := True;
end;

Procedure RenderSide(Vertices : Array of Integer; Dots : integer; Canvas : TCanvas; _2D : T_2D);
var
I : integer;
begin
Canvas.MoveTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
For I := 0 to 3 do
Canvas.LineTo(_2D[Vertices[I]].x + 300,_2D[Vertices[I]].y + 200);
Canvas.LineTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
end;

Procedure DrawSide(Side : integer; Canvas : TCanvas; _2D : T_2D);
begin
Case Side of
1 : begin
if CheckSide([4,3,2,1],_2D) then
RenderSide([4,3,2,1],1,Canvas,_2D);
end;
2 : begin
if CheckSide([5,6,7,8],_2D) then
RenderSide([5,6,7,8],6,Canvas,_2D);
end;
3 : begin
if CheckSide([2,6,5,1],_2D) then
RenderSide([2,6,5,1],3,Canvas,_2D);
end;
4 : begin
if CheckSide([4,8,7,3],_2D) then
RenderSide([4,8,7,3],4,Canvas,_2D);
end;
5 : begin
if CheckSide([5,8,4,1],_2D) then
RenderSide([5,8,4,1],2,Canvas,_2D);
end;
6 : begin
if CheckSide([2,3,7,6],_2D) then
RenderSide([2,3,7,6],5,Canvas,_2D);
end;
end;
end;

Procedure DrawCube(RotX, RotY, RotZ : extended; Size : integer; var C : TCanvas);
var
I : integer;
Poly : TPoly;
_2D : T_2D;
begin
C.Pen.Color := 0;
C.Brush.Color := 0;
C.Rectangle(0,0,600,400);
Poly := SetupBox(Size);
For I := 1 to 8 do
_2D[I] := _3DTo2D(Poly[I].x,Poly[I].y, Poly[I].z, RotX, RotY, RotZ);
C.Pen.Color := 16777215;
For I := 1 to 6 do
DrawSide(I, C, _2D);

end;

begin
DisplayDebugImgWindow(600,400);
DebugCanv :=GetDebugCanvas;
TempBmp := BitmapFromString(600, 400, '');
CubeCanv := GetBitmapCanvas(TempBMP);
Repeat
DrawCube(XRot, YRot , 0.0, 100,CubeCanv);
CopyCanvas(CubeCanv, DebugCanv, 0, 0, 600, 400, 0, 0, 600, 400);
YRot := YRot - 0.01*Pi;
XRot := XRot - 0.01*Pi;
wait(30);
Until(False);
end.

Have fun:)
Ow and credits to Raymond for helping me with a huge memory leak and some other bugs

Markus

2008-01-02 07:18:49

This is simply amazing! May there come more stuff like this from you :D

n3ss3s

2008-01-02 07:27:02

Lol, nice! :)

I give you rep++ when you make the pen color dark, and paint the whole cube, then by the angle adjust the darkness O.o

Killerdou

2008-01-02 07:34:23

i dont really know a lot about how lightning works… but if you want that, try the directx api ;)

Dan Cardin

2008-01-02 07:55:23

itd be cool if you could drag the shape yourself to rotate it :)
other than that amazing. (before n3ss3s' idea maybe add more shapes :p)

Killerdou

2008-01-02 08:02:59

well, if you change to loops to use array length, it should be possible to make any shape… feel free to edit this code as you want:)

Rikje

2008-01-02 08:38:59

Wow thats cool! :)
poeple can do more with scar then i ever thought.

osmm

2008-01-02 12:38:55

This is really cool. Only bad thing is when you try and make the cube * 2 it is too big for the debug image and then if you try and make the debug window bigger the cube disappears.

Killerdou

2008-01-02 12:41:11

well, the cube is already 2*2 XD 1–1 = 2^^ anyways, whats the point of just increasing its size, it will still be the same shape, but to do this, change all the sizes of the canvases/bitmaps

mastaraymond

2008-01-02 13:20:17

Its truly amazing!

Jason2gs

2008-01-02 13:30:54

Wow, fun ^_^

And in less than 200 lines. Nice.

Negaal

2008-01-02 13:33:04

From: mastaraymondAndNegaal
Its truly amazing!

.

ZaSz

2008-01-02 13:35:52

Sweet dude gw!

lordsaturn

2008-01-02 13:38:58

That's rather sexy. GJ

Gu3st...

2008-01-09 10:29:04

i made it so that you can steer the rotation:

program New;
var
Mx, My : Integer;

type
MyPoint = record
x,y,z : Integer;
end;

Type
TPoly = Array[1..8] of MyPoint;
Type
T_2D = Array[1..8] of TPoint;

var
YRot, XRot : Extended;
DebugCanv, CubeCanv : TCanvas;
TempBmp : integer;

Function SetupBox(Size : integer) : TPoly;
Var
Poly : TPoly;
begin
Poly[1].x := 1 * size;
Poly[1].y := 1 * size;
Poly[1].z := 1 * size;
Poly[2].x := -1 * size;
Poly[2].y := 1 * size;
Poly[2].z := 1 * size;
Poly[3].x := -1 * size;
Poly[3].y := 1 * size;
Poly[3].z := -1 * size;
Poly[4].x := 1 * size;
Poly[4].y := 1 * size;
Poly[4].z := -1 * size;
Poly[5].x := 1 * size;
Poly[5].y := -1 * size;
Poly[5].z := 1 * size;
Poly[6].x := -1 * size;
Poly[6].y := -1 * size;
Poly[6].z := 1 * size;
Poly[7].x := -1 * size;
Poly[7].y := -1 * size;
Poly[7].z := -1 * size;
Poly[8].x := 1 * size;
Poly[8].y := -1 * size;
Poly[8].z := -1 * size;
Result := Poly;
end;

Function _3DTo2D(X,Y,Z: integer; RotX, RotY, RotZ : extended) : TPoint;
begin
Result.x := Round((X*Cos(RotY)-Z*Sin(RotY)));
Result.y := -Round((Y*Cos(RotX)+(-X*Sin(RotY)-Z*Cos(RotY))*Sin(RotX)));
end;



Function CheckSide(Vertices : array of integer; _2D : T_2D) : boolean;
var
I : integer;
Middle : TPoint;
PreviousAngle : Extended;
CurrentAngle : Extended;
BiggerCount : integer;
begin
Middle.x := 0;
Middle.y := 0;
BiggerCount := 0;
For I := 0 to 3 do
begin
Middle.X := Middle.X + _2D[Vertices[i]].x;
Middle.Y := Middle.Y + _2D[Vertices[i]].y;
end;
Middle.x := Round(Middle.x/4);
Middle.y := Round(Middle.y/4);
PreviousAngle := ArcTan2(_2D[Vertices[3]].x - Middle.x, _2d[Vertices[3]].y - Middle.y);
For I := 0 to 3 do
begin
CurrentAngle := ArcTan2(_2D[Vertices[i]].x - Middle.x, _2d[Vertices[i]].y - Middle.y);
If CurrentAngle > PreviousAngle then Inc(BiggerCount);
PreviousAngle := CurrentAngle;
end;
If (BiggerCount >= 3) then Result := True;
end;

Procedure RenderSide(Vertices : Array of Integer; Dots : integer; Canvas : TCanvas; _2D : T_2D);
var
I : integer;
begin
Canvas.MoveTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
For I := 0 to 3 do
Canvas.LineTo(_2D[Vertices[i]].x + 300,_2D[Vertices[i]].y + 200);
Canvas.LineTo(_2D[Vertices[0]].x + 300,_2D[Vertices[0]].y + 200);
end;

Procedure DrawSide(Side : integer; Canvas : TCanvas; _2D : T_2D);
begin
Case Side of
1 : begin
if CheckSide([4,3,2,1],_2D) then
RenderSide([4,3,2,1],1,Canvas,_2D);
end;
2 : begin
if CheckSide([5,6,7,8],_2D) then
RenderSide([5,6,7,8],6,Canvas,_2D);
end;
3 : begin
if CheckSide([2,6,5,1],_2D) then
RenderSide([2,6,5,1],3,Canvas,_2D);
end;
4 : begin
if CheckSide([4,8,7,3],_2D) then
RenderSide([4,8,7,3],4,Canvas,_2D);
end;
5 : begin
if CheckSide([5,8,4,1],_2D) then
RenderSide([5,8,4,1],2,Canvas,_2D);
end;
6 : begin
if CheckSide([2,3,7,6],_2D) then
RenderSide([2,3,7,6],5,Canvas,_2D);
end;
end;
end;

Procedure DrawCube(RotX, RotY, RotZ : extended; Size : integer; var C : TCanvas);
var
I : integer;
Poly : TPoly;
_2D : T_2D;
begin
C.Pen.Color := 0;
C.Brush.Color := 0;
C.Rectangle(0,0,600,400);
Poly := SetupBox(Size);
For I := 1 to 8 do
_2D[i] := _3DTo2D(Poly[i].x,Poly[i].y, Poly[i].z, RotX, RotY, RotZ);
C.Pen.Color := 16777215;
For I := 1 to 6 do
DrawSide(I, C, _2D);

end;

begin
DisplayDebugImgWindow(600,400);
DebugCanv :=GetDebugCanvas;
TempBmp := BitmapFromString(600, 400, '');
CubeCanv := GetBitmapCanvas(TempBMP);
Repeat
DrawCube(XRot, YRot , 0.0, 100,CubeCanv);
CopyCanvas(CubeCanv, DebugCanv, 0, 0, 600, 400, 0, 0, 600, 400);
getmousepos(MX, MY);
if ((Mx < 200) and (My < 768)) then
begin
YRot := YRot - 0.02*Pi;
XRot := XRot - 0.02*Pi;
end;
if (Mx > 824) and (My > 0) then
begin
YRot := YRot + 0.02*Pi;
XRot := XRot + 0.02*Pi;
end;
wait(30);
Until(False);
end.

Just move the mouse to left or right of your screen and it will move that direction! :)

Killerdou

2008-01-09 10:51:27

nice, i did it in delphi, put it will rotate only if you hold the mouse button, anyways, does anyone know how to do z-axis rotation?

Hugolord

2008-01-09 11:01:19

From: Killerdou
nice, i did it in delphi, put it will rotate only if you hold the mouse button, anyways, does anyone know how to do z-axis rotation?


woah I love it and Zytex I get an error on your version =S

red eyes 20

2008-01-09 14:28:57

total 'oonage for get ownage this is oonage! =p
keep devoping it untill it's good as blender? k? lol jk very cool, fun to watch aswell.

almost

2008-04-25 16:51:10

Wow this is amazing!
Completely different than the way i do mine.
Yours uses ArcTan2, i was thinking of doing that.
You can check mine out in the other scripts section.

haavy

2008-05-18 15:53:46

Wow, simply awesome. Make it a dice ;)

Laur€ns

2008-07-11 09:33:15

Make it a cone :p

TomTuff

2009-04-05 01:23:20

yo this looks like the objects that spin in some random events :O pretty soon someone will utilize this to make sides of the objects and find which object to click/identify…. lol i could see it now…

PWN PWN SPAM

Smarter Child

2009-04-05 01:49:48

Amazing, i love it lol.