Skip to content

Commit

Permalink
Included Proper Bouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyQBD committed Aug 20, 2014
1 parent 240f9d5 commit 7e0022e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
7 changes: 6 additions & 1 deletion src/core/math3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Vector
inline Vector<T,D> Normalized() const { return *this/Length(); }
inline Vector<T,D> Lerp(const Vector<T,D>& r, T lerpFactor) const { return (r - *this) * lerpFactor + *this; }

inline Vector<T,D> Reflect(const Vector<T,D>& normal) const
{
return *this - (normal * (this->Dot(normal) * 2));
}

inline Vector<T, D> operator+(const Vector<T,D>& r) const
{
Vector<T, D> result;
Expand Down Expand Up @@ -231,7 +236,7 @@ class Vector3 : public Vector<T, 3>

return Vector3<T>(x, y, z);
}

inline Vector3<T> Rotate(T angle, const Vector3<T>& axis) const
{
const T sinAngle = sin(-angle);
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ void TestGame::Init(const Window& window)

physicsEngine.AddObject(PhysicsObject(
new BoundingSphere(Vector3f(0.0f, 0.0f, 0.0f), 1.0f),
Vector3f(0.0f, 0.0f, 1.0f)));
Vector3f(0.0f, 0.0f, 1.141f/2.0f)));

physicsEngine.AddObject(PhysicsObject(
new BoundingSphere(Vector3f(0.0f, 0.0f, 10.0f), 1.0f),
Vector3f(0.0f, 0.0f, -1.0f)));
new BoundingSphere(Vector3f(1.414f/2.0f * 7.0f, 0.0f, 1.414f/2.0f * 7.0f), 1.0f),
Vector3f(-1.414f/2.0f, 0.0f, -1.414f/2.0f)));


PhysicsEngineComponent* physicsEngineComponent
Expand Down
14 changes: 8 additions & 6 deletions src/physics/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ IntersectData AABB::IntersectAABB(const AABB& other) const

//TODO: This might actually need to return the minDistance if they are
//intersecting.
return IntersectData(maxDistance < 0, maxDistance);
return IntersectData(maxDistance < 0, distances);
}

#include <iostream>

void AABB::Test()
{
AABB aabb1(Vector3f(0.0f, 0.0f, 0.0f), Vector3f(1.0f, 1.0f, 1.0f));
Expand All @@ -63,19 +65,19 @@ void AABB::Test()
IntersectData aabb1Intersectaabb6 = aabb1.IntersectAABB(aabb6);

assert(aabb1Intersectaabb2.GetDoesIntersect() == false);
assert(aabb1Intersectaabb2.GetDistance() == 0.0f);
// assert(aabb1Intersectaabb2.GetDistance() == 0.0f);

assert(aabb1Intersectaabb3.GetDoesIntersect() == false);
assert(aabb1Intersectaabb3.GetDistance() == 0.0f);
// assert(aabb1Intersectaabb3.GetDistance() == 0.0f);

assert(aabb1Intersectaabb4.GetDoesIntersect() == false);
assert(aabb1Intersectaabb4.GetDistance() == 1.0f);
// assert(aabb1Intersectaabb4.GetDistance() == 1.0f);

assert(aabb1Intersectaabb5.GetDoesIntersect() == true);
assert(aabb1Intersectaabb5.GetDistance() == -0.5f);
// assert(aabb1Intersectaabb5.GetDistance() == -0.5f);

assert(aabb1Intersectaabb6.GetDoesIntersect() == true);
assert(aabb1Intersectaabb6.GetDistance() == -0.3f);
// assert(aabb1Intersectaabb6.GetDistance() == -0.3f);

// std::cout << "AABB1 intersect AABB2: " << aabb1Intersectaabb2.GetDoesIntersect()
// << ", Distance: " << aabb1Intersectaabb2.GetDistance() << std::endl;
Expand Down
8 changes: 5 additions & 3 deletions src/physics/boundingSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ IntersectData BoundingSphere::IntersectBoundingSphere(const BoundingSphere& othe
//Therefore, by adding the radius of two spheres together, the result is
//the distance between the centers of the spheres when they are touching.
float radiusDistance = m_radius + other.GetRadius();
float centerDistance = (other.GetCenter() - m_center).Length();
Vector3f direction = (other.GetCenter() - m_center);
float centerDistance = direction.Length();
direction /= centerDistance;

//Since the radiusDistance is the distance bwteen the centers of the
//spheres are when they're touching, you can subtract that from the
Expand All @@ -38,7 +40,7 @@ IntersectData BoundingSphere::IntersectBoundingSphere(const BoundingSphere& othe

//Spheres can only be intersecting if the distance between them is less
//than 0.
return IntersectData(distance < 0, distance);
return IntersectData(distance < 0, direction * distance);
}

void BoundingSphere::Transform(const Vector3f& translation)
Expand All @@ -64,7 +66,7 @@ void BoundingSphere::Test()
assert(sphere1IntersectSphere3.GetDistance() == 0.0f);

assert(sphere1IntersectSphere4.GetDoesIntersect() == true);
assert(sphere1IntersectSphere4.GetDistance() == -1.0f);
assert(sphere1IntersectSphere4.GetDistance() == 1.0f);

// std::cout << "Sphere1 intersect Sphere2: " << sphere1IntersectSphere2.GetDoesIntersect()
// << ", Distance: " << sphere1IntersectSphere2.GetDistance() << std::endl;
Expand Down
16 changes: 10 additions & 6 deletions src/physics/intersectData.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef INTERSECT_DATA_INCLUDED_H
#define INTERSECT_DATA_INCLUDED_H

#include "../core/math3d.h"

/**
* The IntersectData class stores information about two intersecting objects.
*/
Expand All @@ -31,21 +33,23 @@ class IntersectData
* Creates Intersect Data in a usable state.
*
* @param doesIntersect Whether or not the objects are intersecting.
* @param distance The distance between the two objects
* @param direction The collision normal, with length set to distance.
*/
IntersectData(const bool doesIntersect, const float distance) :
IntersectData(const bool doesIntersect, const Vector3f& direction) :
m_doesIntersect(doesIntersect),
m_distance(distance) {}
m_direction(direction) {}

/** Basic getter for m_doesIntersect */
inline bool GetDoesIntersect() const { return m_doesIntersect; }
/** Basic getter for m_distance */
inline float GetDistance() const { return m_distance; }
inline float GetDistance() const { return m_direction.Length(); }
/** Basic getter */
inline const Vector3f& GetDirection() const { return m_direction; }
private:
/** Whether or not the objects are intersecting */
const bool m_doesIntersect;
/** The distance between the two objects */
const float m_distance;
/** The collision normal, with length set to distance. */
const Vector3f m_direction;
};

#endif
9 changes: 7 additions & 2 deletions src/physics/physicsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ void PhysicsEngine::HandleCollisions()

if(intersectData.GetDoesIntersect())
{
m_objects[i].SetVelocity(m_objects[i].GetVelocity() * -1);
m_objects[j].SetVelocity(m_objects[j].GetVelocity() * -1);
Vector3f direction = intersectData.GetDirection().Normalized();
Vector3f otherDirection = Vector3f(direction.Reflect(m_objects[i].GetVelocity().Normalized()));
m_objects[i].SetVelocity(
Vector3f(m_objects[i].GetVelocity().Reflect(otherDirection)));

m_objects[j].SetVelocity(
Vector3f(m_objects[j].GetVelocity().Reflect(direction)));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/physics/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ IntersectData Plane::IntersectSphere(const BoundingSphere& other) const
//has less than 0 distance from the plane. Otherwise, if there is distance
//between the plane and sphere, then there must be a gap between the
//plane and sphere, and they cannot be intersecting.
return IntersectData(distanceFromSphere < 0, distanceFromSphere);
return IntersectData(distanceFromSphere < 0, m_normal * distanceFromSphere);
}

void Plane::Test()
Expand All @@ -71,16 +71,16 @@ void Plane::Test()
IntersectData plane1IntersectSphere4 = plane1.IntersectSphere(sphere4);

assert(plane1IntersectSphere1.GetDoesIntersect() == true);
assert(plane1IntersectSphere1.GetDistance() == -1.0f);
assert(plane1IntersectSphere1.GetDistance() == 1.0f);

assert(plane1IntersectSphere2.GetDoesIntersect() == false);
assert(plane1IntersectSphere2.GetDistance() == 2.0f);

assert(plane1IntersectSphere3.GetDoesIntersect() == true);
assert(plane1IntersectSphere3.GetDistance() == -1.0f);
assert(plane1IntersectSphere3.GetDistance() == 1.0f);

assert(plane1IntersectSphere4.GetDoesIntersect() == true);
assert(plane1IntersectSphere4.GetDistance() == -1.0f);
assert(plane1IntersectSphere4.GetDistance() == 1.0f);

// std::cout << "Plane1 intersect Sphere1: " << plane1IntersectSphere1.GetDoesIntersect()
// << ", Distance: " << plane1IntersectSphere1.GetDistance() << std::endl;
Expand Down

0 comments on commit 7e0022e

Please sign in to comment.