User Tools

Site Tools


3d_geometric_shapes

3D Geometry in Impulse Physics

Currently, there is support for the following primitives:

  • AABB
  • Line
  • OrientedBox
  • Plane
  • Rays (for use in Raycasting)
  • Sphere
  • Triangle

AABB

An Axis-Aligned Bounding Box, or 'AABB', is as the name implies a box that is aligned with the coordinate system's axes. It is a rectangular box that completely encloses an object or set of objects by defining the minimum and maximum points in each of the three dimensions. The edges of the box are parallel to the x, y, and z axes, which simplifies collision detection and spatial calculations.

The AABB (Axis-Aligned Bounding Box) struct represents a rectangular cuboid in 3D space that is aligned with the X, Y, and Z axes.

It has three fields:

  1. origin: a Point (x,y,z) representing the center of the cuboid
  2. size: a Vector3 (x,y,z) representing the length, width, and height of the cuboid

It also has three constructors:

  1. AABB(): sets the origin to (0,0,0) and size to (1,1,1)
  2. AABB(const Point& o, const Vector3& s): sets the origin to the given point and size to the given vector
  3. AABB(const Vector3& min, const Vector3& max, bool isHalved = true): creates an AABB from two opposing corner points, min and max. If isHalved is true (default), the origin is set to the center of the cuboid and the size is half the distance between the min and max points. If isHalved is false, the origin is set to the average of the min and max points and the size is the full distance between them.

The struct also provides two methods for getting the minimum and maximum points of the AABB:

  1. GetMin(): returns a Vector3 representing the minimum x, y, and z values of the AABB
  2. GetMax(): returns a Vector3 representing the maximum x, y, and z values of the AABB

Example usage:

AABB box1; // creates an AABB centered at (0,0,0) with size (1,1,1)
AABB box2(Point(1,2,3), Vector3(2,3,4)); // creates an AABB centered at (1,2,3) with size (2,3,4)
AABB box3(Vector3(-1,-1,-1), Vector3(1,1,1)); // creates an AABB centered at (0,0,0) with size (2,2,2)

Line

The Line struct represents a line in 3D space with a start and end point.

It has two fields:

  1. start: a Point (x,y,z) representing the starting point of the line
  2. end: a Point (x,y,z) representing the ending point of the line

It also has two constructors:

  1. Line(): sets both start and end points to (0,0,0)
  2. Line(const Point& s, const Point& e): sets the start point to the given point s and the end point to the given point e

The struct provides two static methods for getting the length of a line:

  1. Length(const Line& line): returns the length of the given line using the Magnitude() method of the vector between its start and end points
  2. LengthFast(const Line& line): returns an approximation of the length of the given line using the MagnitudeFast() method of the vector between its start and end points, which trades accuracy for speed.

Example usage:

Line line1; // creates a line with start and end points at (0,0,0)
Line line2(Point(1,2,3), Point(4,5,6)); // creates a line with start at (1,2,3) and end at (4,5,6)
float length = Line::Length(line2); // computes the length of line2 using the precise Magnitude() method
float lengthFast = Line::LengthFast(line2); // computes an approximation of the length of line2 using the faster MagnitudeFast() method

Oriented Box

The OrientedBox struct represents a box-shaped object in 3D space, oriented in any direction.

It has three fields:

  1. position: a Point (x,y,z) representing the position of the center of the box in 3D space
  2. size: a Vector3 (x,y,z) representing the dimensions of the box (length, width, height)
  3. orientation: a Matrix3x3 representing the rotation of the box in 3D space

It has three constructors:

  1. OrientedBox(): sets position to (0,0,0), size to (1,1,1), and orientation to the identity matrix (no rotation)
  2. OrientedBox(const Point& p, const Vector3& s): sets the position to the given point p, the size to the given vector s, and orientation to the identity matrix (no rotation)
  3. OrientedBox(const Point& p, const Vector3& s, const Matrix3x3& o): sets the position to the given point p, the size to the given vector s, and orientation to the given matrix o (for arbitrary rotation)

Example usage:

OrientedBox box1; // creates a unit box centered at (0,0,0) with no rotation
OrientedBox box2(Point(1,2,3), Vector3(2,3,4)); // creates a box with center at (1,2,3), size (2,3,4), and no rotation
Matrix3x3 rotation = // some arbitrary 3x3 rotation matrix
OrientedBox box3(Point(0,0,0), Vector3(1,1,1), rotation); // creates a unit box at the origin, with rotation specified by the given matrix

Plane

The Plane struct represents a mathematical plane in 3D space. It is defined by a normal vector and a distance from the origin.

It has two fields:

  1. normal: a Vector3 representing the normal vector of the plane
  2. distance: a float representing the distance of the plane from the origin along the normal vector

It has two constructors:

  1. Plane(): sets the normal vector to (1, 0, 0) and calculates the distance based on the distance from the origin to the normal vector
  2. Plane(const Vector3& n, float d): sets the normal vector to the given vector n and the distance to the given float d

It has one function:

  1. PlaneEquation(const Point& pt): takes a Point pt and returns the result of the plane equation for that point, which is the dot product of the point and the normal vector, minus the distance from the origin.

Example usage:

Plane p1; // creates a plane with normal vector (1,0,0) and distance calculated from the origin
Plane p2(Vector3(0,1,0), 5); // creates a plane with normal vector (0,1,0) and distance 5
Point pt(1,2,3);
float planeEq = p1.PlaneEquation(pt); // calculates the result of the plane equation for point pt on plane p1

Ray

The Ray struct represents a ray in 3D space. It is defined by an origin point and a direction vector.

It has two fields:

  1. origin: a Point representing the starting point of the ray
  2. direction: a Vector3 representing the direction of the ray

It has three constructors:

  1. Ray(): creates a default ray with an origin of (0, 0, 0) and a direction of (0, 0, 1)
  2. Ray(const Line& line): creates a ray from a Line object, using the start of the line as the origin and the vector between the start and end points as the direction
  3. Ray(const Point& o, const Vector3& d): creates a ray with the given origin point and direction vector. The direction vector is automatically normalized using the NormalizeDirection() function.

It has one function:

  1. NormalizeDirection(): normalizes the direction vector of the ray.

Example usage:

Ray r1; // creates a default ray with origin (0,0,0) and direction (0,0,1)
Line l1(Point(1,2,3), Point(4,5,6));
Ray r2(l1); // creates a ray with origin (1,2,3) and direction (3,3,3)
Ray r3(Point(1,2,3), Vector3(4,5,6)); // creates a ray with origin (1,2,3) and direction (0.45584, 0.5698, 0.68276)
r3.NormalizeDirection(); // normalizes the direction vector of r3

Sphere

The Sphere struct represents a sphere in 3D space. It is defined by an origin point and a radius.

It has two fields:

  1. origin: a Point representing the center of the sphere
  2. radius: a float representing the radius of the sphere

It has two constructors:

  1. Sphere(): creates a default sphere with an origin of (0, 0, 0) and a radius of 1.0
  2. Sphere(const Point& o, float r): creates a sphere with the given origin point and radius

Example usage:

Sphere s1; // creates a default sphere with center (0,0,0) and radius 1.0
Sphere s2(Point(1,2,3), 4.0f); // creates a sphere with center (1,2,3) and radius 4.0

Triangle

The Triangle struct represents a triangle in 3D space. It is defined by three vertices.

It has one union field containing:

  1. struct fields:
    1. p1: a Vector3 representing the first vertex of the triangle
    2. p2: a Vector3 representing the second vertex of the triangle
    3. p3: a Vector3 representing the third vertex of the triangle
  2. array fields:
    1. points: an array of 3 Vector3s representing the vertices of the triangle
    2. points_f: an array of 9 floats representing the vertices of the triangle as a flat array of floats

It has two constructors:

  1. Triangle(): creates a default triangle with all vertices set to the zero vector
  2. Triangle(const Vector3& p1, const Vector3& p2, const Vector3& p3): creates a triangle with the given vertices

Example usage:

Triangle t1; // creates a default triangle with all vertices at (0, 0, 0)
Triangle t2(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)); // creates a triangle with vertices (1, 2, 3), (4, 5, 6), and (7, 8, 9)
3d_geometric_shapes.txt · Last modified: 2023/04/13 19:56 by max