Hi Victor,
Sorry for the late reaction, kinda busy lately...
first an intro on the class:
the CollisionMap is a class that fires events based on colors, it
DOESN"T generate reactions commands
if you want something that bounce, trigger a sound: you have to
define
custom action yourself.
like
function hitBlue(e:Event)
bounce();
The class should be able to trigger 65000 different color events.
never tested :))
the key for a successfull handling: the map and world coordinates
must
be sync.
thats why the class needs a factor x and y
here a little example
var collisionbmd:BitmapData --> a regular bitmapdata, holding the
colors
var factorx:Number;
var factory:Number;
you have a room, say a cube, you have an avatar, you want place some
furniture in the room, and the avatar must not be able to go across
them.
for the ease, lets place just one, a cube, right in the middle of
your
room (the big cube)
make your map, collisionbmd, for instance 1000x1000
now you draw a rectangle shape in it, representing your little cube.
the rect of this shape is say x,250,y,250,width,500, height: 500
you choose the clor red, FF0000 for thsi shape. You know now that
if a
check done on this color is positive that it will be the hit on the
cube.
pseudo code: if pix testes == red, then avatar is on red, its the
cube
in the middle --> do something
now, you have made your cubes.
the main cube is 2000 x2000x2000 and the small one is 1000x1000x1000
in your 3d scene
so considering the scene3d units and the pixels of the map, thats
exactly twice the size of your map.
so you pass now:
factorx = factory = 2;
here the declaration
( import + collisionmap class var on top class)
collisionmap = new CollisionMap(collisionbmd, factorx , factory);
collisionmap.setColorEvent(0xFF0000, "red", onRed);
//declare if required more events if you want to trigger other stuffs
collisionmap.setColorEvent(0xFFFF00, "yellow", onYellow);
private function onRed(event:Event):void
{
trace("hey you hit the little cube in middle of the room");
}
private function onYellow(event:Event):void
{
trace("hey you hit the yellow part");
}
k, so now our little system is declared and using same scale
time to add the checks:
in youre enterframe, or whenever you want to check the collisions for
your avatar just add the following code:
collisionmap.read(myAvatar.x, -myAvatar.z); --> note here the -z
check.
this can be necessary depending on ways you display items, for
instance if your room model comes from a right handed system
Thats all!
on read, if the color is found at myAvatar.x, -myAvatar.z, in scale
factorx and y == red, the red event is fired automatically!
Dev tip:
if you want test the easy way, to see if code and map are sync, or
check wanna check if your furnitures are at the right place
use your map as bitmapmaterial and set it to the cube bottom (the
floor of your room) to see if correct.
also you can do easy checks such as:
bmd.setPixels(myAvatar.x*factorx, -myAvatar.z*factory, somecolor);
Know issues:
If you compress your swf and use Flash IDE lib, the colors might be
affected
so say you know for instance that a pixel is yellow at 100x100 on
your
map
you want to check if at runtime, after the swf is compiled using
compression if the color as been altered,
trace(collisionmap.getColorAt(100, 100));
if the returned uint is not the yellow, the event onYellow will not
be
triggered, change then the listener declaration with the color value
you just have traced
next compile, the event will be fired.
again, this class works as a trigger, not a reaction handler!
if you want see an example of reaction, look for instance at the room
demo into the trunk.
I hope it will help you
Fabrice
On Sep 18, 2009, at 8:13 AM, Victor Ribeiro wrote:
Ok guy, here is the deal.
I can't find any decent documentation about on how the collisionMap
works, and I want it to work so bad...
Here is some facts about my project:
my floor is 5 x 5 meters.
my texture's floor is 512 x 512 pixels.
When I import the floor into the flash and don't have to rotate or
scale it anymore. everything is all right so far.
which parameters would you guys recommend me to use on my
collisionMap
creation?
How does factorX and factorY works?
Please, if anyone can help me it'll be real nice.
=)