Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
raycast bug — Gideros Forum

raycast bug

duke2017duke2017 Member
edited November 2012 in Bugs and issues
Found a bug in the raycast. Data is not optional. If data is missing, function arguments are shifted to the right.
but, how it works below:

function ray(data, fixture, hitX, hitY, vectX, vectY, fraction)
print("(x, y)"..hitX,hitY)
end

ray1 = stage.world:rayCast(x1, y1, x2, y2, ray, {})

and how its not work:

function ray(fixture, hitX, hitY, vectX, vectY, fraction)
print("(x, y)"..hitX,hitY)
end

ray1 = stage.world:rayCast(x1, y1, x2, y2, ray)
Tagged:

Comments

  • atilimatilim Maintainer
    edited November 2012
    Hi,

    I couldn't catch the bug. On my side both
    world:rayCast(x1, y1, x2, y2, function(fixture, x, y, nx, ny, fraction) end)
    and
    world:rayCast(x1, y1, x2, y2, function(data, fixture, x, y, nx, ny, fraction) end, data)
    worked well.

    Here is my full test code:
    require "box2d"
     
    b2.setScale(20)
     
    local world = b2.World.new(0, 9.8)
     
    local function createBox(x, y)
    	local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = x, y = y}}
     
    	local shape = b2.PolygonShape.new()
    	shape:setAsBox(40, 40)
    	body:createFixture{shape = shape, density = 1, restitution = 0.2, friction = 0.3}
    end
     
    local ground = world:createBody({})
     
    local shape = b2.EdgeShape.new(-200, 400, 520, 400)
    ground:createFixture({shape = shape, density = 0})
     
    createBox(140, -30)
    createBox(180, 300)
     
    local function onEnterFrame()
    	world:step(1/60, 8, 3)
     
    	world:rayCast(0, 240, 320, 240, function(fixture, x, y, nx, ny, fraction)
    		print(fixture, x, y, nx, ny, fraction)
    	end)
     
    	world:rayCast(0, 240, 320, 240, function(data, fixture, x, y, px, py, fraction)
    		print(data, fixture, x, y, nx, ny, fraction)
    	end, "my data")
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
     
    local debugDraw = b2.DebugDraw.new()
    world:setDebugDraw(debugDraw)
    stage:addChild(debugDraw)
    You can look at the lines 26 and 30.
  • no, in your code ray always passes through one object. Try this
    createBox(100, -30)
    createBox(190, -30)

    Output:

    table: 078B3D98 60 240 -1 0 0.1875

    nil table: 078B2230 150 240 -1 0 (its arg+1 bug)

    my data table: 078B3D98 60 240 nil nil 0.1875

    my data table: 078B2230 150 240 nil nil 0.46875

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    Accepted Answer
    oh yes. it's a bug :) now I'm fixing it. thank you. :)
  • bcos79bcos79 Member
    edited February 2013
    @atilim

    I am trying to use rayCast with a class-based structure, and I am still seeing this bug. Here is an example setup:

    main.lua
    require "box2d"
    test = Test.new()
    test.lua
    Test = Core.class(Sprite)
     
    function Test:init()
    	self.world = b2.World.new(0, 0)	
    	self:createBox(100, 100)
     	self.world:rayCast(0, 0, 100, 100, self.rayCastCallback)
     	self.world:rayCast(0, 0, 100, 100, self.rayCastCallbackWithData, {})
    end
     
    function Test:createBox(x, y)
    	local body = self.world:createBody{type = b2.DYNAMIC_BODY, position = {x = x, y = y}}
    	local shape = b2.PolygonShape.new()
    	shape:setAsBox(50, 50)
    	body:createFixture{shape = shape, density = 1, restitution = 0, friction = 0}
    end
     
    function Test:rayCastCallback(fixture, x, y, nx, ny, fraction)
    	print(fixture, x, y, nx, ny, fraction)
    end
     
    function Test:rayCastCallbackWithData(data, fixture, x, y, nx, ny, fraction)
    	print(data, fixture, x, y, nx, ny, fraction)
    end
    Output
    49.999998807907	49.999998807907	0	-1	0.5	nil
    table: 08240FB8	49.999998807907	49.999998807907	0	-1	0.5	nil
    The callback function parameter values are shifted as duke017 pointed out above and there's no fixture value.

    If I remove the class-based implementation and switch everything to a simple script, it works fine. Example:

    main.lua
    local world = b2.World.new(0, 0)
     
    local function createBox(x, y)
    	local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = x, y = y}}
    	local shape = b2.PolygonShape.new()
    	shape:setAsBox(50, 50)
    	body:createFixture{shape = shape, density = 1, restitution = 0, friction = 0}
    end
     
    local function rayCastCallback(fixture, x, y, nx, ny, fraction)
    	print(fixture, x, y, nx, ny, fraction)
    end
     
    local function rayCastCallbackWithData(data, fixture, x, y, nx, ny, fraction)
    	print(data, fixture, x, y, nx, ny, fraction)
    end
     
    createBox(100, 100)
    world:rayCast(0, 0, 100, 100, rayCastCallback)
    world:rayCast(0, 0, 100, 100, rayCastCallbackWithData, {})
    Output
    table: 082627D8	49.999998807907	49.999998807907	0	-1	0.5
    table: 08240FB8	table: 082627D8	49.999998807907	49.999998807907	0	-1	0.5
    In this case the callbacks works fine.

    Am I missing something or is this a bug?
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    @bcos79
    if you define function as method of class like this (with colon):
    function Test:rayCastCallback(fixture, x, y, nx, ny, fraction)
    	print(fixture, x, y, nx, ny, fraction)
    end
    Then as a first argument it expects the reference to the instance. That's why all arguments are shifted.

    To bypassed that (like in all listeners), you would need to provide reference to instance as self when you add your listener as data parameter, like this:
    self.world:rayCast(0, 0, 100, 100, self.rayCastCallback, self)
    self.world:rayCast(0, 0, 100, 100, self.rayCastCallbackWithData, self)
    If you need to pass additional data, make it as a property of your class, thus it will be also available in all it's methods including rayCastCallbackWithData
  • @ar2rsawseen

    Thank you for clarifying that! My apologies for thinking it was a bug. :)
  • It's completely ok, we are all here to learn and evolve ;)
  • Ohh... great((... fix one bug led to another. I upgraded from version .1 to .9, and found that rаyсast works now is not correct absolutely. not yet understand the reason, but has now changed all objects checks. its work random) earlier was checking 1,2,3,4.. objects from end. Now 2,3,1,4.. or 2,1 or depending on the position of the object.. damn.. will have to return to .1 ((
  • nice.. I cheated myself, earler cheking is also random)) I just used the bug as a feature)
    but.. but why so? why checking 1,2, and coordinate 2 is (x,y). but if coordinate 2 is (x,y+n), then checking is 2,1.. and if (x,y+m) then 1,2 again.. I'm stumped
Sign In or Register to comment.