[Teleus-cvs] teleus/src/obj collision.cpp,NONE,1.1 collision.hpp,NONE,1.1 sphere.cpp,NONE,1.1 sphere
Status: Inactive
Brought to you by:
spiffgq
|
From: Daniel R. <sp...@us...> - 2004-05-25 23:08:18
|
Update of /cvsroot/teleus/teleus/src/obj In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16978/src/obj Added Files: collision.cpp collision.hpp sphere.cpp sphere.hpp Log Message: Added obj::Sphere and obj::Collision classes. --- NEW FILE: sphere.cpp --- /* * Teleus Space Combat Simulator * Copyright 2004 Daniel Royer * sp...@us... * http://teleus.sf.net * * This package is free software; you can redistribute it and/or * modify it under the terms of the Teleus Artistic License as * detailed on the Teleus website. * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE. * * A copy of the Teleus Artistic License is provided along with * this source code package. You can also find a copy at or * http://teleus.sf.net/license.php * */ /* * $Revision: 1.1 $ * $Date: 2004/05/25 23:08:09 $ */ #include "sphere.hpp" #include "object.hpp" #include "ship.hpp" #include "math/vector.hpp" #include "math/geometry.hpp" #include "gfx/gfx.hpp" #include "gfx/color.hpp" #include "debug.hpp" namespace teleus { namespace obj { Sphere::Sphere (float radius, const gfx::Color3f& color, unsigned int slices, unsigned int stacks, const Vector3d& pos, const Vector3d& vel) : Ship(), mColor (color), mGeometry(slices, stacks) { setRadius(radius); setPos(pos); setVel (vel); } Sphere::Sphere (float radius, const gfx::Color3f& color, const Vector3d& pos, const Vector3d& vel) : Ship(), mColor (color), mGeometry(12, 12) { setRadius(radius); setPos(pos); setVel (vel); } Sphere::~Sphere () {/* geometry is deleted in ~ApproxSphere() */ } void Sphere::setStacks (unsigned int /*stacks*/) { //mGeometry = math::ApproxSphere(mGeometry.slices(), stacks); #warning Sphere::setStacks() not written assertNotReached(); } unsigned int Sphere::stacks () const { return mGeometry.stacks(); } void Sphere::setSlices (unsigned int /*slices*/) { #warning Sphere::setSlices() not written assertNotReached(); } unsigned int Sphere::slices () const { return mGeometry.slices(); } void Sphere::setGeometry (const math::ApproxSphere& /*copy*/) { #warning Sphere::setGeometry() not written assertNotReached(); } const math::ApproxSphere Sphere::geometry() const { return mGeometry; } /** * \brief Updates the physics * * Updates the location and orientation of the object using * parent class methods. * * \param timeInc the time increment in seconds * * \author Daniel Royer */ void Sphere::updatePhysics (double timeInc){ //resetForces(); Ship::updatePhysics(timeInc); } /** * \brief Changes this object's color. * * \param c the new color * \author Daniel Royer */ void Sphere::setColor (const gfx::Color3f& c){ mColor = c; } /** * \brief Returns this object's color * * \returns this object's color * \author Daniel Royer */ const gfx::Color3f& Sphere::color () const{ return mColor; } /** * \class obj::Sphere obj/sphere.hpp "obj/cube.hpp" * \brief A spherical object * * This is just a temporary class that I am using to test the * collision detection and response part of the engine. * * \author Daniel Royer */ } // END NAMESPACE obj } // END NAMESPACE teleus /** * \file obj/sphere.cpp * \brief The source file for the obj::Sphere class. * \author Daniel Royer */ /** * \file obj/sphere.hpp * \brief The header file for the obj::Sphere class. * \author Daniel Royer */ --- NEW FILE: sphere.hpp --- /* * Teleus Space Combat Simulator * Copyright 2004 Daniel Royer * sp...@us... * http://teleus.sf.net * * This package is free software; you can redistribute it and/or * modify it under the terms of the Teleus Artistic License as * detailed on the Teleus website. * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE. * * A copy of the Teleus Artistic License is provided along with * this source code package. You can also find a copy at or * http://teleus.sf.net/license.php * */ /* * $Revision: 1.1 $ * $Date: 2004/05/25 23:08:09 $ */ #ifndef TELEUS_SPHERE_OBJECT_HEADER #define TELEUS_SPHERE_OBJECT_HEADER 1 #include "object.hpp" #include "ship.hpp" #include "gfx/gfx.hpp" #include "gfx/color.hpp" #include "math/geometry.hpp" namespace teleus { namespace obj { class Sphere : public Ship { protected: gfx::Color3f mColor; math::ApproxSphere mGeometry; public: Sphere (float radius, const gfx::Color3f& color, unsigned int slices, unsigned int stacks, const Vector3d& pos, const Vector3d& vel); Sphere (float radius, const gfx::Color3f& color, const Vector3d& pos, const Vector3d& vel); virtual ~Sphere (); virtual void updatePhysics (const double timeInc); void setColor (const gfx::Color3f&); const gfx::Color3f& color () const; void setStacks (unsigned int stacks); unsigned int stacks () const; void setSlices (unsigned int slices); unsigned int slices () const; void setGeometry (const math::ApproxSphere&); const math::ApproxSphere geometry() const; }; } // END NAMESPACE obj } // END NAMESPACE teleus #endif --- NEW FILE: collision.cpp --- /* * Teleus Space Combat Simulator * Copyright 2004 Daniel Royer * sp...@us... * http://teleus.sf.net * * This package is free software; you can redistribute it and/or * modify it under the terms of the Teleus Artistic License as * detailed on the Teleus website. * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE. * * A copy of the Teleus Artistic License is provided along with * this source code package. You can also find a copy at or * http://teleus.sf.net/license.php * */ /* * $Revision: 1.1 $ * $Date: 2004/05/25 23:08:09 $ */ #include "collision.hpp" #include "object.hpp" #include "math/vector.hpp" #include "debug.hpp" namespace teleus { namespace obj { Collision::Collision () : mRefObj(0), mColObj(0), mStatus(Collision::NONE) {} Collision::Collision (Object* ref, Object* col) : mRefObj(ref), mColObj(col) { calcCollision(); } Collision::Collision (const Collision& copy) : mRefObj(copy.mRefObj), mColObj(copy.mColObj), mNormal(copy.mNormal), mPoint(copy.mPoint), mTangent(copy.mTangent), mRelVel(copy.mRelVel), mRelAccel(copy.mRelAccel), mStatus(copy.mStatus) { // TODO: Maybe it would be a good idea to validate the new // values? } const Object* Collision::refObj () const { return mRefObj; } const Object* Collision::colObj () const { return mColObj; } const Vector3d& Collision::normal() const { return mNormal; } const Vector3d& Collision::point() const { return mPoint; } const Vector3d& Collision::tangent() const { return mTangent; } const Vector3d& Collision::relVel() const { return mRelVel; } const Vector3d& Collision::relAccel() const { return mRelAccel; } Collision::STATUS Collision::status() const { return mStatus; } bool Collision::colliding() const { return mStatus == CONTACT; } bool Collision::penetrating () const { return mStatus == PENETRATION; } bool Collision::touching () const { return colliding() || penetrating(); } void Collision::setRefObj (Object * ref) { mRefObj = ref; calcCollision(); } void Collision::setColObj (Object * col) { mColObj = col; calcCollision (); } void Collision::calcCollision() { #warning Collision::calcCollision() not written yet assertNotReached(); // First, we reset the collision data. zeroData(); // we need to make sure we have two non-null objects // to check. if (mRefObj == 0 || mColObj == 0){ return; } // The bounding spheres intersect. There _may_ be a collision, // so we must run some further checks. // TODO: // Check objects' geometries to see if they are colliding or penetrating. // Calculate the collision normal // Calculate the collision point // Calculate the collision tangent // Calculate the relative velocity of the collision object // Calculate the relative acceleration of the collision object } void Collision::zeroData() { mNormal.zero(); mPoint.zero(); mTangent.zero(); mRelVel.zero(); mRelAccel.zero(); mStatus = Collision::NONE; } bool Collision::boundingSphereCheck (const Object& a, const Object& b) { // // from http://www.gamedev.net/reference/articles/article1234.asp // // TODO: This method doesn't work as written. We need to add // a check to see if the objects passed completely through // each other in a single time step. For example, consider // two spheres of radius 50 traveling toward each other from // starting points 1200 units apart at a velocity of 10,000 // units each. In a single time step of greater than 0.06 // seconds, the objects will have passed completely through // each other and this collision detection scheme won't even // have noticed. // // Not to worry, though. A simple solution is to use the // last position, stored in PointMass, and see if a line from // it to the current position intersects the path of the // other object to within bounding-sphere tolerance. Vector3d relVel = b.vel() - a.vel(); Vector3d relPos = b.pos() - a.pos(); double minDist = a.radius() + b.radius(); double pp = relPos.lengthSquared() - (minDist * minDist); /*if (pp < 0.0){ DEBUG_PRINT ("Collision::boundingSphereCheck() true by pp\n"); return true; }*/ return pp <= 0.0; /* double pv = relPos.x * relVel.x + relPos.y * relVel.y + relPos.z * relVel.z; if (pv >= 0.0){ DEBUG_PRINT ("Collision::boundingSphereCheck() true by pv\n"); return false; } double vv = relVel.lengthSquared(); if ( (pv + vv) <= 0.0 && (vv + 2.0 * pv + pp) >= 0){ DEBUG_PRINT ("Collision::boundingSphereCheck() false\n"); return false; } double tmin = -pv / vv; return (pp + pv * tmin > 0.0);*/ } } // END NAMESPACE obj } // END NAMESPACE teleus --- NEW FILE: collision.hpp --- /* * Teleus Space Combat Simulator * Copyright 2004 Daniel Royer * sp...@us... * http://teleus.sf.net * * This package is free software; you can redistribute it and/or * modify it under the terms of the Teleus Artistic License as * detailed on the Teleus website. * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE. * * A copy of the Teleus Artistic License is provided along with * this source code package. You can also find a copy at or * http://teleus.sf.net/license.php * */ /* * $Revision: 1.1 $ * $Date: 2004/05/25 23:08:09 $ */ #ifndef TELEUS_OBJECT_COLLISION_HEADER #define TELEUS_OBJECT_COLLISION_HEADER 1 #include "object.hpp" #include "math/vector.hpp" namespace teleus { namespace obj { using math::Vector3d; class Collision { public: enum STATUS{NONE, CONTACT, PENETRATION}; private: Object *mRefObj; Object *mColObj; Vector3d mNormal; Vector3d mPoint; Vector3d mTangent; Vector3d mRelVel; Vector3d mRelAccel; STATUS mStatus; void calcCollision(); void zeroData(); public: Collision (); Collision (Object*, Object*); Collision (const Collision&); const Object* refObj () const; const Object* colObj () const; const Vector3d& normal() const; const Vector3d& point() const; const Vector3d& tangent() const; const Vector3d& relVel() const; const Vector3d& relAccel() const; STATUS status() const; bool colliding() const; bool penetrating () const; bool touching () const; void setRefObj (Object *); void setColObj (Object *); // TODO: More methods: // update // impulse force static bool boundingSphereCheck (const Object& a, const Object& b); }; } // END NAMESPACE obj } // END NAMESPACE teleus #endif |