Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Removing falling object with onBeginContact — Gideros Forum

Removing falling object with onBeginContact

DikkesnoekDikkesnoek Member
edited April 2013 in General questions
Hi,

I don't know how to fix this. I have several object falling from the sky. If I use a
sprite:getY() in the onEnterFrame() handler I can remove one the objects. When
I create a body on the bottom and use the onBeginContact() handler it will react
weird. I think that it has something to do that I could'nt determine the exact
sprite which is contacting the bottomBody. If I pick a object that has been created
later (I create the objects with a timer) and I throw it down (it will pass an other
object) than it will not be destroyed. Here is a piece of code:
 
local bottomBody = world:createBody({type = b2.STATIC_BODY})
local bottomShape = b2.CircleShape.new(480, 670, 220)
bottomBody:createFixture{shape = bottomShape, density = 0}
bottomBody.name = "bottomBody"
 
function onEnterFrame(event)
	world:step(1/60, 8, 3)
 
	for body,sprite in pairs(actors) do
		sprite:setPosition(body:getPosition())
		sprite:setRotation(body:getAngle() * 180 / math.pi)		
 
		--if sprite:getY() > 340 then  -- THIS WORKS --
		if (removeObject) then         -- THIS NOT --
			world:destroyBody(body)
			stage:removeChild(sprite)
			actors[body] = nil
			removeObject = false
		end
	end
end
 
local function onBeginContact(event)
	-- You can get the fixtures and bodies in this contact like:
	local fixtureA = event.fixtureA
	local fixtureB = event.fixtureB
	local bodyA = fixtureA:getBody()
	local bodyB = fixtureB:getBody()
 
	if ((bodyA.name == "bottomBody")  and (bodyB.name == "fallingBody") or 
	    (bodyA.name == "fallingBody") and (bodyB.name == "bottomBody")) then
 
		score = score + 1
		scoreText:setText(score)
 
		removeObject = true
	end
end
If you'll try to remove the specific falling object in the onBeginContact() handler
it will remove all the sprites.

Thanks,

Marc

Comments

  • Is there a way to detect the x and y positions of the collisions?
    event.x and event.y will not work in the onBeginContact(event)
    handler.

    Regards,

    Marc
  • ar2rsawseenar2rsawseen Maintainer
    @Dikkesnoek to destroy bodies, you can create another queue like this:
    http://www.giderosmobile.com/forum/discussion/comment/5908#Comment_5908

    There is a way to get collision coordinates, check this thread:
    http://www.giderosmobile.com/forum/discussion/comment/22493#Comment_22493

    But what I would recommend is to set up the sensors, where your body's might land, etc, so everytime you get a collision with your hidden sensor, you know body should be removed :)
  • Thanks ar2rsawseen. This is a nice solution. I've seen the locked
    message a lot. I did the following by the way:
    function onEnterFrame(event)
    	world:step(1/60, 8, 3)
     
    	for body,sprite in pairs(actors) do
    		sprite:setPosition(body:getPosition())
    		sprite:setRotation(body:getAngle() * 180 / math.pi)		
     
    		if (sprite.removeMe) then
    			world:destroyBody(body)
    			stage:removeChild(sprite)
    			actors[body] = nil
     
    			sprite.removeMe = false
    			sun:setRotation(sun:getRotation() - 30)
    		end
    	end
    end
     
    local function onBeginContact(event)
    	-- You can get the fixtures and bodies in this contact like:
    	local fixtureA = event.fixtureA
    	local fixtureB = event.fixtureB
    	local bodyA = fixtureA:getBody()
    	local bodyB = fixtureB:getBody()
     
    	if ((bodyA.name == "bottomBody")  and (bodyB.name == "fallingBody") or 
    	    (bodyA.name == "fallingBody") and (bodyB.name == "bottomBody")) then
     
    		if  (bodyA.name == "fallingBody") then
    			for body,sprite in pairs(actors) do
    				if (body == bodyA) then
    					sprite.removeMe = true
    				end
    			end
    		end
    		if  (bodyB.name == "fallingBody") then
    			for bodyB,sprite in pairs(actors) do
    				if (body == bodyB) then
    					sprite.removeMe = true
    				end
    			end
    		end
     
    		score = score + 1
    		scoreText:setText(score)
     
    		print("COLLISION: ", score)
    	end
    end
    This works too.

    Regards,

    Marc
Sign In or Register to comment.