Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: taj on May 20, 2007

Title: Oldskool effect help needed
Post by: taj on May 20, 2007
Guys,

I cant imagine a better group to ask this of. I ahev been asked to write some oldskool effect for a demo and I need help. I'm so totally out of my depth. Several standard effects have gone already (rotozoom, tunnel, fire). I figured I might do "rubber" or I think its sometimes called "twister" ubt I am unclear how. I know *you* know shockie.

I have 320x240 display and fixed point but no real maths lib.

Can someone help with a mini tutorial, link, C code, basic code. The processor is 400Mhz so I think I can manage the effect in real time without too many precalculated tables.

Anyway, I'm also open to suggestions.  I'd like ot get something very simple up and running and then complicate later.

So can anyone help me?

Chris

(shockie hmmm, I wanted to talk about an algorithm here, dont care if its in C, basic etc. I couldnt find anywhere sort of most appropriate on the board. We have a group for genereal programming and languages but its become just languages. Maybe admins could consider an "algorithms" section, something like "fractals, textures, oldskool, shaders, software rendering, hardware rendering...". I would volunteer to look after the shaders one!)

Title: Re: Oldskool effect help needed
Post by: ninogenio on May 20, 2007
hey taj im not sure if i totally understand what you mean mate but if its the rubberd cube your after jim and shockwave gave examples to the board jims example is in the yabasic section an is mabey a little clearer to understand as it doesnt do texturing and shockys example is in freebasic.

the way they work if memory serves is they do scanlines or *lines in jims* example all the way down the box with sin/cos attached at the end points to controll the span width to give the illusion that the box is twisting.

please correct that if im wrong.
Title: Re: Oldskool effect help needed
Post by: Shockwave on May 20, 2007
A twister is about as simple as you can get, I will code a very, very basic one for you in an hour and explain it. :) You can then re-write and embelish it later.
Title: Re: Oldskool effect help needed
Post by: Shockwave on May 20, 2007
I was lucky, I found a very simple and unoptimised version of that effect. The code is posted below, but before that, an explanation of how that technique works is needed, perhaps not so much for you, but for anyone else who comes across this listing.

You have a small screen open, 320 * 240.
All that is needed is to loop down the screen.

The good news is that no 3D calculations are necessary and as you have no sin/cos, you will need to have a table of integers.

As you loop down the screen you are generating 4 x co-ords (or grabbing 4 x co-ords from a table).. something simple like;

X1 = (99 * SIN (THETA)) + 100
X2 = (99 * SIN (THETA+90)) + 100
X3 = (99 * SIN (THETA+180)) + 100
X4 = (99 * SIN (THETA+270)) + 100

add some offset to theta as you move down the screen to make the bar twist.

Then it is simply a matter of doing a very basic hidden line check..

For example if the line is X2 - X3 only draw it if X3>X2

This would work with lots of different kinds of shapes, not just 4 sided ones.
Make the effect look even better by making the lines brighter the longer they are, this gives the illusion of light sourcing :P

Even better still, you can use a basic linear interpolation to map a texture.. I have sources that do this if you want to see them.

Any further questions, please do ask :)


Code: [Select]
' ROTATING BAR THING BY SHOCKWAVE (C) 2006 --
'
'
'--------------------------------------------

'===============================================================================
'                      DEFINE CONSTANTS AND INITIALISE STUFF;
'===============================================================================

    OPTION STATIC
    OPTION EXPLICIT

    CONST   XRES = 640 :           ' SCREEN WIDTH.
    CONST   YRES = 480 :           ' SCREEN HEIGHT.
   
    CONST  XCENT = XRES / 2 :      ' X CENTRE OF SCREEN.
    CONST  YCENT = YRES / 2 :      ' Y CENTRE OF SCREEN.

    CONST RAD_2_DEG AS DOUBLE = ( (4*ATN(1)) / 180 )

    #define ptc_win
    #Include Once "tinyptc.bi"
    DIM SHARED AS UINTEGER BUFFER ( XRES * YRES ) :' THE SCREEN BUFFER.
    DECLARE SUB TWISTY()
    DIM SHARED GADD AS DOUBLE
'===============================================================================
'                               OPEN THE SCREEN;
'===============================================================================

        IF ( PTC_OPEN ( "TWISTER!", XRES, YRES ) = 0 ) THEN
        END -1
        END IF

'===============================================================================
'                               MAIN LOOP;
'===============================================================================

DO
    TWISTY
    PTC_UPDATE@BUFFER(0)
    ERASE BUFFER
LOOP UNTIL INKEY$=CHR$(27)
END

SUB TWISTY()
    DIM AS INTEGER X1,X2,X3,X4,L,V,RR
    DIM AS INTEGER Y1,Y2,Y3,Y4
    DIM Q AS INTEGER
    DIM AS DOUBLE QQ,VVV
    VVV=.7*SIN(GADD*RAD_2_DEG)
   
    GADD=GADD+.5
FOR Q =0 TO YRES-1
    X1=XCENT+(150*SIN((GADD+QQ)*RAD_2_DEG))
    X2=XCENT+(150*SIN((GADD+90+QQ)*RAD_2_DEG))
    X3=XCENT+(150*SIN((GADD+180+QQ)*RAD_2_DEG))
    X4=XCENT+(150*SIN((GADD+270+QQ)*RAD_2_DEG))   
   
    IF X2>X1 THEN
        V=X2-X1
        RR=RGB(0,0,V)
        FOR L=X1 TO X2
            BUFFER(XRES*Q+(L))=RR
        NEXT
    END IF
    IF X3>X2 THEN
        V=X3-X2
        RR=RGB(V,0,0)
        FOR L=X2 TO X3
            BUFFER(XRES*Q+(L))=RR
        NEXT
    END IF
    IF X4>X3 THEN
        V=X4-X3
        RR=RGB(V,0,V)
        FOR L=X3 TO X4
            BUFFER(XRES*Q+(L))=RR
        NEXT
    END IF
    IF X1>X4 THEN
        V=X1-X4
        RR=RGB(0,V,0)
        FOR L=X4 TO X1
            BUFFER(XRES*Q+(L))=RR
        NEXT
    END IF



    QQ=QQ+VVV
NEXT Q
END SUB
Title: Re: Oldskool effect help needed
Post by: taj on May 20, 2007
Shockie,

1. I have nooo idea how this works
2. Thanks very much, Ill recode in C and see what turns up
3. Im amazed you can find time to do this _and_ breast feed Jacob.
4. K++

Chris
Title: Re: Oldskool effect help needed
Post by: Shockwave on May 20, 2007
Mmm. Ok, I will explain a little more...

To create a point that "bounces" smoothly back and forth we could say;

Code: [Select]
PSEUDO CODE;

FOR THETA = 0 TO 359
X = ( 100 * SIN (THETA ) ) + 100
NEXT THETA

To Break that down;

X = ( AMPLITUDE * SIN ( VARIABLE ANGLE ) ) + OFFSET

Would make a number that ranged between 0 and 200, smoothly moving between the two values.

If you did;

Code: [Select]
PSEUDO CODE;

FOR THETA = 0 TO 359
X1 = ( 100 * SIN (THETA ) ) + 100
X2 = ( 100 * SIN ((THETA+90) ) ) + 100
X3 = ( 100 * SIN ((THETA+180) ) ) + 100
X4 = ( 100 * SIN ((THETA+270) ) ) + 100
NEXT THETA

You now have 4 numbers at 90 degree offests to each other... Imagine the 4 points as the corners of a rectangle on it's side.

It's simply a matter of checking to see if the line is visible ie;

if X2 >= X1 Then line X1 to X2

And it will work.

You could make this effect more complicated easily, but just look at it that way and it will become clear hopefully.

It's just a little bit of sin and join the dots.

You should even be able to code this very easily with a precacalculated table if you don't have sin/cos.

Again, please do feel free ask more questions if you are still stuck.
And my nipples are very sore and Jacob is still hungry :)
Title: Re: Oldskool effect help needed
Post by: taj on May 20, 2007
"Sorry, you can't repeat a karma action without waiting 3 hours."

Well cheers for the sore nipples anyway ;-)

Chris
(ps that may put some off joining this forum. Please be aware that shockwaves sore nipples do not feature in too many posts. I count only two, so far. Sore nipples do not result from posting here. UsuallY. Ow.)
Title: Re: Oldskool effect help needed
Post by: Shockwave on May 20, 2007
My Little eyes twinkled brightly above btw when I noticed an off to look after a shaders area :)

Those suggestions will definately go into the pot :)

By the way,

http://www.intro-inferno.com/production.php?id=1522

Uses the technique above.. Only difference is that the texture is being "rotated" when the decrunch bars are showing at the beginning.

in actual fact, the texture is just scaled along it's x axis and darkened as it gets smaller. this is then copied straight out of memory instead of drawing a line so there's plenty of scope to tart this effect up.

Please do let me know how this goes, I love twisters, they are one of my favourite effects.
Title: Re: Oldskool effect help needed
Post by: taj on May 20, 2007

the way they work if memory serves is they do scanlines or *lines in jims* example all the way down the box with sin/cos attached at the end points to controll the span width to give the illusion that the box is twisting.

please correct that if im wrong.

No u were dead right and exactly what I was after but shockies offer of code was too tempting in the same way chocolate icecream is on a sunny day. ;-)

Thanks nino.

Chris
Title: Re: Oldskool effect help needed
Post by: ninogenio on May 20, 2007
Cant wait to see what you do with this is it for a pda or something?
Title: Re: Oldskool effect help needed
Post by: taj on May 20, 2007
Honestly some guy in Rebels said do an oldskool fx for us, so I agreed. No idea platform.
On the other hand I was coding demo fx in 1987  so I should cope, Noever done rubber tho'.u
Title: Re: Oldskool effect help needed
Post by: Shockwave on May 20, 2007
Get h2o to do a nice logo to texturemap onto the twisty if it's for Rebels :)
Title: Re: Oldskool effect help needed
Post by: ninogenio on May 20, 2007
oh right, i thought it might have been for a gizmo as you were specific in saying it was a 400mhz cpu with no real maths lib and need`d fixed point.

it just sounded like a few enviroments ive been in before :)