Home   Archive   Permalink



Cube going round -> hidden surfaces

I made this video: https://youtu.be/kJsSaWSFUVg And now I need HELP! I programmed 600 images/frames and made the video with MakeAvi program. How I take away the hidden back surfaces?
    
The code is here: https://petke.info/cube.r
    
Yes: I have read the theory, but that doesn't help me. I need workin example, the code!

posted by:   Petri Keckman     27-Jan-2023/13:51:48-8:00



Hi nice start. Well it appears there is a bug because 1 side is showing a triangle and the other side through that.
Perhaps you can isolate the picture generated at that point by examining the relevant values for that picture and then only generate that sole picture and watch closely what exactly happens and the differences your adjustments make. Perhaps I have some time to look closer later this weekend.

posted by:   Arnold     28-Jan-2023/8:34:50-8:00



length is not spelled lenght, but done consistently so no errors there ;-)
Apparently divider never is 0, the jakaja is nowhere else used and there was no division by zero error ;-)
And it looked as if the top was missing, it is an open box. As the images are painted on a transparent canvas, when flipped you see the mirror image. Perhaps it is also a good idea to use 1 black image and 5 other colored ones and rotate the use of the black image so you can see where the black face of the cube goes each time.
Next I would limit the use of FOR loops. When I don't need the index I prefer LOOP 8 [] and when I need an index I often use REPEAT i 8 [].
The APPEND PLOT COMPOSE part starting at line 83 looks ok to me, all sides are used.
Also tricky with 3D stuff is you need to 'paint' things at the back first, so things in the front do not get overwritten by things in the back.
    
HALT at the end is according to the docs "Useful for program debugging." not needed here.

posted by:   Arnold     28-Jan-2023/11:10:57-8:00



x0, y0 and z0 are always 0??!!
    
If you would like to debug your code and decided on a frame number then you could add
    
if picind = framenumber [
     prin "x:" repeat i 8 [prin x/(i)] print ""
     ; print all other info as well
]
when going in the loop "for picind 1 600 1 ["
and repeat at the end and catch all values like that. Then check if all are correct.
    
You can comment out the actual writing of the files if you like.
    
It would be nice if you could inform the forum how the debugging went and like to see the complete spinning cube

posted by:   Arnold     28-Jan-2023/13:38:13-8:00



Hi Petri,
    
I adapted your code and added some prints like descibed above
    
R E B O L []
framenumber: 200 ; debug this frame
maxx: 1920 ; The width of Image
maxy: 1080 ; The Hight of Image -> FullHD
alfax: 1 ;How many degree x-axis between frames?
alfay: 2 ;                y    
alfaz: 3 ;                z
x0: 0
y0: 0
z0: 0
print "wait"
images: copy[]
repeat i 6 [
    pic: load to-file rejoin ["image" i ".jpg"]
    append images pic
]
    
origx: 0
origy: -100
origz: 0
    
x: copy[] ;x corners
y: copy[] ; y corners
z: copy[] ; z corners
xp: copy[] ;3D space x,y,z to 2D on the image
yp: copy[]
    
length: 300
loop 8 [ ;set numbers to table so it can be pointed with index
    append x 0
]
y: copy x
z: copy x
xp: copy x
yp: copy x
    
x/1: x0 - length ;Setting up the koordinates of the corners.
x/2: x0 + length ;There is eight corner in cube.
x/3: x/2
x/4: x/1
    
x/5: x/1
x/6: x/2
x/7: x/3
x/8: x/4
    
y/1: y0 - length
y/2: y/1
y/3: y0 + length
y/4: y/3
    
y/5: y/1
y/6: y/2
y/7: y/3
y/8: y/4
    
z/1: 100
z/2: 100
z/3: 100
z/4: 100
    
z/5: z/1 - (2 * length)
z/6: z/2 - (2 * length)
z/7: z/3 - (2 * length)
z/8: z/4 - (2 * length)
    
for frame 1 600 1 [ ;lets make 600 frames to MakeAvi program
     if frame = framenumber [
         prin "length:" print length
         prin "x0:" print x0
         prin "y0:" print y0
         prin "z0:" print z0
         prin "x :" repeat i 8 [prin x/(i) prin " "] print ""
         prin "y :" repeat i 8 [prin y/(i) prin " "] print ""
         prin "z :" repeat i 8 [prin z/(i) prin " "] print ""
         prin "xp:" repeat i 8 [prin xp/(i) prin " "] print ""
         prin "yp:" repeat i 8 [prin yp/(i) prin " "] print ""
         prin "a :" print a
         prin "b :" print b
         ; print all other info as well
     ]        
    plot: copy[]
    ;Project 3D x,y,z to 2D xp,yp
    repeat i 8 [
        divider: (maxx - (z/(i) + origz))
        either divider = 0 [
             temp: 0.00000000001 ;can't divide by zero ;' correct syntax highlighting
         ][
             temp: maxx / divider
            xp/(i): x0 + ((x/(i) + origx) * temp)
            xp/(i): xp/(i) + (maxx / 2)
            yp/(i): y0 - ((y/(i) + origy) * temp)
            yp/(i): yp/(i) + (maxy / 2)
         ]
    ]
    
    a: 0
    b: -200
    
    append plot compose [
        image (as-pair (a + xp/1) (b + yp/1)) (as-pair (a + xp/2) (b + yp/2)) (as-pair (a + xp/3) (b + yp/3)) (as-pair (a + xp/4) (b + yp/4)) (images/(1)) ;etusivu
        image (as-pair (a + xp/6) (b + yp/6)) (as-pair (a + xp/7) (b + yp/7)) (as-pair (a + xp/8) (b + yp/8)) (as-pair (a + xp/5) (b + yp/5)) (images/(2)) ;takaseinä
        image (as-pair (a + xp/1) (b + yp/1)) (as-pair (a + xp/2) (b + yp/2)) (as-pair (a + xp/6) (b + yp/6)) (as-pair (a + xp/5) (b + yp/5)) (images/(3)) ;yläpuoli    
        image (as-pair (a + xp/4) (b + yp/4)) (as-pair (a + xp/3) (b + yp/3)) (as-pair (a + xp/7) (b + yp/7)) (as-pair (a + xp/8) (b + yp/8)) (images/(4)) ;alapuoli
        image (as-pair (a + xp/1) (b + yp/1)) (as-pair (a + xp/5) (b + yp/5)) (as-pair (a + xp/8) (b + yp/8)) (as-pair (a + xp/4) (b + yp/4)) (images/(5)) ;vasenreuna
        image (as-pair (a + xp/2) (b + yp/2)) (as-pair (a + xp/6) (b + yp/6)) (as-pair (a + xp/7) (b + yp/7)) (as-pair (a + xp/3) (b + yp/3)) (images/(6)) ;oikereuna
    ]
    
    ;moves
    repeat i 8 [
        x/(i): x/(i) - x0
        y/(i): y/(i) - y0
        z/(i): z/(i) - z0
    ]
    
    
    ;going round x-axel
    repeat i 8 [
        apu: y/(i)
        y/(i): ((cosine alfax) * y/(i)) + ((sine alfax) * z/(i))
        z/(i): ((- (sine alfax)) * apu) + ((cosine alfax) * z/(i))
    ]
    
    ;going round y-axel
    repeat i 8 [
        apu: x/(i)
        x/(i): ((cosine alfay) * x/(i)) + ((- sine alfay) * z/(i))
        z/(i): ((sine alfay) * apu) + ((cosine alfay) * z/(i))
    ]
    
    ;going round z-axel
    repeat i 8 [
        apu: x/(i)
        x/(i): ((cosine alfaz) * x/(i)) + ((sine alfaz) * y/(i))
        y/(i): ((- (sine alfaz)) * apu) + ((cosine alfaz) * y/(i))
    ]
    
    ;moves back
    repeat i 8 [
        x/(i): x/(i) + x0
        y/(i): y/(i) + y0
        z/(i): z/(i) + z0
    ]
     if frame = framenumber [
         prin "x :" repeat i 8 [prin x/(i) prin " "] print ""
         prin "y :" repeat i 8 [prin y/(i) prin " "] print ""
         prin "z :" repeat i 8 [prin z/(i) prin " "] print ""
         prin "xp:" repeat i 8 [prin xp/(i) prin " "] print ""
         prin "yp:" repeat i 8 [prin yp/(i) prin " "] print ""
         prin "a :" print a
         prin "b :" print b
         ; print all other info as well
     ]        
        
    picture: layout [
        box (as-pair (maxx) (maxy)) white effect reduce ['draw plot]
    ] ; ' undo the quote for syntax highlighting
    kuv: copy/part skip to-image picture 20x20 (as-pair maxx maxy)
    ;save/png to-file rejoin ["cube" frame ".png"] kuv
    ;print frame
     dummy: "Ready ;-)"
]
    
    
This is the code I used
    
And this were the results of frame 200:
length:300
x0:0
y0:0
z0:0
x :-406.35199861432 131.901413394932 363.119435204459 -175.133976804793 -276.654308384737 261.599103624514 492.817125434041 -45.4362865752094
    
y :-152.423172081318 -363.915392301663 187.579913650005 399.072133870351 -257.893396787379 -469.385617007724 82.1096889439429 293.601909164288
    
z :40.5614328496593 200.418572967544 151.519146552021 -8.33799356586332 -535.680305355383 -375.823165237499 -424.72259165302 -584.579731770904
    
xp:552.946151612193 1134.86243744969 1346.61041840806 763.506577114072 739.618990706275 1186.16900931833 1344.66437448555 897.635407000202
yp:821.950199674487 1050.00943507736 427.793068984464 249.87355940672 824.21364606436 995.010812038152 527.405399985656 385.491699796097
a :0
b :-200
x :-414.992986062021 105.575299506101 367.189171912448 -153.379113655674 -271.581543613303 248.986741954819 510.600614361165 -9.96767120695554
    
y :-130.151375859751 -366.389688421292 171.213163305025 407.451475866568 -253.336738195574 -489.575050757116 48.0278009692009 284.266113530743
    
z :29.007600403026 211.216600561475 160.804741041235 -21.4042591172145 -540.429423929757 -358.220423771308 -408.632283291547 -590.841283449996
    
xp:544.878244505513 1107.27462726514 1354.23062694361 785.623289803361 743.694860059635 1178.77568210142 1363.54832772191 925.168739043209
yp:797.870887012272 1057.98509754571 444.916451947917 242.221032336124 819.822793029374 1016.17795708658 554.649663610489 391.585612995197
a :0
b :-200
== "Ready ;-)"
>>
    
Hope you can use this to locate and fix your code.


posted by:   Arnold     28-Jan-2023/14:22:49-8:00



Sori, I forget to tell here that I already solved the problem, but thank You very uch Your answers.

posted by:   Petri Keckman     28-Jan-2023/18:04:13-8:00



This what I have done: https://youtu.be/BjpkPuhLiyo
    
Sorry, it is inn finnish language because it is about our national song.

posted by:   Petri Keckman     29-Jan-2023/6:23:46-8:00



Of course it is in Finnish!
Good thing you solved your problem. I found a rotating cube in R2 on another site of Nick here http://re-bol.com/rebol.html
You will see it is even possible to do this in Rebol without using the intermediate step with saving all those pictures
    
    
z: 10 h: z * 12 j: negate h c: as-pair z * 5 z * 5 l: z * 4 w: z * 20
img: to-image layout [box effect [draw [pen logo.gif circle c l]]]
q: make object! [x: 0 y: 0 z: 0]
cube: [[h h j] [h h h] [h j j] [h j h] [j h j] [j h h] [j j j] [j j h]]
view center-face layout [
     f: box 400x400 rate 0 feel [engage: func [f a e] [
         b: copy [] q/x: q/x + 5 q/y: q/y + 8 q/z: q/z + 3
         repeat n 8 [
             p: reduce pick cube n ; point
             zx: (p/1 * cosine q/z) - (p/2 * sine q/z) - p/1
             zy: (p/1 * sine q/z) + (p/2 * cosine q/z) - p/2
             yx: (p/1 + zx * cosine q/y) - (p/3 * sine q/y) - p/1 - zx
             yz: (p/1 + zx * sine q/y) + (p/3 * cosine q/y) - p/3
             xy: (p/2 + zy * cosine q/x) - (p/3 + yz * sine q/x) - p/2 - zy
             append b as-pair (p/1 + yx + zx + w) (p/2 + zy + xy + w)
         ]
         f/effect: [draw [
             fill-pen 255.0.0.100 polygon b/6 b/2 b/4 b/8
             image img b/6 b/5 b/1 b/2
             fill-pen 255.159.215.100 polygon b/2 b/1 b/3 b/4
             fill-pen 54.232.255.100 polygon b/1 b/5 b/7 b/3
             fill-pen 0.0.255.100 polygon b/5 b/6 b/8 b/7
             fill-pen 248.255.54.100 polygon b/8 b/4 b/3 b/7
         ]]
         show f
     ]]
]

posted by:   Arnold     31-Jan-2023/14:40:27-8:00



Name:


Message:


Type the reverse of this captcha text: "r i d - r u c - e s r a p"



Home