Breakout is one of the earliest arcade video games built in 1976. Since then it has been used as one of the classic games and cloned for many different platforms. The game was originally manufactured by Atari, which released it in 1972. This guide specifically discusses how to build Block Breaker (a Breakout clone) with Unity and Infineon's 3D magnetic 2GO kit & Rotator Knob based on their TLE493D as controller.
Step 1: Set up 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 pagehttps://github.com/Infineon/TLE493D-W2B6-3DMagnetic-SensorSo we can add the TLE493D Library to Arduino
And once Arduino IDE is installed, we also have to add XMC as aprt of the device per Infineon's github pagehttps://github.com/Infineon/XMC-for-Arduino
In short, we just need to add following to the Preference so that XMC Microcontroller shows up. And add XMC Microcontroller so the board.
https://github.com/Infineon/Assets/releases/download/current/package_infineon_index.json
After that we can start testing the magnet sensors from following code. Somehow there is a bug so you will need to do Tle493dMagnetic3DSensor.begin(); twice in order to get it running. Below code is the Arduino code for the entire program
#include <Tle493d_w2b6.h>
Tle493d_w2b6 Tle493dMagnetic3DSensor = Tle493d_w2b6();
//for firing
float norm = 0;
bool start = false;
int frame = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(14, OUTPUT);
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.setWakeUpThreshold(1,-1,1,-1,1,-1);
Tle493dMagnetic3DSensor.disableTemp();
norm = Tle493dMagnetic3DSensor.getNorm();
}
void loop() {
Tle493dMagnetic3DSensor.updateData();
Serial.print(Tle493dMagnetic3DSensor.getNorm());
Serial.print(" ; ");
Serial.println(Tle493dMagnetic3DSensor.getAzimuth());
//It doubles when press down, and halves when releases
if((abs(Tle493dMagnetic3DSensor.getNorm()) > abs(norm*1.5))){
start = true;
}
else if
((abs(Tle493dMagnetic3DSensor.getNorm()) < abs(norm/1.5)))
{
start = false;
}
norm = Tle493dMagnetic3DSensor.getNorm();
if(start)
{
digitalWrite(14, HIGH);
}
else
{
digitalWrite(14, LOW);
}
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 quiet 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 Norm for click and Azimuth for rotation
Now we can determine whether we are rotating left or right, as well as clicking. Left and Right can be seen through rotation, and click is always double of what current value is fro the Knob. Because of the latency, it's much easier for Unity to do the logic for the controller, but in the meanwhile, we can make the LED work while pressing. Similar logic will be written in Unity, but we can have some LED fun with the board to light up the LED when firing.
//It doubles when press down, and halves when releases
if((abs(Tle493dMagnetic3DSensor.getNorm()) > abs(norm*1.5))){
start= true;
}
else if
((abs(Tle493dMagnetic3DSensor.getNorm()) < abs(norm/1.5)))
{
start = false;
}
if(start)
{
digitalWrite(14, HIGH);
}
else
{
digitalWrite(14, LOW);
}
When succeed, it will come up with something like this.
This one can be easily done via
git clone https://github.com/Nyceane/05-Block-Breaker-Original
For those who are interested in building out Block Breaker step by step please visit https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-breakout-game, or UDemy's Unity Complete course at https://www.udemy.com/unitycourse/as the tutorial is free and widely available for anyone who is interested in building out games.
Step 5: Connect USB controls to UnityWe first need to find 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 Paddle.cs, we will start to implemented the code we've previously wrote in Step 3. 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, whenever the knob is pressed, similarly, we can detect the rotation of the knob.
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class Paddle : MonoBehaviour {
public bool autoPlay = false;
public float minX, maxX;
SerialPort serial;
private float norm = 0;
private float azimuth = 0;
bool startTrigger = false;
float knob = 0.0f;
float position = 0.0f;
private Ball ball;
void Start () {
ball = GameObject.FindObjectOfType<Ball>();
serial = new SerialPort("COM3", 9600);
serial.Open ();
serial.DiscardInBuffer ();
// Debug.Log (serial.ReadLine ());
string[] temp = serial.ReadLine ().Split (';');
if (temp.Length == 2) {
norm = float.Parse (temp [0].Trim ());
azimuth = float.Parse (temp [1].Trim ());
}
}
// Update is called once per frame
void Update () {
Play();
if (serial.IsOpen) {
string[] temp = serial.ReadLine ().Split (';');
if (temp.Length == 2) {
float azimuth2 = float.Parse (temp [1].Trim ());
float norm2 = float.Parse (temp [0].Trim ());
if ((Mathf.Abs (norm2) > Mathf.Abs (norm) * 1.5)) {
startTrigger = true;
} else if ((Mathf.Abs (norm2) < Mathf.Abs (norm) / 1.5)) {
startTrigger = false;
}
if ((int)(azimuth2 * 10) > (int)(azimuth * 10)) {
knob = -0.2f;
} else if ((int)(azimuth2 * 10) < (int)(azimuth * 10)) {
knob = 0.2f;
} else {
knob = 0.0f;
}
//reset value
norm = norm2;
azimuth = azimuth2;
}
if (startTrigger) {
ball.startGame ();
}
}
}
void Play() {
float moveHorizontal = 0.0f;
if (moveHorizontal == 0.0f && knob != 0.0f) {
moveHorizontal = knob;
}
Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
Vector3 ballPos = ball.transform.position;
paddlePos.x = Mathf.Clamp(position, minX, maxX) + moveHorizontal;
this.transform.position = paddlePos;
position = paddlePos.x;
}
}
There you have it, now you have 3D magnetic sensor fully playing!
Comments