I'm having a bit of trouble with collision detection. I've written a pong game and so far I've got the ball bouncing around the screen and the paddles moving up and down. I can't seem to get the ball to bounce off the paddles. I did a search and found other threads with snipets of code but can't get them to work with what I've already done.
This is the code I've used for the bouncing ball:
Dim RtnValue As Long
'Erase Ball
RtnValue = BitBlt(picField.hDC, BallX, BallY, picBall.ScaleWidth, picBall.ScaleHeight, picBlank.hDC, CLng(0), CLng(0), SRCCOPY)
'Compute Ball position / Check for bounces
BallX = BallX + BallXDir * BallXSpd
BallY = BallY + BallYDir * BallYSpd
If BallX < 0 Then
BallX = 0
BallXDir = 1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
lblScore2.Caption = Str(Val(lblScore2.Caption) + 1)
ElseIf BallX + picBall.ScaleWidth > picField.ScaleWidth Then
BallX = picField.ScaleWidth - picBall.ScaleWidth
BallXDir = -1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
lblScore1.Caption = Str(Val(lblScore1.Caption) + 1)
End If
If BallY < 0 Then
BallY = 0
BallYDir = 1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
ElseIf BallY + picBall.ScaleHeight > picField.ScaleHeight Then
BallY = picField.ScaleHeight - picBall.ScaleHeight
BallYDir = -1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
End If
RtnValue = BitBlt(picField.hDC, BallX, BallY, picBall.ScaleWidth, picBall.ScaleHeight, picBall.hDC, CLng(0), CLng(0), SRCCOPY)
picPaddle1 is on the left and picPaddle2 is on the right.
I'm sure it's easy to do, but I'm a beginner and I need it explained in "Dummy" terms.
I don't know what kind of an effect your going for. I assume in a pong game when the ball hits the paddel it get reflected off in the opposite direction that in came in, can't tell you how to do that based on your code.
I'd detect the collison by measureing from the center point of the ball and compareing it to the distance from the regtangular area of the paddle. If the difference is less then the radius of the circle you have a collison. The math for that can be found in most gaming books.
Its hard to give you an exact answer based on your code.
All will fall before the might of the Black Sashi...
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Did you look at this link i gave you in the last thread? It has some collision detection stuff in it.
It's probably more than you need, as the ball doesn't have to be too accurate, so here's a simple example of how to do the one on the left (don't forget to remove the code you have to make the ball bounce of the left wall):
VB Code:
'Is the ball near the paddle & in the same height as the paddle
If BallX < PicPaddle1.Left + PicPaddle1.Width And BallY > PicPaddle1.Top And BallY < PicPaddle1.Top + PicPaddle1.Height Then
BallXDir Abs(BallXDir) 'Make the ball move away from the left paddle
End If
Last edited by SLH; May 14th, 2002 at 04:04 AM.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Sorry to bother y'all again but I'm still having probs.
I added the code for the ball to bounce off the left paddle and got it working but when I added the code for the right paddle, neither left nor right paddle would work.
This is the code:
Private Sub timGame_Timer()
Dim RtnValue As Long
'Erase Ball
RtnValue = BitBlt(picField.hDC, BallX, BallY, picBall.ScaleWidth, picBall.ScaleHeight, picBlank.hDC, CLng(0), CLng(0), SRCCOPY)
'Compute Ball position / Check for bounces
BallX = BallX + BallXDir * BallXSpd
BallY = BallY + BallYDir * BallYSpd
If BallX < 0 Then
BallX = 0
BallXDir = 1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
lblScore2.Caption = Str(Val(lblScore2.Caption) + 1)
ElseIf BallX + picBall.ScaleWidth > picField.ScaleWidth Then
BallX = picField.ScaleWidth - picBall.ScaleWidth
BallXDir = -1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
lblScore1.Caption = Str(Val(lblScore1.Caption) + 1)
End If
If BallY < 0 Then
BallY = 0
BallYDir = 1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
ElseIf BallY + picBall.ScaleHeight > picField.ScaleHeight Then
BallY = picField.ScaleHeight - picBall.ScaleHeight
BallYDir = -1
RtnValue = sndPlaySound(Bounce, SND_FILENAME Or SND_ASYNC)
End If
RtnValue = BitBlt(picField.hDC, BallX, BallY, picBall.ScaleWidth, picBall.ScaleHeight, picBall.hDC, CLng(0), CLng(0), SRCCOPY)
'Collision with left Paddle
If BallX < picPaddle1.Left + picPaddle1.Width And BallY > picPaddle1.Top And BallY < picPaddle1.Top + picPaddle1.Height Then
BallXDir = 1
RtnValue = sndPlaySound(Laser, SND_FILENAME Or SND_ASYNC)
lblScore1.Caption = Str(Val(lblScore1.Caption) + 1)
End If
'Collision with right Paddle
If BallX + picBall.ScaleWidth > picPaddle2.Left Then
BallXDir = -1
RtnValue = sndPlaySound(Laser, SND_FILENAME Or SND_ASYNC)
lblScore2.Caption = Str(Val(lblScore2.Caption) + 1)
End If
I need help again. I'm using the following code for the paddles:
'Collision with left Paddle
If BallX < picPaddle1.Left + picPaddle1.Width + 6 And BallY > picPaddle1.Top And BallY < picPaddle1.Top + picPaddle1.Height Then
BallXDir = 1
RtnValue = sndPlaySound(Laser, SND_FILENAME Or SND_ASYNC)
lblScore1.Caption = Str(Val(lblScore1.Caption) + 1)
BallXSpd = BallXSpd + 1
End If
'Collision with right Paddle
If BallX + picBall.ScaleWidth + 6 > picPaddle2.Left And BallY > picPaddle2.Top And BallY < picPaddle2.Top + picPaddle2.Height Then
BallXDir = -1
RtnValue = sndPlaySound(Laser, SND_FILENAME Or SND_ASYNC)
lblScore2.Caption = Str(Val(lblScore2.Caption) + 1)
BallXSpd = BallXSpd + 1
End If
It works fine until the games starts picking up speed then the ball starts to go through the paddles instead of bouncing off.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Why do you have the +6? If you do have that as well, wouldn't it be -6 for the right paddle.
Try this, for the left paddle: (I'm guessing that BallX + (BallXSpd*BallXDir) will be the next location of the ball's x location, and it will be similar for y)
VB Code:
If BallX + (BallXSpd*BallXDir) < picPaddle1.Left + picPaddle1.Width And BallY + (BallYSpd*BallYDir) > picPaddle1.Top And BallY + (BallSpd*BallYDir) < picPaddle1.Top + picPaddle1.Height Then
and this for the right paddle:
VB Code:
If BallX + (BallXSpd*BallXDir) + picBall.ScaleWidth > picPaddle2.Left And BallY + (BallYSpd*BallYDir) > picPaddle2.Top And BallY + (BallYSpd*BallYDir) < picPaddle2.Top + picPaddle2.Height Then
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
I added the +6 because the ball was going through the paddle. Using -6 on the other one didn't work, it works with the +6 too. Don't know why, but it does, at least until things start to speed up.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Seems like you've got something strange going on, do u mind posting all your code, if it's not too long? (use the [.vbcode.] tag (with out the dots)
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Ok, i had a look at your code. The first thing i noticed was the 4 timers. The interval of the timPaddles, was 1, this is much faster than necessisary. 50 would do nicely, to get the paddles to move at the same speed just make them move further each time.
Also, why put in an endgame timer, when you have a timer that counts down anyway. You could just put the endgame timer code, at the end of the countdown timer. And as the endgame timer's interval was 1, this will save a lot of CPU time.
About your main question, the reason why the ball isn't bouncing properly is because in 1 'move' it is jumping past the paddle and although it still turns around, it would still go through the paddle before turning around. The pic below shows this.
As your game timer is running at 100, you can make the ball move more smoothly by setting it's interval to 10, and decreasing the ball speeds (don't forget to make the ballspeed variables Long, as the integer isn't precise enough now. This means that the ball would have to be going 10 times as fast for it to jump the paddle.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
glad i could help
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons