Week 2: The Story Of Many Boards And Pieces
March 17, 2023
“When you see a good move, look for a better one” -Emanuel Lasker
If you’re still here, I’m proud of you. Don’t go looking for a better project, I promise this one is the best (I’m half joking)
Hello everyone, welcome to the second week of my senior project!!! Because I was actually home this week, I got to spend a decent amount of time working on my AR system. Sadly though, I’m starting to realize that this project is far more difficult than I anticipated. But that’s okay!!! One can never expect ease when working with augmented reality. Let’s take a look at what I’ve done, and what I will need to do in the near future.
- Setting Up The System
- Finding 3D Assets
- Programming The Chess Board
- The “Uh-Oh” Moment
Setting Up The System
Spatial Anchors
As I mentioned last week, I’ve been working with my mentor for quite some time. Our old project has a very important system that I plan on utilizing in my current project – Azure Spatial Anchors. What this does is that it essentially “anchors” virtual content to physical locations in the real world, allowing users to feel as if the object is actually part of the environment they are in. Since the goal of this project is to convey the realism of the projected chess board, its interactions with the physical environment will be very important. Here’s a screenshot of what the anchors look like:
Inside Unity, I need to build the virtual environment around these anchors. Then, during the startup sequence of the AR headsets, I can move the anchors (or pins as they are called) to anywhere inside the physical room. The virtual objects would then be transported along with them. For my project, I would simply need to move the anchors onto the table so that the chess board will be at the appropriate elevation and location.
Windows Compatibility
Because my mentor spent quite a while getting the anchor system to work properly, I wanted to make a copy of our old project so I’ll have the system ready to go. I met with him to get permission to do so (which I did), but there was the issue of platform compatibility. I generally use my Mac computer, since my Windows one is very slow, but the project was on Windows. Luckily, I remember I spent $100 getting a Windows virtual machine called Parallels to play video games. Ha! I knew it would come in handy some day. I already had Unity installed on there, but some of the coding autocomplete tools were missing. After downloading the Visual Studio Code add-on for Unity and making a copy of my old project, I had a Windows version of everything all setup on my Mac.
Github Repository
I also spent some time linking my project to a Github repository. Although this is not my first time using Github, it is my first time creating my own repository. I’ve generally taken the dangerous route of not backing up my project, but I’m too scared to lose 10 weeks of work. Also, I’ve discovered that instead of using command line, I can just use Github Desktop, which makes everything much easier. Now, I can make edits without worrying that I might break something (which is inevitable). After organizing the Unity editor and deleting files that (I think) I don’t need, I can finally start working on my project!
Finding The 3D Assets
I’m not the greatest 3D modeler, so it was in my best interest to find professionally-made 3D assets. I actually found quite a few free 3D chess sets on the Unity Asset Store. I decided to combine two of them – one had pretty nice chess pieces, but the other one has built-in code for setting up the chessboard. I’m not sure how useful it’ll be, but the built-in script allows me to select squares on the board and returns the coordinates. Obviously, for AR, the user would not have a mouse available. However, I may be able to adapt the system for hand gestures. Anyways, here’s what the chess set looks like right now:
The links to the sources for the two assets used are at the end of the blog. I’m also going to include the links to the other chess sets I found. Let me know in the comments which ones you think look best!
Programming The Chess Board
Obviously, other people have programmed chess before. I was hoping that I could find a pre-made Unity project that I can directly use and adapt to fit my own need. I found a complete chess project by ErkrodC, who uploaded everything onto a public Github repository. However, although it includes a lot of useful functionality, the project seems too complicated, and I can’t seem to find the scripts for the rules of chess itself. I will continue taking a look at this, but for now, I need a new solution. Luckily, on Medium.com, I found a step-by-step guide to creating a basic chess game. The author, Marcel Jurtz, goes through the code for every task, from setting up the chessboard to moving the pieces. All the scripts are shown and well-explained, so this was perfect for me to get started.
I began by writing the classes for each of the chess pieces. Although Jurtz’s ChessFigures abstract class and Pawn class were useful, I found a better way to code the possible movements of the other pieces. I took advantage of a directions array so that I can account for all the movements in a single for-loop. This made the code much shorter, cleaner, and reusable. Here is a quick example of what I mean:
The queen can move in 8 different directions, so I made two arrays (one for the row, one for the column) that indicate what the position changes are for each direction. Once I have this, I can practically copy and paste the same code for the other pieces. The only thing I need to change would be the two direction arrays.
The “Uh-Oh” Moment
I “finished” writing the script for each of the pieces, but then I realized something – I didn’t actually finish. There are so many special situations that I did not take into account of. Let me list them out:
1) En passant: This is a special pawn move that only becomes available if the opponent’s pawn moved 2 squares and your pawn is right next to it on the 5th rank. Accounting for this would require me to keep track of the most recent pawn move. It’s not too difficult, but a bit annoying implementation wise.
2) Promotion: Another special pawn move that becomes available when it reaches the 8th rank. I would need to “transform” the pawn into another piece of the player’s choosing.
3) Castle: This is a special king move, where the king gets to move 2 squares and the rook gets to jump over a piece. Accounting for this is also a bit annoying. I would need to keep track of whether or not the king and rooks have moved.
4) Check: The worst of them all. If the king is in check, then only a very limited number of moves become available. Unfortunately, moving the king is not the only option available when it is in check. Other pieces may block, or capture the attacking piece. But what about double checks??? What about pieces pinned to the king???
Plan For Next Week
So as you can see, I have quite a lot to do. I must somehow account for all of these special cases (especially the checks). I’ll keep looking online in case I find a script that does take all of those into account. In that case, my burden would be greatly eased. If not, I guess I’ll just have to keep coding
However, because I have a rough framework of the movements already, I’m going to take a break from implementing the special cases and try to get a working prototype of the chess game down. That way, I would be able to test everything as I go, which makes debugging much easier. In the meantime, I’ll also be meeting with my mentor to discuss the best way to allow users to interact with the pieces. Right now, I’m thinking of placing a button at every square on the chess board. If I have time, I’ll try implementing it in Unity this week, though I’ll prioritize prototyping the chess game first.
That’s all for today! Come back next week to see how far I get with the chess board!!! Also, I linked all the 3D assets I found for the chess set below, so take a look and let me know which one you like best.
References
3D Assets Used In Project:
- https://Assetstore.Unity.Com/Packages/3d/Chess-Pieces-And-Board-95641
- https://Assetstore.Unity.Com/Packages/3d/Props/Low-Poly-Chess-Pack-50405
Code For Chess Game:
- Jurtz, Marcel. “Building A Chess Game In Unity.” Medium, Medium, 12 Sept. 2018, Https://Jurtzmarcel.Medium.Com/Building-A-Chess-Game-In-Unity-85408f86b749.
- ErkrodC. “ERKRODC/Unitychess: A 3D Chess Game Made With Unity. Core Game Library Submodule: Https://Github.Com/Erkrodc/Unitychesslib.” GitHub, 15 Jan. 2022, Https://Github.Com/ErkrodC/UnityChess.
All 3D Assets
- https://Assetstore.Unity.Com/Packages/3d/Props/Low-Poly-Chess-Pack-50405
- https://Assetstore.Unity.Com/Packages/3d/Props/3d-Wooden-Chess-Set-183336
- https://Assetstore.Unity.Com/Packages/3d/Props/Low-Poly-Chess-Set-104629
- https://Assetstore.Unity.Com/Packages/3d/Props/Ag-Chess-Set-Low-Poly-192162
- https://Assetstore.Unity.Com/Packages/3d/Props/Free-Low-Poly-Chess-Set-116856
- https://Assetstore.Unity.Com/Packages/Tools/Camera/Ar-Chess-160951
- https://Assetstore.Unity.Com/Packages/3d/Props/Chess-Pieces-Board-70092
- https://Assetstore.Unity.Com/Packages/3d/Chess-Pieces-And-Board-95641