Welcome, Guest. Please login or register.
July 31, 2010, 07:58:42 PM
Home Help Search Login Register
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base

Qbasicnews.com  |  General  |  General/Misc  |  Topic: cool 3d thing (edit: now available in screensaver form!) « previous next »
Pages: [1] Print
Author Topic: cool 3d thing (edit: now available in screensaver form!)  (Read 1840 times)
Deleter
Na_th_an
*****
Posts: 1292



WWW
cool 3d thing (edit: now available in screensaver form!)
« on: November 11, 2005, 05:09:32 PM »

its not too complicated, so dont get your hopes too high, but i like it.  Cheesy
Code:
DECLARE SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
DECLARE FUNCTION dstc(x1!,y1!,x2!,y2!,d!) AS BYTE
CONST pi = 3.14159 / 180
DIM AS SINGLE x,y,z,thetax,thetay,thetaz, c,ox,oy,bx,by,bz,cx,cy,tm
DIM AS INTEGER sc(639*479+1),mousex,mousey,garbage
RANDOMIZE TIMER
SCREENRES 640,480,32,2
SCREENSET 1,0
ox = 320
oy = 240
cx = -5
cy = -8
tm = TIMER + .03
DO
    FOR thetax = 0 TO 180 STEP 36
        FOR thetay = 0 TO 360 STEP 6
            rotpt (0,0,125,thetay,thetax+90+thetaz,thetaz+90,x,z,y)
            c = (y+128) SHL 16
            LINE (ox-x-1,oy-z-1)-(ox-x+1,oy-z+1),c, bf
            rotpt (0,0,225,-thetay,-thetax+90-thetaz,-thetaz+90,bx,bz,by)
            c = ((y*.55)+128)
            LINE (319-bx,239-bz)-(321-bx,241-bz),c, bf
        NEXT
    NEXT
    SCREENCOPY 1,0
    GET (0,0)-(638,478),sc
    CLS
    DO:LOOP UNTIL TIMER >= tm
    tm = TIMER + .03
    PUT(1,1),sc,alpha,225
    LINE(0,0)-(639,0),0
    LINE(0,0)-(0,479),0
    thetaz +=2
    ox += cx
    IF ox < 125 OR ox > 514 THEN cx = -cx    
    oy += cy
    IF oy < 125 OR oy > 354 THEN cy = -cy
LOOP UNTIL MULTIKEY(1)
END
SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
    DIM AS SINGLE rx,ry,rz
    rx = rotx!
    ry = roty!
    rz = rotz!
    'xrot
    nx1! = x1!
    ny1! = z1! * SIN(pi * rx) + y1! * COS(pi * rx)
    nz1! = z1! * COS(pi * rx) - y1! * SIN(pi * rx)
   'yrot
    nx2! = nx1! * COS(pi * ry) - nz1! * SIN(pi * ry)
    ny2! = ny1!
    nz2! = nx1! * SIN(pi * ry) + nz1! * COS(pi * ry)
    'zrot
    nx! = nx2! * COS(pi * rz) - ny2! * SIN(pi * rz)
    ny! = nx2! * SIN(pi * rz) + ny2! * COS(pi * rz)
    nz! = nz2!    
END SUB

FUNCTION dstc(x1!,y1!,x2!,y2!,d!) AS BYTE
    IF SQR((x1!-x2!)*(x1!-x2!)+(y1!-y2!)*(y1!-y2!)) > d! THEN RETURN -1 ELSE RETURN 0
END FUNCTION
Logged

Deleter
Na_th_an
*****
Posts: 1292



WWW
cool 3d thing (edit: now available in screensaver form!)
« Reply #1 on: November 11, 2005, 06:04:50 PM »

ok, following the advice of rattrap, i made it into a screensaver.
Download screensaver
Here is the code for those who are interested:

edit: changed the code to make the empty settings thing look more windows-y, lol

Code:
IF LEFT$( COMMAND$, 2 ) = "/c" THEN 'settings
    DIM AS INTEGER mx,my,mb, txt(340*40)
    SCREENRES 400,300,32,2,0
    SCREENSET 1,0
    WINDOWTITLE "Sphereoyds© Settings"
    PAINT (0,0), RGB(255,0,255)
    COLOR RGB(0,0,0), RGB(255,0,255)
    PRINT "Sorry, no settings are currently available."
    PRINT
    PRINT "           Screensaver by Deleter,"
    PRINT
    PRINT "            'Sphereoyds' (c) 2005"
    GET (0,0)-(339,39),txt
    CLS
    PAINT(0,0), RGB(212,208,200)
    PUT (29,129),txt,trans
    SCREENCOPY 1,0
    SLEEP
    END
END IF
IF LEFT$( COMMAND$, 2 ) = "/p" THEN END
DECLARE SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
DECLARE FUNCTION keypressed()
CONST pi = 3.14159 / 180
DIM AS SINGLE x,y,z,thetax,thetay,thetaz, c,ox,oy,bx,by,bz,cx,cy,tm
DIM AS INTEGER sc(639*479+1),mousex,mousey,garbage
RANDOMIZE TIMER
SCREENRES 640,480,32,2,1
SCREENSET 1,0
SETMOUSE 320,240,0
ox = 320
oy = 240
cx = -2.5
cy = -4
tm = TIMER + .03

DO
    FOR thetax = 0 TO 180 STEP 36
        FOR thetay = 0 TO 360 STEP 6
            rotpt (0,0,125,thetay,thetax+90+thetaz,thetaz+90,x,z,y)
            c = (y+128) SHL 16
            LINE (ox-x-1,oy-z-1)-(ox-x+1,oy-z+1),c, bf
            rotpt (0,0,225,-thetay,-thetax+90-thetaz,-thetaz+90,bx,bz,by)
            c = ((y*.55)+128)
            LINE (319-bx,239-bz)-(321-bx,241-bz),c, bf
        NEXT
        SLEEP 1
    NEXT
   
    SCREENCOPY 1,0
    GET (0,0)-(638,478),sc
    CLS
    DO:LOOP UNTIL TIMER >= tm
    tm = TIMER + .03
    PUT(1,1),sc,alpha,225
    LINE(0,0)-(639,0),0
    LINE(0,0)-(0,479),0
    thetaz +=2
    ox += cx
    IF ox < 125 OR ox > 514 THEN cx = -cx    
    oy += cy
    IF oy < 125 OR oy > 354 THEN cy = -cy
LOOP UNTIL keypressed
END
SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
    DIM AS SINGLE rx,ry,rz
    rx = rotx!
    ry = roty!
    rz = rotz!
    'xrot
    nx1! = x1!
    ny1! = z1! * SIN(pi * rx) + y1! * COS(pi * rx)
    nz1! = z1! * COS(pi * rx) - y1! * SIN(pi * rx)
   'yrot
    nx2! = nx1! * COS(pi * ry) - nz1! * SIN(pi * ry)
    ny2! = ny1!
    nz2! = nx1! * SIN(pi * ry) + nz1! * COS(pi * ry)
    'zrot
    nx! = nx2! * COS(pi * rz) - ny2! * SIN(pi * rz)
    ny! = nx2! * SIN(pi * rz) + ny2! * COS(pi * rz)
    nz! = nz2!    
END SUB

FUNCTION keypressed()
    DIM AS INTEGER temp,tx,ty
    FOR temp = 0 TO 127
        IF MULTIKEY(temp) THEN RETURN -1
    NEXT
    GETMOUSE tx,ty,,temp
    IF tx <> 320 OR ty <> 240 OR temp <> 0 THEN RETURN -1
    RETURN 0
END FUNCTION
Logged

speedlemon
I hold this place together
*****
Posts: 874



cool 3d thing (edit: now available in screensaver form!)
« Reply #2 on: November 11, 2005, 07:04:14 PM »

good job deleter.  Keep it up.  I could definately use that as a screensaver. Cool  and then if someone asks how it was made, then ill introduce them to freebasic.  :lol:
Logged
Dio
I hold this place together
*****
Posts: 874



cool 3d thing (edit: now available in screensaver form!)
« Reply #3 on: November 11, 2005, 10:17:17 PM »

how was it made? i'm not kidding.
oh let's start with rotpt

btw, where the hell do you people learn any of this stuff? did i miss something? goddamn, where the hell is a good, thorough, tutorial to help me?! Damn.

thank you that is all.

oh, and that look pretty cool  Cool

edit: i used the wrong word. go figure
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Deleter
Na_th_an
*****
Posts: 1292



WWW
cool 3d thing (edit: now available in screensaver form!)
« Reply #4 on: November 11, 2005, 10:20:13 PM »

well the rotpt sub i got from syn9 and edited it a bit. i asked him and he wasnt quite sure how/why it worked.....

Its the magical formula!


lol, jk

As soon as I figure it out, I'll tell you how it works, lol. But i can tell you it deals with a lot of trig. I get everything up until z rotation, that just blows me out of the water...
Logged

ShadowWolf
Wandering Guru
***
Posts: 389



cool 3d thing (edit: now available in screensaver form!)
« Reply #5 on: November 11, 2005, 11:18:37 PM »

all 3d stuff breaks down to simple Vector math that's it nothing more.
and 3d vectors are simple 2d vectors but just the Z component on the vector has a relation ship X or Y components of the vector doesn't matter really which.

Rel did a really good tutorial on this i think it's still up on the main QBN front page.
Logged
Dio
I hold this place together
*****
Posts: 874



cool 3d thing (edit: now available in screensaver form!)
« Reply #6 on: November 12, 2005, 01:06:01 AM »

ooooooooh, rotpt is a sub! damn. well ok. n/m  :oops:
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Rattrapmax6
__/--\__
*****
Posts: 2577



WWW
cool 3d thing (edit: now available in screensaver form!)
« Reply #7 on: November 12, 2005, 11:31:33 AM »

:lol: Yeah,

Code:
SUB rotpt (x1!, y1!, z1!, rotx!, roty!, rotz!, nx!, ny!, nz!)
    DIM AS SINGLE rx,ry,rz
    rx = rotx!
    ry = roty!
    rz = rotz!
    'xrot
    nx1! = x1!
    ny1! = z1! * SIN(pi * rx) + y1! * COS(pi * rx)
    nz1! = z1! * COS(pi * rx) - y1! * SIN(pi * rx)
   'yrot
    nx2! = nx1! * COS(pi * ry) - nz1! * SIN(pi * ry)
    ny2! = ny1!
    nz2! = nx1! * SIN(pi * ry) + nz1! * COS(pi * ry)
    'zrot
    nx! = nx2! * COS(pi * rz) - ny2! * SIN(pi * rz)
    ny! = nx2! * SIN(pi * rz) + ny2! * COS(pi * rz)
    nz! = nz2!  
END SUB


 Tongue Read up on Relsolf's 3D tuts for QB if you want a basic understanding (pun intended)  :wink:

Edit: Ah, yeah, I found a replacement for my saver for awhile..  Cool ..That just looks too cool..  :lol:
Logged

Kevin (x.t.r.GRAPHICS)

syn9
Senior Member
**
Posts: 175



WWW
cool 3d thing (edit: now available in screensaver form!)
« Reply #8 on: November 13, 2005, 11:47:53 PM »

hah! i didnt say i didnt know how it worked... i said i didnt know why all my returned X points were negative! >D

heres a tutorial i wrote on why it works... >)


for rotation on a 2d plane, take a normal x/y axis and place a point on the x axis somewhere

a simple exercise would be to rotate that point by 90 degrees up to the y axis

so point [10,0] becomes [0,10]

well, cos(0degrees) = 1 and sin(90 degrees) = 1

so if you want the x value to be at 10 on the x axis at 0 degrees then multiply the x value by cos(angle), this will give you the rotated x coordinate

now, since you want that initial point on the x axis to be put up to the top of the y axis, you'll multiply the x point times sin(90) so that at 90 degrees the rotated y value will be 10 units high

so far you have

rotatedx = x * cos(angle)
rotatedy = x * sin(angle)

so that if you put in the coordinate [10,0] it will be on the x plane at value 10 at 0 degrees and it will be on the y plane at 90 degrees...

now, say you want the point [10,2] the be rotated 90 degrees, well, at 0 degrees y ou want the point to be 2 units above the x axis

so you add to your rotatedy value y * cos(0) because at 0 degrees cos(0) = 1 and sin(0) = 0... so that your x*sin[0] = 0 and y*cos[0] = 2, now your rotatedy formula will acurately rotate this point that is not exactly on the x axis

next is to fix the rotatedx formula so that at 90 degrees the original [10,2] is rotated to [-2,10] in order to do this your rotated x value needs to end up at -2 at 90 degrees, well, cos[90] = 0 so you need to use sin[90] and multiply it by the negative of the original y value

so you end up with

rotatedx = x * cos[angle] - y * sin[angle]
rotatedy = x * sin[angle] + y * cos[angle]

this will allow rotation on one plane, in 3d you have 3 planes, so the outputed rotatedx/y from this formula will be applied to the next plane, and so on to have a final rotated coordinate in 3d

to rotate onthe x axis, you'll be rotating on a plane that only uses z and y values
on the y axis, it'll be x and z values etc...


simplified it would look like this

rx1 = basex
ry1 = basez * sin(xangle) + basey * cos(xangle)
rz1 = basez * cos(xangle) - basey * sin(xangle)

rx2 = rx1 * cos(yangle) - rz1 * sin(yangle)
ry2 = ry1
rz2 = rx1 * sin(yangle) + rz1 * cos(yangle)

rx3 = rx2 * cos(zangle) - ry2 * sin(zangle)
ry3 = rx2 * sin(zangle) + ry2 * cos(zangle)
rz3 = rz2

rxyz3 will be your final rotated coordinates
Logged
Deleter
Na_th_an
*****
Posts: 1292



WWW
cool 3d thing (edit: now available in screensaver form!)
« Reply #9 on: November 14, 2005, 12:37:51 PM »

oh, sorry about that  :oops: its good that someone knows  :wink:
Logged

axipher
Ancient Guru
****
Posts: 544



cool 3d thing (edit: now available in screensaver form!)
« Reply #10 on: December 18, 2005, 05:07:35 PM »

That's just awsome, I even went and set every screensaver in my school to that one and set it to come after 1 minute Cheesy
Logged

et the power of the chicken burger compel you! -Axipher-

-Anonymous member  of the No More Religion Threads movement-
DirkFist
New Member

Posts: 5



cool 3d thing (edit: now available in screensaver form!)
« Reply #11 on: March 19, 2006, 05:37:52 AM »

If you think about coordinates as a recipe for getting from one point to another, (eg. so many steps forward so many to the right) rotation makes more sense.
Lets say you have a two dimensional system:
F=Forward
R=Right

Now if we think of F and R as vectors in say X,Y
F.x= 0.6:  F.y= 0.8
R.x= 0.8:  R.y=-0.6

Then a point in F1,R1 gets transformed to X,Y by
X1 = F1*F.x + R1*R.x
Y1 = F1*F.y + R1*R.y

so
1,0 => 0.6, 0.8
0,1 => 0.8,-0.6
2,0 => 1.2, 1.6
1,1 => 1.4, 0.2

So where do sin and cos come in ?
cos,sin form a unit vector

The length of base vectors F and R determines the scaling of the result
The direction of the base vectors determines the direction of the result
The angle between the base vectors determines the skew of the result

Example use mouse to rotate without sin,cos  Cheesy
Code:

dim as single a,b,            
dim as single Fx,Fy,Rx,Ry   'base vectors
dim as single f(10),r(10)   'point sequence
dim as integer mx,my        'mouse coordinates
screen 13
' space shippy kindof of shape
f(0)= 0 :f(1)=48:f(2)=54:f(3)=54:f(4)= 48
f(5)= 0 :f(6)=31:f(7)=26:f(8)=26:f(9)=31:f(10)=0
r(0)= 20:r(1)=16:r(2)= 6:r(3)=-6:r(4)=-16
r(5)=-20:r(6)=-16:r(7)=-6:r(8)= 6:r(9)= 16:r(10)=20
do
  getmouse  mx,my
  Fx=(mx-160)/54:Fy=(my-100)/54
  Rx=Fy:Ry=-Fx   'R is perpendicular to F
  for a=0 to 9
    b=a+1
    line(Fx*f(a)+Rx*r(a)+160,Fy*f(a)+Ry*r(a)+100)-(Fx*f(b)+Rx*r(b)+160,Fy*f(b)+Ry*r(b)+100)
  next a
  screensync:  cls:  k$=right$(inkey$,1):? "move mouse around : esc to exit"
loop until k$="k" or k$=chr$(27)
Logged
Pages: [1] Print 
Qbasicnews.com  |  General  |  General/Misc  |  Topic: cool 3d thing (edit: now available in screensaver form!) « previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
SMF Theme © Gaia, Hosting by Employers Job Post
Valid XHTML 1.0! Valid CSS!