Hex Based 4X Game Tutorial

September 9, 2019

Unity 4X Game Tutorial

Part Based

Mostly Civilized: A Hex-Based 4x Game Engine for Unity – Part 1

Youtube – Link

By: quill18creates

I was looking to add another Unity game genre tutorial that I could follow along with, and happened to come across one for a fairly complex genre I enjoy so this seemed like a nice more advanced tutorial program to go through. I have checked out some of quill18creates content before and it usually serves as some good practice for more advanced topics, while also helping teach you some fundamentals about optimization and general project architecture.

This immediately caught my attention with the hex tile map math shown at the beginning of the video. There they show how to use various coordinate systems, such as cube coordinates, in a hex tile system in a very efficient and sensible way. The link to this information can be found here:

Hexagonal Grids from Red Blob Games

Red Blob Games – Link

Video Game Physics – Constrained Rigid Body Simulation by Nilson Souto

January 5, 2019

Video Game Physics – Constrained Rigid Body Simulation by Nilson Souto

Toptal – Video Game Physics Tutorial – Part III: Constrained Rigid Body Simulation

By: Nilson Souto

Link to Erin Catto’s box2d.org

This appears to be a good follow up to the game physics GDC talk by Erin Catto I watched earlier. This serves as another listing of physics constraints and how they can apply in games, specifically when dealing with rigid body elements. I also learned the constraint type is “revolute”, not “resolute”. This article actually references Erin Catto and his Box2D site since it goes into his creation of the sequential impulse method.

Instantiating Objects Randomly Within a Given Polygon

June 11th, 2018

Randomly Selecting Points within a Given Polygon

References Used:

Stack Overflow – Random Points in a Quadrilateral
Wolfram Alpha Information on Points in a Triangle

Notes

I needed to be able to randomly but evenly distribute a number of objects within a 2D area so these are the sources I started with. The Stack Overflow question ended up being a very good starting point, but there were errors with the calculations that I needed to solve in order to actually use their logic.

Fixes from Stack Overflow

First calculation to fix was the originally randomly selected position of the object. It turned out that it was very close, but it actually only worked if v0 was the origin. It was simple enough to fix by adding v0 to the x equation.

The second equation to fix was the v3 equation. They were subtracting v0 twice which is incorrect (again something unnoticeable if v0 is that origin as you’re simply subtracting (0,0) twice). v0 should only be subtracted once to obtain the correct v3 value.

Finally, a correction that was found in the comments was that there are two rotation equations for x’ and the larger one is incorrect. In the text, it can be found that a rotation of pi is necessary. This can be mathematically applied by simply reversing (x – v3) to (v3 – x).

Work to do

Fixing these mathematical errors has appeared to give me the desired results with some further testing required after a final issue. I still need to determine how to let it know mathematically when to “flip” the x position into the x’ position. The original thought was to simply compare the distance from x to v3 and v0, and if it was closer to v3 (distance was less), then apply the x’ flip transformation. This however does not seem to be a geometrically sound solution as I am sometimes getting the flipped applied when it is within the proper bounds.

July 2nd, 2018

Update: Resolved issue for determining when to flip the spawn positions

Reference:

Point in Triangle Test: Math Foundation

Building off of the previous work and calculations being done, the points for the triangles to contain the objects to spawn already exist. Using these with the calculations found in the reference for “Point in Triangle Test”, it can be determined if the point the collectible will be spawned at is indeed within the 3 vector locations of the vertices of the triangle. If it is not, then the “flip” algorithm is applied to put it into the triangle.

The test uses the existing triangle to basically check the cross products of each edge with the potential spawn location and the location of the third/final vertex of the triangle. As long as the directions of the cross product results of these two checks are the same, it can be concluded that the points are on the same side of the tested edge. If this is applied to all three edges, and the case is true for all three, it can be concluded that the point is within the triangle. If any of these are false, it is outside of the triangle and the “flip” algorithm is applied.


Video of Spawner in Action

This shows a few test runs of having the collectibles randomly spawned to check if they consistently spawn within the play area. The small red orbs that sometimes appear are error checking markers added to help determine if everything was working properly. These are spawned right before an out of bounds collectible if flipped, to leave an indication of where it was at to see if it was truly out of bounds. Some of them appear very close to the edges of the play area, but those still appear to be just outside of the triangle and seem to be getting flipped into the play area effectively so those seem correct as well.

May 19th, 2018

Math for Game Programmers – Talks from GDC – Notes

GDC 2015 – Math for Game Programmers: Fast and Funky 1D Nonlinear Transformations

Presenter: Squirrel Eiserloh

Youtube link to presentation

Notes:

  • Other names used for these concepts in other fields:
    • Easing functions
    • Filter functions
    • Lerping functions
    • Tweening functions
  • Implicit vs. Parametric Equations
    • Implicit: Ex. x^2 + y^2 = R^2
    • Parametric: Ex. Px = R * cos(2*pi*t); Py = R * sin(2*pi*t)
  • Parametric Equations use a single variable (usually a float) as input
  • Opportunities to Use Parametric Transformations
    • Anywhere you have a single float to change
    • Anywhere that could be expressed as a single float
    • Anytime you use time
  • The two most important number ranges in mathematics and computer science are:
    • 0 to 1: Good for fractions and percentages
    • -1 to 1: Good for deviations from some original value
  • These functions are normalized
    • Input of 0 has output of 0, and input of 1 has output of 1
    • The in-between values are those that vary function to function
  • Types of functions
    • Smooth Start: exponential functions with various orders
    • Ex: t^2
    • Smooth Stop: gives opposite feel of smooth start
    • Ex: 1 – (1-t)^2
    • Mix functions
    • Crossfade: mix but “blend weight” is the t parameter itself
    • Scale: multiply something by t; can also multiply functions together
    • Bezier Curves
Other suggested talks:

Juice it or Lose it – by: Martin Jonasson and Petri Purho
The art of screen shake – by: jan willem Nijman vlambeer

Math for Game Programmers – Talks from GDC – Notes

GDC 2013 – Math for Game Programmers: Interaction With 3D Geometry

Presenter: Stan Melax

Youtube link to presentation

Notes:

  • Basic Vector Math
    • Dot product
    • Cross product
    • Outer product
    • Jacobian
    • Determinants
  • Geometry Building Blocks
    • Traingles and planes
    • In games, triangles and planes need to know above and below
    • Triangles use normals
    • Planes use Ax + By + Cz + D == 0
  • Ray-Triangle Intersections
  • Ray-Mesh Intersection
    • Could check against all triangles, but there are ways to remove some for efficiency
    • Need to check if you’ve hit the nearest triangle
    • Mesh must be intact, can have issues if there are holes
  • Convex Mesh
    • Neighboring face normals tilt away
    • Volume bounded by number of planes
    • Every vertex lies at/below every other face
    • Convex used because it makes a lot of tests/calculations simpler
    • If point lies under every plane, it is inside
    • Can move rays as they intersect triangles
  • Convex Hulls
    • Techniques for converting meshes into convex meshes
    • There are two main techniques:
    • Expand Outward – start with small mesh and expand out until all vertices are accounted for
    • Reduce Inward – start with a large mesh and shrink it down to fit all vertices
  • Convex Decomposition: breaking down complex shapes into separate convex meshes
  • Objects in Motion – Spatial Properties
    • Find the center of triangles, then the center of tetrahedrons
    • Find the area of triangles and the volumes of tetrahedrons
    • These properties can be used for basic accurate physics simulations of objects
  • Time Integration without Numerical Drift
    • Forward Euler update versus Runge Kutta update
    • Forward Euler applies derivative at every step, which can give inaccurate results in too large of time steps
    • Runge Kutta looks at multiple time steps and their derivatives and takes a weighted average to make steps more consistent
  • Soft Body Meshes
    • Connect points/vertices of mesh with spring-like connections
    • Two main techniques:
    • Kinematic: can have issues with stressed state oscillating as it will not come to rest, forces continue to act
    • Dynamic: has better resting stressed states that will settle down
  • How to learn more
    • Practice tinkering with your own physics engine code
    • Write gjk algorithm
    • Help understand physics engines and their limitations
May 13th, 2018

Procedural Generation

Unite 2016 – The Power of Procedural Meshes

Unity Talk by Alexander Birke

Youtube Link to Talk

Suggested Reading:

“Essential Mathematics for Games – A Programmers Guide” by James M. Van Verth & Lars M. Bishop

Links to examples to learn about procedural generation of meshes
Notes from talk:
    Why use procedural meshes?

  • Player Made Content (Ex. Spore)
  • Unique Mechanics (Ex. Gish)
  • Procedural Generation (Ex. Sir You’re Being Hunted)
Unity Procedural Meshes
  • Mesh starts by creating vertices, and then making triangles from those vertices
  • Components Needed:
    • MeshFilter – stores the mesh
    • MeshRenderer – shows the mesh
    • Script – to generate the mesh
  • Normal vectors used to determine surface orientation
  • Colors
    • Used to be determined by assigning values to vertices
    • Can be used to provide more information to vertices still
    • Can be done as bytes or floats
    • Bytes generally better
  • UVs
    • 2d coordinates for each vertex
    • Used to map textures
    • Create vector2d array to assign to mesh object
  • Setting Trangle Indices
    • Most difficult part
    • Find common features of topology that repeats
    • Draw it out to help find these patterns!
  • Delaunay Triangulation
    • Maximizes angle of all angles of triangles in triangulation
    • Good for GPU rendering since skinny triangles can lead to visual artifacts
    • Port of triangle.net can be found in an above link
  • Debugging Procedural Meshes
    • Very important to do
    • Use Gizmos and Debug classes
    • Rotate camera to check if tris are facing correct direction
    • Turn on wireframe rendering
  • Optimizing Meshes
    • StaticBatchingUtility: batches multiple game objects together into fewer drawcalls but where each can still be culled
    • Mesh.CombineMeshes: Puts many meshes into one, good if individual meshes not likely to cull, but can break things if not done carefully
  • Optimizing Dynamic Meshes
    • Mesh.MarkDynamic: allocates a memory buffer of sorts to designated mesh to let graphics API know mesh data will change often
    • Base data structure to store data in is flat arrays when possible
    • Lists can be used too, especially if you are unsure how many vertices you will need before creating mesh
    • Frustrum Culling
    • Skinned Meshes
  • Multithreaded Mesh Generation: Unity API not thread safe
  • Compute Shaders
    • GPU great for running parallel tasks
    • Only available on modern platforms: PS4, Xbone, DirectX 11, OpenGL 4.3, OpenGL ES 3.1
    • Unity can cross compile with these options
Summary

Procedurally generating a mesh starts with creating vertices, and then triangles from these vertices. Normals and UVs can be added to this data for more informed meshes. In Unity, your mesh will need a MeshFilter and MeshRenderer component. Debugging your mesh is important, and some tools in Unity to help with that are the gizmo and debug classes, moving the camera around, and turning on wireframe rendering. There are also several options to look into to either optimize your existing meshes how they are, or compute them more efficiently.

More to Research
  • Bezier Curves: parametric mathematical curves used commonly in generating computer graphics
  • Use of GPU to do parallel calculations, and how to connect that with Unity scripting
  • Thread and Multithread Mesh Generation: Not sure what this actually meant
  • Computing shaders
  • Talk through Unity by “Sir You’re Being Hunted” on 3D Mesh Generation