Physics Tutorial Needed

BlitzMax Forums/BlitzMax Beginners Area/Physics Tutorial Needed

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

'dy : this is how far the ball moves each step
'ddy : this is how much dy will change each step
Rem
So we need to have a little ddy aka acceleration aka gravity for this case, let's make it -0.2 because we want things to fall downwards
y = 10, dy = -1, ddy = -0.2
y = 9, dy = -1.2, ddy = -0.2
y = 7.8, dy = -1.4, ddy = -0.2
y = 6.4, dy = -1.6, ddy = -0.2
...etc
Now as the ball moves down, the rate at which it moves down increases as we expect.

Same goes for throwing a ball upwards from the ground:
y = 0, dy = 1, ddy = -0.2
y = 1, dy = 0.8, ddy = -0.2
y = 1.8, dy = 0.6, ddy = -0.2
y = 2.4, dy = 0.4, ddy = -0.2
y = 2.8, dy = 0.2, ddy = -0.2
y = 3.0, dy = 0.0, ddy = -0.2
End Rem

Type GameObject
	Field x#,y#,dx#,ddx#,dy#,ddy#
	Field VDir ' 1 for up, 2 for down, 0 for still
	Field HDir ' 1 for left, 2 for right, 0 for still
	Field BB#
End Type
list:TList=CreateList()

Local Ball:GameObject = New GameObject

Ball.x# 	= 	400.0
Ball.y# 	= 	500.0
Ball.dx# 	= 	0.0
Ball.ddx#	= 	0.0
Ball.dy#	=	-10.0 ' Velocity
Ball.ddy#	=	0.5 ' Gravity
Ball.VDir	=	1
Ball.HDir	=	0
Ball.BB# 	=	-10.0 ' Bounce Velocity
Graphics 800,600,0
Repeat
	Cls
	DrawOval(ball.x#,ball.y#,20+ball.dx#+Rand(0,1),20+ball.dy#+Rand(0,1)) 
		DrawText "y = "+ball.y#+" dy ="+ball.dy#+" ddy ="+ball.ddy#,0,0
		
		If KeyDown(Key_Left)	 Ball.dx# = ball.dx# - 0.3; ball.ddx# = 0.001
		If KeyDown(Key_Right) Ball.dx# = ball.dx# + 0.3; ball.ddx# = -0.001
				
		If ball.x# < -40 Then ball.x# = 820
		If ball.x# > 840 Then ball.x# = -20
		If ball.dx# < -2 Then ball.dx# = -2
		If ball.dx# > 2 Then ball.dx# = 2
		If ball.y# > 550
			If Ball.BB# < 0
				Ball.BB#	=	Ball.BB#	+	0.9
				Ball.dy#	=	0+Ball.BB#
			EndIf
			If Ball.BB# > 0
				Ball.dy# = 0
				Ball.ddy#=0
			EndIf
		EndIf
		If KeyHit(Key_SPACE) And ball.y# > 520
			Ball.dy#=-10.0
			Ball.ddy#=0.5
			Ball.BB#=Ball.dy
		EndIf
		
		Ball.dx# = Ball.dx# + ball.ddx#
		Ball.x# = Ball.x# + Ball.dx#
		
		Ball.dy# = Ball.dy# + ball.ddy#
		Ball.y# = Ball.y# + Ball.dy#
	
	Flip
Until KeyHit(Key_ESCAPE)