Games [XUI2D] Mouse following a path

Erel

B4X founder
Staff member
Licensed User
Longtime User
test-gif.73821


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: https://www.b4x.com/android/forum/posts/622812/

It looks like this:
B4X:
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:
B4X:
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.
 
Top