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 widthx_dif stands for position difference






