locked
Very strange LD poligon behavior! RRS feed

  • Question

  • I was messing around with LDshapes.addpolygon when I ran into a very weird problem.

    When I add a polygon like this, It works:

    points[0]["X"] = 0
    points[1]["X"] = 50
    
    points[3]["X"] = 75 '< -- num 3
    points[2]["X"] = 50 '< -- num 2
    
    points[4]["X"] = 0
    points[5]["X"] = -25
    points[0]["Y"] = 0
    points[1]["Y"] = 0
    points[2]["Y"] = 50
    points[3]["Y"] = 25
    points[4]["Y"] = 50
    points[5]["Y"] = 25
    
    polygon = LDShapes.AddPolygon(points)
    
    Shapes.Move(polygon,100,100


    Notice that

    points[3]["X"]

    is before

    points[2]["X"]

    this works well unless I put them in order. like so:

    points[0]["X"] = 0
    points[1]["X"] = 50
    
    points[2]["X"] = 50 '< -- num 2
    points[3]["X"] = 75 '< -- num 3
    
    
    points[4]["X"] = 0
    points[5]["X"] = -25
    points[0]["Y"] = 0
    points[1]["Y"] = 0
    points[2]["Y"] = 50
    points[3]["Y"] = 25
    points[4]["Y"] = 50
    points[5]["Y"] = 25
    
    polygon = LDShapes.AddPolygon(points)
    
    Shapes.Move(polygon,100,100)

    Here is a picture of the difference:

    3 before 2:


    2 before 3:


    Thanks!!



    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, May 6, 2013 11:21 PM
    Answerer

Answers

  • Interesting!  This means that values of indices are not used as order for vertices of polygon.  Definition order is used as order for vertices of polygon.  Thanks.

    Picture illustrates that definition order is important for LDShapes.AddPolygon()


    Nonki Takahashi

    • Marked as answer by Zock77Editor Tuesday, May 7, 2013 4:51 PM
    Tuesday, May 7, 2013 1:40 AM

All replies

  • Interesting!  This means that values of indices are not used as order for vertices of polygon.  Definition order is used as order for vertices of polygon.  Thanks.

    Picture illustrates that definition order is important for LDShapes.AddPolygon()


    Nonki Takahashi

    • Marked as answer by Zock77Editor Tuesday, May 7, 2013 4:51 PM
    Tuesday, May 7, 2013 1:40 AM
  • Wow! Neat!

    So I can switch them in a For loop and it doesn't matter.

    For i = 1 To 10
      Points[i]["X"] = (i * 10)
      Points[i]["Y"] = 0
    EndFor
    
    For i = 11 To 20
      Points[i]["X"] = 100
      Points[i]["Y"] = (i - 10) * 10
    EndFor
    
    
      LDShapes.AddPolygon(Points)

    --^-- This works as well as this --v--

    For i = 11 To 20
      Points[i]["X"] = 100
      Points[i]["Y"] = (i - 10) * 10
    EndFor
    
    For i = 1 To 10
      Points[i]["X"] = (i * 10)
      Points[i]["Y"] = 0
    EndFor
    
      LDShapes.AddPolygon(Points)


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Tuesday, May 7, 2013 4:51 PM
    Answerer
  • The reason is that the indices in SmallBasic are not assumed to be integers and so Points[1]["X"] isn't necessarily the first point, the user could have indexed from 0 i.e. Points[0]["X"] or in fact any other way, even Points["A"][1].  The second index also behaves the same, so the X value for each point should be defined first.  This could cause problems or at least severe confusion!

    What it does is equivalent to Array.GetAllIndices so it is the order they are created that counts.

    Well done for spotting and Nonki for correctly diagnosing.

    • Edited by litdev Tuesday, May 7, 2013 6:34 PM
    Tuesday, May 7, 2013 6:12 PM
  • So I made a gear here, but it won't work in the physics extension (It adds it but it won't move). any idea why?

    Points2 = "1=X\=109\;Y\=0\;;2=X\=116\;Y\=24\;;3=X\=132\;Y\=30\;;4=X\=152\;Y\=15\;;5=X\=168\;Y\=25\;;6=X\=168\;Y\=25\;;7=X\=159\;Y\=50\;;8=X\=169\;Y\=63\;;9=X\=193\;Y\=62\;;10=X\=200\;Y\=77\;;11=X\=178\;Y\=94\;;12=X\=178\;Y\=106\;;13=X\=197\;Y\=120\;;14=X\=194\;Y\=137\;;15=X\=168\;Y\=138\;;16=X\=159\;Y\=152\;;17=X\=167\;Y\=175\;;18=X\=152\;Y\=185\;;19=X\=132\;Y\=171\;;20=X\=116\;Y\=175\;;21=X\=108\;Y\=198\;;22=X\=90\;Y\=199\;;23=X\=82\;Y\=176\;;24=X\=68\;Y\=170\;;25=X\=47\;Y\=184\;;26=X\=33\;Y\=174\;;27=X\=40\;Y\=151\;;28=X\=32\;Y\=139\;;29=X\=7\;Y\=138\;;30=X\=0\;Y\=121\;;31=X\=21\;Y\=108\;;32=X\=22\;Y\=93\;;33=X\=1\;Y\=78\;;34=X\=6\;Y\=61\;;35=X\=30\;Y\=62\;;36=X\=40\;Y\=50\;;37=X\=33\;Y\=25\;;38=X\=48\;Y\=15\;;39=X\=65\;Y\=28\;;40=X\=83\;Y\=25\;;41=X\=91\;Y\=1\;;42=X\=108\;Y\=2\;;" For i = 1 To 42 Points[i]["X"] = Points2[i]["X"] / 3 Points[i]["Y"] = Points2[i]["Y"] / 3 EndFor

    For i = 1 To 10
      polygon = LDShapes.AddPolygon(points)
      LDPhysics.AddMovingShape(polygon,0.5,0.8,1)
      LDPhysics.SetPosition(polygon,60*i,100,0)
      LDPhysics.SetTorque(polygon,100*LDPhysics.GetInertia(polygon))
    EndFor

    While ("True")
      LDPhysics.DoTimestep()
      Program.Delay(20)
    EndWhile

    --^-- this doesn't work,

    points[0]["X"] = 0
    points[0]["Y"] = 0
    points[1]["X"] = 40
    points[1]["Y"] = 0
    points[2]["X"] = 60
    points[2]["Y"] = 40
    points[3]["X"] = 40
    points[3]["Y"] = 80
    points[4]["X"] = 0
    points[4]["Y"] = 20
    
    For i = 1 To 10
      polygon = LDShapes.AddPolygon(points)
      LDPhysics.AddMovingShape(polygon,0.5,0.8,1)
      LDPhysics.SetPosition(polygon,60*i,100,0)
      LDPhysics.SetTorque(polygon,100*LDPhysics.GetInertia(polygon))
    EndFor
    
    While ("True")
      LDPhysics.DoTimestep()
      Program.Delay(20)
    EndWhile

    --^-- this does.


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11


    Tuesday, May 7, 2013 7:23 PM
    Answerer
  • 2 Points;

    1] It turns out there is an internal limit on the number of vertices on a primitive shape in Box2D - this is 8.  Probably for a good reason.

    2] The primitive shapes should also be convex.

    My guess is the best approach is to create and group several primitive shapes into one compound shape using LDPhysics.GroupShapes.

    Tuesday, May 7, 2013 8:02 PM
  • If you are still looking at this I had to make a change to handle polygons when they are not created at the origin (0,0).  A modified version of the extension is uploaded and here is a simple cog test, import QJR853.

    Also added a 2 cog turning each other sample with the download.


    • Edited by litdev Wednesday, May 8, 2013 9:11 PM
    Wednesday, May 8, 2013 8:17 PM