math & physics
aultraze at December 3rd, 2009 00:34 — #1
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:
;Create player type
Global ball.ball = New ball
While Not KeyHit(1)
;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
;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
;Draw the ball and draw the ball speed at the top left corner of the screen
Text 0,0, "speed:" + Str(ball\speed)
reedbeta at December 3rd, 2009 00:36 — #2
Please use ...[/code] tags to post code on the forum. [code]...[/code****] tags to post code on the forum.
aultraze at December 3rd, 2009 00:39 — #3
thank you. Im new here and i couldn't find a way to do that =D
vrnunes at December 3rd, 2009 06:22 — #4
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.
fireside at December 3rd, 2009 10:38 — #5
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.
alphadog at December 3rd, 2009 10:43 — #6
This post has a homeworky smell...
aultraze at December 3rd, 2009 18:55 — #7
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 email@example.com. I read it all. but It stunts me in so many ways.
h4tt3n at December 4th, 2009 01:12 — #8
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
if .psn.x < .radius then .psn.x = .radius: .vel.x = -.vel.x
if .psn.x > screen_x - .radius - 1 then .psn.x = screen_x -.radius - 1: .vel.x = -.vel.x
if .psn.y < .radius then .psn.y = .radius: .vel.y = -.vel.y
if .psn.y > screen_y - .radius - 1 then .psn.y = screen_y -.radius - 1: .vel.y = -.vel.y
alphadog at December 4th, 2009 10:24 — #9
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...
aultraze at December 7th, 2009 18:50 — #10
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.