Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

Collision detection, 2d environment with C#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 azorac   Members   -  Reputation: 185

Like
0Likes
Like

Posted 16 June 2012 - 01:56 PM

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?

Sponsor:

#2 6677   Members   -  Reputation: 1050

Like
0Likes
Like

Posted 16 June 2012 - 03:22 PM

*collision

#3 laztrezort   Members   -  Reputation: 802

Like
1Likes
Like

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.

#4 Xanather   Members   -  Reputation: 652

Like
0Likes
Like

Posted 16 June 2012 - 10:06 PM

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...

#5 Xanather   Members   -  Reputation: 652

Like
0Likes
Like

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:

Posted Image
Red squares are rectangles.

Edited by Xanather, 16 June 2012 - 10:52 PM.


#6 azorac   Members   -  Reputation: 185

Like
0Likes
Like

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS