My First “Real” Magic Leap Project: Part 4

We implement the Placement mode

John Tucker
Coinmonks
Published in
3 min readAug 8, 2018

--

This article is part of a multi-part series starting with My First “Real” Magic Leap Project: Part 1.

The completed application for this article is available for download from the part4 branch of the bounce repository.

Re-Refactor

While the Redux pattern works well in (most) web development situations, I started to have second thoughts on using it for all state in Unity. In particular, in the last example we we updating the position and scale of the MeshingZone into the global state. The problem with this approach is that these updates happen very quickly, up to 60 times a second, and we really don’t want a lot of extra code to run in these updates.

So, in this case we can get around using the global state by simply creating a reference in MLSpatialMapper2 to MeshingZone. We can then get rid of all the global state relating to the MeshingZone position and scale, e.g., MLPositionX.

Assets/Table/MLSpatialMapper2.cs

Placement

Now we go ahead and implement the next mode, Placement, in which the user places a ball on the table (the mesh). The ball’s position is based on the user’s head pose (camera position and orientation).

As the user moves their head, the ball will slowly track to a new position.

By giving the Ok hand pose with the right hand, the ball’s position is locked in.

The Code

The new global state includes:

  • Placement: The position where the ball is placed; stored as three integers (a custom Triplet class). Integers are used because floats cannot be reliably compared for equality
  • PlacementValid: Boolean indicating if the placement location is valid, i.e., on the table (mesh)

The head pose (Main Camera) sets the Placement and PlacementValid global state.

Assets / Table / MainCamera.cs

Observations:

  • The placement of the ball is based on the Unity Physics.Raycast; instead of the Magic Leap MLWorldRays. This is because we already have created the mesh
  • For performance considerations, we limit the casting to only happen once per second

The Ball uses the Mode, Placement, and PlacementValid global state to control itself

Assets / Table / Ball.cs

Observations:

  • We store the target position in the private variable _position and use the Vector3.MoveToward method to slowly position the ball to it
  • The next mode is Aim; we begin with dropping the ball into the locked position

Next Step

In the next article in the series, My First “Real” Magic Leap Project: Part 5, we implement the rest of the Aim mode (and Action mode).

--

--

John Tucker
Coinmonks

Broad infrastructure, development, and soft-skill background