Hi,
I'm trying to combine tiledmaps generated with Tiled with a BOX2D world. I'd like to generate the ground polylines within Tiled and load the polylines from the object layer by code.
The goal is to render this and together with the map but via 2 render methods. I;m not sure if the thing i'd like to achieve is even possible.
If it is possible it will become very easy to set the boundaries of the level and applying physics additional generated objects.
I have the following code to load the level:
I know the code is somewhat messed up, this is because i'm just testing and learning capabilities of libgdx. Feel free to comment on better performance or methods.
The error i'm currently gettign is:
I'm trying to combine tiledmaps generated with Tiled with a BOX2D world. I'd like to generate the ground polylines within Tiled and load the polylines from the object layer by code.
The goal is to render this and together with the map but via 2 render methods. I;m not sure if the thing i'd like to achieve is even possible.
If it is possible it will become very easy to set the boundaries of the level and applying physics additional generated objects.
I have the following code to load the level:
B4X:
'Class module
Sub Class_Globals
Private Maps As lgMapTiledMap
Private MapRenderer As lgMapOrthogonalTiledMapRenderer
Dim Box2Drenderer As lgBox2DDebugRenderer
Dim CurrentLayer As lgMapTiledMapLayer
Dim MapWidth, MapHeight As Int 'in tiles
Dim TileSize As Float 'in pixels
Dim World As lgBox2DWorld
'Dim WorldGround As lgMapPolylineMapObject
Dim MapObject As lgMapObject
Dim bdyGround As lgBox2DBody
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Filename As String, Width As Int, Height As Int, GutterSize As Float)
'Create the Box2D physics world
Log("Begin map loading")
Dim vGravity As lgMathVector2
World.Initialize(vGravity.set(0, -10), True, "Box2D")
World.SetContinuousPhysics(True)
World.SetWarmStarting(True)
loadmap(Filename)
MapWidth = Maps.Properties.get("width")
MapHeight = Maps.Properties.get("height")
Log ("MapWidth:" & MapWidth)
Log ("MapHeight:" & MapHeight)
Dim TileWidth As Int = 20
Dim TileHeight As Int = 20
Dim RatioW As Float = Width / MapWidth
Dim RatioH As Float = Height / MapHeight
'Log("ratioW : "& RatioW )
Log("ratioh : "& RatioH )
If RatioW < RatioH Then
MapRenderer.Initialize3(Maps, RatioH )
TileSize = MapRenderer.UnitScale * TileWidth
Else
MapRenderer.Initialize3(Maps, RatioW)
TileSize = MapRenderer.UnitScale * TileHeight
End If
Log("map loaded... now creating ground")
Create_ground
End Sub
'Each layer is a different maze
Public Sub SelectLayer(Layer As Int)
CurrentLayer = Maps.Layers.get(Layer)
End Sub
Public Sub FromTileToPixel_X(TileX As Int) As Int
Return Round((TileX - 0.5) * TileSize)
End Sub
Public Sub FromTileToPixel_Y(TileY As Int) As Int
Return Round((TileY - 0.5) * TileSize)
End Sub
Public Sub FromTileToPixel_InvertedY(TileY As Int) As Int
Return Round((MapHeight - 1.5 - TileY) * TileSize)
End Sub
Public Sub FromPixelToTile_X(PixelX As Int) As Int
Return Round((PixelX / TileSize) + 0.5)
End Sub
Public Sub FromPixelToTile_Y(PixelY As Int) As Int
Return Round((PixelY / TileSize) + 0.5)
End Sub
'Gets the tile at the given coordinates
Public Sub GetTile(TileX As Int, TileY As Int) As lgMapStaticTiledMapTile
Dim Cell As lgMapTiledMapLayerCell = CurrentLayer.GetCell(TileX, TileY)
If Cell = Null Then
Return Null
Else
Return Cell.Tile
End If
End Sub
Private Sub loadmap(Filename As String)
'Loads the mazes (TMX format)
Dim TMXLoader As lgMapTmxMapLoader
Maps = TMXLoader.Initialize2("maps/"& Filename )
End Sub
Public Sub Reload
Maps.Dispose
loadmap("naamloos.tmx")
If MapRenderer.IsInitialized Then
MapRenderer.Map= Maps
Else
MapRenderer.Initialize(Maps)
End If
End Sub
'Draws the current layer
Public Sub Draw(Camera As lgOrthographicCamera)
World.Step(1/60, 8, 3)
MapRenderer.SetCameraView(Camera)
MapRenderer.render()
Box2Drenderer.render(World.InternalObject,Camera.Combined)
End Sub
Public Sub Dispose
Maps.Dispose
MapRenderer.Dispose
End Sub
Public Sub Create_ground
'get the mapground polyline from the maps and transpose to a fixture definition
Dim WorldGround As lgMapPolylineMapObject = Maps.Layers.Get2("ObjTest").Objects.Get2("MapGround")
Dim eshape As lgBox2DPolygonShape
eshape.Set2(WorldGround.Polyline.TransformedVertices)
Dim fd As lgBox2DFixtureDef
'now we have the fixture
fd.shape = eshape
Dim bd As lgBox2DBodyDef
bdyGround = World.CreateBody(bd)
bdyGround.createFixture(fd)
eshape.dispose
bdyGround.Position.Set(WorldGround.Polyline.OriginX,WorldGround.Polyline.OriginY)
End Sub
Sub Box2D_BeginContact(Contact As lgBox2DContact)
'The ground is discarded
If Contact.FixtureA.Body <> bdyGround AND Contact.FixtureB.Body <> bdyGround Then
'The Contact instance is going to be reused, so its data have to be copied
Log("BeginContact")
End If
End Sub
I know the code is somewhat messed up, this is because i'm just testing and learning capabilities of libgdx. Feel free to comment on better performance or methods.
The error i'm currently gettign is:
B4X:
OGL version: OpenGL ES 2.0 2184622
OGL extensions: GL_AMD_compressed_ATC_texture GL_AMD_performance_monitor GL_AMD_program_binary_Z400 GL_EXT_texture_filter_anisotropic GL_EXT_texture_format_BGRA8888 GL_EXT_texture_type_2_10_10_10_REV GL_NV_fence GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_depth24 GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_fragment_precision_high GL_OES_get_program_binary GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_3D GL_OES_texture_float GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_texture_npot GL_OES_vertex_half_float GL_OES_vertex_type_10_10_10_2 GL_OES_vertex_array_object GL_QCOM_alpha_test GL_QCOM_binning_control GL_QCOM_driver_control GL_QCOM_perfmon_global_mode GL_QCOM_extended_get GL_QCOM_extended_get2 GL_QCOM_tiled_rendering GL_QCOM_writeonly_rendering GL_AMD_compressed_3DC_texture
framebuffer: (5, 6, 5, 0)
depthbuffer: (16)
stencilbuffer: (0)
samples: (0)
coverage sampling: (false)
Managed meshes/app: { }
Managed textures/app: { }
Managed shaders/app: { }
Managed buffers/app: { }
Begin map loading
MapWidth:3000
MapHeight:600
ratioh : 1.3333333730697632
map loaded... now creating ground
Width in Resize= 480
Height in Resize= 800
java.lang.NullPointerException
at com.badlogic.gdx.physics.box2d.Box2DDebugRenderer.render(SourceFile:94)
at flm.b4a.libgdxtest.level2._draw(level2.java:119)
at flm.b4a.libgdxtest.main._lg_render(main.java:437)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.libgdx.LibGDX$b.render(SourceFile:118)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(SourceFile:438)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1470)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1224)
java.lang.NullPointerException
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Dialog.<init>(Dialog.java:127)
at android.app.AlertDialog.<init>(AlertDialog.java:114)
at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
at anywheresoftware.b4a.BA.ShowErrorMsgbox(BA.java:222)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:202)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.libgdx.LibGDX$b.render(SourceFile:118)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(SourceFile:438)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1470)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1224)
** Activity (main) Pause, UserClosed = true **
waiting for pause synchronization took too long; assuming deadlock and killing
Attachments
Last edited: