123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
- /*================================================================
- *
- * Project : Hel
- * Author : Terry 'Mongoose' Hendrix II
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object : Simulation
- * License : No use w/o permission (C) 2003 Mongoose
- * Comments: This class simulates mass physics
- *
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- Test Defines -----------------------------------------------
- *
- * UNIT_TEST_SIMULATION - Builds Simulation class as a console unit test
- *
- *-- History ------------------------------------------------
- *
- * 2003.06.08:
- * Mongoose - Generic collision and basic resolution w/o generic
- * Hel Resolvers
- *
- * 2003.06.02:
- * Mongoose - Created, based on algorithms from Erkin Tunca
- ================================================================*/
-
-
- #ifndef GUARD__HEL_MONGOOSE_SIMULATION_H_
- #define GUARD__HEL_MONGOOSE_SIMULATION_H_
-
- #include <mstl/Vector.h>
- #include <hel/CollisionObject.h>
- #include <hel/Mass.h>
- #include <hel/math.h>
-
-
- class Simulation
- {
- public:
-
- ////////////////////////////////////////////////////////////
- // Constructors
- ////////////////////////////////////////////////////////////
-
- Simulation();
- /*------------------------------------------------------
- * Pre :
- * Post : Constructs an object of Simulation
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual ~Simulation();
- /*------------------------------------------------------
- * Pre : Simulation object is allocated
- * Post : Deconstructs an object of Simulation
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
-
- ////////////////////////////////////////////////////////////
- // Public Accessors
- ////////////////////////////////////////////////////////////
-
- Mass *getMass(unsigned int i);
- /*------------------------------------------------------
- * Pre :
- * Post : Get <i>th Mass element of this simulation
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
-
- ////////////////////////////////////////////////////////////
- // Public Mutators
- ////////////////////////////////////////////////////////////
-
- virtual void attachCollisionObject(CollisionObject *obj);
- /*------------------------------------------------------
- * Pre :
- * Post : Adds <Obj> to the collision list for this simulation
- *
- * TODO : Allow multiple types of collision to be excluded?
- *
- *-- History ------------------------------------------
- *
- * 2003.06.08:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void clear();
- /*------------------------------------------------------
- * Pre :
- * Post : Removes all masses from this simulation
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual bool collisionDetected(Mass *mass);
- /*------------------------------------------------------
- * Pre :
- * Post : Returns true if <Mass> collided with object
- *
- *-- History ------------------------------------------
- *
- * 2003.06.08:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void generateMasses(unsigned int count, vec_t mass);
- /*------------------------------------------------------
- * Pre :
- * Post : <Count> number of masses with <mass> value are added
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void generateMasses(unsigned int count, vec_t mass,
- const Vector3d &position,
- const Vector3d &velocity);
- /*------------------------------------------------------
- * Pre :
- * Post : <Count> number of masses with
- * <mass>, <position>, and <velocity> are added
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void init();
- /*------------------------------------------------------
- * Pre :
- * Post : Reset forces acting on masses
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void resolveCollision(Mass *mass, CollisionObject *obj);
- /*------------------------------------------------------
- * Pre :
- * Post : Resolves a collision of <Obj> and <Mass>
- *
- * The default resolution here is to set mass pos
- * to intersection point with the object, there
- * is no affect on velocity
- *
- *-- History ------------------------------------------
- *
- * 2003.06.08:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void run(vec_t timeDelta);
- /*------------------------------------------------------
- * Pre :
- * Post : Does full init, solve, simulate cycle
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void simulate(vec_t timeDelta);
- /*------------------------------------------------------
- * Pre :
- * Post : Update change in time for each mass
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- virtual void solve ();
- /*------------------------------------------------------
- * Pre :
- * Post : Have forces act on masses
- *
- *-- History ------------------------------------------
- *
- * 2003.06.02:
- * Mongoose - Created
- ------------------------------------------------------*/
-
- Vector<Mass *> mMasses; /* It's a collection of masses, silly */
-
- Vector<CollisionObject *> mObjects; /* Object(s) to test collision with */
-
- unsigned int mMassCount; /* Cache of number of masses in collection */
-
-
- private:
-
- ////////////////////////////////////////////////////////////
- // Private Accessors
- ////////////////////////////////////////////////////////////
-
-
- ////////////////////////////////////////////////////////////
- // Private Mutators
- ////////////////////////////////////////////////////////////
- };
-
-
- class GravitySimulation : public Simulation
- {
- public:
-
- GravitySimulation() : Simulation()
- {
- gravitation = Vector3d(0.0f, -9.81f, 0.0f);
- }
-
-
- virtual void solve()
- {
- Mass *m;
-
- // Apply gravitational force to each mass
- for (mMasses.start(); mMasses.forward(); mMasses.next())
- {
- m = mMasses.current();
-
- // Gravitational force, Force = (Mass)(Gravity)
- m->applyForce(gravitation * m->mMass);
-
- if (collisionDetected(m))
- {
- m->mVelocity = Vector3d(0, 0, 0);
- }
- }
- }
-
-
- Vector3d gravitation; /* Gravitational acceleration */
- };
-
-
- class SpringSimulation : public Simulation
- {
- public:
-
- SpringSimulation() : Simulation()
- {
- }
-
-
- virtual void generateMasses(unsigned int count, vec_t mass,
- const Vector3d &position,
- const Vector3d &velocity)
- {
- Simulation::generateMasses(count, mass,
- connectionPos + position, velocity);
- }
-
-
- virtual void solve()
- {
- Mass *m;
-
- // Apply spring force to each mass
- for (mMasses.start(); mMasses.forward(); mMasses.next())
- {
- m = mMasses.current();
-
- // Vector from mass to spring connection
- Vector3d springVector = m->mPos - connectionPos;
-
- // Apply spring force, spring force formulation
- m->applyForce(-springVector * springConstant);
-
- if (collisionDetected(m))
- {
- //m->mVelocity = Vector3d(0, 0, 0);
- }
- }
- }
-
- vec_t springConstant; /* Stiffness, higher values more spring force */
-
- Vector3d connectionPos; /* Location where mass is connected to spring */
- };
- #endif
|