Mar 21st, 2003, 07:59 AM
#1
Thread Starter
Fanatic Member
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:
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
Last edited by pradeepkrao; Mar 21st, 2003 at 08:03 AM .
Mar 21st, 2003, 08:02 AM
#2
Frenzied Member
please use [Highlight=VB] tags(not [visual basic code], [visual basic] or [php] )...
Mar 21st, 2003, 08:06 AM
#3
Thread Starter
Fanatic Member
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
Mar 21st, 2003, 08:06 AM
#4
Frenzied Member
are the circles just gonna move straight to each other? and how do they move afterwards? (dont have time to examine your code )
Mar 21st, 2003, 08:07 AM
#5
Frenzied Member
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!
Mar 21st, 2003, 08:09 AM
#6
Thread Starter
Fanatic Member
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
Mar 21st, 2003, 08:14 AM
#7
Thread Starter
Fanatic Member
Hi
Well I will be leaving now..
Any way I will post you the whole code..
I will check it out tomorrow..
Thanks,
Pradeep
Mar 21st, 2003, 08:15 AM
#8
Thread Starter
Fanatic Member
Here is the Code
Here is the Code
Thanks,
Pradeep
Attached Files
Mar 21st, 2003, 08:58 AM
#9
Frenzied Member
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 .
Mar 21st, 2003, 09:00 AM
#10
Frenzied Member
i havent gone through your code yet, but ill do that if my previous post didnt help you...
Mar 23rd, 2003, 11:44 PM
#11
Thread Starter
Fanatic Member
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
Mar 24th, 2003, 12:20 AM
#12
Frenzied Member
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...
Mar 24th, 2003, 12:35 AM
#13
Thread Starter
Fanatic Member
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
Mar 24th, 2003, 01:08 AM
#14
Mar 24th, 2003, 01:26 AM
#15
Thread Starter
Fanatic Member
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
Mar 24th, 2003, 01:35 AM
#16
Frenzied Member
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
Mar 24th, 2003, 01:58 AM
#17
Frenzied Member
sorry for making all these zips, but ive made some changes in this one...
damping, filled balls with numbers on them, etc...
Attached Files
Mar 24th, 2003, 02:04 AM
#18
Thread Starter
Fanatic Member
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
Mar 24th, 2003, 02:07 AM
#19
Thread Starter
Fanatic Member
Hi
I have achived it...
VB Code:
Public Sub BoostBalls()
Dim i As Long
i = 0
'For i = 0 To BallStack.Count
BallStack.Ball(i).Speed = BallStack.Ball(i).Speed + 10 * Rnd + 1
'Next
End Sub
Thanks,
Pradeep
Mar 24th, 2003, 02:07 AM
#20
Frenzied Member
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!
Mar 24th, 2003, 02:08 AM
#21
Frenzied Member
hehe! yeah!
but it would be nice if one could select the angle by moving the mouse!
Mar 24th, 2003, 02:14 AM
#22
Thread Starter
Fanatic Member
Hi
I am doing that.. now.. I will let you know.. If I have suceeded...
Thanks a lot...
Keep posting..
Thanks
Pradeep
Mar 24th, 2003, 02:24 AM
#23
Mar 24th, 2003, 02:26 AM
#24
Frenzied Member
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...
Mar 24th, 2003, 02:30 AM
#25
Thread Starter
Fanatic Member
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
Mar 24th, 2003, 02:39 AM
#26
Frenzied Member
I'd like to see your version too
Yes, when its all done, we'll post it in the code bank...
Mar 24th, 2003, 06:59 AM
#27
Thread Starter
Fanatic Member
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
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width