How do you code for Collision detection in a bounce balls program
I want it so you can turn it one and off. i have 5 timers and 3 sub routines for this program(i got very bored in class) i don't want to have to go back in the code in all of them to make the change if possible. im using the circle cmd to draw the balls. if you want i can upload my project in a zip file.
i know this is pretty confusing thanks for bearing with me.
Re: How do you code for Collision detection in a bounce balls program
Uploading it would be a good idea... collision detection isn't that difficult in two dimensions, but the five timers seems a little overkill... I'm programming Pong with only one...
Re: How do you code for Collision detection in a bounce balls program
This thread is about circle collision detection.. Do you have yourballs bouncing properly?
Re: How do you code for Collision detection in a bounce balls program
For circles (and balls in 3D) collision detection is simple.
Just calculate the range between the two balls' center and test if that range is equal to or less than the sum of both balls' radius.
If that test returns true, they are colliding.
1 Attachment(s)
Re: How do you code for Collision detection in a bounce balls program
here is the project
also im still new to this i have been thinking how how i could do that but i cant fiqure it out. i dont want want to be babied through but a small portion of the code would be helpful.
also i have 5 timers because i have different effects.
Re: How do you code for Collision detection in a bounce balls program
Re: How do you code for Collision detection in a bounce balls program
first off, for simplicity of reading and using, I would have created a new Type for the balls, and dimmed that as an array. Example:
VB Code:
Type Balls
X As Integer
Y As Integer
Red As Integer
Green As Integer
Blue As Integer
XInc As Integer
YInc As Integer
End Type
Dim myBalls(count) As Balls
That will make keeping track of everything easier, and it would make the collision detection somewhat simpler to keep track of as well.
Second, I'd put in a Me.Cls whenever the user changes a setting. That way it starts fresh every time and doesn't look messy.
As for the detection itself, it's just as jeroen said. Check each ball against each other ball and if the center distance is less than the diameter, add the collision code. It's an easy loop:
VB Code:
For i = 1 To BallCount
For j = i to BallCount
'check i vs. j
'will check all combinations
Next j
Next i
Re: How do you code for Collision detection in a bounce balls program
Quote:
Originally Posted by timeshifter
first off, for simplicity of reading and using, I would have created a new Type for the balls, and dimmed that as an array. Example:
VB Code:
Type Balls
X As Integer
Y As Integer
Red As Integer
Green As Integer
Blue As Integer
XInc As Integer
YInc As Integer
End Type
Dim myBalls(count) As Balls
That will make keeping track of everything easier, and it would make the collision detection somewhat simpler to keep track of as well.
Im in a class for this first i have no clue what type is. second this is how my teacher told us to write the code.
Quote:
Second, I'd put in a Me.Cls whenever the user changes a setting. That way it starts fresh every time and doesn't look messy.
once again no clue what that is
Quote:
As for the detection itself, it's just as jeroen said. Check each ball against each other ball and if the center distance is less than the diameter, add the collision code. It's an easy loop:
VB Code:
For i = 1 To BallCount
For j = i to BallCount
'check i vs. j
'will check all combinations
Next j
Next i
thank you for the code.
I am taking a class for VB and he is slowly introducing us to different things. I know my project isn't the best way to do this but im using what little i know
right now. i heard at the end of the year the projects get pretty hard.
anyway thank you for the code.
That code didnt work i put it in both the form load and bouncyball timer and i changed ballcount to asize (what the teacher used in his example program so i used it) where do i put it?
Re: How do you code for Collision detection in a bounce balls program
Here lies the problem with so many timers. If you only had one timer, you'd put it there. With five timers, that's a lot of redundant code.. generally considered poor style.
Me.Cls is just a command that clears everything off the form. Just mess around with it whenever the user changes something, and go from there. Trust me, it'll look a lot better.
Re: How do you code for Collision detection in a bounce balls program
This program incorporates some very basic collision detection with circles. I'd have to dive into some trig to get a more realistic output, but this is a start for you.
Re: How do you code for Collision detection in a bounce balls program
This detection is a bit better... still doesn't incorporate angles, but it does look better than the other one. Have a look and see how I did it. All the detection stuff is in the Timer event. :wave:
Re: How do you code for Collision detection in a bounce balls program
Re: How do you code for Collision detection in a bounce balls program
That is meant for polygons, not circles. It's a lot simpler with circles to detect a collision, but it's harder to react to the collision.
Re: How do you code for Collision detection in a bounce balls program
So polygons... but it's a collision. :)
Re: How do you code for Collision detection in a bounce balls program
And to be a bit more specific, at this time JR's program simply detects the collision. It doesn't do anything about it. Mine may be sketchy, but at least it works. I just don't wanna take the time to work out all the math to create accurate collisions.
Re: How do you code for Collision detection in a bounce balls program
Well, I wasn't trying to compare yours to JR's - just pointed to where to find another sample, that's all.
I'm sure both samples (yours and Jacob's) are quite good, indeed. :wave: