Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Box2dEasy - no collision / isSensor question - Gideros Forum

Box2dEasy - no collision / isSensor question

NinjadoodleNinjadoodle Member
edited October 2017 in General questions
Hi guys

I'm making some shards of a bottle breaking and I don't want them to collide, astray get created in the same spot. I'm using Box2dEasy, but can't figure out how to disable collision.

I've tried setting isSensor = true, but it doesn't seem to have an effect.

Any help would be awesome :)

Thanks in advance!
local shard1 = Prop.new(self.pack, "shard.png", target.sprite:getX(), target.sprite:getY() - 96, 0.5, 0.5, mg)
world:createRectangle(shard1, {type = "dynamic", isSensor = true})
shard1:set("linearVelocityX", 5 * math.cos(math.random(360)))
shard1:set("linearVelocityY", 5 * math.sin(math.random(360)))
local shard2 = Prop.new(self.pack, "shard.png", target.sprite:getX(), target.sprite:getY() - 96, 0.5, 0.5, mg)
world:createRectangle(shard2, {type = "dynamic", isSensor = true})
shard2:set("linearVelocityX", 5 * math.cos(math.random(360)))
shard2:set("linearVelocityY", 5 * math.sin(math.random(360)))


  • antixantix Member
    edited October 2017 Accepted Answer
    Check out collision filtering. If you make these objects in a group that doesn't collide with any others that should get you the desired result. If you want to read more about this checkout the iForce tutorial... http://www.iforce2d.net/b2dtut/collision-filtering

    and here's a wee example I made over breakfast :)
    -- Simple box2d objects collision filter example
    -- objects in this example collide with only the walls, not other objects
    require ("box2d")
    local random, sin, cos = math.random, math.sin, math.cos
    local w = application:getContentWidth()
    local h = application:getContentHeight()
    -- our collision masks
    BALL_MASK = 1
    WALL_MASK = 2
    -- init box2d
    local world = b2.World.new(0, 0, true)
    -- set box2d debug
    local debugDraw = b2.DebugDraw.new()
    -- create walls
    local walls = world:createBody{type = b2.STATIC_BODY}
    local chain = b2.ChainShape.new()
    chain:createLoop(1, 1, w, 1, w, h, 1, h)
    walls.fixture = walls:createFixture{shape = chain, density = 1.0, friction = 0, restitution = 1}
    walls.fixture:setFilterData({categoryBits = WALL_MASK, maskBits = BALL_MASK, groupIndex = 0}) -- determines what collides with this object
    MAX_BALLS = 50
    local heading = 0
    -- create balls
    for i = 1, MAX_BALLS do
      local x, y = w * 0.5, h * 0.5
      local ball = world:createBody{type = b2.DYNAMIC_BODY} -- create the box2d thing
      local circle = b2.CircleShape.new(0, 0, 10)
      ball.fixture = ball:createFixture{shape = circle, density = 0.1, friction = 0, restitution = 0}
      ball.fixture.userData = TYPE_BALL
      ball.fixture:setFilterData({categoryBits = BALL_MASK, maskBits = WALL_MASK, groupIndex = 0}) -- determines what the ball collides with
      ball:setPosition(x, y)
      local location = {x = x + sin(heading), y = y + cos(heading)}  -- set ball moving at random speed in random direction
      local mag = random() * 2 + 2.34 -- velocity
      local vx = (x - location.x) * mag
      local vy = (y - location.y) * mag
      print(xVect, yVect, x, y, location.x, location.y)
      ball:applyLinearImpulse(vx, vy, location.x, location.y) -- go ball go!
      heading = heading + (360 / MAX_BALLS)
    -- run this every frame
    local function onEnterFrame(event)
      world:step(1/60, 8, 3) -- run box2d simulation
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing
  • antixantix Member
    edited October 2017
    Then again, if you don't require any collision at all then maybe you could just use the new Particle system built into Gideros?
    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing
  • NinjadoodleNinjadoodle Member
    edited October 2017
    Hi @antix

    Thanks for the reply, I pretty much got it going after a bit of hair pulling, but I'm having an issue.

    I can't seem to add a physics sprite into a group and have it effected by the physics world (probably doing something wrong here).

    Instead I have to add it to the scene e.g:self:addChild(sprite)

    What I really need to do is to add it to a group I've created called 'bg' - bg:addChild(sprite)

    Everything is working fine when I don't try and add it to the group.

    This probably has something to do with me not understanding how the physics world etc. works.

    Any ideas would be awesome!

    Thanks again!
  • Got it! It was an error I've made in the onEnterFrame function :)

    Thanks heaps for your help!

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • YanYan Member
    Is better to use FilterCollisions, because sensors, are handled any way, but when you use filters, bodies are ignoring totally
  • Hi @Yan

    Thank for the tip, I'll keep that in mind!

    In this case I'm only using 4 small physics objects that get destroyed after half a second, so it should not cause any performance issues :)
Sign In or Register to comment.