PyGame-based visualization for 2D RayCaster
The next sections explain the mathematical concepts behind ray casting and how it works in 2D. For installation and getting this running on your machine scroll down
Ray casting is a technique used in computer graphics and computational geometry to project rays from a point (usually representing the position of an observer or a light source) into a scene to determine the visibility of objects, calculate intersections, and simulate various optical effects.
The basic idea of ray casting involves tracing the path of rays from a specific origin point in a particular direction through a scene. Each ray continues until it intersects with an object in the scene or reaches a maximum distance. The intersections of these rays with objects can then be used for various purposes, such as:
- Visibility Determination: Determining which objects or parts of objects are visible from a certain viewpoint.
- Shadow Casting: Calculating shadows by tracing rays from light sources to surfaces.
- Collision Detection: Determining if and where a moving object intersects other objects.
Ray casting is used in many areas, including:
- Computer Graphics: Ray casting is a fundamental part of ray tracing, a rendering technique used to generate photorealistic images by simulating the way rays of light interact with surfaces.
- Game Development: Used for line-of-sight calculations, AI vision, and more.
- Physics Simulations: Determining paths of particles and waves, calculating reflections and refractions.
- Robotics: Helping robots understand their environment by simulating sensors like LIDAR.
- Ray Generation: Rays are generated from the origin point in specific directions. In graphics, these rays are often generated per pixel in the image plane.
- Intersection Calculation: For each ray, the algorithm checks for intersections with objects in the scene.
- Ray visualization: After an intersection is found the rays are visualized to show the collision points
Consider a simple 2D example where we want to determine which parts of a scene are visible from a given point:
- Generate Rays: Rays are cast from the observer's position in various directions.
- Check Intersections: Each ray is checked for intersections with objects in the scene.
- Determine Visibility: If a ray intersects an object, that part of the object is considered visible.
In this Pygame implementation of ray casting, we start by setting up a black screen and then casting rays from the player's position to determine visibility and render the scene accordingly.
Consider a particle located at a point
The direction vector
The endpoint
Now, let's determine the intersection of this ray with a wall represented by the line segment between points
The intersection occurs when:
This can be written as a system of linear equations:
Solving for t
and u
yields:
For the intersection to be valid:
-
$0 \leq u \leq 1$ : The intersection point lies within the segment. -
$t \geq 0$ : The intersection point lies on the ray extending from the particle.
If these conditions are met, the intersection point is:
- Python 3.x
- Clone the repository
- Navigate to the project directory
This simulation uses Pygame for visualization and Numpy for vectorization. If you haven't installed Pygame or Numpy yet, you can install them using pip. Open your terminal and run the following command:
pip install pygame numpy
To run the simulation, run main.py
python main.py
this should start a pygame window where you can move the particle by moving the mouse, change the environment by using mouse buttons and increasing/decreasing the number of rays via scroll wheel