Quote:
Originally Posted by VeX_RaT_Boy
Truely amazing. Mind commenting that code? :O
|
Thats too big..
=) I will comment other so.. the commands are the keys: 1,2,3 or 4
PHP Code:
if(rotatex==true)ax = (ax+0.1)%6.28;
if(rotatey==true)ay = (ay+0.1)%6.28;
if(rotatez==true)az = (az+0.1)%6.28;
if (created){
//make everything black
for(i=0;i<4096;i++)tiles[int(i/64),i%64]=632;
total = 400;
setarray xx,total;
setarray yy,total;
setarray zz,total;
for(i=0;i<total;i++){
xx[i]=random(-2000,2000);
yy[i]=random(-2000,2000);
zz[i]=random(-1000,500);
}
lente = 256;
}
for(i=0;i<total;i++){
sx = xx[i] - camx;
sy = yy[i] - camy;
sz = zz[i] - camz;
//pré calculo
xx = cos(ay)*cos(az);
xy = sin(ax)*sin(ay)*cos(az) - cos(ax)*sin(az);
xz = cos(ax)*sin(ay)*cos(az) + sin(ax)*sin(az);
yx = cos(ay)*sin(az);
yy = cos(ax)*cos(az) + sin(ax)*sin(ay)*sin(az);
yz = -sin(ax)*cos(az) + cos(ax)*sin(ay)*sin(az);
zx = -sin(ay);
zy = sin(ax)*cos(ay);
zz = cos(ax)*cos(ay);
//calculo
sx = sx*xx + sy*xy + sz*xz;
sy = sx*yx + sy*yy + sz*yz;
sz = sx*zx + sy*zy + sz*zz ;
distancia = lente - sz;
if(distancia>0){
meux= (screenwidth/2) + (lente * sx / distancia);
meuy= (screenheight/2) - (lente * sy / distancia);
showimg 300+i,light4.png,meux,meuy;
if((sz/300)>0.05){
changeimgzoom 300+i,sz/300;
}else{
changeimgzoom 300+i,0.05;
}
changeimgcolors 300+i,1,0,1,.99;
changeimgvis 300+i,5;
}
if(come>0){
if(zz[i]>lente){
xx[i]=random(-500,500);
yy[i]=random(-500,500);
zz[i]=random(-500,500);
}
zz[i]+=10;
}
}
hideplayer 0.05;
timeout = 0.05;
if(keypressed){
if(strcontains(#p(1),1)){
come=1-come;
}
if(strcontains(#p(1),2)){
rotatex=1-rotatex;
}
if(strcontains(#p(1),3)){
rotatey=1-rotatey;
}
if(strcontains(#p(1),4)){
rotatez=1-rotatez;
}
}
By parts =)
We will make Stars coming to our Eyes when we press the key '1' ok?
We can rotate things in 3D world.. like a Caracter you can rotate it to left or to right.. or rotate the whole world to make the Camera moviments.. its pretty easy.. This par will make the rotate moviments..
PHP Code:
/*
If it has to rotate in X.. add some value to 'AX' not leting be bigger then 360o.. Or 2*PI.. ok?
*/
if(rotatex==true)ax = (ax+0.1)%6.28;
/*
The same for Y
*/
if(rotatey==true)ay = (ay+0.1)%6.28;
/*
The same for Z
when Az be equal 2*PI.. it will back to 0.. because MOD(%) means the rest of the divides
*/
if(rotatez==true)az = (az+0.1)%6.28;
When creating the script it will set some values..
'Total' means how many stars we will draw on the screen..
so we will make their positions..
In 2D world you normally use (X,Y) right?
showimg 30,door.png,20,20;
So its 20 blocks from left and 20 from top right?
In 3D world you have to use Z..
Try to look at your window.. do you see a tree out there? You can say from your eyes its 200 points from left and 20 from top.. But you cant get it.. couse its far.. It has a big Z.. and your arms size isnt bigger then its Z.. thats why you cant get it..
So 3D worls use (X,Y,Z)
But graal doesnt have a function like:
showimg3d index,name,x,y,z;
right?
So we have to make a 3D position be in a 2D world.. There are many ways to do that.. I know better 2 ways..
Left Hand
and
Right Hand
Left hand.. Bigger value of Z... near the screen it is..
Right hand.. Bigger value of Z.. more far from screen it is..
Its pretty easy to imagine how 3D world works in a 2D screen..
X is to the right.. how you know..
Y is to down.. how you know..
Z make the things be smaller.. Bigger Z in Left hand way.. Bigger in the screen it is.. because its near your.. Like you can see a Big mountain.. and the mointain is some times bigger then your eyes. But its far.. So its smaller.. has a big Z value in Right hand way.. and a Small Z value in Left hand..
easy huh?
So we will make an array so each star has it own (X,Y,Z)
X for me is XX
Y for me is YY
Z for me is ZZ
ok?
PHP Code:
if (created){
total = 400;
setarray xx,total;
setarray yy,total;
setarray zz,total;
So lets random any value for each star position =)
PHP Code:
for(i=0;i<total;i++){
xx[i]=random(-2000,2000);
yy[i]=random(-2000,2000);
zz[i]=random(-1000,500);
}
Well.. we usually use a LENS value... why? Couse its better to know from where it starts showing something.. In old games they didnt use a LENS value.. they used the LENS like 0.. you can use like 0.. But you will see the diference
My LENS is "lente", couse Im brazillian
Ok when created the Script all the standart values where created.
Lets make the moviment.. It gets each Star..
So get a new value for each cordinate..
SX is equal the star position LESS our Camera position..
What that means?
Later we just change 'camz', 'camy' and 'camx' positions and fantastic things happenes..
PHP Code:
for(i=0;i<total;i++){
sx = xx[i] - camx;
sy = yy[i] - camy;
sz = zz[i] - camz;
The difficult part
Rotating... just kidding its not difficult.. we just have to pay atention..
Do you know “SOHCAHTOA” ?
Strange word? We use the same word in Brazil, and I found a person using the same word in an american site, so probably you should know it
SOH-CAH-TOA means:
SOH = Sin(Theta) = Opposite/Hypoteneuse
CAH = Cos(Theta) = Adjacent/Hypoteneuse
TOA = Tan(Theta) = Take a wild guess.
You remeber a triangule right? Not sure how this triangle is called in English.. but in Brazil we call it “Retangule Triangule”
| \
a |_ \ b
| .|_\
c
B its our Hypoteneuse.. And what will be Opposite or Adjacent you have to imagine an angle.. a^b = c is the Opposite and ‘a’ is Adjacent.. ‘b’ is always Hypoteneuse..
We will never (ops.. almost never) use this but Its always good to know:
Tan(a) = Sin(a)/Cos(a)
Sec(a) = 1/Cos(a)
Csc(a) = 1/Sin(a)
Cot(a) = 1/Tan(a) = Cos(a)/Sin(a)
Do you know PI?
Pi means 180 degree...
Some centuries ago they used GRADOS.. Like 90 degree was 100 grados.. So 360 degree was 400 grados.. But this things dont care, just stupid nerds like me know that..
Ok.. Pi value is.. 3.1425926535..
A japanese guy found like 4000 number after ‘3’.. and the funny part is noone number is the same o.O like 11, 22, 33...
If Pi is 180 degree.. and its 3.1415..
How much is 360 degree (or the whole circule) ?
2* 3.1415 =PPP ohh.. smart guy..
I will try to explain 2D rotations.. so we use those in 3D rotaions..
x' = x*cos(Theta) - y*sin(Theta)
y' = y*cos(Theta) + x*sin(Theta)
WTF? Lol... wait XD
Imagine a Circule.. and cut it in 4 pieces.. Like 4 piecies of a Pizza (hmm.. pizza =(~~~ )..
The center of our pizza is (0,0) I said 2D remember?
How can we find a tomato on the pizza position from the center? O.O
If we use a line.. from the center to the tomato we have a RADIOS.
^_^
This radios (not sure how its the English word.. Radius? .. hmm try to understand it. Sorry my English )..
Well.. this radius is our Hypoteneuse.. Make a tringule from the center to the tomato position.. where one site of the Triangle is where you cut the pizza with the knife.. The other is a line directly from the center to the line (Hypoteneuse) the last is the height from where we cut the pizza making 90 degrees..
Use SOH-CAH-TOA... so we know that:
sin(theta)=tomatoy/r_or_Hypoteneuse
cos(theta)=tomatox/r_or_Hypoteneuse
Difficult? Dont care too much with this right now ok? With time things will be easier.. I promise..
Lets play a litlle bit more with that..
Multiplying both sides by r...
r(Sin(Theta) = y/r)r
r(Cos(Theta) = x/r)r
=
r*(Sin(Theta) = y
r*(Cos(Theta) = x
=
EQ. 1
x = r * cos(Theta)
EQ. 1-1
y = r * sin(Theta)
These 2 equations.. Alot of people on Graal knows.. and now should know from where they come
)
Since on a unit circle r = 1 then
x = 1 * cos(Theta)
y = 1 * sin(Theta)
or
EQ. 2
x = cos(Theta)
EQ. 2-1
y = sin(Theta)
To change from polar to cartesian:
r = Sqr(x^2 + y^2)
Theta = (y/x);
X needs to be different then 0
WTF is a polar cartesian? We used in our pizza.. Dont care too much about that
Ok.. now its less difficult to understand the Rotate 2D:
x' = x*cos(Theta) - y*sin(Theta)
y' = y*cos(Theta) + x*sin(Theta)
I said before remember?