ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
BoundarySphere.cpp
Go to the documentation of this file.
1#include "BoundarySphere.h"
2
4 this->object = object;
5 this->position = object->GetTransform()->GetPosition();
6 this->radius = radius;
7}
8
9BoundarySphere::BoundarySphere(Object3D* object, Vector3D position, float radius) {
10 this->object = object;
11 this->position = position;
12 this->radius = radius;
13}
14
16 return radius;
17}
18
22
23void BoundarySphere::Update(float dT, Vector3D acceleration, Quaternion rotation) {
24 Quaternion rotationChange = rotation.Multiply(previousRotation.MultiplicativeInverse());
25 velocity = rotationChange.RotateVector(velocity) * 0.999f + acceleration * dT; // maintain momentum of existing velocity, apply local acceleration
26 velocity = velocity.Constrain(-2500, 2500);
27 position = position + velocity * dT;
28
29 previousRotation = rotation;
30}
31
33 return radius + bO->GetRadius() > fabs((position - bO->position).Magnitude());
34}
35
36void BoundarySphere::Collide(float elasticity, BoundarySphere* bO) {
37 if (IsIntersecting(bO)) {
38 // collision
39 Vector3D direction = (position - bO->position).Normal();
40 Vector3D vDiff = velocity - bO->velocity;
41 float fellingSpeed = vDiff.DotProduct(direction);
42
43 if (fellingSpeed >= 0) {
44 return;
45 }
46
47 float mass1 = 1.0f;
48 float mass2 = 1.0f;
49
50 float speed1 = (2 * mass2 * fellingSpeed) / (mass1 + mass2);
51 float speed2 = (fellingSpeed * (mass2 - mass1)) / (mass1 + mass2);
52
53 bO->velocity = bO->velocity + direction * speed1;
54 this->velocity = this->velocity + direction * (speed2 - fellingSpeed);
55 } // else{//no collision}
56}
Defines the BoundarySphere class for simulating spherical boundary objects.
Represents a spherical boundary object for motion and collision simulations.
void Update(float dT, Vector3D acceleration, Quaternion rotation)
Updates the sphere's position and velocity based on acceleration and rotation.
BoundarySphere(Object3D *object, float radius)
Constructs a BoundarySphere with a given object and radius.
Object3D * object
Pointer to the 3D object associated with the sphere.
Vector3D velocity
Velocity vector of the sphere.
float radius
Radius of the sphere.
bool IsIntersecting(BoundarySphere *bO)
Checks if the sphere intersects with another BoundarySphere.
Vector3D position
Current position of the sphere.
float GetRadius()
Gets the radius of the sphere.
void Collide(float elasticity, BoundarySphere *bO)
Resolves collision between this sphere and another BoundarySphere.
Object3D * GetObject3D()
Gets the associated Object3D of the sphere.
Quaternion previousRotation
Previous rotation of the sphere.
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
Vector3D Constrain(const float &minimum, const float &maximum) const
Constrains each component of this vector between two scalar bounds.
Definition Vector3D.cpp:120
float DotProduct(const Vector3D &vector) const
Computes the dot product of this vector with another Vector3D.
Definition Vector3D.cpp:155