Results 1 to 27 of 27

Thread: A simple Collision

Threaded View

  1. #1

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    A simple Collision

    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:
    1. Public DirAngle As Single
    2. Public Vel As Single
    3. Public X As Single
    4. Public Y As Single
    5. Public Radius As Single
    6.  
    7. Public Sub initCircle(Xc As Single, Yc As Single, A As Single, V As Single, R As Single)
    8.    
    9.     X = Xc
    10.     Y = Yc
    11.     DirAngle = A
    12.     Vel = V
    13.     Radius = R
    14.    
    15. End Sub
    16.  
    17.  
    18. Public Sub Move()
    19.    
    20.     X = X + Vel * Cos(DegToRad(DirAngle))
    21.     Y = Y + Vel * Sin(DegToRad(DirAngle))
    22.    
    23. End Sub

    In the main loop i move two balls and check for their collision

    VB Code:
    1. Public Sub Collide(m As Integer, n As Integer)
    2. Dim A1 As Single, A2 As Single
    3. Dim Vx1 As Single, Vx2 As Single, A As Single
    4. Dim Vy1 As Single, Vy2 As Single
    5. On Error Resume Next
    6. Dim Dist As Single
    7.  
    8. Dist = DistanceBtw(Ball(m).X, Ball(m).Y, Ball(n).X, Ball(n).Y)
    9.  
    10. If (Dist <= Ball(m).Radius + Ball(n).Radius And 2 * Dist > Ball(m).Radius + Ball(n).Radius) Then
    11.  
    12.     A1 = -AngleBtw(VectorC(Ball(m).X, Ball(m).Y), VectorC(Ball(n).X, Ball(n).Y)) + _
    13.          DegToRad(Ball(m).DirAngle)
    14.     A2 = -AngleBtw(VectorC(Ball(n).X, Ball(n).Y), VectorC(Ball(m).X, Ball(m).Y)) + _
    15.          DegToRad(Ball(n).DirAngle)
    16.  
    17.     Vx1 = Ball(m).Vel * Cos(A1)
    18.     Vx1 = Vx1 + Ball(n).Vel * Cos(A2)
    19.     Vx2 = Vx1
    20.    
    21.     Vy1 = Ball(m).Vel * Sin(A1)
    22.     Vy2 = Ball(n).Vel * Sin(A2)
    23.    
    24.    
    25.     A = Vy1 / Sqr(Vx1 * Vx1 + Vy1 * Vy1)
    26.     Ball(m).DirAngle = RadToDeg(Atn(-A / Sqr(-A * A + 1)) + 2 * Atn(1))
    27.    
    28.    
    29.     A = Vy2 / Sqr(Vx2 * Vx2 + Vy2 * Vy2)
    30.    
    31.     Ball(n).DirAngle = RadToDeg(Atn(-A / Sqr(-A * A + 1)) + 2 * Atn(1))
    32.  
    33. End If
    34. 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
    Last edited by pradeepkrao; Mar 21st, 2003 at 08:03 AM.
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width