math.random returns the same numbers

  • #20, by ke4Tuesday, 15. November 2016, 15:06 8 years ago
    I'm aware of the issue.

    So this is what i have done. Always returns false though.
    a "is polygon inside of a polygon" thing.
    function objectDetect()
    
     Objects.OBJ_symbol.ObjectOffset = { x = ( getCursorPos().x - 35 ), y = ( getCursorPos().y - 39 )}
    
     for k,v in pairs(Objects.OBJ_symbol.ObjectPolygon) do
    
      local point = { x = v["x"], y = v["y"]}
    
      local poly = Objects.OBJ_heart_01.ObjectPolygon
    
      if isPointInsidePolygon(point, poly) then
    
       print( "true")
    
      end
    
     end	
    
    end
    
    
    
    registerEventHandler("mainLoop", "objectDetect")

    Key Killer

    810 Posts


  • #21, by afrlmeTuesday, 15. November 2016, 15:13 8 years ago
    You mean you are trying to force it to loop through each point inside of the cursor? I would think that would be really slow - well with Lua anyway. The thing I shared just checks to see if all 4 corners are outside of the polygon, if they are then it's not touching else it is touching at some point. You'd have to adjust my function a bit though as your cursor default x,y is located on the top right rather than left.

    Imperator

    7278 Posts

  • #22, by ke4Tuesday, 15. November 2016, 15:19 8 years ago
    No i'm not using the cursor at all. I have a object "symbol" that represents the cursor which is invisibile itself. I'm seting a position for the "symbol" object where the cursor is so it looks like a cursor.

    And then i'm checking if any of the point from the "symbol" object polygon is inside of the "heart_01" object polygon.

    I'm starting to think that what i'm trying to do here is too complicated. On the image the first two images should be detected as as collision, and the third not ( which is ) as i want the collision to happen when the white graphics is touching the heart.

    Key Killer

    810 Posts

  • #23, by afrlmeTuesday, 15. November 2016, 15:24 8 years ago
    isPointInsidePolygon only checks a single x,y coordinate against the polygon not 2 polygons, so you need to check the 4 points of a rectangle instead. 4 queries.

    Also, quick note: you've set a negative offset for y position of getcursorpos which means you are setting it higher than current cursor position rather than lower.

    Imperator

    7278 Posts

  • #24, by ke4Tuesday, 15. November 2016, 15:30 8 years ago
    for k,v in pairs(Objects.OBJ_symbol.ObjectPolygon) do
    
        point = { x = v["x"], y = v["y"]}
    
        poly = Objects.OBJ_heart_01.ObjectPolygon
    
        if isPointInsidePolygon(point, poly) then
    
            print( "true") 
        end
    
    end

    But this should always check a single x, y coordinate at a time, no?

    It should be:

    point = { x = 52, y = 117} 

    The next loop:

    point = { x = 65, y = 9} 

    With the cursor visible:

    Key Killer

    810 Posts

  • #25, by afrlmeTuesday, 15. November 2016, 16:07 8 years ago
    Interesting, but your for loop is also a loop inside of a loop, so it's probably not checking the entire thing - in other words I think that approach though interesting is just too slow as you are checking random points against random points - I assume the isPointInsidePolygon function also iterates through each point with the query in a similar way too.

    Imperator

    7278 Posts

  • #26, by ke4Tuesday, 15. November 2016, 20:29 8 years ago
    Loopception grin I found the real problem though. It returns the same polygon coordinates after the object if moved.

    Objects.OBJ_symbol.ObjectPolygon
    Objects.OBJ_symbol.ObjectOffset = { x = 500, y = 500 }
    Objects.OBJ_symbol.ObjectPolygon --still the same points

    Key Killer

    810 Posts

  • #27, by ke4Wednesday, 16. November 2016, 15:11 8 years ago
    I sorted it out eventually. However there's a lot of happening.

    Since the ObjectPolygon doesn't return updated coordinates after the object is moved with ObjectOffset i created a function which returns updated coordinates based on the current offset.

    function sortPolygon(obj)
    
        local polygonTable = {}
    
        for i = 1, #obj.ObjectPolygon do
    
            table.insert(polygonTable, { x = obj.ObjectPolygon[i].x + obj.ObjectOffset.x, y = obj.ObjectPolygon[i].y + obj.ObjectOffset.y})
    
        end
    
     
    
        return polygonTable
    
    end

    For some reason the isPointInsidePolygon fuction was returning false to me, so i created the same function based on stackowerflow ( the function is crazy, i have no idea how it works )

    function pointInPolygon(polygon, x, y)
    
        local i, j = #polygon, #polygon
    
        local oddNodes = false
    
    
    
        for i = 1, #polygon do
    
            if ((polygon[i].y = y
    
                    or polygon[j].y = y) and (polygon[i].x <= x
    
                    or polygon[j].x <= x)) then
    
                    if (polygon[i].x+(y-polygon[i].y)/(polygon[j].y-polygon[i].y)*(polygon[j].x-polygon[i].x) < x) then
    
                            oddNodes = not oddNodes
    
                    end
    
            end
    
            j = i
    
        end
    
    
    
        return oddNodes
    
    end

    And then finally a function to check if a polygon is inside of a polygon

    function polygonInPolygon(polygon, object)
    
        for i =1, #object do
    
            if pointInPolygon(polygon, object[i].x, object[i].y) then
    
                --anything here // collision
                return true
    
            end
    
        end
    
    
    
        return false
    
    end

    Well and since i want to check a more polygons

    function polygonInPolygons(polygons, object)
    
        for i = 1, #polygons do
    
            if polygonInPolygon(sortPolygon(Objects[polygons[i]]), object) then
    
                break
    
            end
    
        end
    
    end

    + i need a table of the polygons i want to check

    polygons = {"OBJ_heart_01", "OBJ_heart_02", "OBJ_heart_03", "OBJ_heart_04", "OBJ_heart_05"}

    That would be all for the definition script part. At the beginning of the scene where i want to run this i need to run a loop for setting a position for the symbol representing a cursor

    function setSymbol()
    
        Objects.OBJ_symbol.ObjectOffset = { x = ( getCursorPos().x - 35 ), y = ( getCursorPos().y - 39 )}
    
    end
    
    
    
    registerEventHandler("mainLoop", "setSymbol")

    And then i'm calling a Visionaire action which is also a loop but runs once in a 100ms becase it takes a while to run all the code. All i need there is:

    The object i'm checking if is inside of a polygon

    local object = sortPolygon(Objects.OBJ_symbol)

    And finally run the main function

    polygonInPolygons(polygons, object)

    I also have a loop that animates the objects with the :to() function, it runs once in a 500ms.

    Also i reduced the amount of objects from 10 to 5 to make it faster.


    Key Killer

    810 Posts

  • #28, by afrlmeWednesday, 16. November 2016, 16:16 8 years ago
    Damn mate, that's some mad looking shit!

    I understand what you are doing in the first code block, which is basically generating a table entry of x, y for each point inside of the linked polygon. Seeing as you are able to run a for loop against the objectpolygon that must mean that all the entries were included in the table as index values, otherwise you wouldn't have been able to return the #total amount of entries or iterate through them with a for i loop.

    The second code block - well... I sort of get it but the equation stuff is beyond me! Ah mate, the whole thing just looks over complicated.

    Anyway, well done for sorting it out yourself! wink

    Imperator

    7278 Posts

  • #29, by ke4Wednesday, 16. November 2016, 19:28 8 years ago
    I'm just taking the ObjectPolygon table, edit each row with the offset and put it in a new table.
    Mate, i have no idea how the equation works. I Googled that shit. Pretty cool that it works for all polygons.

    Yeah... well i'm just glad it works the way i want.

    Key Killer

    810 Posts

  • #30, by afrlmeWednesday, 16. November 2016, 20:13 8 years ago
    I tried googling for polygon inside polygon too, but there's just so many different versions it's hard to know which one works / is the better option. The thing you found was for Lua script or something else?

    Imperator

    7278 Posts