# Simple Ball Bounce [Help Needed]

9 replies to this topic

### #1Aultraze

New Member

• Members
• 4 posts

Posted 03 December 2009 - 05:34 AM

So i have been wanting to program some physics in my games for some time now. But untill now I have only thought of doing so. But the other day i tried and tried to make this ball start at the top of the screen.

and as long as it is above the bottom of the screen then it shall fall.

once it hits the bottom its speed is reversed and a variable called decel is subtracted from the total speed so it looses a bit of speed untill finally it has none. and according to my thought process. the ball should bounce... bounce...bounce... and then stop once the speed reaches zero. because when speed is zero. usually things stop right?

well i cant seem to get this ball to find rest. it likes to bounce... bounce .... bounce.. and once it looses a certain amount of speed. it continues to bounce within a small range at the bottom of the screen.

I am using Blitz3D but i am only using 2D functions and therefore only 2 dimensions. I just want to get this to work....

here is my code:

;Graphics setup
Graphics 800,600,32,2
SetBuffer BackBuffer()

;Create player type
Type ball
Field x,y
Field speed#
Field accel#
Field decel#
Field topspeed#
End Type

Global ball.ball = New ball
ball\x=GraphicsWidth()/2
ball\y=5
ball\speed=1
ball\accel=.1
ball\decel=1
ball\topspeed=50

;Main loop
While Not KeyHit(1)
Cls

updateball()
drawball()

Flip
Wend

Function updateball()
;If ball is above bottom then fall
If ball\y =< GraphicsHeight()-50 Then ball\speed = ball\speed + ball\accel

;If the ball has touched the ground then reverse the speed and decelerate
Else ball\speed=-Abs(ball\speed-ball\decel)

;Make sure the ball doesn't exceed terminal velocity(topspeed)
If ball\speed = ball\topspeed Then ball\speed = ball\topspeed

;Make the ball move at speed given
ball\y = ball\y + ball\speed

End Function

;Draw the ball and draw the ball speed at the top left corner of the screen
Function drawball()
Oval ball\x,ball\y,20,20,1
Text 0,0, "speed:" + Str(ball\speed)
End Function
End

### #2Reedbeta

DevMaster Staff

• 5340 posts
• LocationSanta Clara, CA

Posted 03 December 2009 - 05:36 AM

Please use [code]...[/code] tags to post code on the forum.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3Aultraze

New Member

• Members
• 4 posts

Posted 03 December 2009 - 05:39 AM

thank you. Im new here and i couldn't find a way to do that =D

### #4imerso

Senior Member

• Members
• 433 posts
• LocationBrasil

Posted 03 December 2009 - 11:22 AM

When the ball hits the bottom, just put a threshold there testing if the new up velocity will be above some value. If not, clear it and stop the ball at the bottom.

### #5fireside

Senior Member

• Members
• 1616 posts

Posted 03 December 2009 - 03:38 PM

Yeah, I think you are simulating a ball that doesn't lose energy because of friction. In real life, there would be a loss of force every time the ball changes direction and bounces up again depending on the type of material. One way to do this is to constantly be adding a negative amount of distance from a free falling ball or a ball which has some upward speed every game loop, but you couldn't have a direct speed reversal when changing direction on the floor, there would have to be some loss. That would have to bring you to zero sooner or later. Horizontal speed would have to also decay over time, but it's unrelated to gravity.
Currently using Blender and Unity.

DevMaster Staff

• Moderators
• 1716 posts

Posted 03 December 2009 - 03:43 PM

This post has a homeworky smell...

Try: http://web.kellegous.com/ecrits/000858
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.

### #7Aultraze

New Member

• Members
• 4 posts

Posted 03 December 2009 - 11:55 PM

well. I have a lot of homework to do if this is all the truth. I just put this together logically. but i didn't use any of these formulas that you learn in physics. (probably because i moved in the middle of the year and my new school didnt have physics for sophmores.

Maybe if someone could message me they could explain some of this >http://web.kellegous.com/ecrits/000858 < to me..? my msn is coldshellproductions@hotmail.com. I read it all. but It stunts me in so many ways.

### #8h4tt3n

New Member

• Members
• 3 posts

Posted 04 December 2009 - 06:12 AM

I think you need to first move the ball back into the visible screen and then flip the velocity. If you don't do this, the particle may not get back "inside" in one timestep, and then it's velocity gets flipped again, moving it outside the screen ect. ect.

in simple physics simulations I do something like this (damping not included in this example):


for i as integer = 1 to num_particles

with Particle(i)

if .psn.x > screen_x - .radius - 1 then .psn.x = screen_x -.radius - 1: .vel.x = -.vel.x

if .psn.y > screen_y - .radius - 1 then .psn.y = screen_y -.radius - 1: .vel.y = -.vel.y

end with

next



Cheers,
Mike

DevMaster Staff

• Moderators
• 1716 posts

Posted 04 December 2009 - 03:24 PM

Aultraze said:

Maybe if someone could message me they could explain some of this.

The page has access to the full source for the applet on the page. That should do the trick...
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.

### #10Aultraze

New Member

• Members
• 4 posts

Posted 07 December 2009 - 11:50 PM

Allright everyone. Thank you much for your input =D. Once i get this resolved or have another issue ill get back. But for now, I have what i need essentially and ill continue from here.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users