Hi,
I'm trying to get collision detection (box2D) working... right now without success
.
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
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 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
... and thank's for the link, too!