Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Collision detection/filtering Box2D — Gideros Forum

Collision detection/filtering Box2D

gyrospgyrosp Member
edited November 2012 in General questions
Hi,

I'm trying to get collision detection (box2D) working... right now without success :D.
I want to check if a chain (some revolute joints) hits a certain sprite. I just want to check the detection for this sprite right now. It has it's own class as you can see in the source code below and tries to get the CONTACT_BEGIN event but it does not seem to work.

I've taken a look at several samples in the forum and docs but I don't see a mistake in my code. A promising link in the wiki has been deleted ;) (http://giderosmobile.com/DevCenter/index.php/Collision_filtering)

this is my code of the sprite class:
--[[
 filename : Points.lua
--]]
 
Points = Core.class(Sprite)
 
function Points:init()
	self.Counter 			= 1
	self.CollisionCounter 	= 0
	self.MaxCollisions 		= 5
 
	self:addEventListener(Event.BEGIN_CONTACT, self.onCollision, self)
	self:addEventListener(Event.END_CONTACT, self.onCollisionEnd, self)
end
 
...
 
function Points:create(actors, fixtureDef, b2, world, stage)
	local ground = world:createBody({})
 
	local shape = b2.CircleShape.new()
	shape:set(0.5, 0.5, 15)
	local myFixtureDef = {shape = shape, density = 1, restitution = 0.2}
 
	local bodyDef = {type = b2.DYNAMIC_BODY, position = {x = 260, y = 360}}
	local body = world:createBody(bodyDef)
	body:createFixture(myFixtureDef)
end
 
...
 
function Points:onCollision()
	print("huhu")
	self.CollisionCounter = self.CollisionCounter + 1
 
	if (self.MaxCollisions <= self.CollisionCounter) then
		--creating timer to delay removing bodies
		--1 milisecond
		--Timer.delayedCall(1, function() 
			--remove bodies
		--	world:destroyBody(bodyA)
		--	world:destroyBody(bodyB)
		--end)	
	end
 
	print(self.CollisionCounter)
end
 
function Points:onCollisionEnd()
	self.CollisionCounter = self.CollisionCounter - 1
end
this is the code from main.lua
...
local points = Points.new()
points:create(actors, fixtureDef, b2, world, stage)
stage:addChild(points)
...
When I write something like this in the main.lua it works
local function bla()
	print("awefafe")
end
world:addEventListener(Event.BEGIN_CONTACT, bla)
But this gives me all collisions and I just want to get the collision of one sprite and I want to handle it in the Point class.

Many thanks in advance

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited November 2012 Accepted Answer
    Well Points is a Sprite object (inherited from it) and it does not receive BEGIN_CONTACT or END_CONTACT. only world instance does.

    What you can do is set up global collision handler in main lua and then based on colliding objects, call different handler functions.

    For example:
    inside Points:create add to the last line
    body.object = self
    and then add this to main.lua or wherever you have your world defined
    --define collision event handler function
    function onBeginContact(e)
        --getting contact bodies
        local fixtureA = e.fixtureA
        local fixtureB = e.fixtureB
        local bodyA = fixtureA:getBody()
        local bodyB = fixtureB:getBody()
        --check if body has object and collision handle
        if bodyA.object and bodyA.object.onCollision then
            --call collision handler
            bodyA.object:onCollision()
        end
        --same for other body
        if bodyB.object and bodyB.object.onCollision then
            bodyB.object:onCollision()
        end
    end
     
    world:addEventListener(Event.BEGIN_CONTACT, onBeginContact)
    and that's it

    And here is a great tutorial on collision filters:
    http://www.giderosmobile.com/forum/discussion/1070/a-little-information-about-collision-filters#Item_1

    Likes: gyrosp

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you, it's working fine now :D.
    ... and thank's for the link, too!
Sign In or Register to comment.