How do you use vectors, dot product and cross product in game development?
What the interviewer wants to hear
- Vectors represent positions, directions and velocities
- Normalising gives a direction of length 1
- Dot product: angle between vectors, front/behind, view cones, lighting
- Cross product: perpendicular vector, left/right test, surface normals
- Distance and magnitude for range checks
Model answer
Vectors are everywhere in games. A position is a point, a velocity is a direction with speed, and the difference between two positions gives the direction and distance from one object to another. Normalising a vector gives a pure direction of length 1. The dot product of two unit vectors gives the cosine of the angle between them, so I use it to check whether an enemy is in front of the player, to build a view cone, or for lighting, where light intensity depends on the angle to the surface normal. The cross product gives a vector perpendicular to two others. I use it to find surface normals, to build a coordinate frame for a camera, and to check whether a target is to the left or right of the player.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Game Developer questions
- What is a game loop, and why is delta time important?
- In Unity, what is the difference between Awake, Start, Update, FixedUpdate and LateUpdate?
- How does collision detection work in a game engine, and what is the difference between a collider and a trigger?
- Tell me about a game or game feature you built. What was the hardest problem and how did you solve it?
- Why do game engines like Unity and Unreal favour components over deep inheritance trees for game objects?
- How would you set up version control for a small game team with programmers and artists?
- What do you do as a programmer to make a game "feel" good to play?
- What are coroutines in Unity, and when would you use them instead of Update or async/await?
- How do you use prefabs, nested prefabs and prefab variants to keep a Unity project organised?