Sign in to follow this  
savail

pixel perfect collision between sprites of different sizes

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!
[CODE]
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];
[/CODE]
frame_w stands for frame width
x_dif stands for position difference

Share this post


Link to post
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 [url="http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/"]this[/url] 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this