Author Topic: making a cube  (Read 8330 times)

0 Members and 1 Guest are viewing this topic.

gooner

  • Guest
making a cube
« on: July 08, 2008 »
Hi everybody our good friend Shockwave has set me a challenge of making a cube and giving me this code as a starting point
Code: [Select]
'                                DOUBLE BUFFERING!
'              SMOOTH MOVEMENT IN FREEBASIC WITHOUT HAVING TO USE "SLEEP"
'
'                 THIS PROGRAM SHOWS HOW TO FIX FLICKERING GRAPHICS.
'
'-------------------------------------------------------------------------------



    SCREEN 19,,2,
   
    X1=0:' X POINT 1
    Y1=10:' Y POINT 1
    X2=100:' X POINT 2
    Y2=100:' YPOINT 2
   
    XF1=1:' X MOVEMENT FLAG FOR POINT 1
    XF2=1:' X MOVEMENT FLAG FOR POINT 2
    YF1=0:' Y MOVEMENT FLAG FOR POINT 1
    YF2=1:' Y MOVEMENT FLAG FOR POINT 2
   
   
    SCREENSET 1,0:' SET WORKPAGE 1, VISIBLE PAGE 0 (DONT WORRY!)
DO
    ' DRAW A LINE
    LINE(X1,Y1)-(X2,Y2)
'-------------------------------------------------------------------------------
' MOVE POINT 1
'-------------------------------------------------------------------------------
    IF XF1=1 THEN
        X1=X1+1
        IF X1>800 THEN XF1=0
    END IF
    IF XF1=0 THEN
        X1=X1-1
        IF X1<0 THEN XF1=1
    END IF

    IF YF1=1 THEN
        Y1=Y1+1
        IF Y1>600 THEN YF1=0
    END IF
    IF YF1=0 THEN
        Y1=Y1-1
        IF Y1<0 THEN YF1=1
    END IF
'-------------------------------------------------------------------------------
' MOVE POINT 2
'-------------------------------------------------------------------------------   
    IF XF2=1 THEN
        X2=X2+1
        IF X2>800 THEN XF2=0
    END IF
    IF XF2=0 THEN
        X2=X2-1
        IF X2<0 THEN XF2=1
    END IF

    IF YF2=1 THEN
        Y2=Y2+2
        IF Y2>600 THEN YF2=0
    END IF
    IF YF2=0 THEN
        Y2=Y2-1
        IF Y2<0 THEN YF2=1
    END IF




    SCREENCOPY

'-------------------------------------------------------------------------------
'
'-------------------------------------------------------------------------------
    SLEEP 2
    CLS
   
LOOP UNTIL INKEY$<>""

So to be able to do this i really need to understand what each part of the code is doing which i can't quite get my head around at the moment.So i would appreciate any help to explain to me what is happening to help me solve this.I could quite easily just ring Nick up and ask him but this would defeat the object of this exercise and more importantly any help you give might assist any other novices reading this.Thanks  Wayne  :-[

Offline stormbringer

  • Time moves by fast, no second chance
  • Amiga 1200
  • ****
  • Posts: 453
  • Karma: 73
    • View Profile
    • www.retro-remakes.net
Re: making a cube
« Reply #1 on: July 08, 2008 »
this code seems just to draw a line from <x1,y1> to <x2,y2>

the code increments coordinates and checks if the coordinates (x1y1 and x2 y2) reach the borders of the screen/window. if a coordinate hits a border of the screen, the increments are changed into decrements and the coordinate goes then in the opposite direction.

With this code you should have a line that bounces when the vertices of the line hit the screen borders.. that's it.

I'm not a FB coder so I cannot give you more info are there is not enough code to help you any further ;)
We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: making a cube
« Reply #2 on: July 08, 2008 »
perhaps you will see better what is happening if I were to rewrite the code like this

Code: [Select]
'                                DOUBLE BUFFERING!
'              SMOOTH MOVEMENT IN FREEBASIC WITHOUT HAVING TO USE "SLEEP"
'
'                 THIS PROGRAM SHOWS HOW TO FIX FLICKERING GRAPHICS.
'
'-------------------------------------------------------------------------------



    ' OPEN A SCREEN 800 PIXELS WIDE 600 PIXELS HIGH
    SCREEN 19,,2,
   
    ' DECLARE VARIABLES THAT WILL BE USED (REQUIRED BY MY VERSION OF FB NOTHING TO WORRY ABOUT)
    dim as integer X1,Y1,X2,Y2,XF1,XF2,YF1,YF2, RIGHTEDGE,LEFTEDGE,TOPEDGE,BOTTOMEDGE
    LEFTEDGE=0
    RIGHTEDGE=800
    TOPEDGE=0
    BOTTOMEDGE=600
   
    X1=0:' X POINT 1
    Y1=10:' Y POINT 1
    X2=100:' X POINT 2
    Y2=100:' YPOINT 2
   
    XF1=1:' X MOVEMENT FLAG FOR POINT 1
    XF2=1:' X MOVEMENT FLAG FOR POINT 2
  ' YF1=0:' Y MOVEMENT FLAG FOR POINT 1
    YF1=-1:' Y MOVEMENT FLAG FOR POINT 1
    YF2=-1:' Y MOVEMENT FLAG FOR POINT 2
   
    SCREENSET 1,0:' SET WORKPAGE 1, VISIBLE PAGE 0 (DONT WORRY!)
DO
    ' DRAW A LINE
    LINE(X1,Y1)-(X2,Y2)

'-------------------------------------------------------------------------------
' MOVE POINT 1
'-------------------------------------------------------------------------------
    X1 = X1 + XF1 ' MOVE POINT 1 LEFT AND RIGHT
    IF (X1>=RIGHTEDGE) OR (X1<=LEFTEDGE) THEN
        XF1 = -XF1' REVERSE MOVEMENT WHEN WE REACH LEFT OR RIGHT
    END IF
   
    Y1 = Y1 + YF1 ' MOVE POINT 1 UP AND DOWN
    IF (Y1>=BOTTOMEDGE) OR (Y1<=TOPEDGE) THEN
        YF1 = -YF1 ' REVERSE MOVEMENT WHEN WE TOP OR BOTTOM
    END IF
   
'-------------------------------------------------------------------------------
' MOVE POINT 2
'-------------------------------------------------------------------------------   
    X2 = X2 + XF2 ' MOVE POINT 1 LEFT AND RIGHT
    IF (X2>=RIGHTEDGE) OR (X2<=LEFTEDGE) THEN
        XF2 = -XF2' REVERSE MOVEMENT WHEN WE REACH LEFT OR RIGHT
    END IF
   
    Y2 = Y2 + YF2 ' MOVE POINT 1 UP AND DOWN
    IF (Y2>=BOTTOMEDGE) OR (Y2<=TOPEDGE) THEN
        YF2 = -YF2 ' REVERSE MOVEMENT WHEN WE TOP OR BOTTOM
    END IF

    SCREENCOPY ' UPDATE SCREEN WITH NEW IMAGE

'-------------------------------------------------------------------------------
'
'-------------------------------------------------------------------------------
    SLEEP 2 ' WAIT FOR A SHORT TIME FOR SCREEN UPDATE TO COMPLETE THIS AVOIDS FLICKER
    CLS ' CLEAR THE SCREEN SO THAT WE CAN DRAW THE NEXT IMAGE
   
LOOP UNTIL INKEY$<>"" ' ETERNAL LOOP UNTIL USER PUSHES ANY KEY
As stormbringer mentioned this code draws a line between X1,Y1 and X2,Y2. these variables are co-ordinates within the screen. the movement is cotrolled by the variables XF1,YF1,XF2,YF2. the values kept in the movement variables is added to X1,Y1,X2,Y2 respectively. a test is perfomed to see if either point has reached the edge of the sceen. and if so the movement values are reversed. this means that instead of continuing to move right past the edge instead the point will begin to move left until it reaches the left edge of the screen then the movement values are reversed yet again and the whole process starts repeats

do you understand what is happening here?
Code: [Select]
    SCREENCOPY
    SLEEP 2
    CLS
LOOP UNTIL INKEY$<>""

the screen copy updates the screen with the new image (the line that you drew from X1,Y1 to X2,Y2)
sleep waits for a short moment to allow the screen to update with the new image
cls clears the screen so that when we draw the next line the old line will be erased

just for fun try commenting out cls so that it reads like this -
Code: [Select]
   'CLS

the result is intersting. Is there anything that is unclear?

Challenge Trophies Won:

gooner

  • Guest
Re: making a cube
« Reply #3 on: July 09, 2008 »
@ Stormbringer Thanks for that Stormbringer that certainly helped.

@Rainstorm Thanks for that mate.Your code looks like its gonna be a great help.I will

 have a play with it when i get home from work and let you know how i get on.

 :cheers:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: making a cube
« Reply #4 on: July 09, 2008 »
Btw, don't worry about dim as integer etc at the moment, the challenge wasn't to make a cube though mate!

It was to make a square

Clue you need 4 corners :)
Shockwave ^ Codigos
Challenge Trophies Won:

gooner

  • Guest
Re:Making a "Square"
« Reply #5 on: July 09, 2008 »
Ok Shockie that makes it a bit easier then.Better change the name of the topic then.
 :-[

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: making a cube
« Reply #6 on: July 09, 2008 »
Cubes are trickier :)
I'd be impressed with a square for now.
Shockwave ^ Codigos
Challenge Trophies Won:

gooner

  • Guest
Re: making a SQUARE
« Reply #7 on: July 10, 2008 »
Finally the penny dropped while driving the van today.Thanks to Stormbringer,Rainstorm and Shockwave for your help and giving me the determination to succeed.K+ to all three of you.
 :cheers:
« Last Edit: September 20, 2009 by Shockwave »

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: making a cube
« Reply #8 on: July 10, 2008 »
@gooner:

Karma up for you, mate! That's awesome. Very well done.
I am impressed - looks cool!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: making a cube
« Reply #9 on: July 10, 2008 »
Welldone Gooner :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: making a cube
« Reply #10 on: July 10, 2008 »
Karmic boost there for you Gooner!

It's a cool looking effect too :) Well done, I am really really pleased for you, considering that this time last week you had never programmed anything before it's even more special.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: making a cube
« Reply #11 on: July 10, 2008 »
Another one bites the dust

Well done man :cheers:

Challenge Trophies Won:

Offline Motorherp

  • C= 64
  • **
  • Posts: 57
  • Karma: 8
    • View Profile
    • Shmup Dev
Re: making a cube
« Reply #12 on: July 10, 2008 »
That's actualy really cool, especialy when it hits a coner.  I love how you've managed to make something funky even though you restricted yourself to only having one square.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: making a cube
« Reply #13 on: July 10, 2008 »
Here's his next task...

He needs to do this.. SHould give him something to think about in work tomorrow :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Naptha

  • C= 64
  • **
  • Posts: 53
  • Karma: 3
    • View Profile
Re: making a cube
« Reply #14 on: July 10, 2008 »
Nice work Gooner!

@Shockwave: is the circle supposed to move?

Also how do you give karma?

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: making a cube
« Reply #15 on: July 10, 2008 »
@Naptha: Theres an applaud button under karma.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Naptha

  • C= 64
  • **
  • Posts: 53
  • Karma: 3
    • View Profile
Re: making a cube
« Reply #16 on: July 10, 2008 »
@Naptha: Theres an applaud button under karma.
Hmm, do you need to have karma to give?  :-[

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: making a cube
« Reply #17 on: July 10, 2008 »
Yor level is ZX81 I think you must be C64 to give karma. These levels are tied to your post count. Well its the thought that counts

Challenge Trophies Won:

Offline Naptha

  • C= 64
  • **
  • Posts: 53
  • Karma: 3
    • View Profile
Re: making a cube
« Reply #18 on: July 10, 2008 »
Oh well, virtual karma for you Gooner. :D

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: making a cube
« Reply #19 on: July 10, 2008 »
I will give him karma for you :cheers:

Challenge Trophies Won: