Collision Behavior

Date: Mon Nov 30 07:15:58 MST 1998
From: Brennan Young <brennan@YOUNG.NET>
Subject: Collision Behavior
A load of people here are suddenly interested in a general, abstract collision behavior. You're all in luck. I just finished one, modelled on the "Collision Messenger" from mTropolis, which means that it can be used for collisions 'entering', 'within', 'exiting' or 'entering and exiting'. This has been tested for functionality, but categorically NOT for speed, or in a real-life combat situation. There are some obvious optimisations to be made, for example replacing the 'contains' tests with integer or symbol equality. Get yourself a G3 if it's too slow.

I haven't added a getBehaviorDescription handler yet.

You drop it on any sprite which is going to actively test for collisions, add the sprites it should test for in a list and choose a message which it will send to any of those sprites when the selected type of collision occurs. What happens at the other end of the messaging is up to you. Three parameter fields are offered, which should be enough.

BE CAREFUL NOT TO ADD THE SPRITE'S OWN CHANNEL TO THE COLLISION LIST. That also means WATCH OUT if you drag a configured sprite to another sprite channel. Your hard drive will be erased if you ignore this advice. You have been warned.

It offers the opportunity to 'force' the sprites to be matte ink, because this is the only ink which supports decent edge detection. If you wanted background transparent or blend or something, you'd better make all your sprites rectangular.

If you find it useful please send me an fawning, self-subjugating email and (optional) $1000. You're also required to buy "Lingo Sorcery" or one of the other rare good books on Lingo (most of them are crap, especially the Macromedia ones) and find out how to do this kind of thing yourself.

I'd also strongly appreciate a mention if you use it in a public shockwave game. The laws of karma would then lead to my publishing more juicy code. There's more where this came from.

Brennan

--Score Script: Collision Messenger
property mysprite
property collisionlist
property message
property parameter1
property parameter2
property parameter3
property forceInk
property forceAllInk
property collisionType
property lastCollisions

on beginsprite me
  set mysprite to the spritenum of me
  if forceInk then set the ink of sprite the spritenum of me to 8
  if forceAllInk then
    repeat with othersprite in collisionlist
      set the ink of sprite othersprite to 8
    end repeat
  end if
  set lastCollisions to []
end

on exitframe me
  set collidees to []
  set exitees to []
  repeat with othersprite in collisionlist
    if sprite mysprite intersects othersprite then
      add collidees, othersprite
    else
      if getpos(lastCollisions, othersprite) then
        add exitees, othersprite
      end if
    end if
  end repeat
  repeat with othersprite in collidees
    case true of
      (collisionType contains "Entering" or collisionType contains "Exiting") :
        if not getpos(lastCollisions, othersprite) then
          add lastCollisions, othersprite
          if collisionType contains "Entering" then
            sendsprite othersprite, message, parameter1, parameter2, parameter3
          end if
        end if
      (collisionType = "While within"):
        sendsprite othersprite, message, parameter1, parameter2, parameter3
    end case
  end repeat
  repeat with othersprite in exitees
    if collisionType contains "Exiting" then
      sendsprite othersprite, message, parameter1, parameter2, parameter3
    end if
    deleteOne lastCollisions, othersprite
  end repeat
end

on getPropertyDescriptionList me
  set pdlist to [:]
  set ct to ["Entering", "While within", "Exiting", "Entering and exiting"]
  addprop pdlist, #collisionType, [#comment:"Collision type", #format:#symbol, #default:"Entering", #range:ct]
  addprop pdlist, #collisionlist, [#comment:"List of sprites to test for collisions", #format:#list, #default:[]]
  addprop pdlist, #message, [#comment:"Message", #format:#symbol, #default:#hit]
  addprop pdlist, #parameter1, [#comment:"Parameter1", #format:#any, #default:0]
  addprop pdlist, #parameter2, [#comment:"Parameter2", #format:#any, #default:0]
  addprop pdlist, #parameter3, [#comment:"Parameter3", #format:#any, #default:0]
  addprop pdlist, #forceInk, [#comment:"Force matte ink on this sprite", #format:#boolean, #default:1]
  addprop pdlist, #forceAllInk, [#comment:"Force matte ink on collision sprites", #format:#boolean, #default:1]
  return pdlist
end getPropertyDescriptionList