Programmer's Guide - PatrickGe/Molecular-Construction-Kit GitHub Wiki
Prerequisites
In order to work on the Molecular Construction Kit, some basic requirements are necessary. The program runs under the Unity Version 2018.3.12f1, additionally the current version of SteamVR is required. The hardware is a HTC Vive headset with VIVE controllers and the VIVE base stations to track the headset and the movements in the room. An HTC Vive Pro or better is recommended to avoid compatibility issues. These requirements are sufficient to use the program.
To open or edit the program code, Microsoft Visual Studio 2019 or newer is also required.
Structure
The easiest way to get an overview of the program is to use Unity. Here you can see all GameObjects in the hierarchy. As usual in Unity, the center of the program is the camera. This is the same as the user's HMD, the HTC VIVE headset. The controllers are counted as child objects by default. Both controllers must be connected to control the program correctly. The left and right controller have identical functionality and scripts. The sketch therefore shows which scripts are controlled by the controllers.
There are multiple Scripts located in the "Skripte" folder. Some of these are older relics from early stages of the program when SenseGlove was still in use. They are still included to be extensible and to provide basic functionality for SenseGlove, but other features for SenseGlove are not currently a priority during development.
The diagram below shows an overview of the scripts currently in use and how they are related.

Scripts
It is important that the basic tracking scripts should not be changed. If adjustments are necessary for a better user experience, these can be made in the respective script. Here is a brief overview over the most important scripts. Detailed comments can be found in the code.
Global Control Script
The Global Control script contains all basic functions of the program. The individual methods are divided by function and, if possible, one method represents one function. First of all, the start method scans for all GameObjects and creates a list of the existing atoms. The update method here only scans for updates in the GUI, which is available if the user saves or loads a created molecule.
- createCarbon method instantiates a new carbon atom
- saveMolecule method adds data from each atom in the molecule to a list, which is then delivered to another script and saved as XML file
- loadMolecule method loads a previously created and saved molecule for reuse
- loadGUILoad method loads the GUI parameters from the XML file. Available created molecules to load are now shown to the user.
- textChange method updates the written text while the user types the name of a created molecule to save it.
- destroyMolecule method destroys the whole molecule and delets all corresponding GameObjects
- recycle method decides if the whole molecule should be destroyed with the method above or if only a single atom should be deleted
Force Field Script
The force field is another huge part of the project. It calculates the forces between the atoms and performs the movement of the atoms according to their forces. At the moment, these forces consist of bond forces and angle forces with the possibility to be extended. The update method calls each frame the method to update the bond and angle lists. It checks if all atoms are included and adds possible new atoms. Afterwards it calls the methods to calculate the forces and lastly moves the atoms according to their forces.
The forces, which are calculated during the process are all added up to a movement vector belonging to each atom. At the end of each frame, the movement gets applied to the atoms and a small movement is visible. These forces are created if the user grasps only a single atom from the molecule and pulls it apart from the oter atoms. In this case the connections between these atoms also have to be scaled and updated.
Laser Pointer Script
The laser pointer script, as the name suggests, deals with the laser pointer, which is a main function of the program. The user is able to activate the laser pointer by touching and holding the touchpad on the upper button. Now the laser gets rendered in a straight line from the controller object. The update method checks each frame if the button is pressed instead of touched, this will confirm the selection. In the background it is checked, which hitbox the laser pointer has hit. The respective hitbox is the target of the selection and a corresponding action gets exectued, e.g. a new atom is created.
For the programmer this means that the laser pointer script is the interface between the user interaction with the program and the Global Control script. Depending on which target is chosen by the user, a different method is called by the Laser Pointer script in the Global Control script
Grab Controller Script
The Grab Controller script focuses on the grab function, i.e. all processes that occur when the user grabs an object. Basically, every frame a check is performed, if the trigger is pressed. If the trigger gets from the state "not pressed" to the state "pressed", the algorithm calculates the nearest object, which collides with the controller hitbox to make sure the correct object is picked up. Now the user is able to drag the object. Depending on whether the user has grasped the whole molecule or just a single atom, calculations and transformations are then performed to enable correct rotation.
If the trigger gets from the state "pressed" to the state "not pressed", the grasped atom or molecule gets dropped and floats in the air again.
Unity Objects
The GameObjects in Unity are the only things directly visible to the user. A brief overview over these GameObjects should also help to understand the program.
The grayed out objects at the beginning of this lists are again relicts from the beginning of this project and belong to the SenseGlove. They can be reactivated if required. The new camera object for use with the VIVE controllers is at the bottom of this list. If the object is opened up, the controllers are visible as child objects.
The first activated object "Raum" contains all objects belonging to the room. These aren't functional and only for decorative purposes. Next, the "PeriodensystemC" object is the tile which needs to be pointed at to create a new carbon atom. The next four objects are the sliders or buttons to control the handling of the molecule. "ButtonON" and "ButtonOFF" control the force field, whereas the other two decide whether the whole molecule is grabbed or only a single atom. The next GameObject is called "Molekül". All atoms, which are connected to a molecule, as well as their connections are stored as child objects inside this GameObject. Next, the "Öffnen" and "Speichern" objects with their GUIs below are the tiles to save and load the created molecules. The GUIs of course only are activated as soon as the respective button is pressed. Last, the four UI objects, which are only empty boxes for text. They are invisible on the ground if not needed. Once the user marks one atom red, the bond angles to the connected atoms are shown by filling the four empty boxes with the number in degrees and placing them above the connected atoms.