Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is it possible to detect a collision between two sensors? — Gideros Forum

Is it possible to detect a collision between two sensors?

mykyl66mykyl66 Member
edited January 2012 in General questions
I have two items on screen that I move around using gtween. I move the physics bodies around with the sprites. No issue there. Everything works fine. I use physics as it seemed to be the easiest way to detect collisions (Couldn't find a way to detect collisions between images that ignored transparent areas.)

However when I try and detect a collision between them nothing triggers.

This is some of the code I am using. Some taken from forum posts and adjusted for my uses. Basically trying to learn to code purely in the Gideros way.

Any ideas where I am failing.

Mike

world:addEventListener(Event.BEGIN_CONTACT, onBeginContact)

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()

print("begin contact: "..bodyA.name.." "..bodyB.name)
end

the following is how I create the main player object that I can move around the screen.

function createPlayer()
local frame1 = Bitmap.new(Texture.new("Red3.png"))
frame1:setAnchorPoint(0.5,0.5)
local frame2 = Bitmap.new(Texture.new("Red2.png"))
frame2:setAnchorPoint(0.5,0.5)
local frame3 = Bitmap.new(Texture.new("Red1.png"))
frame3:setAnchorPoint(0.5,0.5)
player1 = MovieClip.new{
{1, 10, frame1},
{11, 20, frame2},
{21, 30, frame3}
}
player1:setPosition(160,64)
local playerBody = world:createBody{b2.DYNAMIC_BODY, position = {x = player1:getX(),y = player1:getY()}}
local playerShape = b2.CircleShape.new(0,8,20)
playerBody:createFixture{shape = playerShape, density = 0,restitution = 0.0, friction = 0.0, isSensor = true}
player1.body = playerBody
playerBody.name = "Player"
playerBody.MovieClip = player1
actors[#actors + 1] = player1
end

function createObject()
item1 = Bitmap.new(Texture.new("Ring.png"))
item1:setAnchorPoint(0.5,0.5)
item1:setPosition(160,320)

local itemBody = world:createBody{b2.DYNAMIC_BODY, position = {x = item1:getX(),y = item1:getY()}}
local itemShape = b2.CircleShape.new(0,4,12)
itemBody:createFixture{shape = itemShape, density = 0,restitution = 0.0, friction = 0.0, isActive = true}
item1.body = itemBody
itemBody.name = "item"
itemBody.MovieClip = item1
actors[#actors + 1] = item1
end
What would you do for your other half?

http://www.sharksoupstudios.com

Comments

  • I have set them to dynamic bodies with no sensor information at all now and still cannot get a result.

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • have you tried to setup debug mode for physics
    and see what happens with the physical models?

    it helped me a lot!
    		local debugDraw = b2.DebugDraw.new()
    		world:setDebugDraw(debugDraw)
    		stage:addChild(debugDraw)
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • Yes I have debug draw switched on and yet still cannot get a collision to trigger at all. I can see the two bodies overlapping each other.

    Cheers

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • MikeHartMikeHart Guru
    edited January 2012
    Move
    world:addEventListener(Event.BEGIN_CONTACT, onBeginContact)
    below the local onBeginContact function definition. You are assigning a function that is nil at this very moment.

    Or do this:
    local onBeginContact
     
    world:addEventListener(Event.BEGIN_CONTACT, onBeginContact)
     
    onBeginContact = function (event)
        --....
    end

    Likes: Yan

    +1 -1 (+1 / -0 )Share on Facebook
  • Unfortunately I had tried that. In fact currently my event listener is right at the bottom of my code and I even went as far as removing the local from the function itself. Still the same result.

    I am beginning to think that it is impossible. It seems that unless you are letting the physics world do the moving the physics objects cannot be used for detecting collisions.

    Let me try and clarify a bit more.
    I click somewhere on screen and the sprite moves to that position. The body moves with the sprite by having the line "body:setPosition(sprite:getPosition())" within the enterFrame function.

    I get no errors just no trigger.

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • in my game GidHelix i'm moving "birds" in code and they are sensors
    but collision works fine...
    function birds:onEnterFrame(event)
     
    	if self.bird_body then 
    		x, y = self.bird_body:getPosition()
    	else
    		x, y = self:getPosition()
    	end
     
    	if x > application:getLogicalWidth()+32 then	
    		self.xSpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*-1
    		self.ySpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*self.randomDirection()
    	elseif x < -32 then
    		self.xSpeed = ((math.random(0, maxSpeed)/100))+minSpeed
    		self.ySpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*self.randomDirection()
    	end
    	if y < -32 then
    		self.xSpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*self.randomDirection()
    		self.ySpeed = ((math.random(0, maxSpeed)/100)+minSpeed)
    	elseif y > application:getLogicalHeight()+32 then
    		self.xSpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*self.randomDirection()
    		self.ySpeed = ((math.random(0, maxSpeed)/100)+minSpeed)*-1
    	end
     
    	x = x + (self.xSpeed * 100 * event.deltaTime)
    	y = y + (self.ySpeed * 100 * event.deltaTime)
     
    	if self.bird_body  then
    		self.bird_body:setPosition(x, y)
    	else
    		self:setPosition(x,y)
    	end
    end
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • mykyl66mykyl66 Member
    edited January 2012
    Everything points to it being able to work but so far nothing I have tried gets it working. Are you using movieclip to animate your object and gtween to move it around?

    I am close to forgetting it. Have little enough time at the moment without being defeated by what should be so simple.

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • GregBUGGregBUG Guru
    edited January 2012
    i'm using movieclip only for "animate" the bird sprite
    ...for moving i'm using the function on the above post. (no gtween)

    if you want you can send me the source and i can try to help you..!

    Likes: chipster123

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+1 / -0 )Share on Facebook
  • I have created a cut down version with exactly the same result. It would be fantastic if you could spot what is wrong.

    Thank you.

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • GregBUGGregBUG Guru
    edited January 2012
    ok.mykyl66

    a little error in body definition:
    local playerBody = world:createBody{b2.DYNAMIC_BODY, position = {x = player1:getX(),y = player1:getY()}}

    you forgot to type "type=" in the definition of DYNAMIC BODY... ... it happens .. ;)

    change the line in function "setupCharacters"
    with:
    local playerBody = world:createBody{type=b2.DYNAMIC_BODY, position = {x = player1:getX(),y = player1:getY()}}
    and

    in function createColliders()
    local ringBody = world:createBody{type=b2.DYNAMIC_BODY, position = {x = ring1:getX(),y = ring1:getY()}}
    then it works like a sharm... :P

    hope it helps.

    ciao Gianluca.
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+2 / -0 )Share on Facebook
  • Grrrrrrrr. How blasted stupid can I be. lol. Thank you so much. I am placing the dunces cap on my head and sitting in the corner as punishment.

    Cheers

    Mike

    Likes: gorkem

    What would you do for your other half?

    http://www.sharksoupstudios.com
    +1 -1 (+1 / -0 )Share on Facebook
  • ;)
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
Sign In or Register to comment.