Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Collision detection, 2d environment with C#

Started by azorac Jun 16, 2012 at 7:56 PM 4 replies 2k views
Original Post
azorac
azorac
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, ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 3
|| test.test(position, ball.Width, ball.Height, blockPosition[j], blocks.Width, blocks.Height) == 4)
{
//restoring position
position.Y = position_backup.Y;

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

}
}
}[/source]

Any Ideas or suggestions?
laztrezort
laztrezort
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.
Xanather
Xanather
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...
azorac
azorac
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.