math.random returns the same numbers

  • #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


  • #31, by ke4Wednesday, 16. November 2016, 20:23 8 years ago
    I Googled only the point in polygon thing. First i found something like C++ code but then i found the same thing in Lua.

    I made the polygon in polygon function myself, i don't really know if you could call that a polygon inside of a polygon. It's just checking all the points from one polygon inside of a another. It works though.

    Edit: I manage the Simon's function work, so i'm gonna use that.

    Key Killer

    810 Posts

  • #32, by afrlmeThursday, 17. November 2016, 12:50 8 years ago
    Simon's function?

    You're supposed to share "how"! wink

    Imperator

    7278 Posts

  • #33, by ke4Thursday, 17. November 2016, 12:59 8 years ago
    Simon's function AKA
    isPointInsidePolygon()

    I guess a forgot to use "x =" and "y =" in the table before. Or i don't know, it didn't work for me for me for some reason before.

    isPointInsidePolygon({ x = object[i].x, y = object[i].y}, polygon)

    I've noticed there's some licence i don't like and now when it works i can't see a reason why not using the Simon's one.

    The last function there is the one i mentioned before.
    https://github.com/minism/leaf/blob/master/polygon.lua

    Key Killer

    810 Posts

  • #34, by afrlmeThursday, 17. November 2016, 13:15 8 years ago
    So basically...

    function isPolygonInsidePolygon(poly1, poly2)
    
      poly1 = game.CurrentScene.SceneObjects[poly1].ObjectPolygon
    
      poly2 = game.CurrentScene.SceneObjects[poly2].ObjectPolygon
    
    
    
      for i = 1, #poly1 do
    
        if isPointInsidePolygon({x = poly1[i].x, y = poly1[i].y}, poly2) then
    
          return true
    
        end
    
      end
    
       return false
    
    end

    Imperator

    7278 Posts

  • #35, by ke4Thursday, 17. November 2016, 13:25 8 years ago
    You're still gonna need this

    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

    And then

    function isPolygonInsidePolygon(poly1, poly2)
    
        poly1 = sortPolygon(poly1)
    
        poly2 = sortPolygon(poly2)
    
    
        for i = 1, #poly1 do
    
            if isPointInsidePolygon({x = poly1[i].x, y = poly1[i].y}, poly2) then
    
                return true
    
            end
    
        end
    
        return false
    
    end

    Plus i'm checking the object against multiple polygons, so nothing changes, just the one function is being replaced by the Simon wrote.

    polygons = {"OBJ_heart_01", "OBJ_heart_02", "OBJ_heart_03", "OBJ_heart_04", "OBJ_heart_05"}
    
    function isPolygonInsidePolygons(polygons, object)
    
        for i = 1, #polygons do
    
            if isPolygonInsidePolygon(Objects[polygons[i]], object) then
    
                break
    
            end
    
        end
    
    end

    Key Killer

    810 Posts

  • #36, by afrlmeThursday, 17. November 2016, 13:39 8 years ago
    Aisus! I guess I'll look into it more if I ever need it. By the way, I believe I shared an isInRadius() workflow function on the wiki ages back. Checks if a point is inside of a circle - not sure why I didn't create one for rectangle as well, but I guess circle made more sense as you could use it to create a radius around the characters current position for various reasons.

    Could have probably used it for the cursor - well providing you used a cursor with interaction position set in the middle of it. Wouldn't have been as accurate as polygon on polygon mind.

    I'm surprised that the object polygon position doesn't return the current offset with it when you offset an object as the mouse cursor somehow seems to know that the polygon has been offset with the object - though I guess Simon probably included something in the source code to listen out for offset objects...

    Imperator

    7278 Posts

  • #37, by ke4Thursday, 17. November 2016, 13:43 8 years ago
    Basically you understand it all now grin

    I was suprised either, well maybe it will be updated in same upcoming build.
    One thing: Is a variable inside of a function automatically a local variable?

    Key Killer

    810 Posts

  • #38, by afrlmeThursday, 17. November 2016, 15:08 8 years ago
    You are talking about the input variable of a function? Yeah it becomes a local variable for the function.

    Imperator

    7278 Posts

  • #39, by ke4Thursday, 17. November 2016, 15:13 8 years ago
    I mean for example this varibale in the sortPolygon() function, if it needs to be set as local or not.

    local polygonTable = {}

    Key Killer

    810 Posts

  • #40, by sebastianThursday, 17. November 2016, 15:20 8 years ago
    when you make it local inside a function, its only available inside the function

    Thread Captain

    2346 Posts