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;