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

Raycast

duke2017duke2017 Member
edited August 2012 in General questions
Is there any examples of using a raycast? How return parameter of "the fixture hit by the ray"?

wall( 100, 100, 50, 50 )
function rayfunc(event)
???
end
ray = self.world:rayCast(0, 0, 200, 200, rayfunc)

Comments

  • changing the Alpha?
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • Alpha?) No.. I'm talking about b2.World:rayCast(x1, y1, x2, y2, listener, data)
    I need to know which object was crossing ray. how this parameter is considered?
    sorry for my english)
  • the parameter listener is the callback function which accepts 6 or 7 parameters.
    First one is the fixture hit by the ray
  • yes, I understand that, but how exactly do I callback this the first parameter?

    function rayfunc(event)
    if event."First parameter" == box then
    ......
    elseif."First parameter" == crate then
    ....
    end
    end
    ray = self.world:rayCast(0, 0, 200, 200, rayfunc)


    I do not understand how to read its "first parameter"..
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Ok.

    So, for example, you have a variable world where you have world instance and some kind of box2d object ball. You want to project raycast on x axis through all the screen and get fixture if there is one
    local x, y = ball:getPosition()
    local screenWidth = application:getContentWidth()
     
    local raycastCallback function(fixture, hitX, hitY, vectX, vectY, fration)
        --so if this function is called, it means we hit some kind of object
        --and it's fixture is stored in first variable we named "fixture"
        --so we can for example get body
        local body = fixture:getBody()
     
    end
     
    --now we add callback function for projected raycast
    --Parameters:
    --object x coordinate
    --object y coordinate
    --projection vector on x axis
    --projection vector on y axis
    --callback function
    world:rayCast(x, y, x+screenWidth, y, raycastCallback)
  • Oh, sorry for my stupidity)) Thank you very much for the detailed explanation! What would I do without you)
  • @duke2017 thats completely ok, we're all there to learn ;)
  • jdbcjdbc Member
    edited October 2013
    I am trying to use world:rayCasting and after I called to rayCastCallback function, but it seems fixtures are not sorted.

    I mean I defined the function:



    list = {}

    function raycastCallback (fixture, hitX, hitY, vectX, vectY, fration)
    local body = fixture:getBody()
    list[#list + 1] = body
    end


    but list is not sorted in the sense of the raycast. Is it correct?



  • @jdbc it should return the first fixture it hits by the ray cast, and then depending on your return value
    • return no value or -1: ignore this fixture and continue
    • return 0: terminate the ray cast
    • return fraction: clip the ray to this point
    • return 1: don't clip the ray and continue
    either proceed to next fixture or terminate, etc
  • jdbcjdbc Member
    edited November 2013
    I use rayCastCallback function to check collision between a fixed body and a set of bodies on the left, returning 1.

    A list of collision points is built but this list is not ordered by axis X (sense of raycast from fixed body to the left side), it seems to be randomized.

    Anyway I can ordered this list by axis X but I believe raycast will do it the job.
  • Well it should be ordered by the increasing distance from your starting point to the raycasting direction, which might not always be increasing with the x axis values (depending on your direction).

    if its not, maybe there is some other problem
  • jdbcjdbc Member
    edited November 2013
    I execute something like:


    local left = -2000

    -- Left hits
    self.world:rayCast( v:getX(), v:getY(), v:getX() + left, v:getY(), self.raycastCallback, self)



    where v is a sprite object and self.list is the bodies list and contains all bodies in the raycast trajectory but sometimes not ordered.

    I suppose it is a behaviour of box2d engine.
Sign In or Register to comment.