JonBoy_Sprite_Tech_1
("Sprite Objects and Simple Rectangular Collision Detection")
August 28, 2002 (version 0.7.1)
by JonBoy (jonboy@stdnet.com - http://people.stdnet.com/jonathan)

MAXIMUM sprite comfort level: 12 sprites.

It's been so long since I last diddled with JavaScript that I forgot it had custom object support, so I built myself a sprite object called "jonboy_sprite".  (Sprite OBJECTS are so much easier to deal with than PERSONS, IMHO.)  However, I like Sphere's spritesets, so I made it possible for jonboy_sprite to derive information from these sets as well.

Armed with my new sprite objects, I built a VERY SIMPLE rectangular collision detection routine.  The reaction of sprites to a collision is quite primitive right now - they simply head off in a new direction orthogonal to the old direction.  

The demo itself is not very flashy.  (Assuming default settings...)  Three rounds of sprite tests are conducted - one each with 4, 8 and 16 randomly generated sprite objects.  Each test generates about 1000 frames of sprite movement - about 15-17 seconds on most machines.  The GOOD STUFF will be found in the debug log after the demo is over - approximate collision counts and the time it took to run each stage of the demo.  As you can see, 16 sprite objects (all performing collision tests against each other) pretty much grind the engine to a halt.  Obviously, some optimization is required in a "real game" situation.  (I'll work on that another night.)    

REVISED: 500 iterations with 8, 10, 12 and 14 sprites.

In addition to the "jonboy_sprite.js" file, you may also find the "jonboy_debug.js" file useful.  Rather than "all or nothing" debugging, it uses debug levels to control the amount of information output into the logs.  (Try cranking debug up on this demo and watch what happens to the debug log!) 

+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+

Typical Log Output

Log opened: Thu Aug 29 01:29:34 2002
Thu Aug 29 01:29:34 2002 --   Series #0 of 1 (sector size is 384x512)
Thu Aug 29 01:29:34 2002 --   Test #0 of 3 (4 sprites)
Thu Aug 29 01:29:50 2002 --   Test completed in 16654 milliseconds
Thu Aug 29 01:29:50 2002 --    ...collision detection performed in 251 milliseconds
Thu Aug 29 01:29:50 2002 --    ...made 6000 detection attempts
Thu Aug 29 01:29:50 2002 --    ...detected 1 collisions
Thu Aug 29 01:29:50 2002 --   Test #1 of 3 (8 sprites)
Thu Aug 29 01:30:07 2002 --   Test completed in 16644 milliseconds
Thu Aug 29 01:30:07 2002 --    ...collision detection performed in 4423 milliseconds
Thu Aug 29 01:30:07 2002 --    ...made 28000 detection attempts
Thu Aug 29 01:30:07 2002 --    ...detected 55 collisions
Thu Aug 29 01:30:07 2002 --   Test #2 of 3 (16 sprites)
Thu Aug 29 01:30:31 2002 --   Test completed in 23343 milliseconds
Thu Aug 29 01:30:31 2002 --    ...collision detection performed in 20312 milliseconds
Thu Aug 29 01:30:31 2002 --    ...made 120000 detection attempts
Thu Aug 29 01:30:31 2002 --    ...detected 89 collisions
Log closed: Thu Aug 29 01:30:31 2002

+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+

jonboy_sprite documentation

- "NEW": jonboy_sprite(CommonName, SpriteSet, X, Y, Direction, Speed)
  - CommonName: A name for the sprite.  (i.e. "Koopa") Need not be unique.
  - SpriteSet: The name of a Sphere sprite set.  (i.e. "tinyrock.rss") 
    - In this version, jonboy_sprite will only use the FIRST image in the set.
    - A rectangular bounding box (x1,y1,x2,y2) is derived from the spriteset's "base" to assist in RECTANGULAR comparisons
    - A circular center (x,y) and radius is also derived from the spriteset's "base" to assist in CIRCULAR comparisons (not tested)
  - X: The initial X-coordinate of the sprite
  - Y: The initial Y-coordinate of the sprite
  - Direction: The initial direction of the sprite (in RADIANS, 0=NORTH)
  - Speed: The initial speed of the sprite (60 = "normal" walking speed)
- PROPERTIES:
  - direction: (read/write) Direction of sprite. (in RADIANS, 0=NORTH))
  - ID: (read-only, not tested) The 12-char ID.
  - name: (read-only, not tested)  The common name.
  - speed: (read/write) Speed of sprite. (60 = "normal" walking speed) 
  - rectX1: (read-only) Right coordinate of bounding rectangle.
  - rectX2: (read-only) Left coordinate of bounding rectangle.
  - rectY1: (read-only) Top coordinate of bounding rectangle.
  - rectY2: (read-only) Bottom coordinate of bounding rectangle.
  - roundRadius: (read/write, not tested) Radius of bounding circle. 
  - roundX: (read-only, not tested) X-coordinate of bounding circle center.
  - roundY: (read-only, not tested) Y-coordinate of bounding circle center.
  - X: (read/write) X-coordinate of sprite.  
  - Y: (read/write) Y-coordinate of sprite.  
- METHODS:
  - render(): Draws this sprite to the video buffer.
  - update(): Updates position (X,Y) of the sprite based on direction and speed.  Also "wraps" the sprite to the top or bottom of the screen if it falling off the edge.

+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+

* * * REFERENCES * * * 

Look up "Collision Detection - Getting the most out of you collision test" by Dave Roberts (circa 1995) in the "Dr. Dobbs" journal (www.ddj.com)

* * * TECH * * * 
Tested under Sphere Engine v0.97 for Windows (June 17, 2002 release) on Windows 2000 Server

* * * KNOWN ISSUES * * * 
- Sprites touching the edge of the screen act weird.  Either they are nor completely rendered or give incorrect answers to the collision detection routines.  
- 14 sprite objects pretty much bring the engine to its knees, at least on my old Dell Inspiron.  Needless to say, there is NO optimization going on here.  
- The THIRD and final test can get ugly - displaying a black screen almost continuously and seemingly playing random punch sounds in the background.  Please hang in there - the demo WILL finish in less than a minute!  

* * * VERSION HISTORY * * * 
0.7.0 - Released August 28, 2002
0.7.1 (Sep 2, 2002) 
    - Fixed "60/Speed" Bug
    - Removed collision sounds
    - Shortened tests to 500 iterations
    - Changed tested to 8,10,12 and 14 sprites


