Games [XUI2D] AsyncDrawTasks and BCPath

Discussion in 'Game Development' started by Gunther, Nov 8, 2018 at 7:18 PM.

  1. Gunther

    Gunther Active Member Licensed User

    Hi,

    we can add a async drawing task with adding a path and than adding the final path as clone to the task list with:
    Code:
    myPath.Initialize(p1.X, p1.Y)
    myPath.LineTo(p2.X,p2.Y).LineTo(p3.X,p3.Y).LineTo(p1.X, p1.Y)

    GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(myPath.Clone, BrushLineColor, 
    True1))
    Well, how we can be sure that the path can be safely reused (and so its clone) or the drawtask is finished?

    Can we remove that draw task from the draw task list(= that special one, do we have a reference to the added the task to be found in the list?) assuming that there will be some more draw tasks on that list added?

    Thanks.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Once you cloned the path, the original path can be modified immediately. The cloned path is a different path.
     
  3. Gunther

    Gunther Active Member Licensed User

    Thanks, a newly cloned path is NOT overwriting the path clone of the previous one, correct?

    Since I do have about 40 paths in the loop of 5 paths each loop and sometimes I am getting a null pointer exception at different times in the game play.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    A cloned path has no relation to the original path. Each cloned path is a completely new and unique path.
     
  5. Gunther

    Gunther Active Member Licensed User

    Ok than it is an diffrent reason for the exception.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Note that if you are not modifying the paths every loop then it is better to clone the path when you want to modify it.

    Can you post the error message?
     
  7. Gunther

    Gunther Active Member Licensed User

    They are modified every loop.

    the non-optimized code in question is:

    Code:
    Private Sub Drawitems(GS As X2GameStep, mbw As X2BodyWrapper)
     
        
    If mbw=Null Or mbw.IsVisible=False Then Return

        
    Dim Rhombus, Rhombus2, Rhombus3, Rhombus4, Rhombus5  As BCPath
        
    Dim p5, p6, p7, p8 As B2Vec2
     
        
    Dim f As B2Fixture = mbw.Body.FirstFixture

        
    Dim HalbBreite As Float = mbw.X2.GetShapeWidthAndHeight(f.Shape).X/2.
        
    Dim HalbHohe As Float =  mbw.X2.GetShapeWidthAndHeight(f.Shape).Y/2.
     
        
    Dim p1 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X-HalbBreite, mbw.Body.Position.Y-HalbHohe)
        
    Dim p2 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X+HalbBreite, mbw.Body.Position.Y-HalbHohe)
        
    Dim p3 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X+HalbBreite, mbw.Body.Position.Y+HalbHohe)
        
    Dim p4 As B2Vec2 = X2.WorldPointToMainBC(mbw.Body.Position.X-HalbBreite, mbw.Body.Position.Y+HalbHohe)
        
    '
        '
        'Punkte definition:

        
    ' calculating a small offset
        Punkte(p1, p5)
        Punkte(p2, p6)
        Punkte(p3, p7)
        Punkte(p4, p8)

        
    ' top
        If p3.Y > Click.Y Then
            Rhombus.Initialize(p3.X, p3.Y).LineTo(p7.X, p7.Y).LineTo(p8.X,p8.Y).LineTo(p4.X,p4.Y)
            GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus.Clone, BrushLineColor1, 
    True0))
        
        
    End If
        
    ' bottom
        If p1.Y < Click.Y Then
            Rhombus2.Initialize(p2.X, p2.Y).LineTo(p6.X, p6.Y).LineTo(p5.X,p5.Y).LineTo(p1.X,p1.Y)
            GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus2.Clone, BrushLineColor2, 
    True0))
        
    End If
        
    ' left
        If p1.X > Click.X Then
            Rhombus3.Initialize(p4.X, p4.Y).LineTo(p8.X, p8.Y).LineTo(p5.X,p5.Y).LineTo(p1.X,p1.Y)
            GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus3.Clone, BrushLineColor3, 
    True0))
        
    End If
        
    ' right
        If p2.X < Click.X Then
            Rhombus4.Initialize(p2.X, p2.Y).LineTo(p6.X, p6.Y).LineTo(p7.X,p7.Y).LineTo(p3.X,p3.Y)
            GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus4.Clone, BrushLineColor4, 
    True0))
        
    End If
    '    ' back
    '    Rhombus5.Initialize(p1.X, p1.Y).LineTo(p2.X, p2.Y).LineTo(p3.X,p3.Y).LineTo(p4.X,p4.Y)
    '    GS.DrawingTasks.Add(X2.MainBC.AsyncDrawPath(Rhombus5.Clone, BrushLineColor5, True, 0))
     
     
    End Sub
    And the error log:

    Code:
    0
    0
    0
    1
    2
    3
    5
    6
    7
    8
    10
    12
    13
    14
    java.lang.NegativeArraySizeException
        at b4j.example.bitmapcreator._createcolumn(bitmapcreator.java:
    677)
        at b4j.example.bitmapcreator._drawfullpath(bitmapcreator.java:
    2012)
        at b4j.example.bitmapcreator._drawpath2(bitmapcreator.java:
    2187)
        at b4j.example.bitmapcreator._drawcanvasasync(bitmapcreator.java:
    1406)
        at b4j.example.bitmapcreator$
    1.run(bitmapcreator.java:4566)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:
    511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:
    266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:
    1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:
    624)
        at java.lang.Thread.run(
    Thread.java:748)
    skipping frame!!!
    skipping frame!!!
    skipping frame!!!
    skipping frame!!!
    skipping frame!!!
    skipping frame!!!
    skipping frame!!!
     
    Last edited: Nov 9, 2018 at 8:14 AM
  8. Gunther

    Gunther Active Member Licensed User

    it may be that is throuwing that error when the fixtures are outside the screen but only in Y-direction.

    If they are outside the screen in X-direction but the Y-Position is in the range of the Min/Max plus a little more of the screen it works fine.

    Could also be the hight of the TiledMap behind. If the total hight of the TiledMap is visible on the screen than it works (width of the Map is 6x the hight). If the visible hight smaller than the Map than it crashes.
     
    Last edited: Nov 9, 2018 at 11:03 AM
  9. Erel

    Erel Administrator Staff Member Licensed User

    It might be a bug in BitmapCreator. Please upload the project (simplify it as much as you can).
     
  10. Gunther

    Gunther Active Member Licensed User

    let me try to shrink the bigger project (already) to the necessary ... here it is...

    In the Initialize Procedure you may change the factor behind WorldHeight to a lower one (more zoomed in) this will create an NullPointer Exception.

    Code:
    Public Sub Initialize (Parent As B4XView)
        Parent.LoadLayout(
    "1")
        world.Initialize(
    "world", world.CreateVec2(0, -20))
        X2.Initialize(Me, ivForeground, world)
        WorldHeight = 
    40 '
        WorldWidth  = WorldHeight * 1.3333
       
        X2.ConfigureDimensions(world.CreateVec2(WorldWidth / 
    2, WorldHeight / 2), WorldHeight * 1.16'<------ lower Then 1.15 will produce the error
       
        lblStats.TextColor = xui.Color_Black
        lblStats.Color = 
    0x88ffffff
        lblStats.Font = xui.CreateDefaultBoldFont(
    20)
       
       
        
    'comment to disable debug drawing
    '    X2.EnableDebugDraw
        TileMap.Initialize(X2, File.DirAssets, "tank.json"Null)
        TileMap.SetSingleTileDimensionsInMeters(WorldHeight / TileMap.TilesPerColumn, WorldHeight / TileMap.TilesPerColumn)
        TileMap.PrepareObjectsDef(ObjectLayer)
       
    '    TileMap.SetBackgroundColor(0xFF008AFF)  ' <--------------------- first error when uncommented
       
        
    Dim ol As X2ObjectsLayer = TileMap.Layers.Get(ObjectLayer)
        
    For Each template As X2TileObjectTemplate In ol.ObjectsById.Values
            
    If template.Name <> "hinge" Then 
                
    Dim bw As X2BodyWrapper = TileMap.CreateObject(template)
                
    If bw.Name = "tank" Then 
                    Tank = bw
                
    End If
            
    End If
        
    Next
       
       
        TankMovementForce = X2.CreateVec2(
    12,0)
       
        GroundLevel = X2.ScreenAABB.BottomLeft.Y 
       
        BrushLineColor1 = X2.MainBC.CreateBrushFromColor(xui.Color_LightGray)
       
        X2.Start
    End Sub
     

    Attached Files:

    Last edited: Nov 11, 2018 at 11:47 AM
  11. Erel

    Erel Administrator Staff Member Licensed User

    The issue is related to lines that are completely above or below the BC. It is fixed for the next update of BitmapCreator.

    You should avoid drawing bodies that are outside of the visible screen. Change your code to:
    Code:
    If bw.IsVisible And bw.Name.Contains("block"Then
    This will probably prevent this issue.
     
  12. Gunther

    Gunther Active Member Licensed User

    Unfortunately, the project logic asks for bodies with are outside and not visible. This bodies edge points are start or end points of the drawn lines.

    Is this issue only related to lines which are 100% out of BC?

    If the Y-Direction behavious like the X-Direction then it is fine.

    So, I need to wait for the update.
     
    Last edited: Nov 11, 2018 at 4:31 PM
  13. Erel

    Erel Administrator Staff Member Licensed User

    There is no reason to draw a body that is fully invisible. bw.IsVisible will return False when the body is fully invisible.
     
  14. Gunther

    Gunther Active Member Licensed User

    Well, I think yes. It looks very wirred if the extruded drawing is disappering with the box going out to the left. Or the drawing is first drawn if the box is seen on the right.

    2.7D.PNG
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Now it is clear. You can still check the bounds of the drawn area and avoid drawing paths that are completely outside.
     
    Gunther likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice