We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to make 2d Pixel Collision
Page Index Toggle Pages: 1
How to make 2d Pixel Collision (Read 540 times)
How to make 2d Pixel Collision
Jul 22nd, 2009, 10:46am
 
I couldn't seem to find any scripts for simple pixel collisions in processing, so I made this one.

Specificially, this is for 2d shooting base to check for bullet collisions, but you can modify it for anything

For this class object, "enemy", the int variable 'limit' is used to count frames.

The shot object has to keep track of its own boundaries, and (due to its population size) only has one graphic, simply called graphic. (whereas the enemy has a graphic[] array). This graphic width/height is used to set the ints sizex and sizey. (where sizex is graphic.width/2, and sizey is graphic.height/2)

Code:
    int l=x-sizex;  //determine current boundaries
   int r=x+sizex;  //l, r, t, b (left, right, top, bottom)
   int t=y-sizey;
   int b=y+sizey;

   limit++;      //timer check
   if(limit==2){
//begin collision check (every 2nd frame)
   
 for(int i=0; i<150; i++){
//check all shots in shot array
   if(shots[i]!=null){
//if shot exists
     Shot shoti=shots[i];
//store shot
       if(abs(x-shoti.x)<sizex && abs(y-shoti.y)<sizey){
//if shot is within bounds
       int x1 = max(l, shoti.l);  
       int x2 = min(r, shoti.r);
       int y1 = max(t, shoti.t);
       int y2 = min(b, shoti.b);
//determine min/max points of intersection
       for (int g = y1; g < y2; g++)
//for the size of the intersection vertically
       {
           for (int f = x1; f < x2; f++)
//for the size of the intersection horizontally
           {  
              if (alpha
              (graphic[0].pixels[(f - l) + (g - t)*sizex])>0
                           &&
                   alpha(shoti.graphic.pixels[(f - shoti.l)+ ((g - shoti.t)) ] )
                          >0)
          //if both actors have a matching pixel                      
           
   { //Do what you will!;               
    shots[i]=null; //remove bullet
     g=y2; //break out of this loop
     f=x2; //break out of this loop
    }
}}}}}}



Here is the boundary script for the shot:


Code:
    l=x-sizex;
   r=x+sizex;
   t=y-sizey;
   b=y+sizey;
Re: How to make 2d Pixel Collision
Reply #1 - Jul 22nd, 2009, 11:40am
 
So...
What is your syntax question?
Page Index Toggle Pages: 1