Introduction to Vectors in Game
Programming
• Vectors are mathematical entities used to represent
magnitude and direction.
• In game programming, vectors are essential for
movement, physics, and graphics computations.
• In 2D space, a vector is typically represented as:
v⃗ =⟨x,y⟩
• x: horizontal component
• y: vertical component
• It points from the origin (0, 0) to the position (x, y) in
the 2D plane.
Types of Vectors
1. 2D Vectors: Represented as (x, y)
2. 3D Vectors: Represented as (x, y, z)
3. Unit Vectors: Vectors with magnitude 1
4. Zero Vectors: Vectors with all components as
0
Vector Addition
• Vector addition combines two vectors
component-wise.
• Example:
• A = (2, 3), B = (1, 4)
• A + B = (2+1, 3+4) = (3, 7)
• You can "chain" vectors—put the tail of B at
the head of A.
• Visually, this is like walking along vector A, then
continuing in the direction of B.
Vector Subtraction
• Vector subtraction subtracts components of one
vector from another.
• Example:
• A = (5, 2), B = (3, 1)
• A - B = (5-3, 2-1) = (2, 1)
• Shows the direction and magnitude from the
tip of B to the tip of A.
• It's useful to find relative movement between
two positions or velocities.
Scalar Multiplication
• Scalar multiplication scales a vector by a constant.
• Example:
• A = (2, 3), k = 2
• k * A = (4, 6)
• Changes the length (magnitude) of the vector
without changing its direction.
• k > 1: stretches the vector
• 0 < k < 1: shrinks the vector
• k < 0: flips the direction
Dot Product
• Dot product is a scalar representing the cosine
of the angle between two vectors.
• Formula: A · B = Ax * Bx + Ay * By
• Example:
• A = (1, 2), B = (3, 4)
• A · B = 1*3 + 2*4 = 11
Cross Product
• Cross product results in a vector perpendicular
to two input vectors (only in 3D).
• Formula: A × B = (Ay*Bz - Az*By, Az*Bx -
Ax*Bz, Ax*By - Ay*Bx)
• Example:
• A = (1, 0, 0), B = (0, 1, 0)
• A × B = (0, 0, 1)
Applications in Game Development
• 1. Movement and Direction: Vectors
determine object movement.
• 2. Collision Detection: Vectors help calculate
impact forces.
• 3. Lighting and Shading: Dot product used in
lighting models.
• 4. Physics Simulation: Vectors represent forces
and velocities.
Conclusion
• Vectors are fundamental in game
programming.
• Understanding vector operations enables
realistic and dynamic game mechanics.
Practical: 01
• Visualize Vector Addition, Subtraction, and
Scalar Multiplication.
• Draw and visualize vectors (A, B)
• Show:
– A + B (Addition)
– A - B (Subtraction)
– k × A (Scalar multiplication)
Pygame Concepts:
Concept Purpose
pygame.init() Initializes modules
pygame.display.set_mode() Creates window
pygame.draw.line() Draws vectors and axes
pygame.draw.circle() Marks vector tip
pygame.font.SysFont() Renders labels
pygame.event.get() Handles user input
Vector2 Manages vector math
*Import Libraries*
• import pygame
• import sys
pygame: Main game engine library.
sys: Used for exiting the program properly
(sys.exit()).
*Initialize Pygame & Set Up Screen*
• pygame.init()
• WIDTH, HEIGHT = 800, 600
• screen = pygame.display.set_mode((WIDTH, HEIGHT))
• pygame.display.set_caption("Vector Operations")
pygame.init(): Initializes all imported Pygame modules.
screen = ...: Creates a window of 800×600 pixels.
set_caption: Sets window title.
*Define Colors and Origin*
• WHITE, RED, GREEN, BLUE, ORANGE, PURPLE,
BLACK = ...
• origin = (WIDTH // 2, HEIGHT // 2)
• font = pygame.font.SysFont("Arial", 20)
Colors are defined as RGB tuples.
origin: Center of the screen – (400, 300).
font: A font object for rendering text.
*Define Vectors*
• A = pygame.math.Vector2(100, -50)
• B = pygame.math.Vector2(50, -100)
• scalar = 1.5
Vector2: A Pygame class for 2D vectors.
Vectors point in specific directions:
A goes right 100, up 50.
B goes right 50, up 100.
scalar: Used for multiplying A.
*Function to Draw Vectors*
• def draw_vector(v, color, label, offset=(0, 0)):
• end = (origin[0] + v.x + offset[0], origin[1] + v.y + offset[1])
• pygame.draw.line(screen, color, origin, end, 3)
• pygame.draw.circle(screen, color, end, 5)
• screen.blit(font.render(label, True, color), end)
• What it does:
• Takes a vector v, draws a line from the origin to its end.
• Draws a small circle at the end point.
• Displays the vector name (label).
• offset: Optional shift to avoid overlapping labels.
*Main Game Loop*
• running = True
• while running:
...
This loop runs until the user quits.
*Handle Events (Quit Condition)*
• for event in pygame.event.get():
• if event.type == pygame.QUIT:
• running = False
Listens for window events.
If close button is pressed, the loop ends.
*Draw Coordinate Axes*
• pygame.draw.line(screen, BLACK, (0,
HEIGHT//2), (WIDTH, HEIGHT//2), 1)
• pygame.draw.line(screen, BLACK, (WIDTH//2,
0), (WIDTH//2, HEIGHT), 1)
Draws X-axis (horizontal) and Y-axis (vertical)
through screen center.
*Draw Vectors*
• draw_vector(A, RED, "A")
• draw_vector(B, GREEN, "B")
• draw_vector(A + B, BLUE, "A+B")
• draw_vector(A - B, ORANGE, "A-B", offset=(10, 10))
• draw_vector(A * scalar, PURPLE, "1.5*A", offset=(15, -
15))
Each vector is drawn in a different color.
Subtraction and scalar multiplication use an offset to
prevent label overlap.
*Update the Screen*
• pygame.display.flip()
• pygame.time.Clock().tick(60)
flip(): Refreshes the entire display.
Clock().tick(60): Limits frame rate to 60 frames
per second.
*Quit Pygame Safely*
• pygame.quit()
• sys.exit()
Closes the Pygame window and exits the Python
script properly.