BodyConstraint.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  * 1. The origin of this software must not be misrepresented; you must not
12  * claim that you wrote the original software. If you use this software
13  * in a product, an acknowledgment in the product documentation would be
14  * appreciated but is not required.
15  * 2. Altered source versions must be plainly marked as such, and must not be
16  * misrepresented as being the original software.
17  * 3. This notice may not be removed or altered from any source distribution.
18  */
19 
20 #ifndef PLAYRHO_DYNAMICS_CONTACTS_BODYCONSTRAINT_HPP
21 #define PLAYRHO_DYNAMICS_CONTACTS_BODYCONSTRAINT_HPP
22 
23 #include <PlayRho/Common/Math.hpp>
26 
27 namespace playrho {
28 namespace d2 {
29 
36  {
37  public:
38  // Note: Seeing World.TilesComesToRest times of around 5686 ms with this setup.
39 
41  using index_type = std::remove_const<decltype(MaxBodies)>::type;
42 
43  BodyConstraint() = default;
44 
46  PLAYRHO_CONSTEXPR inline
47  BodyConstraint(InvMass invMass, InvRotInertia invRotI, Length2 localCenter,
48  Position position, Velocity velocity) noexcept:
49  m_position{position},
50  m_velocity{velocity},
51  m_localCenter{localCenter},
52  m_invMass{invMass},
53  m_invRotI{invRotI}
54  {
55  assert(IsValid(position));
56  assert(IsValid(velocity));
57  assert(IsValid(localCenter));
58  assert(invMass >= InvMass{0});
59  assert(invRotI >= InvRotInertia{0});
60  }
61 
64  InvMass GetInvMass() const noexcept;
65 
68  InvRotInertia GetInvRotInertia() const noexcept;
69 
71  Length2 GetLocalCenter() const noexcept;
72 
74  Position GetPosition() const noexcept;
75 
77  Velocity GetVelocity() const noexcept;
78 
82  BodyConstraint& SetPosition(Position value) noexcept;
83 
87  BodyConstraint& SetVelocity(Velocity value) noexcept;
88 
89  private:
90  Position m_position;
91  Velocity m_velocity;
92  Length2 m_localCenter;
93  InvMass m_invMass;
94 
97  InvRotInertia m_invRotI;
98  };
99 
100  inline InvMass BodyConstraint::GetInvMass() const noexcept
101  {
102  return m_invMass;
103  }
104 
106  {
107  return m_invRotI;
108  }
109 
110  inline Length2 BodyConstraint::GetLocalCenter() const noexcept
111  {
112  return m_localCenter;
113  }
114 
115  inline Position BodyConstraint::GetPosition() const noexcept
116  {
117  return m_position;
118  }
119 
120  inline Velocity BodyConstraint::GetVelocity() const noexcept
121  {
122  return m_velocity;
123  }
124 
126  {
127  assert(IsValid(value));
128  m_position = value;
129  return *this;
130  }
131 
133  {
134  assert(IsValid(value));
135  m_velocity = value;
136  return *this;
137  }
138 
140  inline BodyConstraint GetBodyConstraint(const Body& body, Time time,
141  MovementConf conf) noexcept
142  {
143  return BodyConstraint{
144  body.GetInvMass(),
145  body.GetInvRotInertia(),
146  body.GetLocalCenter(),
147  GetPosition1(body),
148  Cap(GetVelocity(body, time), time, conf)
149  };
150  }
151 
152 } // namespace d2
153 } // namespace playrho
154 
155 #endif // PLAYRHO_DYNAMICS_CONTACTS_BODYCONSTRAINT_HPP