ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
BoundaryCube.cpp
Go to the documentation of this file.
1#include "BoundaryCube.h"
2
4 this->centerPosition = object->GetTransform()->GetPosition() + object->GetCenterOffset();
5 object->GetMinMaxDimensions(minimum, maximum);
6}
7
8BoundaryCube::BoundaryCube(Vector3D centerPosition, Vector3D objectSize) {
9 this->centerPosition = centerPosition;
10 this->minimum = centerPosition - objectSize / 2.0f;
11 this->maximum = centerPosition + objectSize / 2.0f;
12}
13
17
21
25
29
30void BoundaryCube::Update(float dT, Vector3D acceleration, Quaternion rotation) {
31 Quaternion rotationChange = rotation.Multiply(previousRotation.MultiplicativeInverse());
32 velocity = rotationChange.RotateVector(velocity) + acceleration * dT; // maintain momentum of existing velocity, apply local acceleration
33 position = position + velocity * dT;
34
35 previousRotation = rotation;
36}
37
39 Vector3D collision;
40
41 collision.X = maximum.X >= bO->GetMinimum().X ? 0 : 1;
42 collision.X = bO->GetMaximum().X >= minimum.X ? 0 : -1;
43 collision.Y = maximum.Y >= bO->GetMinimum().Y ? 0 : 1;
44 collision.Y = bO->GetMaximum().Y >= minimum.Y ? 0 : -1;
45 collision.Z = maximum.Z >= bO->GetMinimum().Z ? 0 : 1;
46 collision.Z = bO->GetMaximum().Z >= minimum.Z ? 0 : -1;
47
48 return collision;
49}
50
52 Vector3D collision;
53 Vector3D minimumExt, maximumExt;
54 Vector3D sphereSize = Vector3D(bO->GetRadius(), bO->GetRadius(), bO->GetRadius());
55
56 minimumExt = bO->position - sphereSize;
57 maximumExt = bO->position + sphereSize;
58
59 collision.X = minimum.X <= maximumExt.X && maximum.X >= minimumExt.X;
60 collision.Y = minimum.Y <= maximumExt.Y && maximum.Y >= minimumExt.Y;
61 collision.Z = minimum.Z <= maximumExt.Z && maximum.Z >= minimumExt.Z;
62
63 return collision;
64}
65
66void BoundaryCube::CollideSphere(float elasticity, BoundarySphere* bO) {
67 // if sphere is not within the bounds of the prism, collide
68 Vector3D collision = IsIntersecting(bO);
69 Vector3D shiftCorrection;
70
71 if (collision.X)
72 shiftCorrection.X = bO->velocity.X * -0.01f;
73 if (collision.Y)
74 shiftCorrection.Y = bO->velocity.Y * -0.01f;
75 if (collision.Z)
76 shiftCorrection.Z = bO->velocity.Z * -0.01f;
77
78 bO->velocity.X = collision.X == 1 ? bO->velocity.X : -bO->velocity.X * elasticity;
79 bO->velocity.Y = collision.Y == 1 ? bO->velocity.Y : -bO->velocity.Y * elasticity;
80 bO->velocity.Z = collision.Z == 1 ? bO->velocity.Z : -bO->velocity.Z * elasticity;
81
85
86 bO->position = bO->position + shiftCorrection;
87}
Defines the BoundaryCube class for representing and managing axis-aligned bounding cubes.
Represents an axis-aligned bounding cube for collision detection and physics.
Vector3D GetMinimum()
Retrieves the minimum coordinates of the cube.
void Update(float dT, Vector3D acceleration, Quaternion rotation)
Updates the cube's state based on time, acceleration, and rotation.
Vector3D velocity
Velocity of the cube.
void CollideSphere(float elasticity, BoundarySphere *bO)
Handles collision with a BoundarySphere.
Vector3D GetSize()
Retrieves the size of the cube.
Vector3D position
Current position of the cube.
Vector3D GetMaximum()
Retrieves the maximum coordinates of the cube.
Vector3D centerPosition
Center position of the bounding cube.
Vector3D GetPosition()
Retrieves the current position of the cube.
BoundaryCube(Object3D *object)
Constructs a BoundaryCube object based on the given 3D object.
Vector3D IsIntersecting(BoundaryCube *bO)
Checks for intersection with another BoundaryCube.
Vector3D minimum
Minimum coordinates of the bounding cube.
Vector3D maximum
Maximum coordinates of the bounding cube.
Quaternion previousRotation
Previous rotation of the object.
Represents a spherical boundary object for motion and collision simulations.
Vector3D velocity
Velocity vector of the sphere.
Vector3D position
Current position of the sphere.
float GetRadius()
Gets the radius of the sphere.
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
Represents a 3D object with geometry, material, and transformation data.
Definition Object3D.h:28
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Quaternion MultiplicativeInverse() const
Returns the multiplicative inverse of this quaternion, such that q * q^-1 = identity.
Vector2D RotateVector(const Vector2D &v) const
Rotates a 2D vector by this quaternion, projecting it in 2D.
Quaternion Multiply(const Quaternion &quaternion) const
Multiplies (composes) this quaternion with another (order matters).
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29