-1

This question is regarding WPF.

I have two balls, one static and the other is moving towards the static ball. On collision the balls would move in the direction that would be decided by the collision that at which angle they would collide and move accordingly.

My question is basically for suggestions for what is the easiest and the most effective way of colliding the objects in WPF and after collision giving them a path to move accordingly. Apart from deceleration etc, what should be the best strategy that should be applied to DETECT collision and GIVE NEW PATHS TO THE BALLS AFTER COLLISION.

halfer
  • 19,824
  • 17
  • 99
  • 186
Affuu
  • 151
  • 2
  • 2
  • 9
  • 3
    **Close-Voting:** *Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist* – Federico Berasategui Dec 10 '13 at 21:03
  • Your are very true about it sir but i asked for SUGGESTIONS as it is clearly written in my description – Affuu Dec 10 '13 at 21:08
  • possible duplicate of [Ball to Ball Collision - Detection and Handling](http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling) – Gayot Fow Dec 10 '13 at 21:15
  • i have read all collision questions here but i could not get any information on how to get the new paths of collided balls IN "WPF". What functions or properties are actually used to determine them. – Affuu Dec 11 '13 at 19:15
  • @GarryVass if you could possibly tell me how to get it all done in WPF i would really appreciate it – Affuu Dec 11 '13 at 19:16

2 Answers2

0

I would calculate the distance (pythagoras) between them, if the distance is smaller than the two radius added, a collision occurs.

Check here for the angle between the points: Math Calculation to retrieve angle between two points?

You could check this one: Ball to Ball Collision - Detection and Handling

Community
  • 1
  • 1
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • i appreciate your answer sir but could you please explain that? i mean collision part is fine, but how would you suggest me that i should work on getting the paths AFTER their collision has taken place? – Affuu Dec 11 '13 at 19:19
  • Did you read the second link? I think there's a great explaination there. – Jeroen van Langen Dec 11 '13 at 22:20
0

Simply figure out the distance of the centers of the 2 balls.

if (distance(ball1.Center, ball2.Center) <= ball1.Radius + ball2.Radius) 
{
    // collision
}

For the distance use this:

double x = ball1.Center.X - ball2.Center.X;
double y = ball1.Center.Y - ball2.Center.Y;
double distance = Math.sqrt(x*x + y*y); // pythagoras

For calculating the new directions you'll need some more math. Have a look for a geometrics library like it is delivered with XNA. Try googeling your question again and use XNA in the search instead of WPF - this will solve your problem, i guess.

rulebot
  • 232
  • 3
  • 7
  • Or don't calculate this on your own and just write `(ball1.Center - ball2.Center).Length` (from [Vector](http://msdn.microsoft.com/en-us/library/System.Windows.Vector.aspx)). – Clemens Dec 10 '13 at 22:01
  • That's true. Using built-in methods are aven better and less error-prone. I didn't use WPF for years and did not know about the existence of the Vector class until now. If it works like the Vector class of XNA, it's a a full scale toolkit for all your needs. :) – rulebot Dec 10 '13 at 22:17
  • @rulebot i am restricted to WPF only. XNA is not an option right now. – Affuu Dec 11 '13 at 19:22
  • Hi, mathematics and geometry works the same way in WPF and XNA ;) This is just a quite common question in the XNA community. – rulebot Dec 11 '13 at 19:25
  • Alright, i have an idea can you help me out in this? if i have a ball colliding with an other, the both will collide on some point, meaning some point on their circumference. If i can stretch a line from that point through the center of the ball, will it do the direction part easily? – Affuu Dec 11 '13 at 19:47
  • Did you look for this? http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling It was posted in the answer below. I guess the second animation does represent your answer?! You'll need some basic Vector Math, better watch out for a geometry lib - it's faster than re-inventing the wheel ;) – rulebot Dec 11 '13 at 20:09
  • No problem. I hope it helped :) – rulebot Dec 11 '13 at 20:23