Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Game Programming > how to make the pong ball go off on angles


Reply
 
Thread Tools Display Modes
  #1  
Old 07-18-2003, 05:06 AM
Hypnotic_Monkey's Avatar
Hypnotic_Monkey Hypnotic_Monkey is offline
Centurion
 
Join Date: Jun 2003
Location: Australia
Posts: 117
Default how to make the pong ball go off on angles


Well the subject says it all i want to make it so the pong ball bounces off at different angles i looked at one of the pong games posted on here but it was ALOT of Code and most of it i didnt need and then i lost track of it all so could someone help me out this is the cod i have so far...

Code:
Dim xdirection As Long, ydirection As Long, gameover As Boolean Private Type rect: left As Single top As Single right As Single bottom As Single End Type 'checking 2 squares for overlapping Function check_collide(pic1 As PictureBox, pic2 As PictureBox) As Boolean If pic1.Visible = False Then Exit Function End If Dim rect1 As rect, rect2 As rect 'set the area of the first picture With rect1 .left = pic1.left .right = pic1.left + pic1.Width .top = pic1.top .bottom = pic1.top + pic1.Height End With 'set the area of the second picture With rect2 .left = pic2.left .right = pic2.left + pic2.Width .top = pic2.top .bottom = pic2.top + pic2.Height End With 'check if they over lap If ((rect2.right > rect1.left And rect2.left < rect1.right) And (rect2.bottom > rect1.top And rect2.top < rect1.bottom)) Then check_collide = True Else check_collide = False End If End Function Private Sub cmdStart_Click() Dim v As Boolean, i As Integer cmdStart.Visible = False gameover = False Do Until gameover = True picBall.top = picBall.top + ydirection picBall.left = picBall.left + xdirection picPaddle.left = picBall.left - (picPaddle.Width / 2) picPaddle2.left = picBall.left - (picPaddle2.Width / 2) v = check_collide(picPaddle, picBall) If v = True Then ydirection = -(ydirection) End If v = check_collide(picPaddle2, picBall) If v = True Then ydirection = -(ydirection) End If For i = 0 To 3 v = check_collide(picWall(i), picBall) If v = True Then If i <= 1 Then ydirection = -(ydirection) Else xdirection = -(xdirection) End If End If Next i DoEvents Loop End Sub Private Sub Form_Load() ydirection = 1 xdirection = 1 End Sub Private Sub Form_Unload(Cancel As Integer) End End Sub

i have set it so the paddles just follow the ball it makes it easy for test so yea if someone could help me out i would be very greatful thnx
Reply With Quote
  #2  
Old 07-18-2003, 05:15 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

Quote:
Originally Posted by Hypnotic_Monkey
Well the subject says it all i want to make it so the pong ball bounces off at different angles i looked at one of the pong games posted on here but it was ALOT of Code and most of it i didnt need and then i lost track of it all so could someone help me out this is the cod i have so far...

i have set it so the paddles just follow the ball it makes it easy for test so yea if someone could help me out i would be very greatful thnx




If you just want a number of fixed angles, you could use a table, or calculate a value based on where you hit the paddle.

Another way is to use an angle, and use sin and cos to set your X and
Y direction values. Attached is a simple pong that uses angles, to bounce
the ball in a range of +- 70 degrees off the paddle.

You can probably extract the paddle bounce code from this for your game.
Attached Files
File Type: zip jpong1.zip (18.6 KB, 37 views)
Reply With Quote
  #3  
Old 07-18-2003, 05:19 AM
Hypnotic_Monkey's Avatar
Hypnotic_Monkey Hypnotic_Monkey is offline
Centurion
 
Join Date: Jun 2003
Location: Australia
Posts: 117
Default

yea thats the one i tryed getting the angle's from but it was like huge and full of comments thats y i was wondering if someone could help me what do u mean exactly by use a angle then use sin and cos??
Reply With Quote
  #4  
Old 07-18-2003, 08:07 AM
clockworkorange clockworkorange is offline
Junior Contributor
 
Join Date: Mar 2003
Posts: 238
Default

lol don't worry about how hard sin and cos is. Sooner or later you will pick it up.
just a note. When using sin and cos its in radians, therefore 2*Pi = 360 degrees.

For your game just times the x and y direction by -1.
Reply With Quote
  #5  
Old 07-18-2003, 08:18 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

Quote:
Originally Posted by Hypnotic_Monkey
yea thats the one i tryed getting the angle's from but it was like huge and full of comments thats y i was wondering if someone could help me what do u mean exactly by use a angle then use sin and cos??



Yeah, I added all the comments to hopefully explain what it was doing in enough detail that someone could understand it, but the comments do outweight the code by quite a bit.

Essentially, Sin and Cos is the ratio of X/Y (sine) or Y/X (cosine), for a
particular angle. Thus giving an Angle, you can find the ratio of how much X changes in relation to Y.

The ratio (the values returned) will always be between the values of 0.0 and 1.0. Therefore, if you want to move something 5 pixels at 20 degrees, you move it 5*sin(20) in X and 5*cos(20) in Y.

So basic calculation for a relative move is:
RelX = distance * sin(Angle)
RelY = distance * cos(Angle)

So when you bounce the ball and determine the angle you want to travel, at that point, convert the angle into your X and Y increment values and continue adding them to your ball's position until you bounce
off of something else.

If you skip all the comments about timing, and how the speed is regulated in my game, and just look at the paddle and wall bounce subroutines, maybe it won't be so overwhelming.

I've run your code and it seems like a good start.

I have to go to an appointment, so I'll not be able to respond for a few hours.

The other option you can take to add some variety which may be preferable is to just start with several fixed angles.
If you hit near the center of a paddle bounce staight off,
(say 2 for x and 0 for y)
if you hit the upper of lower part of the paddle, make it (1 for x and 2 for y)
You could divide it into two more zones nearer the center and assign (2 X and 1 Y) as your magnitudes.
You would then still only need to invert the sign when you bounce off of things, and not worry about Angles, Sines or Cosines.

That would give you 7 angles off the paddle, (1 center, and three above and below).

Code:
To summarize: Left Paddle, Right Paddle X's would be negative (X, Y) Paddle Top 1, -2 2, -2 2, -1 Paddle Center 2 , 0 2, 1 2, 2 Paddle Bottom 1, -2
Give that a try if the Angle(Cos/Sin) thing seems complicated.

Last edited by passel; 07-18-2003 at 08:34 AM.
Reply With Quote
  #6  
Old 07-18-2003, 08:35 AM
Tweakish's Avatar
Tweakish Tweakish is offline
Centurion
 
Join Date: Jul 2003
Location: Canada
Posts: 94
Default

I would probably just use
Code:
if <collision> then ballxspeed = ballxspeed + paddlexspeed
It may need some fiddling, but the basic idea of it works. When the paddle is going left the speed is negative, and subtracts from the ball's speed. When going right, the speed is positive and add's to the ball's speed.
__________________
"nanana NEO! nanana SPORIN!" ~Some old guy
"It's not polite to dropkick little girls." ~Jackie Chan
Reply With Quote
  #7  
Old 07-18-2003, 09:02 AM
clockworkorange clockworkorange is offline
Junior Contributor
 
Join Date: Mar 2003
Posts: 238
Default

[QUOTEPOST='passel']
Quote:
Originally Posted by Hypnotic_Monkey

So basic calculation for a relative move is:
RelX = distance * sin(Angle)
RelY = distance * cos(Angle)




i think its good practice to leave cos with x and sin with y


and just change the angle eg add pi to change angle...i thinks its pi
Reply With Quote
  #8  
Old 07-19-2003, 04:58 AM
Hypnotic_Monkey's Avatar
Hypnotic_Monkey Hypnotic_Monkey is offline
Centurion
 
Join Date: Jun 2003
Location: Australia
Posts: 117
Default

Alrighty I "borrowed" some of the angle code from ur pong version passel and i tryed it out this is what i have got now

Code:
Dim xdirection As Long, ydirection As Long, gameover As Boolean Dim speed As Long, py As Single, by As Single, bx As Single Const DEG2RAD As Double = 3.1415926 / 180# Private Type rect: left As Single top As Single right As Single bottom As Single End Type 'checking 2 squares for overlapping Function check_collide(pic1 As PictureBox, pic2 As PictureBox) As Boolean If pic1.Visible = False Then Exit Function End If Dim rect1 As rect, rect2 As rect 'set the area of the first picture With rect1 .left = pic1.left .right = pic1.left + pic1.Width .top = pic1.top .bottom = pic1.top + pic1.Height End With 'set the area of the second picture With rect2 .left = pic2.left .right = pic2.left + pic2.Width .top = pic2.top .bottom = pic2.top + pic2.Height End With 'check if they over lap If ((rect2.right > rect1.left And rect2.left < rect1.right) And (rect2.bottom > rect1.top And rect2.top < rect1.bottom)) Then check_collide = True Else check_collide = False End If End Function Private Sub cmdStart_Click() Dim v As Boolean, i As Integer cmdStart.Visible = False gameover = False Do Until gameover = True picBall.top = picBall.top + by picBall.left = picBall.left + bx v = check_collide(picPaddle, picBall) If v = True Then calc_angle xdirection = (Sin(ball_angle * DEG2RAD)) * speed ydirection = (Cos(ball_angle * DEG2RAD)) * speed by = by - ydirection bx = bx - xdirection End If For i = 0 To 3 v = check_collide(picWall(i), picBall) If v = True Then If i <= 1 Then ydirection = -(ydirection) Else xdirection = -(xdirection) End If End If Next i For i = picBlock.LBound To picBlock.UBound v = check_collide(picBlock(i), picBall) If v = True Then picBlock(i).Visible = False xdirection = (Sin(ball_angle * DEG2RAD)) * speed ydirection = (Cos(ball_angle * DEG2RAD)) * speed by = by + ydirection bx = bx + xdirection Exit For End If Next i DoEvents Loop End Sub Private Sub Form_Load() ball_angle = 0 speed = 10 xdirection = (Sin(ball_angle * DEG2RAD)) * speed ydirection = (Cos(ball_angle * DEG2RAD)) * speed by = by + ydirection bx = bx + xdirection End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) picPaddle.left = X - (picPaddle.Width / 2) py = X - (picPaddle.Width / 2) End Sub Private Sub Form_Unload(Cancel As Integer) End End Sub Private Function calc_angle() Dim bounce_angle_Change As Double bounce_angle_Change = py - by ball_angle = 180 - (0 + ball_angle) + bounce_angle_Change If ball_angle < 110 Then ball_angle = 110 ElseIf ball_angle > 250 Then ball_angle = 250 End If End Function

see my pong game isnt a 2 player one no more its a 1 player and u go up and hit blocks and then it bounces back and you have to get rid of them all. so the paddle is at the bottom it goes side ways it works so when it hits the paddle it goes back up but it is going back up straight at no angle cause when it its the paddle its going straight i was hoping you could go thro and help me fix it.. thnx
Attached Files
File Type: zip NewPong.zip (3.4 KB, 8 views)
Reply With Quote
  #9  
Old 07-19-2003, 05:10 AM
martrinex's Avatar
martrinex martrinex is offline
Junior Contributor
 
Join Date: Jun 2003
Posts: 287
Red face

If you want to move a object in any angle without worrying to much about radians and sin and cos then use this function
Code:
Function GetXY(X, Y, speed, angle) 'convert the angle to radians rad = 3.14159265358979 / 180 angle1 = Int(angle) * rad 'move x and y X = X + ((Sin(angle1) * speed)) Y = Y - ((Cos(angle1) * speed)) End Function Private Sub Timer1_Timer() X = Command1.Left Y = Command1.Top GetXY X, Y, 10, 90 Command1.Move X, Y End Sub
the function in the example simple moves the command button at 90 degrees each time
Reply With Quote
  #10  
Old 07-19-2003, 05:20 AM
Pookie's Avatar
Pookie Pookie is offline
Gaming God

Retired Leader
* Expert *
 
Join Date: Feb 2002
Location: Brisbane, Australia
Posts: 2,363
Default

Function GetXY(X, Y, speed, angle) = Function GetXY(X as Variant, Y as Variant, speed as Variant, angle as Variant) as Variant !!!

I gather all the variables should be declared as doubles or singles, otherwise if the speed is too low, it ball will not move, as x = x + 0.9 means x = x + 0 if it's not...
__________________
. <--- Photo of me viewed from the Hubble telescope
Reply With Quote
  #11  
Old 07-19-2003, 11:18 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

Quote:
Originally Posted by martrinex
If you want to move a object in any angle without worrying to much about radians and sin and cos then use this function
Code:
Function GetXY(X, Y, speed, angle) 'convert the angle to radians rad = 3.14159265358979 / 180 angle1 = Int(angle) * rad 'move x and y X = X + ((Sin(angle1) * speed)) Y = Y - ((Cos(angle1) * speed)) End Function Private Sub Timer1_Timer() X = Command1.Left Y = Command1.Top GetXY X, Y, 10, 90 Command1.Move X, Y End Sub
the function in the example simple moves the command button at 90 degrees each time



Yes, that is essentially what my program does, except it doesn't execute the Sin and Cos functions for every move of the ball. You would
only need to do that if the speed or direction is changing constantly.
' So the only difference, is that I call a function like yours with the
speed and angle and it returns the delta X and Y values that are
added to the balls position each time it moves. I only call the function
to recalculate delta X and Y values, when the ball bounces. Sin and Cos
functions are about 100 times slower than doing an Add or Subtract.

I've made minimal changes to your program, the main thing is that in my
program bx and by represent the balls position, and px and py represent the paddles position.
So I commented out where you are assigning x and y direction to bx and by, I set bx and by offset from the ball picture's left, top where you move
the ball. I set px to the center of the paddle (Mouse X) where you move
the paddle.
I scaled the difference between where the ball hits and the center of
the paddle because you are using twips, because the difference was too big.

That's about all I changed. I'm sending back the files, with a "1" appended to the name so I don't clobber your files.
Attached Files
File Type: zip newpong1.zip (3.4 KB, 16 views)

Last edited by passel; 07-19-2003 at 01:26 PM.
Reply With Quote
  #12  
Old 07-19-2003, 09:59 PM
Hypnotic_Monkey's Avatar
Hypnotic_Monkey Hypnotic_Monkey is offline
Centurion
 
Join Date: Jun 2003
Location: Australia
Posts: 117
Default

Thnx for that passel im still having troubles where when the ball hits the paddle it bounces of on a weird angle where u wouldnt expect it too and sometimes it bounce off back in the same direction that it came from but thanx anyway im gonna continue to work on it and hopfully will fix it up
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My first game... (pong) need help dwoods54 Game Programming 17 05-10-2004 07:50 AM
Need Help With Animation IN game danavis Game Programming 1 04-08-2003 04:00 PM
havin trouble with my ping pong ball Twan Game Programming 5 02-22-2003 05:05 PM
A 3/4 finished game of pong andrewo Game Programming 3 07-22-2001 03:22 AM
The ball in Pong orufet Game Programming 6 07-21-2001 09:50 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->