Exploring Vector Math with Unity - Dot Product

April 23, 2021

Dot Product Unity Project

Vector Math


References

Dot Product Wiki

Wikipedia - Link

Falstad Dotproduct Online Visualizer

Falstad Visualizer - Link
Fig. 1: My Updated Visualizer at Work

Overview

I wanted to explore vector math as a refresher for myself while also creating ways to visualize it through Unity. The dot product seemed like a good focal point to start with since it is such a useful tool in game development but can be a bit strange to fully understand the full reach it has. I took a lot of inspiration from the Falstad visualizer on how to visually represent this.

While similar to the Falstad visualizer, I wanted to create a 3D representation instead of a 2D one. I wanted to have a tool that creates two manueverable vectors that would show all the values of those vectors, as well as their dot product and the projected vector from one onto the other. While most of the math and systems behind this are practically identical in the 3D case, interacting with the vectors becomes a much trickier problem.

Dragging Objects Around 3D Space

I ended up following a tutorial on a really simple 3D dragging setup found here:

Unity Object Dragging || Tutorial: by Kjip

Youtube - Link

This provides a very quick and simple setup that at least allows the user to move objects around in 3D space using the screen to world point methods of Unity. Their exact solution however did not work for me, as the object would always gravitate to the exact position of the camera, eventually leaving it too close to interact with.

The Vector3 they were feeding into the ScreenToWorldPoint method was determined as such:

Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z + transform.position.z);

I eventually just removed the additional transform.position.z of the object added into this vector, which provided a fixed distance from the camera plane to the object at all times. This at least constantly kept it in frame, and allowed decent manueverability. The range of motion is a little weird, but it works well enough for now. I eventually just changed the entirety of the z portion of the vector to a variable float controlled from the inpspector since tying it to the camera position didn't make any sense at this point.

Visualizing the Vectors

Line Renderers

This is the most straight forward way since I can just put in the two end points of a vector as waypoints of the line renderer to directly render that vector in 3D space. However, overlapping line renderer visuals leads to poor rendering in many cases, which is not ideal for showing the overlap of the projected dot product vector with the vector being projected on to.

Rotating/Scaling a 3D Object (Cylinder)

After trying line renderers for a while, I swapped over to using primitive cylinders as a basis for the vectors. By setting the cylinders as children of an empty container gameobject, I was able to falsely move the anchor point of the cylinder for scaling to the base of the cylinders. This allowed me to scale this container to match the exact size of the vector, and this would exactly match up the size of the cylinder while also making sure it specifically scaled up/outward from the origin of the visualization.

As for rotating the cylinder to match the individual vectors, this could easily be done by setting the up vector of the cylinders. This looks like:

cylinderA.transform.up = vectorOfInterest;

Video Samples of My Project

The following links show my first prototype of this project with the line renderer visuals, and the first update using cylinders and some cleaner camera controls.

Vector Projection Simulation - Dot Product - Prototype from Steve Lilley on Vimeo.



Vector Projection Simulation - Dot Product - Update 1 from Steve Lilley on Vimeo.

Comments

Popular posts from this blog

Online Multiplayer Networking Solution Tutorial Using Unity and Mirror - Tutorial by: Jason Weimann

Exporting FBX from Houdini with Color

Sebastian Lague A* Tutorial Series - Path Smoothing - Pt. 08 and Pt. 09