Embrace the timeline….

On creating rich, flashy and immersive advertising experiences online by Owen van Dijk

2.5D, world’s smallest cube?

with 9 comments

I remember when Wolfenstein came out, and was suddenly shooting night after night Nazi’s in Castle Wolfenstein. Graphics paling in comparison with today’s pixelpushing standards, but very addictive when it came to Gameplay. iD Software created a whole new market by distributing the game as shareware and it has sold over 250.000 copies of it.

Wolf3D
As said, graphics were very pixelated, but the game would run fine on a 286 ( a 486 was top of the line ) and sound was awful ( but then again, what would you expect from 8bit soundcards ). The breakthrough was that this was the first game in the first person shooter genre. The game itself wasn’t really 3D, but it gave you the illusion you were really playing B.J. Blazkowicz, captured and trying to escape from Castle Wolfenstein. It was more 2.5D, since bullets were fired straight ahead with no illusion of depth. In fact there was no up and down at all, so that meant you could not jump or crawl. All items in the game would be displayed ‘en face’ no matter where you stood, so although it looked like 3D, it was not.

Now that’s all nice and all, but what does that have to do with this post? Recently i was challenged by several members of a dutch DHTML community to come up with the smallest version of a spinning 3D cube. Ofcourse, i couldn’t resist this challenge so i started working on a version and posting it to their online forum. For a couple of days it we posted our versions, 1,5k, 1,2k, 1k, 900bytes and so on. Finally they managed it to get it down to 598 bytes. I knew my reputation was at stake, so i had to make a smaller version. Leaving out the Z-axis would chop off some more bytes. So hold and below, my version came down to 555 bytes!

Thanks to Flasm, Peter Hall and Casper Schuirink for their help! Ofcourse, if you manage to find a way to get rid of some more bytes, please let me know, always interested.

Below is the code and the Flash file.

FAIL (the browser should render some flash content, not this).

Actionscript code:
k=l=m=n=0;
j=[];
i=8;
while (i--) {
j[i] = [(i & 1)*100-50, (i >> 1 & 1)*100-50, (i >> 1 >> 1 & 1)*100-50];
attachMovie('d', i, i);
}
onEnterFrame = function() {
m += k=d ? (_ymouse-h)/2 : k*.99;
n += l=d ? (g-_xmouse)/2 : l*.99;
g = _xmouse;
h = _ymouse;
i = 8;
while (i--) {
C = j[i][2]*Math.cos(n*.017453)-j[i][0]*Math.sin(n*.017453);
D = 200/(j[i][1]*Math.sin(m*.017453)+C*Math.cos(m*.017453)+200);
with(eval(i)) {
_x = 200+D*(j[i][2]*Math.sin(n*.017453)+j[i][0]*Math.cos(n*.017453));
_y = 200+D*(j[i][1]*Math.cos(m*.017453)-C*Math.sin(m*.017453));
_width = _height=D*6.65+1;
}
}
};
onMouseDown = onMouseUp = function() {
d = !d;
};

Download the Flash source ( *.FLA, 28k )

Written by ohwhen

April 16, 2004 at 4:51 pm

Posted in Web/Tech

9 Responses

Subscribe to comments with RSS.

  1. I still have Wofenstein (and Spear of Destiny) around. It not only ran fine on a 286, it *required* at least a 286! If I remember correctly, I had a 386SX 20MHz machine at the time with a 40MB hardisk, 2MB Ram, 512KB VGA, both 5.25″ and 3.5″ drives but no sound card… Those were the days.

    Burak KALAYCI

    April 16, 2004 at 6:36 pm

  2. I remember playing Doom on my 25 MHz Packard Bell.. and reducing the screen size by 50% so the game would run fast. IDDQD anyone? Ah.. the memories.

    Nice post, by the way.

    darron

    April 16, 2004 at 11:06 pm

  3. Great job Owen … that’s a pretty damn small filesize for a 3D cube :p

    Peter

    April 25, 2004 at 11:10 pm

  4. Wow that’s amazing, only managed to knock off 4bytes by removing the clip from the library and using:

    z = createEmptyMovieClip(i,i);
    z.lineStyle(1);
    z.lineTo(1,1);

    Your code is surely too tight to knock off anymore!?!

    Richard Leggett

    May 6, 2004 at 9:10 pm

  5. Well, somehow on my machine the swf becomes bigger than 555 bytes when I publish your fla, so that probably accounts to your FLASM optimizations afterwards.

    But heres an ugly trick that brings me down to 547 bytes. Maybe you can squeeze that even more with some FLASM cosmetics:

    k=l=m=n=0;

    onEnterFrame = function () {
    m += k=d ? (_ymouse-h)/2 : k*.99;
    n += l=d ? (g-_xmouse)/2 : l*.99;
    g = _xmouse;
    h = _ymouse;
    i = 8;
    while (i–){
    with (attachMovie(‘d’, i, i, {j:[(i & 1)*100-50, (i >> 1 & 1)*100-50, (i >> 1 >> 1 & 1)*100-50]})) {
    C = j[2]*Math.cos(n*.017453)-j[0]*Math.sin(n*.017453);
    D = 200/(j[1]*Math.sin(m*.017453)+C*Math.cos(m*.017453)+200);
    _x = 200+D*(j[2]*Math.sin(n*.017453)+j[0]*Math.cos(n*.017453));
    _y = 200+D*(j[1]*Math.cos(m*.017453)-C*Math.sin(m*.017453));
    _width = _height=D*6.65+1;
    }
    }
    };
    onMouseDown = onMouseUp=function () {
    d = !d;
    };

    Mario Klingemann

    May 12, 2004 at 10:07 pm

  6. 413 bytes of AS code:

    S=Math.sin,T=Math.cos,k=l=M=N=0,q=2*(v=100),i=8,a=.0176,onMouseDown=onMouseUp=function(){d=!d},onEnterFrame=function(){Q=_ymouse,R=_xmouse,M+=(k=d?(Q-h)/2:k*.9)*a,N+=(l=d?(g-R)/2:l*.9)*a,g=R,h=Q,i=8;while(i–)with(eval(i))C=z*T(N)-x*S(N),D=q/(y*S(M)+q+T(M)*C),_x=q+D*(z*S(N)+x*T(N)),_y=q+D*(y*T(M)-C*S(M)),_width=_height=D*6};while(i–)O=attachMovie(‘d’,i,i),O.x=(i&1)*v-50,O.y=(i>>1&1)*v-50,O.z=(i>>1>>1&1)*v-50

    Ludwik

    November 12, 2005 at 7:36 pm

  7. hmmm, the same with cropped lines:

    S=Math.sin,T=Math.cos,k=l=M=N=0,q=2*(v=100),i=8,a=.0176,
    onMouseDown=onMouseUp=function(){d=!d},
    onEnterFrame=function(){
    Q=_ymouse,R=_xmouse,
    M+=(k=d?(Q-h)/2:k*.9)*a,
    N+=(l=d?(g-R)/2:l*.9)*a,
    g=R,h=Q,i=8;
    while(i–)with(eval(i))
    C=z*T(N)-x*S(N),
    D=q/(y*S(M)+q+T(M)*C),
    _x=q+D*(z*S(N)+x*T(N)),
    _y=q+D*(y*T(M)-C*S(M)),
    _width=_height=D*6};
    while(i–)
    O=attachMovie(‘d’,i,i),
    O.x=(i&1)*v-50,
    O.y=(i>>1&1)*v-50,
    O.z=(i>>1>>1&1)*v-50

    Ludwik

    November 12, 2005 at 7:37 pm

  8. It’s possible to further reduce the code size by optimizing the bit operations. Do you know how :)?

    Ludwik

    November 13, 2005 at 4:49 pm

  9. I don’t know Flash from scratch (I’m a VB programmer) so I couldn’t modify this code if my life depended on it, but can someone post a version of this that doesn’t slow down after you “throw” it (if you know what I mean)?

    Email me at asmqb7 at gmail dot com if you want more info / have no idea what I mean.

    asmqb

    February 1, 2007 at 7:49 am


Leave a comment