recongise alpha, shape and position

Apr 2, 2006 at 12:33pm

recongise alpha, shape and position

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

#25190
Apr 9, 2006 at 8:32pm

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

#73865
Apr 10, 2006 at 9:08am

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!!

#73866
Apr 10, 2006 at 1:30pm

collision detection example:

#P window setfont Geneva 9.;
#P window linecount 1;
#N vpatcher 572 77 843 345;
#P window setfont Geneva 9.;
#P hidden newex 79 43 45 9699337 loadbang;
#P inlet 50 35 15 0;
#P window linecount 0;
#P newex 50 78 72 9699337 t b b;
#P window linecount 1;
#P message 112 110 35 9699337 64 64;
#P message 50 110 35 9699337 32 32;
#P outlet 112 170 15 0;
#P outlet 50 170 15 0;
#P connect 5 0 4 0;
#P connect 6 0 4 0;
#P connect 4 0 2 0;
#P connect 2 0 0 0;
#P connect 4 1 3 0;
#P connect 3 0 1 0;
#P pop;
#P hidden newobj 154 33 53 9699337 p startup;
#P comment 292 506 270 9699337 output 1 , else 0;
#P comment 292 490 270 9699337 if collision area is bigger than this
threshold value;
#P flonum 243 490 45 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P comment 270 463 96 9699337 collision area;
#P newex 211 506 42 9699337 > 0.01;
#P flonum 222 462 45 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont Geneva 20.;
#P user ubumenu 255 540 166 9699348 0 0 1 1;
#X add no collision;
#X add collision;
#X prefix_set 0 0 0;
#P toggle 211 533 39 0;
#P window setfont Geneva 9.;
#P newex 211 437 27 9699337 – 0.;
#P flonum 223 404 69 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P comment 641 344 96 9699337 after blending;
#N vpatcher 15 55 615 455;
#P window setfont Geneva 9.;
#P newex 172 107 91 9699337 prepend jit_matrix;
#P newex 50 68 132 9699337 t shape1 shape2;
#P newex 50 106 91 9699337 prepend jit_matrix;
#P inlet 50 30 15 0;
#P outlet 172 153 15 0;
#P outlet 50 151 15 0;
#P connect 2 0 4 0;
#P connect 4 0 3 0;
#P connect 3 0 0 0;
#P connect 4 1 5 0;
#P connect 5 0 1 0;
#P pop;
#P newobj 22 226 107 9699337 p output both shapes;
#P newex 211 380 27 9699337 + 0.;
#P newex 535 319 50 9699337 unpack 0.;
#N vpatcher 15 55 615 455;
#P window setfont Geneva 9.;
#P window linecount 1;
#P comment 425 214 68 9699337 similar to:;
#P newex 68 181 273 9699337 jit.expr @expr “min(in[0].p[0] + in[1].p[0] \,
255)”;
#P newex 477 243 66 9699337 jit.unpack;
#P newex 399 274 88 9699337 jit.op @op +;
#P newex 399 243 66 9699337 jit.unpack;
#P inlet 331 123 15 0;
#P inlet 68 119 15 0;
#P outlet 68 251 15 0;
#P window linecount 0;
#P comment 133 124 149 9699337 add two alpha channels:;
#P connect 2 0 7 0;
#P connect 7 0 1 0;
#P connect 3 0 7 1;
#P connect 4 0 5 0;
#P connect 6 0 5 1;
#P pop;
#P newobj 521 270 113 9699337 p join_alpha_channels;
#P flonum 549 344 84 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 521 291 53 9699337 jit.3m;
#P comment 293 404 96 9699337 before blending;
#P flonum 358 344 58 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 345 319 50 9699337 unpack 0.;
#P newex 331 291 53 9699337 jit.3m;
#P comment 418 344 60 9699337
#P flonum 225 344 58 9 0 0 0 148 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 211 319 50 9699337 unpack 0.;
#P newex 197 291 53 9699337 jit.3m;
#P newex 22 422 107 9699337 jit.alphablend;
#P user jit.pwindow 21 451 162 122 0 0 0 0 1 0;
#N vpatcher 10 55 610 455;
#P window setfont Geneva 9.;
#P newex 50 156 96 9699337 append 0 0 255;
#P newex 116 204 103 9699337 zl reg brgb 0 0 0;
#P newex 50 135 102 9699337 prepend paintoval;
#P newex 104 50 38 9699337 t i i;
#P newex 50 50 38 9699337 t i i;
#P newex 212 88 37 9699337 + 40;
#P newex 158 88 37 9699337 + 40;
#P newex 50 179 77 9699337 t b l clear b;
#P newex 50 112 173 9699337 pack 32 32 64 64;
#P newex 50 268 265 9699337 jit.matrix shape2 4 char 160 120 @planemap 3 1 2
3;
#P newex 50 240 214 9699337 jit.lcd 4 char 160 120 @out_name shape2;
#P inlet 50 30 15 0;
#P inlet 104 30 15 0;
#P outlet 50 296 15 0;
#P connect 2 0 9 0;
#P connect 9 0 5 0;
#P connect 5 0 11 0;
#P connect 11 0 13 0;
#P connect 13 0 6 0;
#P connect 6 0 3 0;
#P fasten 6 2 3 0 99 212 55 212;
#P fasten 6 1 3 0 77 204 55 204;
#P fasten 12 0 3 0 121 228 55 228;
#P connect 3 0 4 0;
#P connect 4 0 0 0;
#P connect 1 0 10 0;
#P connect 10 0 5 1;
#P connect 6 3 12 0;
#P connect 9 1 7 0;
#P connect 7 0 5 2;
#P connect 10 1 8 0;
#P connect 8 0 5 3;
#P pop;
#P newobj 197 188 160 9699337 p draw shape2;
#P user pictslider 197 59 160 120 0 0 0 0 SliderDefaultKnob.pct 1
SliderDefaultBkgnd.pct 1 2163 0 0 7864480 1. 1.;
#N vpatcher 71 55 615 455;
#P window setfont Geneva 9.;
#P newex 50 156 93 9699337 append 255 0 0;
#P newex 116 204 117 9699337 zl reg brgb 0 0 0;
#P newex 50 135 101 9699337 prepend paintoval;
#P newex 104 50 33 9699337 t i i;
#P newex 50 50 33 9699337 t i i;
#P newex 212 88 36 9699337 + 40;
#P newex 158 88 36 9699337 + 40;
#P newex 50 179 77 9699337 t b l clear b;
#P newex 50 112 173 9699337 pack 32 32 64 64;
#P newex 50 268 279 9699337 jit.matrix shape1 4 char 160 120 @planemap 1 1 2
3;
#P newex 50 240 228 9699337 jit.lcd 4 char 160 120 @out_name shape1;
#P inlet 50 23 15 0;
#P inlet 104 23 15 0;
#P outlet 50 305 15 0;
#P connect 2 0 9 0;
#P connect 9 0 5 0;
#P connect 5 0 11 0;
#P connect 11 0 13 0;
#P connect 13 0 6 0;
#P connect 6 0 3 0;
#P fasten 6 2 3 0 99 212 55 212;
#P fasten 6 1 3 0 77 204 55 204;
#P fasten 12 0 3 0 121 228 55 228;
#P connect 3 0 4 0;
#P connect 4 0 0 0;
#P connect 1 0 10 0;
#P connect 10 0 5 1;
#P connect 6 3 12 0;
#P connect 9 1 7 0;
#P connect 7 0 5 2;
#P connect 10 1 8 0;
#P connect 8 0 5 3;
#P pop;
#P newobj 22 188 160 9699337 p draw shape1;
#P user pictslider 22 59 160 120 0 0 0 0 SliderDefaultKnob.pct 1
SliderDefaultBkgnd.pct 1 2163 0 0 7864480 1. 1.;
#P comment 285 344 60 9699337
#P comment 21 35 100 9699337 move both shapes:;
#P hidden fasten 33 0 2 0 159 54 27 54;
#P lcolor 1;
#P connect 2 0 3 0;
#P fasten 5 0 21 0 202 215 27 215;
#P connect 3 0 21 0;
#P connect 21 0 7 0;
#P lcolor 15;
#P connect 7 0 6 0;
#P connect 21 1 7 1;
#P lcolor 13;
#P connect 2 1 3 1;
#P hidden connect 33 1 4 0;
#P lcolor 1;
#P connect 4 0 5 0;
#P fasten 21 0 8 0 27 286 202 286;
#P lcolor 15;
#P connect 8 1 9 0;
#P connect 9 0 20 0;
#P connect 20 0 24 0;
#P connect 24 0 28 0;
#P connect 28 0 25 0;
#P connect 24 0 27 0;
#P connect 20 0 23 0;
#P connect 9 0 10 0;
#P fasten 13 0 20 1 350 373 233 373;
#P fasten 19 0 24 1 540 430 233 430;
#P connect 30 0 28 1;
#P fasten 28 0 26 0 216 530 260 530;
#P fasten 21 1 12 0 124 278 336 278;
#P lcolor 13;
#P connect 12 1 13 0;
#P connect 4 1 5 1;
#P connect 13 0 14 0;
#P fasten 21 0 18 0 27 256 526 256;
#P lcolor 15;
#P connect 18 0 16 0;
#P connect 16 1 19 0;
#P connect 19 0 17 0;
#P fasten 21 1 18 1 124 249 629 249;
#P lcolor 13;
#P window clipboard copycount 34;

best,
nesa

#73867
Apr 11, 2006 at 9:03am

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

#73868
Apr 11, 2006 at 11:27pm

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

#73869
Apr 12, 2006 at 9:39am

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
http://www.maxobjects.com

#73870
Oct 2, 2006 at 4:17pm

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 v2;
#N vpatcher 689 171 1650 909;
#P origin 0 -332;
#P button 418 103 19 0;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P comment 438 310 138 9109513 < < load collision map , or movie;
#P window setfont “Sans Serif” 12.;
#P message 376 103 41 9109516 colide;
#B color 2;
#P window setfont “Sans Serif” 9.;
#P newex 467 481 28 9109513 sel 1;
#B color 5;
#P newex 467 456 34 9109513 > 0.01;
#B color 5;
#P newex 165 379 56 9109513 jit.op @op -;
#P message 302 309 134 9109513 importmovie colide.JPG , bang;
#B color 15;
#P window setfont “Sans Serif” 12.;
#P comment 257 107 86 9109516 < < new speed;
#P window setfont “Sans Serif” 9.;
#P flonum 426 458 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 376 432 60 9109513 unpack 0. 0.;
#P newex 363 407 53 9109513 jit.3m;
#B color 5;
#P user jit.fpsgui 56 152 60 9109513 0;
#P user jit.pwindow 59 411 302 302 0 1 0 0 1 0;
#P newex 302 330 109 9109513 jit.matrix 4 char 200 200;
#P newex 362 386 59 9109513 jit.op @op !-;
#P newex 165 326 109 9109513 jit.matrix 4 char 200 200;
#P newex 229 209 27 9109513 + 10;
#P newex 183 209 27 9109513 + 10;
#P window setfont “Sans Serif” 12.;
#P message 166 107 90 9109516 newRndSpeed;
#P window setfont “Sans Serif” 9.;
#P newex 165 272 48 9109513 t b l reset;
#P window setfont “Sans Serif” 12.;
#P newex 166 136 70 9109516 js collide.js;
#P window setfont “Sans Serif” 9.;
#P newex 166 230 60 9109513 pack 0 0 0 0;
#P newex 184 187 54 9109513 unpack 0 0;
#P window setfont “Sans Serif” 12.;
#P newex 166 162 27 9109516 t l l;
#P window setfont “Sans Serif” 9.;
#P message 165 251 98 9109513 paintoval $1 $2 $3 $4;
#P newex 164 300 95 9109513 jit.lcd 4 char 200 200;
#P window setfont “Sans Serif” 12.;
#P number 110 75 35 12 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 56 125 31 9109516 t b b;
#P toggle 56 62 35 0;
#P newex 56 99 64 9109516 qmetro 22;
#P window setfont “Sans Serif” 18.;
#P comment 48 21 340 9109522 1 channel image based collision detection;
#P connect 2 0 1 0;
#P fasten 1 0 3 0 61 123 61 123;
#P connect 3 0 19 0;
#P fasten 25 0 18 0 170 406 65 406;
#P connect 4 0 1 1;
#P connect 11 2 5 0;
#P connect 11 1 5 0;
#P connect 11 0 5 0;
#P fasten 9 0 6 0 171 250 170 250;
#P fasten 6 0 11 0 170 278 170 278;
#P connect 5 0 15 0;
#P connect 15 0 25 0;
#P connect 12 0 10 0;
#P fasten 28 0 10 0 381 132 171 132;
#P fasten 3 1 10 0 154 147 154 132 171 132;
#P connect 10 0 7 0;
#P connect 7 0 9 0;
#P connect 8 0 13 0;
#P fasten 7 1 8 0 188 187 189 187;
#P connect 13 0 9 2;
#P connect 17 0 25 1;
#P fasten 14 0 9 3 234 229 219 229;
#P connect 8 1 14 0;
#P fasten 24 0 17 0 307 328 307 328;
#P connect 15 0 16 0;
#P fasten 16 0 20 0 367 406 368 406;
#P fasten 27 0 28 0 472 503 580 503 580 86 381 86;
#P connect 20 1 21 0;
#P connect 17 0 16 1;
#P fasten 27 0 30 0 472 503 580 503 580 86 423 86;
#P connect 21 1 22 0;
#P fasten 21 1 26 0 431 453 472 453;
#P connect 26 0 27 0;
#P pop;

//++++++++++++++++++++++++++++++++++
//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<0){
//if this not checked can happen a “stuck” circle
if(circle._x> lcd.width-10){
circle._x= lcd.width-10 ;}
else if(circle._x<0){
circle._x=0;
}
speedx *= -1; //if reach bound reverse direction

}else if(circle._y>= lcd.high-10 || circle._y< =0){
if(circle._y> lcd.high-10 ){
circle._y= lcd.high-10 ;}
else if(circle._y<0){
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
> http://www.maxobjects.com
>
>
>
>
>

#73871

You must be logged in to reply to this topic.