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?
5 replies to this topic
Sponsor:
#3 Members - Reputation: 802
Posted 16 June 2012 - 04:11 PM
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.
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.
#5 Members - Reputation: 652
Posted 16 June 2012 - 10:09 PM
Also look at the Rectangle.Intersects method, it should help you out ALOT in XNA. http://msdn.microsof...intersects.aspx
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:

Red squares are rectangles.
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:

Red squares are rectangles.
Edited by Xanather, 16 June 2012 - 10:52 PM.
#6 Members - Reputation: 185
Posted 17 June 2012 - 06:12 AM
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.






