Vous êtes sur la page 1sur 7

/* Start Header ****************************************************************

*/
/*!
/file PhysicsModel.h
/author Team42, Jiawei Gu
/e-mail jiawei.gu@digipen.edu
/date 11/24/2015
/Brief Copyright (C) 2015 DigiPen Institute of Technology. Reproduction or
disclosure of this file or its contents without the prior written consent of
DigiPen Institute of Technology is prohibited.
*/
/* End Header ******************************************************************
*/
#ifndef A_PHYSICS_MODEL
#define A_PHYSICS_MODEL
#include "../Kinematics/KinematicsModel.h"
#include "../Animation/Transform.h"
#include "../UI/EditorUnit.h"
namespace Anime
{
/// <summary>
/// Stick Base. Also used to represent anchor.
/// </summary>
class StickBase
{
protected:
/// <summary>
/// The center of the stick.
/// </summary>
Vector3 center;
/// <summary>
/// The total force of a stick.
/// </summary>
Vector3 netForce;
/// <summary>
/// The total torque of a stick.
/// </summary>
Vector3 netTorque;
/// <summary>
/// The current velocity of a stick.
/// </summary>
Vector3 velocity;
/// <summary>
/// The current angular velocity of a stick.
/// </summary>
Vector3 angularVelocity;
public:
/// <summary>
/// Initialize the stick base.
/// </summary>
/// <param name="center">The center of the stick.</param>
StickBase(const Vector3 & center);

/// <summary>
/// Get the left end of a stick.
/// </summary>
/// <returns>The left end of a stick.</returns>
virtual Vector3 GetLeftEnd();
/// <summary>
/// Get the right end of a stick.
/// </summary>
/// <returns>The right end of a stick.</returns>
virtual Vector3 GetRightEnd();
/// <summary>
/// Get the center of a stick.
/// </summary>
/// <returns>The center of a stick.</returns>
Vector3 GetCenter();
/// <summary>
/// Reset all the force and torque to 0.
/// </summary>
void ResetForce();
/// <summary>
/// Add force to the stick.
/// </summary>
/// <param name="addForce">The addtion force.</param>
void AddForce(const Vector3 & addForce);
/// <summary>
/// Add torque to the stick.
/// </summary>
/// <param name="addTorque">The addtion torque.</param>
void AddTorque(const Vector3 & addTorque);
friend class PhysicsModel;
};
/// <summary>
/// The concrete stick class.
/// </summary>
class Stick: public StickBase
{
private:
/// <summary>
/// The rotation angle.
/// </summary>
Radian rotationAngle;
/// <summary>
/// The rotation axis.
/// </summary>
const Vector3 rotationAxis;
/// <summary>
/// The stick length.
/// </summary>
float * length;
/// <summary>

/// The stick mass.


/// </summary>
float * mass;
/// <summary>
/// The damping for both velocity and angular velocity.
/// </summary>
float * damping;
/// <summary>
/// The moment of intetia.
/// For a stick rotating about its center: moi = mass * length^2
/ 12.
/// The list of moi can be found in https://en.wikipedia.org/wik
i/List_of_moments_of_inertia
/// </summary>
float moi;
public:
/// <summary>
/// Initialize the stick.
/// </summary>
/// <param name="center">The center of the stick.</param>
/// <param name="length">The stick length.</param>
/// <param name="mass">The stick mass.</param>
/// <param name="damping">The damping.</param>
Stick(const Vector3 & center, float * length, float * mass, floa
t * damping);
/// <summary>
/// Draw the stick.
/// </summary>
/// <param name="shader">The Shader.</param>
void Draw(Shader & shader);
/// <summary>
/// Get the left end of a stick.
/// </summary>
/// <returns>The left end of a stick.</returns>
Vector3 GetLeftEnd() override;
/// <summary>
/// Get the right end of a stick.
/// </summary>
/// <returns>The right end of a stick.</returns>
Vector3 GetRightEnd() override;
friend class PhysicsModel;
};
/// <summary>
/// The spring class.
/// </summary>
class Spring
{
private:
/// <summary>
/// The left connection of a stick.
/// </summary>
StickBase * leftConnection;

/// <summary>
/// The right connection of a stick.
/// </summary>
StickBase * rightConnection;
/// <summary>
/// VAO and VBO.
/// </summary>
GLuint VAO, VBO;
/// <summary>
/// The k in Hooke's law.
/// </summary>
float * k;
/// <summary>
/// The length when the spring is not stretched.
/// </summary>
float * normalLength;
public:
/// <summary>
/// Initialize the spring.
/// </summary>
Spring();
/// <summary>
/// Finalize the spring.
/// </summary>
~Spring();
/// <summary>
/// Draw the spring.
/// </summary>
/// <param name="shader">The Shader.</param>
void Draw(Shader & shader);
/// <summary>
/// Calculate the spring force.
/// </summary>
void CalculateForce();
friend class PhysicsModel;
};
/// <summary>
/// The physics model.
/// </summary>
class PhysicsModel
{
private:
/// <summary>
/// The rigid boies which are sticks.
/// </summary>
std::vector<Stick> bodies;
/// <summary>
/// The springs.
/// </summary>

std::vector<Spring> springs;
/// <summary>
/// The start anchor.
/// </summary>
StickBase pinStart;
/// <summary>
/// The end anchor.
/// </summary>
StickBase pinEnd;
/// <summary>
/// VAO and VBO.
/// </summary>
GLuint VAO, VBO;
/// <summary>
/// The gravity.
/// </summary>
Vector3 gravity;
/// <summary>
/// The length of the sticks.
/// </summary>
float stickLength;
/// <summary>
/// The mass of the sticks.
/// </summary>
float stickMass;
/// <summary>
/// The length of the springs.
/// </summary>
float springLength;
/// <summary>
/// The strenth (k) of the springs.
/// </summary>
float springStrength;
/// <summary>
/// The damping.
/// </summary>
float damping;
/// <summary>
/// Flag to use RK4.
/// </summary>
bool useRK4;
public:
/// <summary>
/// Initialize the physics model.
/// </summary>
/// <param name="center">The number of the sticks.</param>
PhysicsModel(int bodyNum = 8);
/// <summary>

/// Finalize the physics model.


/// </summary>
~PhysicsModel();
/// <summary>
/// Set up OpenGL parameters.
/// </summary>
void SetupOpenGL();
/// <summary>
/// Draw the sticks.
/// </summary>
/// <param name="shader">The Shader.</param>
void DrawRigidBodies(Shader & shader);
/// <summary>
/// Draw the springs.
/// </summary>
/// <param name="shader">The Shader.</param>
void DrawSprings(Shader & shader);
/// <summary>
/// Update the model.
/// </summary>
/// <param name="dt">The delta time.</param>
void UpdateModel(float dt);
/// <summary>
/// Euler Integration.
/// </summary>
/// <param name="dt">The delta time.</param>
void EulerIntegration(float dt);
/// <summary>
/// RK4 Integration.
/// </summary>
/// <param name="dt">The delta time.</param>
void RK4Integration(float dt);
///
///
///
///
///
///
///

<summary>
Recalculate force for RK4.
</summary>
<param name="id">The delta time.</param>
<param name="newPos">The new position.</param>
<param name="newRotAng">The new rotation angle.</param>
<param name="force">The net force at the new position.</para

m>
/// <param name="torque">The net torque at the new position.</pa
ram>
void RecalculateForce(int id, Vector3 newPos, Radian newRotAng,
Vector3 & force, Vector3 & torque);
/// <summary>
/// Set the end anchor.
/// </summary>
/// <param name="newPos">The new position.</param>
void SetPinEnd(const Vector3 & newPos);
/// <summary>
/// Setup the editor for some parameters.

/// </summary>
/// <param name="editor">The editor unit.</param>
void SetupEditor(EditorUnit * editor);
};
}
#endif

Vous aimerez peut-être aussi