Physics Tutorial Needed

BlitzMax Forums/BlitzMax Beginners Area/Physics Tutorial Needed

Matt McFarland(Posted 2005) [#1]
I really want to make games with BlitzMax.. But seeing as how I never took physics in highschool because I am handicapped in mathematics I dont know where I should turn.

Obviously physics is very important to programming, and every physics book I grab to learn physics is beyond me. They use symbols I've never seen (one looks like an omega symbol?) and also use diagrams that make no sense to me.

Why doesnt someone make a physics tutorial that shows how physics is coded? I dont need pictures or diagrams, just show me the code.

In fact, I'm willing to pay anyone who knows physics well enough to make a game, and not well enough to be mr professer whatever. Someone who can type it out for newbies.

Someone that can say.. "hey this is 4 different forces are calculated in programming languages" instead of all that math mumbo jumbo that makes my brain overload.


Robert Cummings(Posted 2005) [#2]
Lets start with movement along x. This is one of the classic physics things you get in games for a nice feel.

if move left using key or joystick then vx=vx-0.3
if move right using key or joystick then vx=vx+0.3

x = x + vx
vx = vx * 0.8

Basically, what happens is your velocity (vx) is added to x all the time, and limited by and slowed down again by multiplying it by 0.8 each frame. It's a classic trick, and once you get the hang of it, cool.

Remember to make all of em floats and have a play around with this sort of stuff to begin with. Then you could go onto stuff like if collision or y>floor then... vx = - vx to bounce...


TMyke(Posted 2005) [#3]
look at there:

http://www.physicsclassroom.com/Class/newtlaws/U2L1a.html


Dubious Drewski(Posted 2005) [#4]
Oh I love this stuff.

Here, I took the time to make this large example program
for you. It's a bit complex, but if you go through it line
by line, you should learn tons.

When you run it, it looks like psychadelic popcorn.


Strict

Global xScreen = 800
Global yScreen = 600
Global particleNum = 35
Graphics xScreen, yScreen

Type particleType		'This is the blueprint for one particle
	Field x:Float     'x position
	Field y:Float			'y position
	Field x1:Float    'x velocity
	Field y1:Float		'y velocity
Rem
It's important to note that I made the variables
for both velocity and position Float variables.
If you make the position variables integers, you'll
get 'chunky' movement, if any at all.
EndRem
	Method update()
		x:+x1						'move the particle by how fast it's going.
		y:+y1
		y1:+.95					'put a constant tug on the Y speed.  This is gravity.
		x1:*.99					'this is what One Eyed Jack was walking about.  This simulates air resistance.
		y1:*.95
		Rem 
		The following four lines of code make sure the particles
		stay on the screen.  This is a very simple way of doing it.
		End Rem
		If x> xScreen Then x1 = -x1
		If y> yScreen Then y1 = -y1
		If x< 0 Then x1 = -x1
		If y< 0 Then y1 = -y1
		If Abs(y1)< 1 And y > 590 Then		'if they slow down too much, shoot them off somehwere fast
			x1= Rnd(-10,10)
			y1= -Rnd(40)
		EndIf
	End Method
	Method draw()
		SetColor x1,y1,200		'this will make crazy colours when they move
		DrawRect x,y,5,5
	End Method
End Type

Local part:particleType[particleNum+1]	'arrange 300 memory slots for the particles 
Local a

For a = 0 To particleNum			'This puts the blueprint inside each slot we just made...
	part[a] = New particleType	'it's a necessary step if you're working with Types.
	part[a].x = Rand(xScreen)		'put them in random starting positions
	part[a].y = Rand(600)		
Next 

Repeat							'this is the actual main loop
	For a = 0 To particleNum
		part[a].update
		part[a].draw
	Next
	Flip;Cls;FlushMem
Until KeyHit(key_escape)



Dubious Drewski(Posted 2005) [#5]
And you might find this cool.

I made it during class today. It's not commented but
it's simple.

Strict
Graphics 800,600
SeedRnd MilliSecs()
Local a
Local stringyNumber = 100
Type stringyType
	Field x:Float
	Field y:Float
	Field x1:Float
	Field y1:Float
End Type
Local stringy:stringyType[stringyNumber+1]
For a = 0 To stringyNumber
	stringy[a] = New stringyType
	stringy[a].x=400'Rand(800)
	stringy[a].y=300'Rand(600)
Next
HideMouse()
SetLineWidth 3
SetClsColor 0,70,10
SetColor 0,190,30
Repeat
	stringy[0].x=MouseX()
	stringy[0].y=MouseY()
	DrawOval stringy[0].x,stringy[0].y,4,4
	For a = 0 To stringyNumber
		If a Then 
'			Plot stringy[a].x,stringy[a].y
'			DrawOval stringy[a].x,stringy[a].y,4,4
			DrawLine Stringy[a].x,Stringy[a].y,stringy[a].x-Stringy[a].x1*3,stringy[a].y-Stringy[a].y1*3
			Local xx= stringy[a-1].x-stringy[a].x
			Local yy= stringy[a-1].y-stringy[a].y
			
			Local dist = Sqr((xx*xx)+(yy*yy))
			If dist>3 Then
				stringy[a].x:+stringy[a].x1
				stringy[a].y:+stringy[a].y1
				stringy[a].x1:+(stringy[a-1].x-stringy[a].x)*.04
				stringy[a].y1:+(stringy[a-1].y-stringy[a].y)*.04
				stringy[a].x1:*.78
				stringy[a].y1:*.78
				
			EndIf
		EndIf	
	Next
	Flip
	Cls
	If KeyHit(key_escape) End
Forever



Tibit(Posted 2005) [#6]
Also check out the asteroids tutorial if you haven't. It's complete with code and everything.

Also I'm writting a tutorial on vector physics for beginners, if you want I could let you beta test it later this week, It could help me sort out which things that need to be explained better, because it assumes you know nothing about physics or vectors. Send me a mail if you are interested.


Matt McFarland(Posted 2005) [#7]
Wave! I am very interested!! I liked your beginners guide alot. ALthough I'm pretty good at grasping commands, I am TERRIBLE at grasping MATH! :-(

I have completed the asteroids tutorial and even added to it. I added particle effects to create explosions and engine trails. Although all I did was copy and paste code which I did not understand. IE: I made particles an object like the shot but made them basically Tspaceobjects etc. I dont know what the heck I'm doing!! haha..

here please go to my website, I am a musician and a game maker! http://mcfarlandgames.5u.com - You can see that I have made a brickout clone so far. But the problem is that I am NOT using physics for the ball movement and lots of people dont like that.

I dont know how to add physics to the ball movement yet. I have been in communication about it at the indie-developer forums.. you can see my thread (my name is leper there) at http://forums.indiegamer.com/showthread.php?t=4827

Please read that thread (especially the bottom) where we discuss code - This will give you an idea on how I really need physics and how a person who does NOT understand physics reads thigs! Also I think I would be a great asset to your documentation testing as I am a physics idiot!


tonyg(Posted 2005) [#8]
Wave's tutorials are always excellent.
In case it helps in the meantime...
' I have problems on my laptop with drawoval and DX drivers.
SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,0
' gravity = gravity, yvel= Ball's speed in y direction, ypos = balls y pos, yground 
Global gravity:Float=0.5, yvel:Float=-10.0,ypos:Float=400.0, yground:Int=400
While Not KeyHit(key_escape)
   Cls
' draw the ground
   DrawLine 0,yground,600,yground
' output yvel so we can see it changing
   DrawText "yvel : " + yvel,0,0
   If yvel < 0 DrawText "Moving up",0,10
   If yvel > 0 DrawText "Moving down",0,10
'draw the 'ball' in current ypos
   DrawOval 200,ypos,20,20
' This will either reduce or increase y velocity depending on direction.
   yvel=yvel+gravity
' change ypos by the speed
   ypos=ypos+yvel
' if ypos hits the ground
   If ypos >= yground 
' reset it's position to the gound
       ypos=yground
' reverse the direction of yvel
       yvel=-10
' Comment the previous line and uncomment the next line to have the ball come to rest.
'      yvel = -yvel
  EndIf
' get rid of unwanted memory
   FlushMem
   Flip
Wend



Matt McFarland(Posted 2005) [#9]
I found something very useful. Believe it or not, the following post by a programmer that goes by the name of "zppz" explained to me the things I needed to hear. Particularly information about acceleration and gravity, which is what I really needed to figure out.

Please look at this, and because this helped me out and I am math-retarted, I wanted Wave to put this in his upcoming documentation:
(the helpful info below is from: http://forums.indiegamer.com/showthread.php?p=63995#6)

Let's narrow it down to three variables only, by assuming the ball is dropping straight downwards.
y : this is the current height of the ball
dy : this is how far the ball will move this time, ie. how much y will change
ddy : this is how much dy will change this time

You could also call these position, velocity and acceleration.

As an example, start with the ball at height 10, and dy = -1 so it is already on the way down.
If we did not have ddy this would happen:
y = 10, dy = -1
y = 9, dy = -1
y = 8, dy = -1
y = 7, dy = -1
...etc
The ball just moves steadily downwards at a constant speed, very unrealistic.

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
Here you can see that in the last loop the velocity has become zero. This is also what you expect a ball to do at some point after you throw it upwards... what goes up must come down


As for the hitting the ground part, you simply change the direction the ball is moving in, that is, multiply dy by -1

(the helpful info was originally from: http://forums.indiegamer.com/showthread.php?p=63995#6)


Wayne(Posted 2005) [#10]
Since BMAX will use ODE for physics why not check the ode documentation at :

www.ode.org

Start getting exposed to the terminology, and check out VIP3Rs ODE demos if you have B3D.


Matt McFarland(Posted 2005) [#11]
All I have and use is BMAX. ODE looks beyond me at this point, although I have yet to see its actual code.


Matt McFarland(Posted 2005) [#12]
'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)



Matt McFarland(Posted 2005) [#13]
I made another cool physics demo.. check it..


Type TSolidBlock
	Global List:TList
	Global Image:TImage
	
	Field x#,y#,Xvel#,Yvel#
	Field Size#
	Field R,G,B		
	Function CreateBlock(x#,y#,size#)

		Local Block:TSolidBlock = New TSolidBlock
		If List = Null List = CreateList()
		List.AddLast Block
 		Block.R=Rnd(10,200)
 		Block.G=Rnd(10,200)
 		Block.B=Rnd(10,200)
		Block.x# 			= x#
		Block.y# 			= y#
		Block.size# 		= size#

	EndFunction

	Function MoveBlocks()
		If Not List Return
		Local Block:TSolidBlock
		For Block = EachIn List
		If MouseDown(1) Friction#=0.0
		If MouseDown(2) Friction#=Rnd(-4.5,4.5)

		Dir#=Rnd(0,360)
		Block.Xvel#:+ Cos(Dir#)*friction#
		Block.Yvel#:+ Sin(Dir#)*friction#
		Block.X# = Block.X# + Block.Xvel# - Friction#
		Block.Y# = Block.Y# + Block.Yvel# - Friction#
		Next
	End Function



	Function DrawBlocks()
		If Not List Return
		Local Block:TSolidBlock
		For Block = EachIn List
			SetColor(block.R,block.G,block.B)
			DrawRect(block.x#-block.size#/2,block.y#-block.size#/2,block.size#,block.size#)
		Next

	End Function

End Type

Graphics 800,600,0

Repeat
	Cls

	SetColor(255,255,255)
	DrawText "Move the mouse",0,0
	DrawText "LMB: Draw  |  RMB: Explode!",0,20
	If MouseDown(1) TSolidBlock.CreateBlock(MouseX(),MouseY(),Rnd(2,10))
	TSolidBlock.MoveBlocks()
	TSolidBlock.DrawBlocks()
	Flip
Until KeyHit(Key_Escape)