Velocity.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
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  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_COMMON_VELOCITY_HPP
22 #define PLAYRHO_COMMON_VELOCITY_HPP
23 
26 #include <utility>
27 
28 namespace playrho {
29 namespace d2 {
30 
31 class VelocityConstraint;
32 
35 struct Velocity
36 {
39 };
40 
43 PLAYRHO_CONSTEXPR inline bool operator==(const Velocity& lhs, const Velocity& rhs)
44 {
45  return (lhs.linear == rhs.linear) && (lhs.angular == rhs.angular);
46 }
47 
50 PLAYRHO_CONSTEXPR inline bool operator!=(const Velocity& lhs, const Velocity& rhs)
51 {
52  return (lhs.linear != rhs.linear) || (lhs.angular != rhs.angular);
53 }
54 
58 {
59  lhs.linear *= rhs;
60  lhs.angular *= rhs;
61  return lhs;
62 }
63 
67 {
68  lhs.linear /= rhs;
69  lhs.angular /= rhs;
70  return lhs;
71 }
72 
76 {
77  lhs.linear += rhs.linear;
78  lhs.angular += rhs.angular;
79  return lhs;
80 }
81 
84 PLAYRHO_CONSTEXPR inline Velocity operator+ (const Velocity& lhs, const Velocity& rhs)
85 {
86  return Velocity{lhs.linear + rhs.linear, lhs.angular + rhs.angular};
87 }
88 
92 {
93  lhs.linear -= rhs.linear;
94  lhs.angular -= rhs.angular;
95  return lhs;
96 }
97 
100 PLAYRHO_CONSTEXPR inline Velocity operator- (const Velocity& lhs, const Velocity& rhs)
101 {
102  return Velocity{lhs.linear - rhs.linear, lhs.angular - rhs.angular};
103 }
104 
108 {
109  return Velocity{-value.linear, -value.angular};
110 }
111 
115 {
116  return value;
117 }
118 
121 PLAYRHO_CONSTEXPR inline Velocity operator* (const Velocity& lhs, const Real rhs)
122 {
123  return Velocity{lhs.linear * rhs, lhs.angular * rhs};
124 }
125 
128 PLAYRHO_CONSTEXPR inline Velocity operator* (const Real lhs, const Velocity& rhs)
129 {
130  return Velocity{rhs.linear * lhs, rhs.angular * lhs};
131 }
132 
135 PLAYRHO_CONSTEXPR inline Velocity operator/ (const Velocity& lhs, const Real rhs)
136 {
137  const auto inverseRhs = Real{1} / rhs;
138  return Velocity{lhs.linear * inverseRhs, lhs.angular * inverseRhs};
139 }
140 
142 using VelocityPair = std::pair<Velocity, Velocity>;
143 
146 
147 } // namespace d2
148 
151 template <>
152 PLAYRHO_CONSTEXPR inline bool IsValid(const d2::Velocity& value) noexcept
153 {
154  return IsValid(value.linear) && IsValid(value.angular);
155 }
156 
157 } // namespace playrho
158 
159 #endif // PLAYRHO_COMMON_VELOCITY_HPP