Games [XUI2D] Mouse following a path


B4X founder
Staff member
Licensed User
Longtime User

This example shows two things:

1. How to use a MotorJoint to make a body follow a path of points.
The difficult part was to make the body turn through the shortest direction.

A useful FindAngleToTarget sub is included in the example.

2. How to use the new async drawing features added in BitmapCreator v4.5. These features are very important as they allow us to make drawings directly to X2.MainBC.
X2.MainBC is the BitmapCreator that all the bodies are drawn to (by default).

Start with this tutorial:

It looks like this:
gs.DrawingTasks.Add(X2.MainBC.AsyncDrawCircle(v.X, v.Y, 5, BrushBackwards, True, 0))
BC.AsyncDraw methods do not draw anything. They return a DrawingTask object. This object holds the information that is required to later make the drawing.
Remember that X2.MainBC is cleared every cycle so you need to draw everything every tick.

Drawing paths asynchronously requires careful handling. You shouldn't modify the path object while it is being drawn.
You should use a "copy on write" implementation:
PathMainBCBackwards = PathMainBCBackwards.Clone
PathMainBCBackwards.LineTo(v.X, v.Y)
This way the previous path is kept intact.

The example is included in the examples pack.