• Create Account

# pixel perfect collision between sprites of different sizes

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1savail  Members   -  Reputation: 333

Like
0Likes
Like

Posted 06 April 2012 - 05:46 AM

Hey,
I've been working long to achieve a well working collision but since several hours I just can't get it work 100% properly. In the code beneath arrays: start and end store values of the area where 2 sprites cross each other. From this area I'll be checking for opaque pixels etc. It works perfect for sprites of same size but for sprites of different sizes it does assign wrong values for the borders.
Could anyone help me solve the problem and tell what's wrong in the code beneath? I will be very grateful!
int x_dif = pos_x[0] - pos_x[1];
if(x_dif >= 0)
{
//if whole width of sprite nr 0 is enclosed in the width of sprite nr 1
if(pos_x[1] + frame_w[1] > pos_x[0] + frame_w[0])
{
start[0] = 0; start[1] = x_dif;
end[0] = frame_w[0];	end[1] = x_dif + frame_w[0];
}
//otherwise widths of sprites cross each other
else
{
start[0] = 0; start[1] = x_dif;
end[0] = frame_w[1] - x_dif;	end[1] = frame_w[1];
}
}
else //(if x_dif < 0)
{
if(pos_x[1] + frame_w[1] < pos_x[0] + frame_w[0])
{
start[0] = -x_dif; start[1] = 0;
end[0] = -x_dif + frame_w[1];	end[1] = frame_w[1];
}
else
{
start[0] = - x_dif; start[1] = 0;
end[0] = frame_w[0];   end[1] = frame_w[0] + x_dif;
}
}
//operations to prevent crash and ensure safety
if(end[0] < start[0]) end[0] = start[0];
if(end[1] < start[1]) end[1] = start[1];

frame_w stands for frame width
x_dif stands for position difference

### #2falconmick  Members   -  Reputation: 80

Like
0Likes
Like

Posted 06 April 2012 - 11:53 AM

I'm not entirely sure what language you are running, the only only time I have done pixel perfect collisions is with ActionScript 3...
I used this to learn how to do it.. A quick glance and I can't see what the problem is. Sorry, maybe that link will help though..

All the best

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS