NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
ColliderSphere.cpp
Go to the documentation of this file.
00001 
00011 #pragma once
00012 
00013 #include <boost/lexical_cast.hpp>
00014 
00015 #include "ColliderSphere.h"
00016 
00017 #include "../../../sharedbase/Entity.h"
00018 #include "../../../sharedbase/EventLogger.h"
00019 
00020 namespace Physics {
00021   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00022   ColliderSphere::ColliderSphere(EntitySPTR owner, ModuleInterface* module) : InterfaceCollider(owner, module) {
00023     this->primaryBuffer = ColliderSphereDataPointer(new ColliderSphereData());
00024     this->secondaryBuffer = ColliderSphereDataPointer(new ColliderSphereData());
00025     
00026     this->SetRadius(1.0f);
00027   }
00028   
00029   ColliderSphere::~ColliderSphere() {
00030   }
00031   
00032   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00033   bool ColliderSphere::SetRadius(float radius) {
00034     if (radius > 0.0f && radius < 1000000.0f) { // sanity check
00035       {
00036         Threading::WriteLock w_lock(this->secondaryBuffer->mutex);
00037         this->secondaryBuffer->radius = radius;
00038       }
00039       
00040       // Buffer swap here as this is the only writer.
00041       {
00042         Threading::WriteLock w1_lock(this->secondaryBuffer->mutex);
00043         Threading::WriteLock w2_lock(this->primaryBuffer->mutex);
00044         Threading::Swap<ColliderSphereDataPointer>(this->primaryBuffer, this->secondaryBuffer);
00045       }
00046       
00047       return true;
00048     }
00049     
00050     LOG(LOG_PRIORITY::INFO, "Attempted to set an invalid radius of " + boost::lexical_cast<std::string>(radius));
00051     
00052     return false;
00053   }
00054   
00055   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00056   BoundingSphere ColliderSphere::GetBoundingSphere() const {
00057     Threading::ReadLock lock(this->primaryBuffer->mutex);
00058     
00059     BoundingSphere sphere = {
00060       this->owner->GetWorldPosition(),
00061       this->primaryBuffer->radius
00062     };
00063     
00064     return sphere;
00065   }
00066   
00067   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00068   AABB ColliderSphere::GetAABB() const {
00069     Threading::ReadLock lock(this->primaryBuffer->mutex);
00070     
00071     Vector3D location = this->owner->GetWorldPosition();
00072     
00073     AABB aabb;
00074     
00075     aabb.lowestCorner.x = location.x - this->primaryBuffer->radius;
00076     aabb.lowestCorner.y = location.y - this->primaryBuffer->radius;
00077     aabb.lowestCorner.z = location.z - this->primaryBuffer->radius;
00078     
00079     aabb.highestCorner.x = location.x + this->primaryBuffer->radius;
00080     aabb.highestCorner.y = location.y + this->primaryBuffer->radius;
00081     aabb.highestCorner.z = location.z + this->primaryBuffer->radius;
00082     
00083     return aabb;
00084   }
00085   
00086   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00087   bool ColliderSphere::CollideRay(const Vector3D& source, const Vector3D& direction) const {
00088     Threading::ReadLock lock(this->primaryBuffer->mutex);
00089     
00090     return D3DXSphereBoundProbe(&this->owner->GetWorldPosition(), this->primaryBuffer->radius, &source, &direction) == TRUE;
00091   }
00092   
00093   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00094   EntitySPTR ColliderSphere::GetOwnerEntity() const {
00095     return this->GetOwner();
00096   }
00097 }