Uploaded January 2019 | Updated November 2026, 2 hours ago
How can we turn input on a single joystick into dynamic movement of a spaceship? Let's explore two new ways we might be able to do that.
## Rotating Horizontal Movement
This controller uses the vertical movement of the controller to control thrust and the horizontal movement to control the rotation of the ship. It's a bit unintuitive because the player needs to base their inputs on the orientation of the ship and that changes every single frame. On the other hand it gives direct control to both thrust and rotation which is pretty handy, especially when you need better, more precise control.
## Dot Product Based Adaptive Movement
This controller uses dot products to calculate the thrust and rotation based on the differences between your ships rotation and the movement input from a controller. Dot products calculate the angle between two vectors. To make this work we'll need to convert the input values we have into a vector before we can use it.
To calculate thrust we can use the dot product of the ships forward vector and our input vector. This means our thrust will increase as the forward direction of the ship and the input come into alignment and slow as they become more orthogonal to one another. It also means we'll reverse automatically if the input direction is greater than 90° from the direction of the ship.
To calculate the rotation we can do a very similar calculation but plug in the ships right direction instead of the forward direction. This increases the speed of our rotation as the ship gets further out of alignment with the input, and causes it to stop turning when already headed towards the destination.
A cool side effect of this solution is that ships will automatically perform a sort of 3 point turn when the input direction is behind and to the side of the ship. The ship will back up and turn away from the target and then accelerate and turn towards it. All without having to be explicitly programmed to do that!
In my own experience I've found this control scheme works really well for top down mobile games where the inputs are limited. It gives you enough control where you feel like you're in control still, while removing some of the more complex parts of having direct control over every part of a ships navigation.
***
Source Code for this series is available on GitHub: github.com/WorldOfZero/top-down-spaceship
A deeper dive into Dot Products: youtu.be/8cZo-c-f1yc
Join the World of Zero Discord: discord.gg/hU5Kq2u
How can we turn input on a single joystick into dynamic movement of a spaceship? Let's explore two new ways we might be able to do that.
## Rotating Horizontal Movement
This controller uses the vertical movement of the controller to control thrust and the horizontal movement to control the rotation of the ship. It's a bit unintuitive because the player needs to base their inputs on the orientation of the ship and that changes every single frame. On the other hand it gives direct control to both thrust and rotation which is pretty handy, especially when you need better, more precise control.
## Dot Product Based Adaptive Movement
This controller uses dot products to calculate the thrust and rotation based on the differences between your ships rotation and the movement input from a controller. Dot products calculate the angle between two vectors. To make this work we'll need to convert the input values we have into a vector before we can use it.
To calculate thrust we can use the dot product of the ships forward vector and our input vector. This means our thrust will increase as the forward direction of the ship and the input come into alignment and slow as they become more orthogonal to one another. It also means we'll reverse automatically if the input direction is greater than 90° from the direction of the ship.
To calculate the rotation we can do a very similar calculation but plug in the ships right direction instead of the forward direction. This increases the speed of our rotation as the ship gets further out of alignment with the input, and causes it to stop turning when already headed towards the destination.
A cool side effect of this solution is that ships will automatically perform a sort of 3 point turn when the input direction is behind and to the side of the ship. The ship will back up and turn away from the target and then accelerate and turn towards it. All without having to be explicitly programmed to do that!
In my own experience I've found this control scheme works really well for top down mobile games where the inputs are limited. It gives you enough control where you feel like you're in control still, while removing some of the more complex parts of having direct control over every part of a ships navigation.
***
Source Code for this series is available on GitHub: github.com/WorldOfZero/top-down-spaceship
A deeper dive into Dot Products: youtu.be/8cZo-c-f1yc
Join the World of Zero Discord: discord.gg/hU5Kq2u







![Correcting Lighting on our Grid Snapped Vertex Shader with Geometry Shaders
Lets fix our grid snapped vertex custom shader by introducing some improved lighting. Because were manipulating our objects geometry inside our shader the normals that are used to calculate lighting are incorrect. This causes objects to appear oddly smooth and lighting effects to look very different from what youd expect based on the geometry you see. We cant just correct the normals of each vertex because doing that would still cause the normal value to be interpolated along each of the triangles of our mesh (causing a smoothing effect). To resolve this were going to implement a geometry shader which allows us to dynamically recreate the mesh inside our shader so we can provide data that can be tweaked to our needs.
If present a geometry shader occurs after an a vertex shader but before the fragment shader following one of these two paths:
```mermaid
graph LR;
vert([Vertex Shader])
geom([Geometry Shader])
frag([Fragment Shader])
vert v2g ˃geom
geom g2f ˃frag
vert v2f ˃frag
```
It is not necessary for a geometry shader to recreate the same geometry that was passed in. The output can be completely modified to introduce additional information (weve used that capability in our [Grass Shader](https://youtu.be/b2AlyCNbYmY)). In this case our needs are simpler though, we are going to be using the geometry shader to isolate each triangle of our mesh. Doing this means that we can avoid interpolation caused by multiple triangles sharing a vertices without having to make those modification in our original mesh beforehand. This allows our shader to be compatible with any shape instead of just meshes specifically designed for this effect.
Our geometry shader recreates each triangle and assigns all the vertices of that triangle a unique normal so the face has uniform lighting. We also calculate the diffuse lighting we use inside of the geometry shader and pass that information along to the fragment shader. The geometry shader were using looks like this (assuming our vertex shader provides world space coordinates):
```shader
[maxvertexcount(3)]
void geom(triangle v2g IN[3], inout TriangleStream˂g2f˃ triStream)
{
float3 lightPosition = _WorldSpaceLightPos0;
float3 v0 = IN[0].pos.xyz;
float3 v1 = IN[1].pos.xyz;
float3 v2 = IN[2].pos.xyz;
float3 normal = normalize(cross(v0 - v1, v1 - v2));
float lightStrength = (dot(lightPosition, normal) + 1) / 2;
g2f OUT;
OUT.norm = normal;
OUT.lightStrength = lightStrength;
OUT.pos = mul(UNITY_MATRIX_VP, IN[0].pos);
triStream.Append(OUT);
OUT.pos = mul(UNITY_MATRIX_VP, IN[1].pos);
triStream.Append(OUT);
OUT.pos = mul(UNITY_MATRIX_VP, IN[2].pos);
triStream.Append(OUT);
}
```
***
My intro to exploring Geometry Shaders: https://youtu.be/HY6qFbmbij8
My video covering implementing diffuse lighting in more detail: https://youtu.be/4XfXOEDzBx4
A collection of other visual effects weve built: https://www.youtube.com/watch?v=l_2uGpjBMl4&list=PLEwYhelKHmig6ttzH0nRL3OOQsGLtVrtX
Watch the entire series where we explore this concept of snapping vertices to a grid: https://www.youtube.com/watch?v=e8NItqF_9hQ&list=PLEwYhelKHmiixWR3Tn5dYt1iUlSCL6OFq
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Correcting Lighting on our Grid Snapped Vertex Shader with Geometry Shaders](https://i.ytimg.com/vi/VC-v874C5xQ/mqdefault.jpg)


. This retrieves a `MonoBehavior` from any `GameObject` in our scene which matches the type were searching for. This search method is not the final solution well end up using later in development, but for testing right now its more than enough to get us what we want. We can use the reference we retrieve to replace the actual instantiation, effectively moving the parts of the code we want into a separate place.
To make things a little easier and to make sure our new component is more useful well still do some of the lifting in the Asteroids code. This includes calculating the rotation and world position of each new Chunk of the Asteroid. We do this so that the controller we built can be used to generate brand new Asteroids and not just create debris from existing ones.
There is still a bit more to do to get a fully functioning destruction system (we dont track health yet for example), but this gets us to a place where object destruction and creation works reliably well.
The base subdivision destruction system was built in a Live Stream which you can find here: https://youtu.be/POVJ-QIyygE
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Creating a Subchunk Spawner for Asteroids VR](https://i.ytimg.com/vi/VaGI6j_4e7Y/mqdefault.jpg)