Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Ragdoll problem — Gideros Forum

Ragdoll problem

hgvyas123hgvyas123 Guru
edited March 2013 in General questions
Hi guys,

i am trying to create ragdoll and it behaves little bit strangely i think i am missing a way to fix rotation limit and need help to fix that

here's the original code from which i am making my ragdoll

https://github.com/MarkHenryC/RagDoll/
 
require "box2d"
 
 
 
world = b2.World.new(0, 5)
 
function drawRect(left,top,width,height)
	local shape = Shape.new()
	shape:setFillStyle(Shape.SOLID, 0x000000, 0.5)
	shape:beginPath()
	shape:moveTo(0,0)
	shape:lineTo(width, 0)
	shape:lineTo(width, height)
	shape:lineTo(0, height)
 
	shape:closePath()
	shape:endPath()
	shape:setPosition(left,top)
	return shape
end
 
originX = 500
originY = 460
local spacing = 1
 
ragdoll= Sprite.new()
stage:addChild(ragdoll)
 
 
	local head = drawRect( 0, 0, 15,15 )
	head.x = originX
	head.y = originY
 
	ragdoll:addChild (head)
 
	local torsoA = drawRect( 0, 0, 45, 24 )
	torsoA.x = originX
	torsoA.y = originY + head:getHeight()
 
	ragdoll:addChild (torsoA)
 
	local torsoB = drawRect( 0, 0, 37.5, 24 )
	torsoB.x = originX
	torsoB.y = torsoA.y + (torsoA:getHeight() * 0.5) + spacing
 
	ragdoll:addChild (torsoB)
 
	local torsoC = drawRect( 0, 0, 30, 24 )
	torsoC.x = originX
	torsoC.y = torsoB.y + (torsoB:getHeight() * 0.5) + spacing
 
	ragdoll:addChild (torsoC)
 
	local pelvis = drawRect( 0, 0, 30, 24 )
	pelvis.x = originX
	pelvis.y = torsoC.y + (torsoC:getHeight() * 0.5) + spacing
 
	ragdoll:addChild (pelvis)
 
	local leftLegA = drawRect( 0, 0, 12, 52.5 )
	leftLegA.x = pelvis.x - ((pelvis:getWidth() - leftLegA:getWidth()) * 0.5)
	leftLegA.y = pelvis.y + (leftLegA:getHeight() * 0.5) + spacing
 
	ragdoll:addChild (leftLegA)
 
	local rightLegA = drawRect( 0, 0, 12, 52.5 )
	rightLegA.x = pelvis.x + ((pelvis:getWidth() - rightLegA:getWidth()) * 0.5)
	rightLegA.y = pelvis.y + (rightLegA:getHeight() * 0.5) + spacing
 
	ragdoll:addChild (rightLegA)
 
	local leftLegB = drawRect( 0, 0, 12, 45 )
	leftLegB.x = leftLegA.x
	leftLegB.y = leftLegA.y + rightLegA:getHeight() - 12
 
	ragdoll:addChild (leftLegB)
 
	local rightLegB = drawRect( 0, 0, 12, 45 )
	rightLegB.x = rightLegA.x
	rightLegB.y = rightLegA.y + rightLegA:getHeight() - 12
 
	ragdoll:addChild (rightLegB)
 
	local leftArmA = drawRect( 125, 70, 10.5, 39 )
	leftArmA.x = torsoA.x - (torsoA:getWidth() * 0.5) - 3
	leftArmA.y = torsoA.y + (leftArmA:getHeight() * 0.5) - 6
 
	ragdoll:addChild (leftArmA)
 
	local rightArmA = drawRect( 185, 70, 10.5, 39 )
	rightArmA.x = torsoA.x + (torsoA:getWidth() * 0.5) + 3
	rightArmA.y = torsoA.y + (rightArmA:getHeight() * 0.5) - 6
 
	ragdoll:addChild (rightArmA)
 
	local leftArmB = drawRect( 125, 105, 10.5, 37.5 )
	leftArmB.x = leftArmA.x
	leftArmB.y = leftArmA.y + (leftArmA:getHeight()) - 6
 
	ragdoll:addChild (leftArmB)
 
	local rightArmB = drawRect( 185, 105, 10.5, 37.5 )
	rightArmB.x = rightArmA.x
	rightArmB.y = rightArmA.y + (rightArmA:getHeight()) - 6
 
	ragdoll:addChild (rightArmB)
 
	function addToPhysics(sprite)
		local shape = b2.PolygonShape.new()
		shape:setAsBox(sprite:getWidth()*0.5, sprite:getHeight()*0.5)
 
		-- and our fixture definition
		local fixtureDef = {shape = shape, density = 20, friction = 0.2}
		local bodyDef = {type = b2.DYNAMIC_BODY, position = {x = sprite:getX()+sprite:getWidth()*0.5, y = sprite:getY()+sprite:getHeight()*0.5}}
		local body = world:createBody(bodyDef)
		body:createFixture(fixtureDef)
		sprite.body = body
	end
 
	function addToPhysics1(sprite)
		local shape = b2.PolygonShape.new()
		shape:setAsBox(sprite:getWidth()*0.5, sprite:getHeight()*0.5)
 
		-- and our fixture definition
		local fixtureDef = {shape = shape, density = 20, friction = 0.2}
		local bodyDef = {type = b2.KINEMATIK_BODY, position = {x = sprite:getX()+sprite:getWidth()*0.5, y = sprite:getY()+sprite:getHeight()*0.5}}
		local body = world:createBody(bodyDef)
		body:createFixture(fixtureDef)
		sprite.body = body
	end
 
	local mySprite = drawRect(0,630,960,10)
	stage:addChild(mySprite)
	addToPhysics1(mySprite)
 
 
 
	for i=1,ragdoll:getNumChildren() do
		local sprite = ragdoll:getChildAt(i)
		sprite:setPosition(sprite.x-sprite:getWidth()*0.5,sprite.y)
		sprite:setAlpha(0.3) --setting this to 0 because shape can not have anchorpoint 0.5 and i didnt want to fight with anchor point
		addToPhysics(sprite)
	end
 
	function ragdoll:addFrictionJoint(a, b, posX, posY, rFrom, rTo, mT) 
 
 
 
		local jointDef = b2.createRevoluteJointDef(a.body, b.body, posX, posY, rFrom, rTo)
		jointDef.collideConnected = false
 
		local revoluteJoint = world:createJoint(jointDef)
		revoluteJoint:enableLimit(true)
		revoluteJoint:setLimits(rFrom*math.pi/180, rTo*math.pi/180)
		revoluteJoint:enableMotor(true)
		revoluteJoint:setMotorSpeed(100)
		revoluteJoint:setMaxMotorTorque(mT or 0.1)
	end
 
	-- neck	
	ragdoll:addFrictionJoint(head, torsoA, torsoA.x, torsoA.y, -22.5, 22.5)
 
	-- backboneA
	 ragdoll:addFrictionJoint(torsoA, torsoB, torsoB.x, torsoB.y, -22.5, 22.5)
 
	-- backboneB
	ragdoll:addFrictionJoint(torsoB, torsoC, torsoC.x, torsoC.y, -22.5, 22.5)
 
	-- backboneC	 
	ragdoll:addFrictionJoint(torsoC, pelvis, pelvis.x, pelvis.y, -22.5, 22.5)
 
	-- leftHip
	ragdoll:addFrictionJoint(pelvis, leftLegA, leftLegA.x, pelvis.y, -45, 90)
 
	-- rightHip
	ragdoll:addFrictionJoint(pelvis, rightLegA, rightLegA.x, pelvis.y, -90, 45)
 
	-- leftKnee
	ragdoll:addFrictionJoint(leftLegA, leftLegB, leftLegB.x, 
		leftLegA.y + leftLegA:getHeight() * 0.5 - 6, -45, 90)
 
	-- rightKnee
	ragdoll:addFrictionJoint(rightLegA, rightLegB, rightLegB.x, 
		rightLegA.y + rightLegA:getHeight() * 0.5 - 6, -90, 45)
 
	-- leftShoulder
	ragdoll:addFrictionJoint(torsoA, leftArmA, leftArmA.x, torsoA.y, 0, 180)
 
	-- rightShoulder
	ragdoll:addFrictionJoint(torsoA, rightArmA, rightArmA.x, torsoA.y, -180, 0)
 
	-- leftElbow
	ragdoll:addFrictionJoint(leftArmA, leftArmB, leftArmB.x, leftArmA.y + 
		leftArmA:getHeight() * 0.5 - 6, -45, 90)
 
	-- rightElbow
	ragdoll:addFrictionJoint(rightArmA, rightArmB, rightArmB.x, rightArmA.y + 
		rightArmA:getHeight() * 0.5 - 6, -90, 45)
 
	local ground11 = world:createBody({})
	local mouseJoint = nil
 
	function onMouseDown(self,event)
		if self:hitTestPoint(event.x, event.y) then
			local jointDef = b2.createMouseJointDef(ground11, self.body, 
			event.x, event.y, 100000)
			mouseJoint = world:createJoint(jointDef)
		end
	end
 
	function onMouseMove(self,event)
		if mouseJoint ~= nil then
			mouseJoint:setTarget(event.x, event.y)
		end
	end
 
	function onMouseUp(self,event)
		if mouseJoint ~= nil then
			world:destroyJoint(mouseJoint)
			mouseJoint = nil
		end
	end
	for i=1,ragdoll:getNumChildren() do
		local sprite = ragdoll:getChildAt(i)
		sprite:addEventListener(Event.MOUSE_DOWN, onMouseDown, sprite)
		sprite:addEventListener(Event.MOUSE_MOVE, onMouseMove, sprite)
		sprite:addEventListener(Event.MOUSE_UP, onMouseUp, sprite)
	end
	function onEnterFrame(event)
 
		world:step(1/60, 8, 3)
 
		for i=1,ragdoll:getNumChildren() do
			local sprite = ragdoll:getChildAt(i)
			if sprite.body then
				sprite:setPosition(sprite.body:getPosition())
				sprite:setRotation(sprite.body:getAngle()*180/math.pi)
			end
		end
	end
	stage:addEventListener(Event.ENTER_FRAME,onEnterFrame)
 
 
	local debugDraw = b2.DebugDraw.new()
	debugDraw:setFlags(b2.DebugDraw.SHAPE_BIT + b2.DebugDraw.JOINT_BIT + b2.DebugDraw.PAIR_BIT + b2.DebugDraw.CENTER_OF_MASS_BIT)
	world:setDebugDraw(debugDraw)
	stage:addChild(debugDraw)
Thnx in advance

Comments

  • hgvyas123hgvyas123 Guru
    edited March 2013
  • fxonefxone Member
    edited March 2013
    @hgvyas123 if you change KINEMATIK to KINEMATIC, it will be a lot better ;)

    edit:
    change this part
    local bodyDef = {type = b2.KINEMATIK_BODY
    to
    local bodyDef = {type = b2.KINEMATIC_BODY
  • hgvyas123hgvyas123 Guru
    edited March 2013
    @fxone

    whoops this english words :D

    :)

    Likes: fxone

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.