# pixel perfect collision between sprites of different sizes

This topic is 2749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75