2D Catch Game is a very simple game built for learning how a game works. The point of the game is to catch bowling balls and not the bomb. The game will be in 2D. In this guide, we bill be using Unity and Infineon's 3D magnetic 2GO kit & Rotator Knob based on their TLE493D as controller.
Step 1: Setup the Infineon 3D magnetic sensors softwareThis part is a little tricky, if you've installed Evalkit for 3D Sensors you are likely to get stuck in an infinite loop.
So under this guide we have to install it via Arduino from Infineon's GitHub page (https://github.com/Infineon/TLE493D-W2B6-3DMagnetic-Sensor) so we can add the TLE493D Library to Arduino.
And once Arduino IDE is installed, we also have to add XMC as a part of the device per Infineon's GitHub page: https://github.com/Infineon/XMC-for-Arduino.
In short, we just need to add the following to the Preference so that XMC Microcontroller shows up. And add XMC Microcontroller to the board.
https://github.com/Infineon/Assets/releases/download/current/package_infineon_index.json
After that we can start testing the magnet sensors from the following code. Somehow there is a bug, so you will need to do Tle493dMagnetic3DSensor.begin(); twice in order to get it running. The below code is the Arduino code for the entire program:
#include <Tle493d_w2b6.h>
Tle493d_w2b6 Tle493dMagnetic3DSensor = Tle493d_w2b6();
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(14, OUTPUT);
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.setWakeUpThreshold(1,-1,1,-1,1,-1);
Tle493dMagnetic3DSensor.disableTemp();
}
void loop() {
Tle493dMagnetic3DSensor.updateData();
Serial.println(Tle493dMagnetic3DSensor.getAzimuth());
delay(10);
}
The default JLink uses v6.00e from the installer, if you get the following error:
[Error] Infineon.DebuggerExceptions: It seems that JLink software is not installed please download from www.segger.com and install it. You can specify it by setting java property xmcFlasher.JLink.dllPath
Make sure you go to www.segger.com and install the latest JLink, the version v6.32d, and Arduino 1.8.5 would compile and upload Arduino code quite successfully.
Step 2: Screw the knob and test the rotationThis step is fairly straight forward, making sure the chips are facing up.
After the knob is installed, we can test out on the same Arduino output to see both rotation as well as click. Specific field we care about here is just Azimuth for rotation.
Reading data from XMC2Go:
Now we can determine whether we are rotating left or right, as well as clicking. Left and Right can be seen through rotation from Azimuth.
Step 3: Building out 2D Catch GameThis one can be easily done via:
git clone
For those who are interested in building our 2D Catch Game from Unity step-by-step, you can visit https://unity3d.com/learn/tutorials/topics/2d-game-creation/2d-catch-game-pt-1, as this is free and easily accessible by anyone.
Step 4: Connect USB controls to UnityWe first need to find the USB port, which can easily be done through Device Manager. This is to ensure our controller gets the Magnetic 3D sensor.
Inside Unity, we need to go to Edit->Project Settings->Player, then go to Other Settings to make sure Api Compatibility is.NET 2.0 and not.NET 2.0 Sublet, this is to ensure the Serial port working.
Next, inside Unity's HatController.cs, we will start to implemented the code we've previously wrote in Step 2. Following code should get the norm and Azimuth running, as we are reading all the sensor values in Unity, we can do all the logic inside Unity as well. In this case, only for the rotation of the knob.
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class HatController : MonoBehaviour {
public Camera cam;
private Rigidbody2D hat;
private Renderer hatRend;
private float maxWidth;
private bool canControl;
SerialPort serial;
private float azimuth = 0;
float knob = 0.0f;
float position = 0.0f;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
hat = GetComponent<Rigidbody2D> ();
hatRend = GetComponent<Renderer> ();
Vector3 uppercorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (uppercorner);
float hatWidth = hatRend.bounds.extents.x;
maxWidth = targetWidth.x-(hatWidth);
serial = new SerialPort ("COM3", 9600);
serial.Open ();
serial.DiscardInBuffer ();
string temp = serial.ReadLine ();
azimuth = float.Parse (temp.Trim ());
}
void Update()
{
if (serial.IsOpen) {
string temp = serial.ReadLine ();
float azimuth2 = float.Parse (temp.Trim ());
Debug.Log (azimuth2);
if ((int)(azimuth2 * 10) > (int)(azimuth * 10)) {
knob = -0.5f;
} else if ((int)(azimuth2 * 10) < (int)(azimuth * 10)) {
knob = 0.5f;
} else {
knob = 0.0f;
}
//reset value
azimuth = azimuth2;
}
}
// FixedUpdate is called once per physics timestep.
void FixedUpdate () {
Vector3 hatPos = new Vector3 (0f, this.transform.position.y, 0f);
hatPos.x = Mathf.Clamp(position, -maxWidth, maxWidth) + knob;
hat.MovePosition (hatPos);
position = hatPos.x;
}
public void ToggleControl(bool toggle)
{
canControl = toggle;
}
}
After this, you should be able to play the game.
Comments