Ult3D
Implementation of "Ultimate 3D Game Engine Design & Architecture" by Allan Sherrod
Loading...
Searching...
No Matches
Spring.h
Go to the documentation of this file.
1#ifndef ULT_PHYSICS_SPRING_H_INCLUDED
2#define ULT_PHYSICS_SPRING_H_INCLUDED
3
4#include "PointMass.h"
5
6namespace Ult
7{
8
13class SpringMass : public PointMass
14{
15public:
20 mNormal(),
21 mFixed(false)
22 {}
23
27 Vector3D GetNormal() const { return mNormal; }
28
32 Vector3D* GetNormalPtr() { return &mNormal; }
33
37 void SetNormal(const Vector3D& n) { mNormal = n; }
38
42 void NormalizeNormal() { mNormal.Normalize(); }
43
47 bool IsFixed() const { return mFixed; }
48
52 void SetIsFixed(const bool val) { mFixed = val; }
53
54private:
55 Vector3D mNormal;
56 bool mFixed;
57};
58
63class Spring
64{
65public:
70 mBall1(nullptr),
71 mBall2(nullptr),
72 mTension(0.0f),
73 mConstant(0.0f),
74 mLength(0.0f)
75 {}
76
80 {}
81
90 void SetSpring(float t, float c, float l, SpringMass* m1, SpringMass* m2)
91 {
92 mBall1 = m1;
93 mBall2 = m2;
94 mTension = t;
95 mConstant = c;
96 mLength = l;
97 }
98
102 void Update()
103 {
104 if (!mBall1 || !mBall2) { return; }
105 Vector3D springVec = mBall2->GetPosition() - mBall1->GetPosition();
106 mTension = mConstant * (springVec.Magnitude() - mLength) / mLength;
107 springVec.Normalize();
108 mBall1->AddForce(springVec * mTension);
109 mBall2->AddForce((springVec * mTension) * -1.0f);
110 }
111
112private:
113 SpringMass* mBall1, *mBall2;
114 float mTension, mConstant, mLength;
115};
116
117} // namespace Ult
118
119#endif // ULT_PHYSICS_SPRING_H_INCLUDED
120
PointMass()
Definition PointMass.h:19
Spring()
Definition Spring.h:69
void Update()
Definition Spring.h:102
~Spring()
Definition Spring.h:79
void SetSpring(float t, float c, float l, SpringMass *m1, SpringMass *m2)
Definition Spring.h:90
Definition Spring.h:14
SpringMass()
Definition Spring.h:19
void SetNormal(const Vector3D &n)
Definition Spring.h:37
Vector3D * GetNormalPtr()
Definition Spring.h:32
void SetIsFixed(const bool val)
Definition Spring.h:52
bool IsFixed() const
Definition Spring.h:47
Vector3D GetNormal() const
Definition Spring.h:27
void NormalizeNormal()
Definition Spring.h:42
Definition Vector3D.h:12
void Normalize()
Definition Vector3D.cpp:157
float Magnitude() const
Definition Vector3D.cpp:152
Definition Archive.h:13