B4A Library libGDX - Game Engine

logo.png


One of the best game engines for Android is now available to B4A users. Unleash your creativity!

You can read a description of this library in this guide.

Download for B4A v10.60 and older (4 MB)
Download for B4A v10.70 and newer (4 MB)

Starting from version 1.13, the library is compiled with Java 8, and thus requires a recent version of B4A and Java 8+.
To install the library, copy the .jar file and the .xml file in your libraries folder.

This library is not compatible with the Debug mode. Ensure that you always compile in Release mode.

Download also the examples and templates.

You can reduce the weight of the library if you want to keep your APK file as small as possible. Read this post to learn how to create your Lite version.

Tutorials:
How to make games
Introduction to the libGDX library
PacDroid: from scratch to fun
Shaders

Take also a look at the Cloney Bird tutorial by andymc.

Plugins:
Box2DLights
SpecialFX

Versions:
v1.14:
- I fixed a bug when Looping was changed during the execution of a sound;
- I added the RenderImageLayerAt function to all map renderers;
- I added the support of the new packaging tool used by B4A v10.70 (this mainly fixes the problem of / in paths of internal assets);
- I updated the manifest of all examples.

v1.13:
- I fixed a bug when the device was rotated (the Timer thread, for example, was not disposed which caused a problem in lgGestureDetector);
- I added two functions for Tiled maps: renderImageLayerAt and renderTileLayerAt.

v1.12:
- I added the .so libraries for 64 bits CPU;
- I added the PinchStop event to lgGestureDetector;
- I added the Orientation property to lgBox2DTransform;
- I fixed minor bugs.

v1.11:
- I fixed several bugs;
- I removed the unwanted delay after a change of screen in the Render event;
- I added the lgAsyncExecutor and lgAsyncTask classes;
- I added the missing file for the Progression demo;
- I updated the Freetype library (.so).

v1.10:
- I fixed several bugs;
- I added the support of expansion files: classes lgZipResourceFile and lgZipEntry, functions lgFiles.SetAPKExpansion and lgFiles.ExpansionFile (untested);
- I added a HashCode function to all lg...Array classes;
- I added a dozen of predefined colors to lgColor;
- I added the "raw_y" property to lgMapObjects;
- I added the grow, growX and growY functions to lgScn2DTableCell;
- I added the lerpAngle and lerpAngleDeg functions to lgMathUtils;
- I improved Atan2 in lgMathUtils;
- the collision objects of TMX tiles are now accessible via the Objects property of lgMapAnimatedTiledMapTile and lgMapStaticTiledMapTile;
- I added two new examples: Progression and Map_Box2D;
- I updated the manifest of most examples and templates.

v1.09:
- I fixed bugs, notably a major bug that crashed sometimes the game in the Render event handler when the current screen is changed;
- I added getColumnWidth and getRowHeight to lgScn2DTable and all classes that inherit from lgScn2DTable;
- I added the defaults function to lgScn2DTableCell;
- TMX files are loaded faster.

1.08:
- I fixed bugs;
- I added the lgFontGeneratorParameters class;
- I added CreateFontWithParam to lgFontGenerator;
- lgFontGenerator can add now a border and a shadow to the generated fonts (via CreateFontWithParam);
- I added Initialize2, LabelAlign and LineAlign to lgScn2DLabel;
- I added SetStateWithoutEvent to lgScn2DButton and lgScn2DTextButton;
- I added TitleLabel and TitleTable to lgScn2DWindow (this deprecates ButtonTable, Title, TitleWidth and TitleAlignment);
- I added getIndex to lgMapLayers and lgMapObjects;
- I added Target to actions of Scene2D;
- I removed Print from lgScn2DGroup (use Log(myGroup) instead);
- lgMapTiledMapImageLayer uses now float values for position;
- lgNinePatch uses now float values for paddings;
- lgPixmapPacker and lgPixmapPackerPage were rewritten (the many changes will break your existing code).

1.07:
- I fixed a few bugs, as usual;
- I added InitializeTransparentView to LibGDX;
- I added the lgKTXTextureData class to support the KTX and ZKTX file formats;
- I added a new demo: Format_KTX;
- I added argb8888 and argb8888ToColor to lgColor
- I added the lgMapAtlasTmxMapLoaderParameters class;
- I added Initialize3 and Load2 to lgMapAtlasTmxMapLoader;
- I added LoadExternal and IsInitialized to lgMapTmxMapLoader;
- I added FrameTiles to lgMapAnimatedTiledMapTile;
- I added a Tag property to lgSprite;
- I added hasOppositeDirection, hasSameDirection, isCollinear, isOnLine, isPerpendicular, AngleRad and rotateRad to lgMathVector2;
- I added rotateRad to lgMathMatrix4;
- I added clamp4, log, log2, Randomize, randomInt, randomInt2, randomSign, randomTriangular and randomTriangular2 to lgMathUtils;
- the random number generator of lgMathUtils uses now the xorshift128+ algorithm;
- I renamed Create2 to Combine in lgMesh;
- I removed the Create and Create3 functions from lgMesh;
- I removed the deprecated Color property from lgVertexAttributesUsage;
- it is possible now to change the KeyFrames array of lgAnimation after its initialization.

v1.06:
- I fixed a few bugs;
- I added a Texture property to lgSprite;
- I added the MotorJoint type to lgBox2DWorld;
- I added GetFixture to lgBox2DBody;
- I added Initialize3 to lgShapeRenderer to allow the use of custom shader programs;
- I added ScaleEffect to lgParticleEffect;
- I added CleanUpBlendFunction to lgParticleEmitter;
- I added scl2 to lgMathVector2;
- I added IsEnabled to lgGLProfiler;
- I added Percent, VisualPercent and VisualInterpolation to lgScn2DProgressBar;
- I added Percent, VisualPercent, VisualInterpolation and VisualInterpolationInverse to lgScn2DSlider;
- I added isLeftEdge, isRightEdge, isTopEdge, isBottomEdge to lgScn2DScrollPane;
- The Reset function of lgScn2DTableCell sets now all constraints to their default values;
- I modified the Map_Hexagonal example to display the coordinates of the touched hex.

v1.05:
- I fixed two bugs;
- the latest TMX format of Tiled Map Editor is now properly decoded (with support of animated tiles and image layers);
- I added the RenderImageLayer function to the map renderers;
- I added the lgMapTiledMapImageLayer class for the image layer of tiled maps;
- I added the OffsetX and OffsetY properties to lgMapAnimatedTiledMapTile and lgMapStaticTiledMapTile;
- I added the CurrentFrame and CurrentFrameIndex properties to lgMapAnimatedTiledMapTile;
- I added the FinishLoadingAsset function to lgAssetManager;
- I added the SetAlpha function to lgBitmapFontCache;
- I added FixtureCount and GetAllFixtures to lgBox2DWorld;
- I added the ResetOnTouchUp property to lgTouchpad;
- I removed Scale and Div from lgMathVector3 because these functions were deprecated.

v1.04:
- I fixed various bugs, notably a bug affecting the Backspace key on the virtual keyboard of some devices;
- I added the missing OpenGL functions in lgGL;
- I added the KeepWithinStage function to lgDragAndDrop;
- I added the dampingRatio and frequencyHz fields to lgBox2DWeldJointDef;
- I added the DampingRatio and Frequency properties to lgBox2DWeldJoint;
- I added the Line6 function to lgShapeRenderer;
- I added VisualScrollPercentX, VisualScrollPercentY and FlickScrollTapSquareSize to lgScn2DScrollPane;
- the LineHeight property of BitmapFonts can be modified;
- I updated the .so files;
- I added an example: Sensors.

v1.03:
- I fixed various bugs;
- I added the VertexColor property to lgPolygonSprite;
- I added the Equals function to lgArray;
- I renamed Ellipse to Ellipsis in lgScn2DLabel;
- I improved the code of lgPixmapIO (less memory used, PNG with compression);
- I fixed a problem with the fragment shaders of two examples.

v1.02:
- I fixed 3 bugs in lgScn2dTable and a few others in other classes;
- I added 4 examples using shaders;
- I removed the lgDefaultGroupStrategy class because it was useless and changed the initialization of lgDecal objects;
- I added the SetRotation function to lgDecal;
- I added the Tint function to lgBitmapFontCache;
- I added the Cancel and Reset functions to lgGestureDetector;
- I added the IsKeyJustPressed function to lgInput;
- I added the Colinear and EnsureCCW functions to lgMathGeometryUtils;
- I added the Equals function to lgMathGridPoint2, lgMathGridPoint3 and lgMathRay;
- I added the IntersectRayRay and DistanceLinePoint2 functions to lgMathIntersector;
- I added the Extract4x3Matrix function to lgMathMatrix4;
- I added the WindowSize property to lgMathWindowedMean;
- I added the IsDrawing function to lgShapeRenderer;
- I added the ColorUnpacked field to lgVertexAttributesUsage;
- I added the Clear function to lgScn2DButtonGroup;
- I added the CancelTouchFocus property to lgScn2DDragAndDrop;
- I added the Rows, Columns, BackgroundTop, BackgroundLeft, BackgroundBottom, and BackgroundRight properties to lgScn2DTable;
- I added the ClearActor function to lgScn2dTableCell;
- I added the ActionsRequestRendering property to lgScn2DStage;
- I renamed CancelTouchFocus2 to CancelTouchFocusExcept in lgScn2DStage;
- I removed the Clear function from lgScn2dTableCell;
- The color markups are now supported by lgScn2dLabel and lgScn2dWindow (title).

v1.01:
- I fixed a handful of minor bugs and a big bug affecting the screen events;
- I added IsDrawing in lgSpriteBatch.

v1.0:
- I fixed bugs in various classes;
- I rewrote big chunks of internal graphics classes;
- the TMX map loaders are now able to read the Rotation property;
- the FrameDuration property of lgAnimation can now be changed during the animation;
- I added an Initialize function to lgMapAnimatedTiledMapTile to be able to set dynamically an animated tile;
- I added two new classes for debugging: lgGLProfiler and lgScn2DDebugRenderer;
- I added DebuggingEnabled and DebugColor to all actors of Scene2D;
- I added ChildrenDebuggingEnabled to all groups of Scene2D;
- I added CellDebugColor to lgScn2DTable and removed all its debug functions;
- I added SetCenterPosition, CenterX and CenterY to all actors of Scene2D;
- I replaced the Widget word by Actor in lgScn2DTableCell;
- I added the Equals and HashCode functions to a few lgMath classes;
- I added the ApproxLength function to lgMathBezier, lgMathBSpline and lgMathCatmullRomSpline;
- I added Set3, SetPosition2, Contains2, Circumference and Area to lgMathCircle;
- I added Set3, SetPosition2, Circumference and Area to lgMathEllipse;
- I added Merge2, Area and Perimeter to lgMathRectangle;
- I added Len and Len2 to lgMathSegment;
- I added Volume and SurfaceArea to lgMathSphere;
- I added Interpolate to lgMathVector2 and lgMathVector3;
- I added the following functions to lgMathMatrix4: mulLeft, Set2, Set3, Set4, Set5, Set6, ScaleX, ScaleY, ScaleZ, ScaleXSquared, ScaleYSquared, ScaleZSquared;
- I added a LoadMipMap event and replaced UseHardwareMipMap by Get/SetGenerationMethod in lgMipMapGenerator;
- Box2D has now its own .so library;
- I added Separations to lgBox2DWorldManifold;
- I added the following functions to lgBox2DEdgeShape: Vertex0, SetVertex0, SetVertex0_xy, HasVertex0, Vertex3, SetVertex3, SetVertex3_xy, HasVertex3;
- I added LocalAxisA to lgBox2DPrismaticJoint;
- I added two new classes to Box2D: lgBox2DMotorJoint and lgBox2DMotorJointDef;
- I added the LoadTextureWithParam function to lgAssetManager and the related lgTextureLoaderParam class;
- I added two new properties (Normalized, Type) and a function (HashCode) to lgVertexAttribute;
- I renamed Color to ColorPacked in lgVertexAttribute;
- I renamed Loop to Repeat in lgSound;
- I renamed consumeCompressedData to consumeCustomData in all TextureData classes;
- I protected some important classes against a repeated initialization to avoid memory leaks and side effects;
- I added a Scene2D_EventsHierarchy demo and modified the following examples: BitmapFont, FrameBuffer, Map_tIDE and Scene2D_Table.

v0.99:
- I fixed numerous bugs, especially in Scene2D where the listeners did not work as expected and actors were not fully re-initialized when Initialize was called again;
- I set to Null the internal objects of classes after a call to Dispose;
- I added the following functions:
+ IsInitialized where it was still missing;​
+ Move to lgArray;​
+ ComputeScaleForPixelHeight to lgBitmapFont;​
+ SetNameForColor, GetColorByName and SetAlpha to lgColor;​
+ Clear to lgMapLayers;​
+ Lerp to lgMathUtils;​
+ Load3 and LoadEmitterImages3 to lgParticleEffect;​
+ SetString to lgScn2DLabel;​
+ Initialize4 to lgScn2DSpriteDrawable, lgScn2DTextureRegionDrawable and lgScn2DTiledDrawable;​
+ UpdateMatrices to lgShapeRenderer;​
+ SetCenterX, SetCenterY and SetCenter to lgSprite;​
- I added a ChildrenCount property to lgScn2DGroup;
- I added a Draw event to lgScn2DImage and lgScn2DLabel;
- I added a Disabled property to lgScn2DScrollPane;
- I added the following predefined colors to lgColor: Olive, Purple, Maroon, Teal, Navy;
- I renamed the alpha function to to255 in lgColor;
- lgBitmapFont and lgBitmapFontCache now support in-string colored text through a simple markup language (see the demo);
- I added the MarkupEnabled property to lgBitmapFont;
- I added a BitmapFont demo;
- I modified the FrameBuffer demo;
- I removed the yUp and yDown parameters from the tiled map loaders and renderers (yUp is now always true);
- I removed Rotation and rotateBy from lgScn2DLabel, lgScn2DList, lgScn2DSelectBox and lgScn2DTouchpad because they were not effective.

v0.98:
- I fixed a bug affecting the scaling and rotation of lgScn2DImage;
- I fixed a bug in lgBox2DParticleEmitter;
- I fixed a bug in lgScn2DButton;
- I added the lgFrameBuffer class and a FrameBuffer demo;
- I added the IsInitialized function where it was missing;
- I added the PolygonRegion type to lgAssetManager;
- I added a Payload parameter to the SrcDragStop event of lgScn2DDragAndDrop;
- I added the Draw2 function to lgScn2DSpriteDrawable;
- I added the Set3 function to lgMathMatrix3.

v0.97:
- I fixed a few bugs (the immersive mode works now as expected);
- I found a solution for the repetition of tiles, so I added two properties (RepeatX, RepeatY) to lgMapTiledMapLayer and I modified the Map demos.
- I added an IsInitialized function to 15 classes;
- I added a RemoveRange function to all the lgArray classes;
- I added the Fling function to lgScn2DScrollPane;
- I added the IsVisualPressed function and the VisualPressedDuration property to lgScn2DClickListener.

v0.96: Major update!
In this version, I:
- removed all the code supporting OpenGL ES 1.x
- fixed a few bugs
- added new classes for decals (2D objects in 3D space): lgDecal, lgDecalBatch, lgCameraGroupStrategy, lgDefaultGroupStrategy, lgSimpleOrthoGroupStrategy
- added an example for decals
- removed the Mesh example as it cannot work with OpenGL 2 without being rewritten
- added the lgScn2DProgressBar class and modified the Scene2D_TextFieldsAndSliders example to show how to use it
- added the IsInitialized function to a dozen of classes
- added these functions:
lgBox2DWheelJoint: isMotorEnabled
lgFontGenerator: CreateFont5, CreateFont6 (for mip maps and filters)
lgMathGeometryUtils: polygonArea, triangleArea
lgMathMatrix3: getScale, getTranslation, setToRotation2, setToRotation3, setToRotationRad, rotateRad
lgMathMatrix4: det3x3, setToRotationRad, setTranslation
lgMathUtils: isEqual, isEqual2, isZero, isZero2
lgMesh: CalculateRadius, CalculateRadius2, CalculateRadiusSquared
lgScn2DDragAndDrop: SetTouchOffset
lgSprite: SetOriginCenter
- added these properties:
lgBox2DJoint: CollideConnected
lgBox2DDistanceJoint, lgBox2DFrictionJoint, lgBox2DRopeJoint, lgBox2DWeldJoint: LocalAnchorA, LocalAnchorB
lgBox2DGearJoint: Joint1, Joint2
lgBox2DPrismaticJoint: LocalAnchorA, LocalAnchorB, MaxMotorForce, ReferenceAngle
lgBox2DWheelJoint: LocalAnchorA, LocalAnchorB, LocalAxisA
lgMathMatrix3: Rotation, RotationRad
lgScn2DActor: isTouchable
lgScn2DScrollPane: ScrollHeight, ScrollWidth, VariableSizeKnobs
lgScn2DWindow: ButtonTable, Resizable, ResizeBorder, TitleWidth
- renamed the functions Translate -> MoveBy, Rotate -> RotateBy and Size -> SizeBy of the lgScn2DActor class
- removed toScreenCoordinates from lgStage and toWindowCoordinates from lgScissorStack
- removed the yDown parameter from ComputePolygon and ComputePolygon2 in the lgMathConvexHull class
- improved a bit the help text of a few functions

v0.95:
Bugfixes and minor improvements
New classes: lgFloatArray, lgIntArray, lgShortArray
The lgMathConvexHull and lgMathEarClippingTriangulator classes are now usable
New functions:
- lgPerspectiveCamera: GetPickRay
- lgScn2DStage: SetViewport2
- lgMathFrustum: pointInFrustum2, sphereInFrustum2, sphereInFrustumWithoutNearFar2
- lgBox2DChainShape: createLoop2
- lgMathIntersector: intersectSegments2
- lgMathVector2: isUnit, isUnit2, isZero, isZero2
- lgMathVector3: set2, isUnit2, isZero2
New configuration setting: useImmersiveMode (for Kitkat)

v0.94:
Bugfixes (including a serious bug with the listeners of Scene2D)
New classes: lgMapTmxMapLoaderParameters and lgMipMapGenerator
New functions:
- lgAnimation: KeyFrames
- lgAssetManager: LoadTMXWithParam
- lgBitmapFontCache: SetColors, SetColors2, SetColorsRGBA
- lgGestureDetector: Initialize2
- lgMapTmxMapLoader: Initialize3, Load2
- lgMathVector2: Rotate90
- lgMesh: UpdateVertices, UpdateVertices2
- lgMusic: Duration, Position
- lgScn2DClickListener: inTapSquare2
- lgScn2DList: ItemHeight
- lgShapeRenderer: RectLine
- lgSocket: RemoteAddress
- lgSprite: Alpha, SetFlip
New parameters: ListBox and ScrollPane styles in lgScn2DSelectBoxStyle.Initialize
New example: Filters
Modified example: Map_tIDE (I fixed the visual issues)
New configuration setting: disableAudio

v0.93:
Bugfixes (including the problem reported for the Get...List functions of Box2D)
Class rewritten: lgTexture (some functions have been added, some have been removed)
New configuration setting: useGLSurfaceViewAPI18 (for Gingerbread and older versions)
New functions: lgFileHandle.equals, lgFileHandle.hashCode, lgShapeRenderer.Triangle2
New class: lgPerspectiveCamera (for 3D)
New example: ShapeRenderer
Modified example: Perf_Skaters (the background scrolls endlessly now)

0.92:
Minor bugfixes
Internal changes for texture handling
Viewport parameters added to CalculateScissors in lgScissorStack
New function in lgScn2DScrollPane: setScrollBarPositions
New function in lgMathVector2: epsilonEquals2
New function in lgMathIntersector: intersectRectangles
Initialize3 and FindDirectionForIsoView functions removed from lgOrthographicCamera

0.91:
Bugfixes (including the problem reported for tIDE properties)
New event for gesture detector: PanStop
New Initialize function for Bitmapfont to allow to flip the font
New Rect functions for the ShapeRenderer
New class: lgMapIsometricStaggeredMapRenderer (for staggered isometric maps)
New function for lgScn2dScrollPane: Cancel
lgMathBezier and lgMathBresenham2 are now fully usable
New functions in lgMathIntersector
A few internal changes to improve performance and stability

0.9:
Bugfixes
New class: lgArray (ordered array with an initial capacity)
Many Lists have been replaced by lgArray for a better performance and more possibilities
Improved documentation (mainly in lgMath and lgSprite classes)
lgMathBSpline and lgMathCatmullRomSpline are now fully usable
New example: SplinePath
New function for particle effects and particle emitters: BoundingBox
New property for lgScn2DLabel: Ellipse

0.8:
First release
 
Last edited by a moderator:

Informatix

Expert
Licensed User
Longtime User
Thank you!!! I was looking for that info!

Another question if I may... Is there any way to limit the speed of the libGDX? Not the animation speed, but the engine speed itself.
Although my project runs quite well on all emulators and devices I tested so far, I'm having a hard time with an HTC M7, it runs too fast.

I managed to create a "speed limit" myself but I'm not entirely happy with the results.
The OpenGL engine runs as fast as the hardware can display the rendering. You cannot change that. It's usually about 60 fps.
If the objects move too fast in your game, then you probably chose a wrong unit for their move. All moves in a game should be expressed in dips/second (or in %xy/second). If you want for example that an alien ship moves horizontally at 200 dips/second, you increase its X position in the Render event with the following computation: lGdx.Graphics.DeltaTime * 200 * Density. The ship will move exactly at the same speed on all devices.
 

wonder

Expert
Licensed User
Longtime User
Thanks for the tip!! I will try to implement it in that manner.

Right now I have something like:
B4X:
actor_x = actor_x + actor_direction_x
actor_y = actor_y + actor_direction_y

In order to implement your method, shall I give up on my "speed limiter" and do it this way?
B4X:
actor_x = actor_x + actor_direction_x * lGdx.Graphics.DeltaTime
actor_y = actor_y + actor_direction_y * lGdx.Graphics.DeltaTime

Is this the right way?
 

Informatix

Expert
Licensed User
Longtime User
Thanks for the tip!! I will try to implement it in that manner.

Right now I have something like:
B4X:
actor_x = actor_x + actor_direction_x
actor_y = actor_y + actor_direction_y

In order to implement your method, shall I give up on my "speed limiter" and do it this way?
B4X:
actor_x = actor_x + actor_direction_x * lGdx.Graphics.DeltaTime
actor_y = actor_y + actor_direction_y * lGdx.Graphics.DeltaTime

Is this the right way?
Yes. The main advantage of this solution is that it takes into account the exact time spent to render your scene. If on a device, DeltaTime is constant at 16 ms, and on another device (slower, on slowed down by other apps) DeltaTime varies from 20 to 30ms, the object will be at the same place on the two devices after the same time span (it will move faster on the slowest device, but this increase in speed will be counterbalanced by the time elapsed between two frames).
 

notedop

Member
Licensed User
Longtime User
Hi @Informatix

I'm moving the player with box2d impulses, can the same logic be applied but then with the amount of force applied to the body?

B4X:
'============================================
'============Write code for movement=========
'============================================
'Handle the player movement based on accelerator
'Only if object is player
'Accel_Base is vector2 which is set onload of the level based on the accelerator position at the moment of load.
'ac_x and ac_y are accelerator position at the moment of draw.
'Deathzone is zone where player movement should stabilize

If IsPlayer Then
    If ac_x> (Main.Accel_Base.x +DeathZone) Then        'check if outside deathzone
        'Log("Tilting top - up")
        If (Bodie.LinearVelocity.y > (MAXIMUM_VELOCITY*-1)) OR (Bodie.LinearVelocity.y > 0) Then
            Bodie.applyLinearImpulse2(0,ACCELERATION*-1, Bodie.WorldCenter.x,Bodie.WorldCenter.y, True)
        End If
    Else If ac_x < (Main.Accel_Base.x -DeathZone) Then'check if outside deathzone
        'Log("Tilting bottom - up")
        If (Bodie.LinearVelocity.y < MAXIMUM_VELOCITY) OR (Bodie.LinearVelocity.y < 0) Then
            Bodie.applyLinearImpulse2(0,ACCELERATION, Bodie.WorldCenter.x,Bodie.WorldCenter.y, True)
        End If
    Else'within deathzone; stabilize player
    'Log("Deathzone: y velocity=" & Bodie.LinearVelocity.y )
        If Bodie.LinearVelocity.y > DAMPING Then
            Bodie.applyLinearImpulse2(0,-DAMPING, Bodie.WorldCenter.x,Bodie.WorldCenter.y, True)
        Else If Bodie.LinearVelocity.y < -DAMPING Then
            Bodie.applyLinearImpulse2(0,DAMPING, Bodie.WorldCenter.x,Bodie.WorldCenter.y, True)
        End If
    End If
End If

Would below code make sense? Adding gamespeed as global setting and including the deltatime.
B4X:
Bodie.applyLinearImpulse2(0,((ACCELERATION*-1)*gamespeed)*deltatime, Bodie.WorldCenter.x,Bodie.WorldCenter.y, True)
 

notedop

Member
Licensed User
Longtime User
http://www.iforce2d.net/b2dtut/worlds
In this example, each call to Step will advance the simulation by 1/20th of a second, so a body moving at 5m/s like in our first scene would move 5/20=0.25m in that time. The timeStep also affects how gravity gets to act on each body. You might find that things appear to fall at different accelerations depending on the time step. To make a realistic looking simulation, you will generally set the timeStep value to match the number of times per second you will be calling the world's Step() function in your game. For example in the testbed, the default framerate is 60 frames per second, so Step() is called 60 times a second with timeStep set to 1/60th of a second.

Am in correct; I should not adjust the force, but adjust the timestep to handle gamespeed on different devices as the world handles the rest?
Looking at the box2dlight example it seems that you've been doing something similair already.

B4X:
    'Settings
    Dim RAYS_PER_BALL As Int = 128
    Dim BALLSNUM As Int = 3
    Dim LIGHT_DISTANCE As Float = 16
    Dim RADIUS As Float = 1
    Dim MAX_FPS As Int = 60
    Dim MIN_FPS As Int = 30
    Dim TIME_STEP As Float = 1 / MAX_FPS
    Dim MAX_TIME_STEP As Float = 1 / MIN_FPS
    Dim VELOCITY_ITERS As Int = 6
    Dim POSITION_ITERS As Int = 2

Sub Fixed_Step(Delta As Float) As Boolean
    PhysicsTimeLeft = PhysicsTimeLeft + Delta
    If PhysicsTimeLeft > MAX_TIME_STEP Then
        PhysicsTimeLeft = MAX_TIME_STEP
    End If
    Dim Stepped As Boolean = False
    Do While PhysicsTimeLeft >= TIME_STEP
        World.Step(TIME_STEP, VELOCITY_ITERS, POSITION_ITERS)
        PhysicsTimeLeft = PhysicsTimeLeft - TIME_STEP
        Stepped = True
    Loop
    Return Stepped
End Sub

This would imply that my 'solution' does not make sense and I should leave it as is, but set a max/min frame rate instead and call the fixed_step function each render.
 

Informatix

Expert
Licensed User
Longtime User
http://www.iforce2d.net/b2dtut/worlds
In this example, each call to Step will advance the simulation by 1/20th of a second, so a body moving at 5m/s like in our first scene would move 5/20=0.25m in that time. The timeStep also affects how gravity gets to act on each body. You might find that things appear to fall at different accelerations depending on the time step. To make a realistic looking simulation, you will generally set the timeStep value to match the number of times per second you will be calling the world's Step() function in your game. For example in the testbed, the default framerate is 60 frames per second, so Step() is called 60 times a second with timeStep set to 1/60th of a second.

Am in correct; I should not adjust the force, but adjust the timestep to handle gamespeed on different devices as the world handles the rest?
Looking at the box2dlight example it seems that you've been doing something similair already.

B4X:
    'Settings
    Dim RAYS_PER_BALL As Int = 128
    Dim BALLSNUM As Int = 3
    Dim LIGHT_DISTANCE As Float = 16
    Dim RADIUS As Float = 1
    Dim MAX_FPS As Int = 60
    Dim MIN_FPS As Int = 30
    Dim TIME_STEP As Float = 1 / MAX_FPS
    Dim MAX_TIME_STEP As Float = 1 / MIN_FPS
    Dim VELOCITY_ITERS As Int = 6
    Dim POSITION_ITERS As Int = 2

Sub Fixed_Step(Delta As Float) As Boolean
    PhysicsTimeLeft = PhysicsTimeLeft + Delta
    If PhysicsTimeLeft > MAX_TIME_STEP Then
        PhysicsTimeLeft = MAX_TIME_STEP
    End If
    Dim Stepped As Boolean = False
    Do While PhysicsTimeLeft >= TIME_STEP
        World.Step(TIME_STEP, VELOCITY_ITERS, POSITION_ITERS)
        PhysicsTimeLeft = PhysicsTimeLeft - TIME_STEP
        Stepped = True
    Loop
    Return Stepped
End Sub
You found the right function. In this code, TIME_STEP is constant and an accumulator is used to determine how many steps have to be computed before being drawn.
 

notedop

Member
Licensed User
Longtime User
Okay, i've implemented above function.

I have a slow device (HTC One V) so I would expect that the player would move same speed as a fast device and instead of moving smoothly, make bigger 'jumps' each draw.
But it's not... There are still moments where I have a lot of tiles visible, which has affect on the device and the player slows down and starts to 'stagger'. The camera is following the player so the level slows down.

There is improvement but not to the full extend I would expect.
Are my expectations correct? The player should move by a greater x/y value on slow devices even if box2d forces/impulses are applied, right?

At first I thought this was due to the timing of the update functions;
I have a object class with a draw function to draw the player, bullets etc.
Within the sub Draw of the object class I call the classes Update.
I should call the Update sub of the object class in the main update sub and not via the draw sub in the class, right?

I changed this, but it does not have the expected effect...

Any other ideas?
 

Informatix

Expert
Licensed User
Longtime User
Okay, i've implemented above function.

I have a slow device (HTC One V) so I would expect that the player would move same speed as a fast device and instead of moving smoothly, make bigger 'jumps' each draw.
But it's not... There are still moments where I have a lot of tiles visible, which has affect on the device and the player slows down and starts to 'stagger'. The camera is following the player so the level slows down.

There is improvement but not to the full extend I would expect.
Are my expectations correct? The player should move by a greater x/y value on slow devices even if box2d forces/impulses are applied, right?

At first I thought this was due to the timing of the update functions;
I have a object class with a draw function to draw the player, bullets etc.
Within the sub Draw of the object class I call the classes Update.
I should call the Update sub of the object class in the main update sub and not via the draw sub in the class, right?

I changed this, but it does not have the expected effect...

Any other ideas?
In my code in post #286, I set a minimum number of FPS (MIN_FPS). If the real number of FPS is under this value, you will notice the slowing down. You can lower this value and see whether that solves your problem, but this already means that your game is probably too demanding for the device. You should improve the speed of your code above all.
 

notedop

Member
Licensed User
Longtime User
I did some tests after my post yesterday evening and indeed noticed that the framerate goes down to 9fps at a certain point. So basically the behavior i'm seeing is as expected.

I guess this leaves me with the following actions:
  1. optimize code to the full extend
  2. buy a faster phone/tablet for testing purposes
 

wonder

Expert
Licensed User
Longtime User
Hello again,

I'm totally stuck with the following situation... I don't know what to do anymore.

Everything works as expected after implementing this method:
B4X:
actor_x = actor_x + actor_direction_x * lGdx.Graphics.DeltaTime
actor_y = actor_y + actor_direction_y * lGdx.Graphics.DeltaTime


However:

When I try to apply it to the Z axis (my Z axis is later converted to X,Y coordinates)
B4X:
jump_direction(player) = initial_jump_direction(player) * lGdx.Graphics.DeltaTime
stage_gravity(active_stage) = initial_stage_gravity(active_stage) * lGdx.Graphics.DeltaTime
The player jumps higher on slower devices (old Sony Xperia: lgdx @ 44 FPS) and jumps lower on faster devices (HTC M7: lgdx @ 77 FPS).


I also tried to do it this way:
B4X:
'When the jump button is pressed the actor_direction_z(player) becomes 330.
'Something like this:
jump_direction(player) =  330
If jump_btn_pressed = True Then actor_direction_z(player) = jump_direction(player)

(...)

actor_direction_z(player) = actor_direction_z(player) + stage_gravity(player)
actor_z(player) = actor_z(player) + actor_direction_z(player) * lgdx.Graphics.DeltaTime
...and it still doesn't work. The player jumps at different heights in different devices.

Is my understanding of physics wrong??

Note: Without the "z = z + dz * lGdx.graphics.DeltaTime" method, the jump height is the same on all devices.
 

Informatix

Expert
Licensed User
Longtime User
Hello again,

I'm totally stuck with the following situation... I don't know what to do anymore.

Everything works as expected after implementing this method:
B4X:
actor_x = actor_x + actor_direction_x * lGdx.Graphics.DeltaTime
actor_y = actor_y + actor_direction_y * lGdx.Graphics.DeltaTime


However:

When I try to apply it to the Z axis (my Z axis is later converted to X,Y coordinates)
B4X:
jump_direction(player) = initial_jump_direction(player) * lGdx.Graphics.DeltaTime
stage_gravity(active_stage) = initial_stage_gravity(active_stage) * lGdx.Graphics.DeltaTime
The player jumps higher on slower devices (old Sony Xperia: lgdx @ 44 FPS) and jumps lower on faster devices (HTC M7: lgdx @ 77 FPS).


I also tried to do it this way:
B4X:
'When the jump button is pressed the actor_direction_z(player) becomes 330.
'Something like this:
jump_direction(player) =  330
If jump_btn_pressed = True Then actor_direction_z(player) = jump_direction(player)

(...)

actor_direction_z(player) = actor_direction_z(player) + stage_gravity(player)
actor_z(player) = actor_z(player) + actor_direction_z(player) * lgdx.Graphics.DeltaTime
...and it still doesn't work. The player jumps at different heights in different devices.

Is my understanding of physics wrong??

Note: Without the "z = z + dz * lGdx.graphics.DeltaTime" method, the jump height is the same on all devices.
Is direction an angle? If yes, then don't add DeltaTime to it. If not, I don't understand what it is.
 

wonder

Expert
Licensed User
Longtime User
It's not an angle, it's a straight vertical direction... I'll send you the APK.
(Press X to jump)

You may press the CIRLCLE button to decrease the engine speed (simulate a slower device).
At 20 FPS (for example) the player jumps way higher than before.
 

Informatix

Expert
Licensed User
Longtime User
It's not an angle, it's a straight vertical direction... I'll send you the APK.
(Press X to jump)

You may press the CIRLCLE button to decrease the engine speed (simulate a slower device).
At 20 FPS (for example) the player jumps way higher than before.
I don't understand how you compute the jump.
You can see in the Perf_Skaters demo an example of jumping sprite (with gravity applied) that uses DeltaTime.
 
Last edited:

wonder

Expert
Licensed User
Longtime User
I basically adapted this JAVA code:

Working example HERE

B4X:
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var positionX = 100.0;
var positionY = 175.0;
var velocityX = 4.0;
var velocityY = 0.0;
var gravity = 0.5;
var onGround = false;

window.addEventListener("mousedown", StartJump, false);
window.addEventListener("mouseup", EndJump, false);

Loop();

function StartJump()
{
    if(onGround)
    {
        velocityY = -12.0;
        onGround = false;
    }
}

function EndJump()
{
    if(velocityY < -6.0)
        velocityY = -6.0;
}

function Loop()
{
    Update();
    Render();
    window.setTimeout(Loop, 33);   
}

function Update()
{
    velocityY += gravity;
    positionY += velocityY;
    positionX += velocityX;
   
    if(positionY > 175.0)
    {
        positionY = 175.0;
        velocityY = 0.0;
        onGround = true;
    }
   
    if(positionX < 10 || positionX > 190)
        velocityX *= -1;
}

function Render()
{
    ctx.clearRect(0, 0, 200, 200);
    ctx.beginPath();
    ctx.moveTo(0,175);
    ctx.lineTo(200,175);
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(positionX - 10, positionY - 20);
    ctx.lineTo(positionX + 10, positionY - 20);
    ctx.lineTo(positionX + 10, positionY);
    ctx.lineTo(positionX - 10, positionY);
    ctx.closePath();
    ctx.stroke();
}
 

wonder

Expert
Licensed User
Longtime User
Alright, I checked the Skaters sample:

B4X:
'Moves the skaters
Skater.X = Skater.X + (Skater.velocityX * timeDeltaSeconds)
Skater.Y = Skater.Y + (Skater.velocityY * timeDeltaSeconds)

'Applies gravity
Skater.velocityY = Skater.velocityY - (SPEED_OF_GRAVITY * timeDeltaSeconds)
This is pretty much what I have so far... I must have a bug somewhere else.
I will revise my code (by the 1000th time) and rewrite the jump module from scratch if necessary.

Anyway, many thanks for pointing me in this direction.
 

Informatix

Expert
Licensed User
Longtime User
Alright, I checked the Skaters sample:

B4X:
'Moves the skaters
Skater.X = Skater.X + (Skater.velocityX * timeDeltaSeconds)
Skater.Y = Skater.Y + (Skater.velocityY * timeDeltaSeconds)

'Applies gravity
Skater.velocityY = Skater.velocityY - (SPEED_OF_GRAVITY * timeDeltaSeconds)
This is pretty much what I have so far... I must have a bug somewhere else.
I will revise my code (by the 1000th time) and rewrite the jump module from scratch if necessary.

Anyway, many thanks for pointing me in this direction.
The easier way to compute a vertical jump is to do a non-linear interpolation (lgMathInterpolation) between 0 and 1 (up) then 1 and 0 (down). The parameter of the Apply function is SumOfDeltaTime/(JumpDurationInSec/2). The result is multiplied by the max. height of jumps:
Yup = interpolation.apply(SumOfDeltaTime/(JumpDurationInSec/2)) * MaxHeight
When the max.height is reached, you reset SumOfDeltaTime to 0 and invert the direction:
Ydown = MaxHeight - (interpolation.apply(SumOfDeltaTime/(JumpDurationInSec/2)) * MaxHeight)
 

wonder

Expert
Licensed User
Longtime User
Thanks for the non-linear interpolation method. At this moment I'm still trying to "fix" the code I already have, since it works flawlessly without the timestep. Reading your tutorials again, I came across this paragraph:
Informatix said:
Each time that you move an object in your game world, you increase or decrease its coordinates. If you do X = X + 2, two units are added whatever the elapsed time between two frames. If you introduce a time step, the formula becomes: X = X + (2 * TimeStep). With this time step, you can accelerate or decelerate your game at will, just by changing this value. Set it to the delta of time between two frames and your objects will move faster when the device is slowed. The player will see a constant move.
A real example:
Code:
Actor.X = Actor.X + Speed * lGdx.Graphics.DeltaTime
Speed is the speed property of the object to move and DeltaTime the time step. It is expressed in seconds between two frames. If everything goes well, its value should be close to 16/1000.

Sometimes the interval between two frames is very long (it’s the case when the antivirus of my phone checks the launched application) and the time step causes an issue called the tunnel effect. The step is so great that the move is really big, and your object can cross walls (it is moved directly on the other side). To avoid this, you should limit the value of your time step.


Although I believe that my problem is other than the tunnel effect, could you please elaborate on this matter?
Thanks in advance! :)
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Although I believe that my problem is other than the tunnel effect, could you please elaborate on this matter?

From wikipedia: "the tunnel effect is the perception as a single object moving beyond an occluding object and then reappearing after a suitable amount of time on the other side of it"

When the step to move an object is too big, it can go through a limit that is not supposed to cross. For example: you fire a bullet to an enemy target. If your bullet moves at 1 pixel/sec, the move is so slow that you can compute a collision with the target with no risk of passing through. On the contrary, if it moves at 2000 pixels/sec and the device takes 150ms to draw the next frame due to a sudden slow down, then your bullet is moved behind the target and never hit it. That's why you should cap the step to use.
 
Top