Hi,
I have achived a simple collision between two circles.
There is some thing wrong here can any body figure it out for me..
I have a Ball Class
VB Code:
Public DirAngle As Single Public Vel As Single Public X As Single Public Y As Single Public Radius As Single Public Sub initCircle(Xc As Single, Yc As Single, A As Single, V As Single, R As Single) X = Xc Y = Yc DirAngle = A Vel = V Radius = R End Sub Public Sub Move() X = X + Vel * Cos(DegToRad(DirAngle)) Y = Y + Vel * Sin(DegToRad(DirAngle)) End Sub
In the main loop i move two balls and check for their collision
VB Code:
Public Sub Collide(m As Integer, n As Integer) Dim A1 As Single, A2 As Single Dim Vx1 As Single, Vx2 As Single, A As Single Dim Vy1 As Single, Vy2 As Single On Error Resume Next Dim Dist As Single Dist = DistanceBtw(Ball(m).X, Ball(m).Y, Ball(n).X, Ball(n).Y) If (Dist <= Ball(m).Radius + Ball(n).Radius And 2 * Dist > Ball(m).Radius + Ball(n).Radius) Then A1 = -AngleBtw(VectorC(Ball(m).X, Ball(m).Y), VectorC(Ball(n).X, Ball(n).Y)) + _ DegToRad(Ball(m).DirAngle) A2 = -AngleBtw(VectorC(Ball(n).X, Ball(n).Y), VectorC(Ball(m).X, Ball(m).Y)) + _ DegToRad(Ball(n).DirAngle) Vx1 = Ball(m).Vel * Cos(A1) Vx1 = Vx1 + Ball(n).Vel * Cos(A2) Vx2 = Vx1 Vy1 = Ball(m).Vel * Sin(A1) Vy2 = Ball(n).Vel * Sin(A2) A = Vy1 / Sqr(Vx1 * Vx1 + Vy1 * Vy1) Ball(m).DirAngle = RadToDeg(Atn(-A / Sqr(-A * A + 1)) + 2 * Atn(1)) A = Vy2 / Sqr(Vx2 * Vx2 + Vy2 * Vy2) Ball(n).DirAngle = RadToDeg(Atn(-A / Sqr(-A * A + 1)) + 2 * Atn(1)) End If End Sub
But still the ball is not behaving perfectly after collision..
It some times stays in touch with other ball.
Any help is appreciated.
Thanks,
Pradeep




Reply With Quote