Results 1 to 27 of 27

Thread: A simple Collision

  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.

  2. #2
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    please use [Highlight=VB] tags(not [visual basic code], [visual basic] or [php] )...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  3. #3

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

    Hi cyborg

    Thankx..

    I am using this forum from past 1.5 years.. and I was not aware of vbcode tag... Thank you for letting me know..

    I have seen your code for ping pong game .. That was good.

    I guess you could help me out..

    Thanks,
    Pradeep
    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.

  4. #4
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    are the circles just gonna move straight to each other? and how do they move afterwards? (dont have time to examine your code )
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  5. #5
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    oh...thanks and no problems...hehe
    yes i think i could help you out...but im gonna meet a frien right now so i'll be back in 15 mins! cya then!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6

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

    Hi

    If you see the Ball Class. You see there is a DirAngle.

    Once the ball collides with respect to their velocities they will have to move in the calculated direction..

    That is what I am trying to do..

    I have done for conservation of momentum..

    But I dint want the balls to stop..

    So I thought of using Velocity as constant..

    I can post my code if you want..

    Thank,
    Pradeep
    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.

  7. #7

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

    Hi

    Well I will be leaving now..

    Any way I will post you the whole code..

    I will check it out tomorrow..

    Thanks,
    Pradeep
    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.

  8. #8

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

    Here is the Code

    Here is the Code

    Thanks,
    Pradeep
    Attached Files Attached Files
    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.

  9. #9
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    im not 100%, but i think you should do something like this:

    do a double loop like this:
    for i = 0 to ballcount
    for j = 0 to ballcount

    next
    next

    inside you should check the distance between the i-ball and j-ball and if its =< i-ball's radius + j-ball's radius, you have a collition...

    when colliding you should move both balls 1 pixel away from each other to avoid them to get stuck...
    Last edited by cyborg; Mar 24th, 2003 at 01:17 AM.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  10. #10
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i havent gone through your code yet, but ill do that if my previous post didnt help you...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  11. #11

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

    Hi cyborg

    Thanks for mailing..

    But in what direction will I have to move the ball.. Its apperent that in opposite direction... I'll try it and let you know the results..

    I know that I am not following the simplest methods of using a 2d matrix for movements.

    Well I shall give a try for what you have said probably it will work...

    If not lets do it together.. and come up with a carrom and a billiards game..

    Thanks,
    Pradeep
    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.

  12. #12
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i have no idea how it should move after collition....

    if you split up the whole circle into 360 pieces and check which one get his, you should get the new angle for the movement... but because the ball was moving at a speed in an angle before it got hit, it wont be that exact angle....i have no idea on how to work this out...you should probably post in the math forum...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  13. #13

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

    Hi Cyborg

    HI,

    I think I can do it.. I can get the direction in which i have to move them as it is in the line of the center of tw circle and their point of collision..


    Do we have a math forum here..?.


    Thanks,
    Pradeep
    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.

  14. #14
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    yes, there is a math forum here....just go to the main vbforums page and scroll down a bit


    i've tried to do a collition code...i works pretty good, but it doesnt consider the previous speed and angle of the ball...

    check it out!
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  15. #15

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

    Hi

    Cyborg,

    That was too good.. I like the idea of redrawing the circle with the background.. It prevents flikering...

    There is no dammping i guess by modifying the code so that the velocity is reduced by a factor after every movement would be good..

    Thanks for that good piece of code..

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

  16. #16
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    thanks!

    this was just a test of the collition...try this new code and see if the new angles are more correct! Now im using the avarage from the new and old angle...but it still doesnt consider the speed...
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  17. #17
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    sorry for making all these zips, but ive made some changes in this one...
    damping, filled balls with numbers on them, etc...
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  18. #18

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

    Hi

    That was Simply Great..

    But All the balls are moving initially.. Cant you keep them at rest and make one of them as a striker... so that it collides and then there is a movement..

    Thanks .. I dont mind if you zip it.. But I liked your programming style..

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

  19. #19

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

    Hi

    I have achived it...

    VB Code:
    1. Public Sub BoostBalls()
    2.     Dim i As Long
    3.     i = 0
    4.     'For i = 0 To BallStack.Count
    5.         BallStack.Ball(i).Speed = BallStack.Ball(i).Speed + 10 * Rnd + 1
    6.     'Next
    7. End Sub


    Thanks,
    Pradeep
    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.

  20. #20
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    hehe thanks!

    well...this wasnt going to be a fully working game, but i think i'll do that anyways

    i think i've got the collition working now...

    i'll make some changes and additions, then i'll post again!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  21. #21
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    hehe! yeah!

    but it would be nice if one could select the angle by moving the mouse!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  22. #22

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

    Hi

    I am doing that.. now.. I will let you know.. If I have suceeded...

    Thanks a lot...

    Keep posting..

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

  23. #23
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    done it!

    try this!
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  24. #24
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i still doesnt like the collition detection and the new angles are still a bit wrong...im not sure how to fix this properly...i've always had problems doing those things...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  25. #25

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

    Hae

    That was Great....

    Well I have done it using a Stick..

    I will post once i do it completely...

    Great that by posting.. you have developed a game ......

    Well post it in the code bank..

    Thanks,
    Pradeep
    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.

  26. #26
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    I'd like to see your version too

    Yes, when its all done, we'll post it in the code bank...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  27. #27

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

    Hi

    Cyborg,

    Sorry for this huge delay.. I had some important work in the office..

    Here is my version.. I will have to modify the angle after collision and the velocity also.. as it is not accurate in the code...


    Thank you a lot for this help..

    Pradeep
    Attached Files Attached Files
    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