Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Change Body Properties — Gideros Forum

Change Body Properties

HubertRonaldHubertRonald Member
edited October 2014 in Code snippets
Hi folks,

While I was improving my application code, I found this link
http://giderosmobile.com/forum/discussion/2328/change-fixedrotation-runtime/p1

@hgvyas123 said: "it's possible to change the body type".
Then I decided to add this function to @ar2rsawseen 's Box2dEasy
function b2.World:changeBodyState(object, config)
	-- remove this condition if you want change terrain body too
	-- if object.body._conf.shape == "circle" or object.body._conf.shape=="rectangle" then
		local conf = {
				type = "static",
				draggable = false,
				}
 
		if config then
			--copying configuration
			for key,value in pairs(config) do
				conf[key]= value
			end
		end
 
		local setType = b2.STATIC_BODY
		if conf.type == "dynamic" then
			setType = b2.DYNAMIC_BODY
		elseif conf.type == "kinematic" then
			setType = b2.KINEMATIC_BODY
		end	
 
 
		--[[
                    --------------------------
                      Update Script Oct-14-2014
                    --------------------------
                    Prevent bug when you change "conf.draggable" many times to "true"
                    for more information see:
                    <a href="http://giderosmobile.com/forum/discussion/4668/change-body-properties#Item_2" rel="nofollow">http://giderosmobile.com/forum/discussion/4668/change-body-properties#Item_2</a>
 
                ]]
                -- update body type
		self.sprites[object.id].body:setType(setType)
		object.body._conf.type = conf.type
 
		-- update draggable
		-- bug when both object.body._conf.draggable and
		-- conf.draggable are true
		if object.body._conf.draggable ~= conf.draggable then	
			object.body._conf.draggable = conf.draggable
 
			if conf.draggable then
				self:makeDraggable(object)
			else
				if object.onDragStart then
					self:undoDraggable(object)
				end
			end
		end
	--else
		-- Do you want terrain dynamic or kinematic or draggable?
		--print(object.body._conf.shape)
	--end
 
end
I hope this little code can help on something and I hope that @ar2rsawseen can include this in his official version of Box2dEasy for help new users of Gideros.

All suggestions are welcome

Likes: talis

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • HubertRonaldHubertRonald Member
    edited October 2014
    Hi @ar2rsawseen after time!

    I found bug in previous script

    For example:
     
    require "Box2dEasy"
     
    local sun = Bitmap.new(Texture.new("sun_shades.png", true))
    sun:setAnchorPoint(0.5, 0.5)
    sun:setPosition(150, 150)
    stage:addChild(sun)
     
    local world = b2.World.new(0, 0, true)
    world:createCircle(sun, {type = "dynamic"})
     
    ---------------------
    -- here's the bug!!!
    ---------------------
    local n=5 --n>=2 
    for i=1,n do
     world:changeBodyState(sun,{type = "static", draggable = true}) -- or type="kinematic" or type="dynamic"
    end
    world:changeBodyState(sun,{draggable = false})
    world:changeBodyState(sun,{draggable = true})
    So the sprite "sun" works like Elastic Joints of Gideros' Example Projects, It happens because previous script erases the last joint and no previous (n>=2 on "for" loop) joints that were created. Then I only had to consider the "draggable config" is different for setup:
     
    --[[
          --------------------------
           Update Script Oct-14-2014
          --------------------------
     ]]
     
           --......
     
                if object.body._conf.draggable ~= conf.draggable then
     
           --.......
    The original Script is already fixed with the comment made ​​earlier

    Cheers

    Likes: talis

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