#### Archived

This topic is now archived and is closed to further replies.

# Ball collision code... with bugs... O_o

This topic is 5716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is for a pool game i made in Vb, i''ll probably post it in the game forum or something, its cool ^_^ buut theres only one bug... its pretty annoying too. My code should work fine, and does most of the time, but sometimes the balls get drawn overlapping (if the balls are moving too fast) which shouldn''t happen... this happens also when a ball collides with the wall/ball and then collides with another ball in the same instance, it overlaps, and gets a bit messy... its not a big deal but its annoying. Nb: this is in Vb, sorry if its badly written or something RecX&Y and PolR&T are just linear polar convertions, everything else should be pretty easy to understand... i''m not THAT bad...

Dim p, e
p = 0

For no = 1 To UBound(Ball)
If Ball(no).InPlay = True Then

''Update
Ball(no).X = Ball(no).X + Ball(no).xV
Ball(no).Y = Ball(no).Y + Ball(no).yV

''See if ball is in hole

''Bottom Left
If Ball(no).X + wid < 90 And Ball(no).Y + (wid) > 464 Then BallIn (no)
''Bottom right
If Ball(no).X + wid > 309 And Ball(no).Y + (wid) > 464 Then BallIn (no)
''Middle Right
If Ball(no).X + (wid) > 320 And Ball(no).Y + (wid) < 284 And Ball(no).Y + (wid) > 260 Then BallIn (no)
''Middle Left
If Ball(no).X + (wid) < 85 And Ball(no).Y + (wid) < 284 And Ball(no).Y + (wid) > 260 Then BallIn (no)
''Top Left
If Ball(no).X + wid < 90 And Ball(no).Y + (wid) < 80 Then BallIn (no)
''Top Right
If Ball(no).X + wid > 309 And Ball(no).Y + (wid) < 80 Then BallIn (no)

''Sides
If Ball(no).Y > Picture1.ScaleHeight - 90 Then Ball(no).Y = (Picture1.ScaleHeight - 90): Ball(no).yV = -Ball(no).yV
If Ball(no).X > Picture1.ScaleWidth - 90 Then Ball(no).X = (Picture1.ScaleWidth - 90): Ball(no).xV = -Ball(no).xV
If Ball(no).X < 73 Then Ball(no).X = 73: Ball(no).xV = -Ball(no).xV
If Ball(no).Y < 63 Then Ball(no).Y = 63: Ball(no).yV = -Ball(no).yV

''Decellerate
Ball(no).xV = Ball(no).xV * 0.98
Ball(no).yV = Ball(no).yV * 0.98

''Stop if going too slow
If Ball(no).yV < 0.3 And Ball(no).yV > -0.3 And Ball(no).xV < 0.3 And Ball(no).xV > -0.3 Then Ball(no).yV = 0: Ball(no).xV = 0
End If
Next no

For no = 1 To UBound(Ball)
If Ball(no).InPlay = True Then
For no2 = 1 To UBound(Ball)
If Ball(no2).InPlay = True Then
If no2 = no Then Exit For
If PolR(Ball(no).X - Ball(no2).X, Ball(no).Y - Ball(no2).Y) < (wid * 2) Then ''Collision

''Play Hit sound

sndPlaySound App.Path & "\balloon.wav", SND_NODEFAULT Or SND_ASYNC Or SND_NOSTOP

''Physics Starts Here
Dim ma, amp, nVh1, nVh2, t1, t2

Theta = PolT(Ball(no2).X - Ball(no).X, Ball(no2).Y - Ball(no).Y) ''Angle between balls

ma = PolR(Ball(no).xV, Ball(no).yV): amp = PolT(Ball(no).xV, Ball(no).yV) - Theta
nVh1 = RecX(ma, amp): nV1 = RecY(ma, amp) ''Take away angle
ma = PolR(Ball(no2).xV, Ball(no2).yV): amp = PolT(Ball(no2).xV, Ball(no2).yV) - Theta
nVh2 = RecX(ma, amp): nV2 = RecY(ma, amp) ''Take away angle

t1 = (nVh1 * ((Ball(no).M - Ball(no2).M) / (Ball(no).M + Ball(no2).M))) + (nVh2 * ((2 * Ball(no2).M) / (Ball(no).M + Ball(no2).M))) ''Horizontal Equation
t2 = (nVh2 * ((Ball(no).M - Ball(no2).M) / (Ball(no).M + Ball(no2).M))) + (nVh1 * ((2 * Ball(no2).M) / (Ball(no).M + Ball(no2).M))) ''Horizontal Equation
nVh1 = t1
nVh2 = t2

ma = PolR(nVh1, nV1): amp = PolT(nVh1, nV1) + Theta
Ball(no).xV = RecX(ma, amp): Ball(no).yV = RecY(ma, amp) ''Add angle Back
ma = PolR(nVh2, nV2): amp = PolT(nVh2, nV2) + Theta
Ball(no2).xV = RecX(ma, amp): Ball(no2).yV = RecY(ma, amp) ''Add angle Back

''Move back to outside collision zone
Dim tempt, temp2, tr, Xmod, Ymod

tempt = PolT(Ball(no).X - Ball(no2).X, Ball(no).Y - Ball(no2).Y)
temp2 = PolT(Ball(no2).X - Ball(no).X, Ball(no2).Y - Ball(no).Y)

tr = (((2 * wid) - PolR(Ball(no).X - Ball(no2).X, Ball(no).Y - Ball(no2).Y)) / 2) + 0.8

Ball(no).Y = Ball(no).Y + (RecY(tr, tempt))
Ball(no2).Y = Ball(no2).Y + (RecY(tr, tempt2))
Ball(no).X = Ball(no).X + (RecX(tr, tempt))
Ball(no2).X = Ball(no2).X + (RecX(tr, tempt2))

End If ''End Physics
End If
Next no2
End If
Next no

If you want to check it out: http://www.deepfriedforums.com/attachment.php?s=&postid=304547 ^ the game

##### Share on other sites
allow movement as a function of time independant of the frame delay ie objects can collide IN BETWEEN frames:

spheres A and B with known positions and velocities
A=Ap+Av*t
B=Bp+Bv*t
let R=Ap-Bp
let S=Av-Bv
R*R + 2*R*S*t + S*S*t*t - (Ar+Br)^2 = 0

solve as a normal quadratic to get the time they collide which can be different to the time elapsed.

********

A Problem Worthy of Attack
Proves It''s Worth by Fighting Back

##### Share on other sites
hi, i also had a problem with my ball getting stuck before, but in a breakout clone, someone adviced me to check collision before it collides the next frame, this thing works and my ball never gets stuck again(i think),

ie,
if(ballposition + (velocity * elapsed time) >= boundingvolumes && <= bounding volumes)

• 10
• 17
• 9
• 14
• 41