Skip to main content

Microsoft Silverlight

Answered Question SFERIC COLLISION SILVERLIGHTRSS Feed

(0)

andry80pa
andry80pa

Member

Member

0 points

1 Posts

SFERIC COLLISION SILVERLIGHT

Hello everyone,
I am following a tutorial on collision on a hypothetical game of billiards in Silverlight.

The sub that controls the collision and therefore the effects of shocks, however, has a part that I can not understand.

The sub control a ball and another ball, the first ball is inside the class of the ball itself. Then check the collision between the ball belongs to the same class (this) and the other ball always passed as an object/class "Ball" in the parameter b of the sub.
The variables _x and _y are the x and y coordinates of the balls.
The variables _vx and _vy are nothing more than the x and y coordinates of the vector direction and then describe the movement of the ball (they are generated by cosine and sine of angle).

Said that the sub is as follows:

public bool DoCollide(Ball b)
2: {
3: // calculate some vectors
4: double dx = this._x - b._x;
5: double dy = this._y - b._y;
6: double distance2 = dx * dx + dy * dy;
9:
10: if (Math.Abs(dx) > this._d || Math.Abs(dy) > this._d)
11: return false;
12: if (distance2 > this._d2)
13: return false;

up to here it's clear.

13:  double dvx = this._vx - b._vx;
14: double dvy = this._vy - b._vy;
15: // make absolutely elastic collision
16: double mag = dvx * dx + dvy * dy;
17:  
18: // test that balls move towards each other
19: if (mag > 0)
20: return false;
21:  
22: mag /= distance2;
23:  
24: double delta_vx = dx * mag;
25: double delta_vy = dy * mag;
26:  
27: this._vx -= delta_vx;
28: this._vy -= delta_vy;
29:  
30: b._vx += delta_vx;
31: b._vy += delta_vy;
32:  
33: return true;
34: }

I do not understand the formulas that he has applied and why ... i know just that it works!
But not enough that it works because I want to understand WHY it works ... anyone have any idea?

The complete tutorial is here:

http://blogs.msdn.com/coding4fun/archive/2007/09/19/5002772.aspx

thanks to all who help me.

vincracker
vincracker

Contributor

Contributor

3116 points

522 Posts

Answered Question

Re: SFERIC COLLISION SILVERLIGHT

HI,

When I need any help related to physics in SL or game related query I just go to Andy's website and get whatever I needed. There is also some physics helper libraries like  Farseer Physics  and Physics Helper for Silverlight which you can use to get some sophisticated physics in your game.

And if you have any problem related to game then post it in Game Development Forum

(If you still have problem then let me know otherwise mark reply as answer.) 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities