Answered by:
Bouncing Ball

Question
-
Hello! Can anyone tell me how to make a ball bounce in the GraphicsWindow?
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
Monday, August 20, 2012 10:39 PM
Answers
-
Oh sorry your original post wasn't very clear.
I believe it's velocity you are refering to. Now I am not very good with physics but here is a simple example for you to look at. Maybe a more experienced member can provide addtional help regarding this subject.
GraphicsWindow.Height = 400 GraphicsWindow.Width = 400 GraphicsWindow.CanResize = "FALSE"
ballx = 100 bally = 100 velx = 1.1 vely = 1.5
ball = Shapes.AddEllipse(20, 20) Shapes.Move(ball, ballx, bally)
While "True" ballx = ballx + velx bally = bally + vely If bally > 380 Then vely = -1.5 ElseIf bally < 0 then vely = 1.5 EndIf If ballx > 380 Then velx = -1.1 ElseIf ballx < 0 then velx = 1.1 EndIf Shapes.Move(ball, ballx, bally) Program.Delay(10) EndWhile
- Edited by kirk_kaf Tuesday, August 21, 2012 12:58 PM
- Marked as answer by Qazwsxedc Qazwsxedc Wednesday, August 22, 2012 10:58 PM
Tuesday, August 21, 2012 12:51 AM
All replies
-
Hi Noah,
This is definately possible and has been done before using SB 0.3 by Vijaye Raji (The creator of SB).
Here is his code
orbImage = ImageList.LoadImage("http://smallbasic.com/drop/orb.png") orb = Shapes.AddImage(orbImage) width = ImageList.GetWidthOfImage(orbImage) height = ImageList.GetHeightOfImage(orbImage) x = 320 - height / 2 startY = 0 w = GraphicsWindow.Width h = GraphicsWindow.Height GraphicsWindow.BackgroundColor = "Black" GraphicsWindow.FillRectangle(0, h, w + 60, 60) GraphicsWindow.CanResize = "False" Shapes.Move(orb, x, y) gravity = 9.8 elasticity = 0.7 direction = 1 time = 0 u = 0 v = 0 GraphicsWindow.MouseDown = OnMouseDown running = 1 While "True" If running = 1 Then Shapes.Move(orb, x, y) time = time + 0.1 v = u + gravity * time * direction y = startY + (u + v) * time * direction / 2 If y > h - height Then y = h - height time = 0 direction = -1 u = v * elasticity startY = y If u < 4 Then running = 0 EndIf EndIf If direction = -1 And Math.Abs(v) < 1 Then time = 0 direction = 1 startY = y u = 0 EndIf EndIf Program.Delay(10) EndWhile GraphicsWindow.ShowMessage("End", u) Sub OnMouseDown x = GraphicsWindow.MouseX - width / 2 y = GraphicsWindow.MouseY - height / 2 startY = y u = 0 v = 0 time = 0 direction = 1 running = 1 EndSub
http://social.msdn.microsoft.com/Forums/en-IE/smallbasic/thread/90c2ed47-7a7d-4093-b57b-0023b7dd3b6c
Please also take alook into Litdev's Box2D port for SB I think it may be easier and faster.
- Edited by kirk_kaf Monday, August 20, 2012 10:48 PM
Monday, August 20, 2012 10:48 PM -
Looks good, awesome! But... not exactly what I was looking for. I am wondering how to make a ball bounce and when it hits a wall, bounce off. I have seen it in some brick breaker games, but I don't know the exact code. Sorry!
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
Tuesday, August 21, 2012 12:19 AM -
Oh sorry your original post wasn't very clear.
I believe it's velocity you are refering to. Now I am not very good with physics but here is a simple example for you to look at. Maybe a more experienced member can provide addtional help regarding this subject.
GraphicsWindow.Height = 400 GraphicsWindow.Width = 400 GraphicsWindow.CanResize = "FALSE"
ballx = 100 bally = 100 velx = 1.1 vely = 1.5
ball = Shapes.AddEllipse(20, 20) Shapes.Move(ball, ballx, bally)
While "True" ballx = ballx + velx bally = bally + vely If bally > 380 Then vely = -1.5 ElseIf bally < 0 then vely = 1.5 EndIf If ballx > 380 Then velx = -1.1 ElseIf ballx < 0 then velx = 1.1 EndIf Shapes.Move(ball, ballx, bally) Program.Delay(10) EndWhile
- Edited by kirk_kaf Tuesday, August 21, 2012 12:58 PM
- Marked as answer by Qazwsxedc Qazwsxedc Wednesday, August 22, 2012 10:58 PM
Tuesday, August 21, 2012 12:51 AM -
Here is a simple way...
Ball = Shapes.AddEllipse(10,10)
x = 30
y = 50
dx = 1
dy = 1
While 1 = 1
Program.Delay(5)
x = x + dx
y = y + dy
Shapes.Move(Ball,x,y)
If y > GraphicsWindow.Height - 10 Or y < 0 Then
dy = - dy
EndIf
If x > GraphicsWindow.Width - 10 Or x < 0 Then
dx = - dx
EndIf
EndWhileOne thing that is impossible is impossible no matter if it is proven so first.
Tuesday, August 21, 2012 5:00 PMAnswerer -
I never thought 'OR' and 'AND' was native SB keywords.Tuesday, August 21, 2012 5:26 PM
-
They are. and they are very usefull
One thing that is impossible is impossible no matter if it is proven so first.
Tuesday, August 21, 2012 5:41 PMAnswerer -
Yes they are usefull.
The reason I thought they never existed in SB was due to the intellisence not showing them unless IF has been typed. Learn something new everyday :)
Tuesday, August 21, 2012 8:07 PM -
Thanks!
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
Wednesday, August 22, 2012 10:57 PM