--
-- b2World
--
function b2CreateWorld ( left, top, right, bottom )

	local aabb = b2AABB ( )
	aabb.lowerBound:Set ( left, top )
	aabb.upperBound:Set ( right, bottom )

	local gravity = b2Vec2 ( 0, 0 )

	return b2World ( aabb, gravity, true )

end

function b2QueryWorld ( world, left, top, right, bottom, limit )

	local aabb = b2AABB ( )
	aabb.lowerBound:Set ( left, top )
	aabb.upperBound:Set ( right, bottom )

	local results = {}
	world:Query ( aabb, results, limit )

	return results

end




--
-- b2Body
--
function b2CreateBody ( world, x, y, angle, linearDamping, angularDamping, fixedRotation, isBullet, allowSleep, isSleeping )

	local body_def = b2BodyDef ( )
	body_def.position = b2Vec2 ( x, y )
	body_def.angle = angle
	body_def.linearDamping = linearDamping
	body_def.angularDamping = angularDamping
	body_def.fixedRotation = fixedRotation
	body_def.isBullet = isBullet
	body_def.allowSleep = allowSleep
	body_def.isSleeping = isSleeping

	local body = world:CreateBody ( body_def )

	return body

end

function b2DestroyBody ( body )

	local world = body:GetWorld ( )
	world:DestroyBody ( body )

end

function b2SetXForm ( body, x, y, angle )

	body:SetXForm ( b2Vec2 ( x, y ), angle )

end




--
-- b2Shape
--
function b2CreateShapeCircle ( body, radius, density, friction, restitution, isSensor )

	local shape_def = b2CircleDef ( )
	shape_def.density = density
	shape_def.friction = friction
	shape_def.restitution = restitution
	shape_def.isSensor = isSensor

	shape_def.radius = radius

	local shape = body:CreateShape ( shape_def )
	body:SetMassFromShapes ( )

	return shape

end

function b2CreateShapePolygon ( body, vertices, density, friction, restitution, isSensor )

	local shape_def = b2PolygonDef ( )
	shape_def.density = density
	shape_def.friction = friction
	shape_def.restitution = restitution
	shape_def.isSensor = isSensor

	shape_def.vertexCount = #vertices
	for i, v in ipairs ( vertices ) do
		-- table.insert ( shape_def.vertices, b2Vec2 ( v.x, v.y ) )
		--
		-- Warning! Using 0 as index of a Lua table
		--
		shape_def.vertices[i - 1]:Set ( v.x, v.y )
	end

	local shape = body:CreateShape ( shape_def )
	body:SetMassFromShapes ( )

	return shape

end

function b2DestroyShape ( shape )

	local body = shape:GetBody ( )
	body:DestroyShape ( shape )

end



--
-- b2Joints
--
function b2CreateJointDistance ( world, body1, body2, a1x, a1y, a2x, a2y )

	joint_def = b2DistanceJointDef ( )
	joint_def:Initialize ( body1, body2, b2Vec2 ( a1x, a1y ), b2Vec2 ( a2x, a2y ) )

	return world:CreateJoint ( joint_def )

end

function b2CreateJointRevolute ( world, body1, body2, ax, ay )

	joint_def = b2RevoluteJointDef ( )
	joint_def:Initialize ( body1, body2, b2Vec2 ( ax, ay ) )

	return world:CreateJoint ( joint_def )

end

function b2CreateJointPulley ( world, body1, body2, ga1x, ga1y, ga2x, ga2y, a1x, a1y, a2x, a2y, ratio )

	joint_def = b2PulleyJointDef ( )
	joint_def:Initialize ( body1, body2, b2Vec2 ( ga1x, ga1y ), b2Vec2 ( ga2x, ga2y ), b2Vec2 ( a1x, a1y ), b2Vec2 ( a2x, a2y ), ratio )

	return world:CreateJoint ( joint_def )

end

function b2DestroyJoint ( world, joint )

	world:DestroyJoint ( joint )

end

