0
101 Dec 03, 2009 at 05:34

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


#### 9 Replies

0
165 Dec 03, 2009 at 05:36

Please use …[/code[b][/b]] tags to post code on the forum. :) [code]…[/code**] tags to post code on the forum. :)

0
101 Dec 03, 2009 at 05:39

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

0
102 Dec 03, 2009 at 11:22

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.

0
140 Dec 03, 2009 at 15:38

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.

0
101 Dec 03, 2009 at 15:43

This post has a homeworky smell…

0
101 Dec 03, 2009 at 23:55

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.

0
101 Dec 04, 2009 at 06:12

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

0
101 Dec 04, 2009 at 15:24

@Aultraze

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…

0
101 Dec 07, 2009 at 23:50

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.