Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Physics Problem - Solved :) — Gideros Forum

Physics Problem - Solved :)

NinjadoodleNinjadoodle Member
edited November 2017 in General questions
Hi guys

I'm trying to get the hang off physics in Gideros and I'm using debug mode.

Everything setups correctly and the physics shapes work the way they are supposed to, but I can't get the images to update/stick to their physics bodies. They are created in the correct place, but not updated.

This is my EnterFrame ...
function Stage09:onEnterFrame()
 
    self.world:step(1/60, 8, 3)
 
    for i = 1, self.ph:getNumChildren() do
 
        local sprite = self.ph:getChildAt(i)
 
        if sprite.body then
 
            local body = sprite.body
            local bodyX, bodyY = body:getPosition()
            sprite:setPosition(bodyX, bodyY)
            sprite:setRotation(body:getAngle() * 180 / math.pi)
 
        end
    end
end
here is one of my objects ...
S09Ball = Core.class(Sprite)
 
function S09Ball:init(x, y, wrld, atlas, container)
 
    self.sprite = Bitmap.new(atlas:getTextureRegion("s05Debris.png"))
    self.sprite:setAnchorPoint(0.5, 0.5)
	self.sprite:setPosition(x, y)
    self:addChild(self.sprite)
	container:addChild(self)
 
	local radius = self.sprite:getWidth() / 2
 
	local body = wrld:createBody{type = b2.DYNAMIC_BODY}
    body:setPosition(self.sprite:getX(), self.sprite:getY())
    body:setAngle(self.sprite:getRotation() * math.pi / 180)
 
    local circle = b2.CircleShape.new(0, 0, radius)
    local fixture = body:createFixture{shape = circle, density = 1.0, friction = 0.1, restitution = 0.2}
 
    self.sprite.body = body
    self.sprite.body.type = "ball"
 
end
and here is how I add it ...
self.onTouch = function(target, event)
 
			if target:hitTestPoint(event.touch.x, event.touch.y) then
 
				self.ball = S09Ball.new(event.touch.x, event.touch.y, self.world, self.pack, self.ph)
 
			end
		end
 
		self:addEventListener(Event.TOUCHES_BEGIN, self.onTouch, self)
Thanks heaps for any help :)

Comments

  • NinjadoodleNinjadoodle Member
    edited November 2017
    Got it! I was not referencing the sprite correctly inside the ENTER_FRAME function ...

    Since I'm using a class, it needs to be something like this ...
    function Stage09:onEnterFrame()
     
        self.world:step(1/60, 8, 3)
     
        for i = 1, self.ph:getNumChildren() do
     
            local item = self.ph:getChildAt(i)
     
            if item.sprite.body then
     
                local body = item.sprite.body
                local bodyX, bodyY = body:getPosition()
                item.sprite:setPosition(bodyX, bodyY)
                item.sprite:setRotation(body:getAngle() * 180 / math.pi)
     
            end
        end
    end

    Likes: antix

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