recongise alpha, shape and position

lochoi's icon

hello,
i have to images that i mix on one same output with jit.alphablend.
they appear like separate fragments on a black screen. with random object i
make the shapes move around.
i need to know when the fragments meet. if i receive a bang at that moment i
tell the fragments to stop moving and they stay connected at the position
when they first met.
the project uses 6 to 9 fragments that move around randomly and when a
fragment meets another they both stop until all fragments have met another
too.
i was thinking....each fragment having an alpha layer , is there an object
that would recognise there are 6 or X alpha layers and when which meets
which...?
thanx for any help
kranky

nesa's icon

hi, another boring bus trip today gave me an idea:

in order to detect collision between two shapes, you could count the number
of white pixels in separate alpha channels (using jit.3m, f.e.), and add
them together(+). Then count the number of white pixels in alpha channel
after alpha blending, and compare. If the sum from separate channels is
bigger than sum after alpha blending, shapes probably collided.
Tell me if you need a demo patch.

best,
nesa

lochoi's icon

hello,
thanx for thinking about our little trouble on yr bus ride....
i'm gonna start working on that object, but since i'm running out of time, i
could use some extra help and have a look at your demo patch.
thanx again....i hope this works, we could create random shapes with that
function.i'll send you the patch and some of the images we'll use once
finished!!

nesa's icon

collision detection example:

Max Patch
Copy patch and select New From Clipboard in Max.

best,
nesa

lochoi's icon

howdy,
thanx for your help...this is a great patch you sent me...
only problem is i have a hard time modifying it so it would work for more
inputs....at the moment my patch is using 7 sources mixed .like 7 colored
balls moving up and down, and zooming in and out randomly....the idea is
when two meet they stop and so on until all have stoped.so out of the 7 i
need to know which one meets which so i can individually stop the ones that
have collided...!!!?
when all have stoped i repeat the operation whith new fragments of forms to
create another random form.
for yr example a fragment is a color ball and a shape is all 7 balls
attached toguether.
for my fragments i use psd images with alpha layer....
thanx nesa for yr help, hopefully soon i will understand how to add more
inputs to yr patch
ciao
lochoi

nesa's icon

hi,

i guess the easiest way to do it would be to check two shapes at a time:

1st step: no checking, just put first shape, A. store it's alpha in
temporary matrix.
2nd step: add second shape, B, check if collides, store in temporary
matrix(from previous step) A.alpha+B.alpha
3rd step: add third shape, C, check if collides with A+B, store matrix A+B+C
4th step: add fourth shape, D, check if collides with A+B+C, store matrix
A+B+C+D
and so on, until all shapes are checked. then go to step 1 for next frame.

this way you're not bound to fixed number of shapes.

imagine this algorithm: no erasing after last step. when there is no more
space for current shape, make it smaller. this would produce some strange
fractal-like effect(smaller and smaller circles, until all space is filled,
wich is - never)

cheers,
nesa

Mathieu Chamagne's icon

I think you should check the PMPD library; much more efficient than doing it
in jitter...
I just posted an example patch that uses the PMPD library (Physical
modelling) in max/Jitter to simulate balls on a pool table (2d environment -
collision / friction / gravity / attraction...)

best

Mathieu Chamagne
http://mathieu.chamagne.free.fr
www.maxobjects.com

yair reshef's icon

expanded a bit on the idea given by nesa, this image based collision
detection implements the most basic collision *response* possible, but it
gives the nice effect and can work with movies also.

i prototyped it on javascript but it runs nicly.

problems:
-every object has to have separate map.
-the bounce (response) is fake, can be much better at price of knowing
physics
-at certain Angles the object can get inside the colliding object, to solve
-cant do 1% of what pmpd can do, but as far as i know pmpd only works with
geometry, this is good for organic forms (a.k.a video)

look at nesa's example, you have to edit the lines a bit, some are warped
included, max, javascript and a simple collision map.
please help make this better.

Max Patch
Copy patch and select New From Clipboard in Max.

//++++++++++++++++++++++++++++++++++
//name this javascript "collision.js" and put in same dir.
//++++++++++++++++++++++++++++++++++
//javascript begin
autowatch=1;
outlets=2
post("nn__compiled__nn");
var circle = {_x:0,_y:0}; //circle is an object
var lcd = {width:200,high:200};//size of output
var speedx=(Math.random()+1)*3; //speed is random
var speedy=(Math.random()+1)*3;

function bang()//simple,nice(&fake) bounding bounce
{
if(circle._x>= lcd.width-10 || circle._x
//if this not checked can happen a "stuck" circle
if(circle._x> lcd.width-10){
circle._x= lcd.width-10 ;}
else if(circle._x
circle._x=0;
}
speedx *= -1; //if reach bound reverse direction

}else if(circle._y>= lcd.high-10 || circle._y
if(circle._y> lcd.high-10 ){
circle._y= lcd.high-10 ;}
else if(circle._y
circle._y=0;}
speedy *= -1;
}
circle._x += speedx;
circle._y += speedy;
outlet(0,circle._x,circle._y);
}
function colide(){speedx*=-1;speedy*=1}//this changes the direction on
jit.3m message

function list()//size of lcd, you can send list
{
var a = arrayfromargs(arguments);
lcd.width=a[0]; lcd.high=a[1];
}
function newRndSpeed()//speed at random
{
speedx=(Math.random()+1)*3;
speedy=(Math.random()+1)*3;
}

//end of javascript

2006/4/12, Mathieu Chamagne :
> I think you should check the PMPD library; much more efficient than doing
it
> in jitter...
> I just posted an example patch that uses the PMPD library (Physical
> modelling) in max/Jitter to simulate balls on a pool table (2d environment
-
> collision / friction / gravity / attraction...)
>
> http://mathieu.chamagne.free.fr/max/mc.PMPD-jitter-example-1.pat
>
> best
>
> Mathieu Chamagne
> http://mathieu.chamagne.free.fr
> www.maxobjects.com
>
>
>
>
>