Sign in to follow this  
azorac

Collision detection, 2d environment with C#

Recommended Posts

azorac    185
Hello, I am trying to create collation detection between two objects and witch side one of the object gets hit. The problem is that only three out of four sides are currently working. And I can’t see what is wrong (I have probably become blind).

[source lang="csharp"]public int test(Vector2 collitionObject1, int width1, int hight1, Vector2 collitionObject2, int width2, int hight2)
{
int conclution = 0;
if (((collitionObject1.X > collitionObject2.X + width2 && collitionObject1.X < collitionObject2.X)
|| (collitionObject2.X > collitionObject1.X && collitionObject2.X < collitionObject1.X + width1))
&& (collitionObject1.Y > collitionObject2.Y && collitionObject1.Y < collitionObject2.Y))
{
conclution = 1;
}
else if (((collitionObject1.X +2> collitionObject2.X + width2 && collitionObject1.X < collitionObject2.X)
|| (collitionObject2.X > collitionObject1.X && collitionObject2.X < collitionObject1.X + width1))
&& (collitionObject1.Y + hight1 > collitionObject2.Y && collitionObject1.Y < collitionObject2.Y))
{
conclution = 2;
}
else if (((collitionObject1.Y > collitionObject2.Y && collitionObject1.Y < collitionObject2.Y + hight2)
|| (collitionObject2.Y > collitionObject1.Y && collitionObject2.Y < collitionObject1.Y + hight1))
&& (collitionObject1.X > collitionObject2.X && collitionObject1.X < collitionObject2.X + width2))
{
conclution = 3;
}
else if (((collitionObject1.Y > collitionObject2.Y && collitionObject1.Y < collitionObject2.Y + hight2)
|| (collitionObject2.Y > collitionObject1.Y && collitionObject2.Y < collitionObject1.Y + hight1))
&& (collitionObject1.X + width1 > collitionObject2.X && collitionObject1.X < collitionObject2.X))
{
conclution = 4;
}
return conclution;
}[/source]
And the method call:

[source lang="csharp"] //Ball collition
for (int i = 0; i < antal; i++)
{

//Mapblock collition
for (int j = 0; j < antaBlock; j++)
{
if (test.test(position[i], ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 3
|| test.test(position[i], ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 4)
{
//restoring position
position[i].Y = position_backup[i].Y;

up[i] *= -1;
position[i].Y += speed * up[i];
}
if (test.test(position[i], ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 1
|| test.test(position[i], ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 2)
{
position[i].X = position_backup[i].X;
left[i] *= -1;
position[i].X += speed * left[i];

}
}
}[/source]

Any Ideas or suggestions?

Share this post


Link to post
Share on other sites
laztrezort    1058
That is pretty difficult to read, there is no way I would want to try and understand all that without a debugger.

This is how I usually tackle these things: get out a piece of graph paper, draw out a simple test case and write down what each variable value should be, then step through the code until there is a discrepancy between the graph paper test case and the watch variables.

Share this post


Link to post
Share on other sites
Xanather    782
I'm assuming your using visual studio, do what lazterzort says and to try debugging it, place break points in the code and analyze what actually happens with the fields after "if" statements, etc...

Share this post


Link to post
Share on other sites
Xanather    782
Also look at the Rectangle.Intersects method, it should help you out ALOT in XNA. [url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersects.aspx"]http://msdn.microsof...intersects.aspx[/url]

Edit: ahh, i see what your doing I think, trying to check which side the object intersects with? It would still probably be easier using the Rectangle class for example:

[img]http://www.xanather.com/custom/misc/example.png[/img]
Red squares are rectangles. Edited by Xanather

Share this post


Link to post
Share on other sites
azorac    185
Thanks for the response, I ended up using some pen and paper to figure out how to approach this. In the end I changed my way of detecting a collision. I only check the corners of object one against the coordinates of object two, witch ended up working as intended.

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