2D Collision, Physics, Rigid body dynamics

BlitzMax Forums/BlitzMax Programming/2D Collision, Physics, Rigid body dynamics

Pantheon, that little explanation helped. I think I have successfuly port World.h/cpp now. Please take a look:
Rem
* Copyright (c) 2006 Erin Catto <a href="http://www.gphysics.com" target="_blank">http://www.gphysics.com</a>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies.
* Erin Catto makes no representations about the suitability 
* of this software for any purpose.  
* It is provided "as is" without express or implied warranty.
End Rem

Function  World:TWorld (gravity:TVec2, iterations:Int)
	Local w:TWorld = New TWorld
	w.gravity = gravity
	w.iterations = iterations
	Return w
End Function
Type TWorld
	Field Bodies:TList
	Field Joints:TList
	Field Arbiters:TList
	Field Gravity:TVec2
	Field iterations:Int
	Method New()
		Bodies = CreateList()
		Joints = CreateList()
		Arbiters = CreateList()
		Gravity = Vec2(0.0,0.0)
	End Method
	Method Add ( obj:Object )
		If TBody(obj) Bodies.AddLast(obj)
		If TJoint(obj) Joints.AddLast(obj)
	End Method
	Method Clear()
		Bodies.Clear
		Joints.Clear
		Arbiters.Clear
	End Method
	
	Method BroadPhase()
		' O(n^2) broad-phase
		Local BodiesArray:Object [] = Bodies.ToArray()
		For Local i:Int = 0 To BodiesArray.Length - 1
			Local bi:Tbody = TBody(BodiesArray[i])
			For Local j:Int = i + 1 To BodiesArray.Length - 1
				Local bj:TBody = TBody(BodiesArray[j])
				If (bi.invMass = 0.0 And bj.invMass = 0.0) Continue
				Local newArb:TArbiter = Arbiter(bi, bj)
				If (newArb.numContacts > 0)
					'Not sure what is being accomplished here:
					Local arb:TLink = Arbiters.FindLink(newArb)
					'ArbIter arb = arbiters.find(newArb)
					If (arb = Arbiters.LastLink())
						Arbiters.AddLast(newArb)
					Else
						TArbiter(arb.Value()).Update(newArb.contacts, newArb.numContacts)
					End If
				Else
					Arbiters.Remove(newArb)
				End If
			Next
		Next
	End Method
	
	Method _Step (dt:Float)
		Local inv_dt:Float
		If dt > 0.0 Then inv_dt = 1.0 / dt Else inv_dt = 0.0
		BroadPhase()
		Local BodiesArray:Object [] = Bodies.ToArray()
		For Local i:Int = 0 To BodiesArray.Length -1
			Local b:TBody = TBody(BodiesArray[i])
			
			If (b.invMass = 0.0)	Continue
	
			b.velocity.AddV(MultFloatVec2(dt,(AddVec2Vec2(gravity, MultFloatVec2(b.invMass, b.force)))))
			b.angularVelocity :+ dt * b.invI * b.torque
		Next

		For Local arb:TArbiter = EachIn Arbiters
			arb.PreStep(inv_dt)
		Next

		Local JointsArray:Object [] = Bodies.ToArray()
		For Local i:Int = 0 To JointsArray.Length - 1
			TJoint(JointsArray[i]).PreStep(inv_dt)
		Next

		For Local i:Int = 0 To iterations
			For Local arb:TArbiter = EachIn Arbiters
				arb.ApplyImpulse()
			Next
			For Local j:Int = 0 To JointsArray.Length - 1
				TJoint(JointsArray[j]).ApplyImpulse()
			Next
		Next

		For Local i:Int = 0 To BodiesArray.Length - 1
			Local b:TBody = TBody(BodiesArray[i])
			b.position.AddV(MultFLoatVec2(dt,b.velocity))
			b.rotation :+ dt * b.angularVelocity
	
			b.force.Set(0.0, 0.0)
			b.torque = 0.0
		Next
	End Method
End Type


I'm glad you like the logo. More on that soon. By the way when should we start a new thread? I've got graphics and stuff for that.