reflexion 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/reflex/application.cpp +5 -5
  3. data/.doc/ext/reflex/body.cpp +167 -0
  4. data/.doc/ext/reflex/capture_event.cpp +4 -4
  5. data/.doc/ext/reflex/draw_event.cpp +4 -4
  6. data/.doc/ext/reflex/event.cpp +4 -4
  7. data/.doc/ext/reflex/fixture.cpp +101 -0
  8. data/.doc/ext/reflex/focus_event.cpp +4 -4
  9. data/.doc/ext/reflex/frame_event.cpp +4 -4
  10. data/.doc/ext/reflex/image_view.cpp +22 -5
  11. data/.doc/ext/reflex/key_event.cpp +4 -4
  12. data/.doc/ext/reflex/native.cpp +6 -0
  13. data/.doc/ext/reflex/pointer_event.cpp +4 -4
  14. data/.doc/ext/reflex/reflex.cpp +2 -3
  15. data/.doc/ext/reflex/scroll_event.cpp +4 -4
  16. data/.doc/ext/reflex/selector.cpp +4 -4
  17. data/.doc/ext/reflex/style.cpp +4 -4
  18. data/.doc/ext/reflex/style_length.cpp +4 -4
  19. data/.doc/ext/reflex/style_length2.cpp +4 -4
  20. data/.doc/ext/reflex/style_length4.cpp +4 -4
  21. data/.doc/ext/reflex/update_event.cpp +4 -4
  22. data/.doc/ext/reflex/view.cpp +110 -8
  23. data/.doc/ext/reflex/wheel_event.cpp +4 -4
  24. data/.doc/ext/reflex/window.cpp +36 -12
  25. data/README.md +2 -2
  26. data/Rakefile +2 -1
  27. data/VERSION +1 -1
  28. data/ext/reflex/application.cpp +8 -8
  29. data/ext/reflex/body.cpp +181 -0
  30. data/ext/reflex/capture_event.cpp +4 -4
  31. data/ext/reflex/draw_event.cpp +4 -4
  32. data/ext/reflex/event.cpp +4 -4
  33. data/ext/reflex/fixture.cpp +108 -0
  34. data/ext/reflex/focus_event.cpp +4 -4
  35. data/ext/reflex/frame_event.cpp +4 -4
  36. data/ext/reflex/image_view.cpp +24 -5
  37. data/ext/reflex/key_event.cpp +4 -4
  38. data/ext/reflex/native.cpp +6 -0
  39. data/ext/reflex/pointer_event.cpp +4 -4
  40. data/ext/reflex/reflex.cpp +2 -3
  41. data/ext/reflex/scroll_event.cpp +4 -4
  42. data/ext/reflex/selector.cpp +4 -4
  43. data/ext/reflex/style.cpp +4 -4
  44. data/ext/reflex/style_length.cpp +4 -4
  45. data/ext/reflex/style_length2.cpp +4 -4
  46. data/ext/reflex/style_length4.cpp +4 -4
  47. data/ext/reflex/update_event.cpp +4 -4
  48. data/ext/reflex/view.cpp +126 -12
  49. data/ext/reflex/wheel_event.cpp +4 -4
  50. data/ext/reflex/window.cpp +39 -12
  51. data/include/reflex/bitmap.h +20 -0
  52. data/include/reflex/body.h +82 -0
  53. data/include/reflex/bounds.h +20 -0
  54. data/include/reflex/color.h +20 -0
  55. data/include/reflex/color_space.h +20 -0
  56. data/include/reflex/defs.h +5 -1
  57. data/include/reflex/event.h +16 -25
  58. data/include/reflex/fixture.h +116 -0
  59. data/include/reflex/font.h +20 -0
  60. data/include/reflex/image.h +20 -0
  61. data/include/reflex/image_view.h +1 -1
  62. data/include/reflex/matrix.h +20 -0
  63. data/include/reflex/painter.h +20 -0
  64. data/include/reflex/point.h +24 -0
  65. data/include/reflex/ruby/application.h +6 -0
  66. data/include/reflex/ruby/body.h +41 -0
  67. data/include/reflex/ruby/event.h +68 -0
  68. data/include/reflex/ruby/fixture.h +41 -0
  69. data/include/reflex/ruby/image_view.h +14 -0
  70. data/include/reflex/ruby/selector.h +14 -0
  71. data/include/reflex/ruby/style.h +14 -0
  72. data/include/reflex/ruby/style_length.h +26 -0
  73. data/include/reflex/ruby/view.h +43 -39
  74. data/include/reflex/ruby/window.h +27 -21
  75. data/include/reflex/ruby.h +2 -0
  76. data/include/reflex/shader.h +20 -0
  77. data/include/reflex/style.h +2 -3
  78. data/include/reflex/texture.h +20 -0
  79. data/include/reflex/view.h +45 -24
  80. data/include/reflex/window.h +11 -14
  81. data/include/reflex.h +3 -0
  82. data/lib/reflex/application.rb +6 -2
  83. data/lib/reflex/body.rb +17 -0
  84. data/lib/reflex/fixture.rb +17 -0
  85. data/lib/reflex/view.rb +16 -1
  86. data/lib/reflex/window.rb +13 -1
  87. data/lib/reflex.rb +2 -0
  88. data/reflex.gemspec +1 -1
  89. data/samples/bats.rb +4 -3
  90. data/samples/checker.rb +4 -3
  91. data/samples/fans.rb +4 -3
  92. data/samples/fps.rb +5 -3
  93. data/samples/grid.rb +5 -4
  94. data/samples/hello.rb +1 -1
  95. data/samples/image.rb +3 -2
  96. data/samples/ios/hello/hello/main.cpp +12 -3
  97. data/samples/ios/hello/hello.xcodeproj/project.pbxproj +376 -123
  98. data/samples/key.rb +5 -4
  99. data/samples/layout.rb +3 -2
  100. data/samples/model.rb +3 -2
  101. data/samples/osx/hello/hello/main.cpp +12 -3
  102. data/samples/osx/hello/hello.xcodeproj/project.pbxproj +375 -120
  103. data/samples/physics.rb +37 -0
  104. data/samples/repl.rb +3 -2
  105. data/samples/shader.rb +4 -4
  106. data/samples/shapes.rb +4 -4
  107. data/samples/text.rb +6 -4
  108. data/samples/tree.rb +5 -4
  109. data/samples/views.rb +3 -2
  110. data/samples/visuals.rb +1 -2
  111. data/src/body.cpp +244 -0
  112. data/src/event.cpp +18 -18
  113. data/src/fixture.cpp +108 -0
  114. data/src/image_view.cpp +1 -1
  115. data/src/ios/native_window.h +2 -0
  116. data/src/ios/native_window.mm +18 -1
  117. data/src/ios/opengl_view.mm +8 -0
  118. data/src/ios/window.mm +24 -3
  119. data/src/ios/window_data.h +3 -3
  120. data/src/osx/native_window.mm +17 -1
  121. data/src/osx/window.mm +24 -3
  122. data/src/osx/window_data.h +3 -3
  123. data/src/physics/Box2D/Box2D.h +68 -0
  124. data/src/physics/Box2D/Collision/Shapes/b2ChainShape.cpp +193 -0
  125. data/src/physics/Box2D/Collision/Shapes/b2ChainShape.h +105 -0
  126. data/src/physics/Box2D/Collision/Shapes/b2CircleShape.cpp +99 -0
  127. data/src/physics/Box2D/Collision/Shapes/b2CircleShape.h +91 -0
  128. data/src/physics/Box2D/Collision/Shapes/b2EdgeShape.cpp +138 -0
  129. data/src/physics/Box2D/Collision/Shapes/b2EdgeShape.h +74 -0
  130. data/src/physics/Box2D/Collision/Shapes/b2PolygonShape.cpp +467 -0
  131. data/src/physics/Box2D/Collision/Shapes/b2PolygonShape.h +101 -0
  132. data/src/physics/Box2D/Collision/Shapes/b2Shape.h +101 -0
  133. data/src/physics/Box2D/Collision/b2BroadPhase.cpp +119 -0
  134. data/src/physics/Box2D/Collision/b2BroadPhase.h +257 -0
  135. data/src/physics/Box2D/Collision/b2CollideCircle.cpp +154 -0
  136. data/src/physics/Box2D/Collision/b2CollideEdge.cpp +698 -0
  137. data/src/physics/Box2D/Collision/b2CollidePolygon.cpp +239 -0
  138. data/src/physics/Box2D/Collision/b2Collision.cpp +252 -0
  139. data/src/physics/Box2D/Collision/b2Collision.h +277 -0
  140. data/src/physics/Box2D/Collision/b2Distance.cpp +603 -0
  141. data/src/physics/Box2D/Collision/b2Distance.h +141 -0
  142. data/src/physics/Box2D/Collision/b2DynamicTree.cpp +778 -0
  143. data/src/physics/Box2D/Collision/b2DynamicTree.h +289 -0
  144. data/src/physics/Box2D/Collision/b2TimeOfImpact.cpp +486 -0
  145. data/src/physics/Box2D/Collision/b2TimeOfImpact.h +58 -0
  146. data/src/physics/Box2D/Common/b2BlockAllocator.cpp +215 -0
  147. data/src/physics/Box2D/Common/b2BlockAllocator.h +62 -0
  148. data/src/physics/Box2D/Common/b2Draw.cpp +44 -0
  149. data/src/physics/Box2D/Common/b2Draw.h +86 -0
  150. data/src/physics/Box2D/Common/b2GrowableStack.h +85 -0
  151. data/src/physics/Box2D/Common/b2Math.cpp +94 -0
  152. data/src/physics/Box2D/Common/b2Math.h +720 -0
  153. data/src/physics/Box2D/Common/b2Settings.cpp +44 -0
  154. data/src/physics/Box2D/Common/b2Settings.h +151 -0
  155. data/src/physics/Box2D/Common/b2StackAllocator.cpp +83 -0
  156. data/src/physics/Box2D/Common/b2StackAllocator.h +60 -0
  157. data/src/physics/Box2D/Common/b2Timer.cpp +101 -0
  158. data/src/physics/Box2D/Common/b2Timer.h +50 -0
  159. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp +53 -0
  160. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h +39 -0
  161. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp +53 -0
  162. data/src/physics/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h +39 -0
  163. data/src/physics/Box2D/Dynamics/Contacts/b2CircleContact.cpp +52 -0
  164. data/src/physics/Box2D/Dynamics/Contacts/b2CircleContact.h +39 -0
  165. data/src/physics/Box2D/Dynamics/Contacts/b2Contact.cpp +247 -0
  166. data/src/physics/Box2D/Dynamics/Contacts/b2Contact.h +349 -0
  167. data/src/physics/Box2D/Dynamics/Contacts/b2ContactSolver.cpp +838 -0
  168. data/src/physics/Box2D/Dynamics/Contacts/b2ContactSolver.h +95 -0
  169. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp +49 -0
  170. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h +39 -0
  171. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp +49 -0
  172. data/src/physics/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h +39 -0
  173. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp +49 -0
  174. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h +38 -0
  175. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonContact.cpp +52 -0
  176. data/src/physics/Box2D/Dynamics/Contacts/b2PolygonContact.h +39 -0
  177. data/src/physics/Box2D/Dynamics/Joints/b2DistanceJoint.cpp +260 -0
  178. data/src/physics/Box2D/Dynamics/Joints/b2DistanceJoint.h +169 -0
  179. data/src/physics/Box2D/Dynamics/Joints/b2FrictionJoint.cpp +251 -0
  180. data/src/physics/Box2D/Dynamics/Joints/b2FrictionJoint.h +119 -0
  181. data/src/physics/Box2D/Dynamics/Joints/b2GearJoint.cpp +419 -0
  182. data/src/physics/Box2D/Dynamics/Joints/b2GearJoint.h +125 -0
  183. data/src/physics/Box2D/Dynamics/Joints/b2Joint.cpp +211 -0
  184. data/src/physics/Box2D/Dynamics/Joints/b2Joint.h +226 -0
  185. data/src/physics/Box2D/Dynamics/Joints/b2MotorJoint.cpp +304 -0
  186. data/src/physics/Box2D/Dynamics/Joints/b2MotorJoint.h +133 -0
  187. data/src/physics/Box2D/Dynamics/Joints/b2MouseJoint.cpp +222 -0
  188. data/src/physics/Box2D/Dynamics/Joints/b2MouseJoint.h +129 -0
  189. data/src/physics/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp +629 -0
  190. data/src/physics/Box2D/Dynamics/Joints/b2PrismaticJoint.h +196 -0
  191. data/src/physics/Box2D/Dynamics/Joints/b2PulleyJoint.cpp +348 -0
  192. data/src/physics/Box2D/Dynamics/Joints/b2PulleyJoint.h +152 -0
  193. data/src/physics/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp +502 -0
  194. data/src/physics/Box2D/Dynamics/Joints/b2RevoluteJoint.h +204 -0
  195. data/src/physics/Box2D/Dynamics/Joints/b2RopeJoint.cpp +241 -0
  196. data/src/physics/Box2D/Dynamics/Joints/b2RopeJoint.h +114 -0
  197. data/src/physics/Box2D/Dynamics/Joints/b2WeldJoint.cpp +344 -0
  198. data/src/physics/Box2D/Dynamics/Joints/b2WeldJoint.h +126 -0
  199. data/src/physics/Box2D/Dynamics/Joints/b2WheelJoint.cpp +419 -0
  200. data/src/physics/Box2D/Dynamics/Joints/b2WheelJoint.h +210 -0
  201. data/src/physics/Box2D/Dynamics/b2Body.cpp +549 -0
  202. data/src/physics/Box2D/Dynamics/b2Body.h +860 -0
  203. data/src/physics/Box2D/Dynamics/b2ContactManager.cpp +296 -0
  204. data/src/physics/Box2D/Dynamics/b2ContactManager.h +52 -0
  205. data/src/physics/Box2D/Dynamics/b2Fixture.cpp +303 -0
  206. data/src/physics/Box2D/Dynamics/b2Fixture.h +345 -0
  207. data/src/physics/Box2D/Dynamics/b2Island.cpp +539 -0
  208. data/src/physics/Box2D/Dynamics/b2Island.h +93 -0
  209. data/src/physics/Box2D/Dynamics/b2TimeStep.h +70 -0
  210. data/src/physics/Box2D/Dynamics/b2World.cpp +1339 -0
  211. data/src/physics/Box2D/Dynamics/b2World.h +354 -0
  212. data/src/physics/Box2D/Dynamics/b2WorldCallbacks.cpp +36 -0
  213. data/src/physics/Box2D/Dynamics/b2WorldCallbacks.h +155 -0
  214. data/src/physics/Box2D/Rope/b2Rope.cpp +259 -0
  215. data/src/physics/Box2D/Rope/b2Rope.h +115 -0
  216. data/src/style.cpp +2 -2
  217. data/src/view.cpp +217 -17
  218. data/src/window.cpp +25 -15
  219. data/src/world.cpp +206 -0
  220. data/src/world.h +96 -0
  221. data/task/box2d.rake +25 -0
  222. data/test/test_view.rb +5 -5
  223. metadata +125 -3
@@ -0,0 +1,304 @@
1
+ /*
2
+ * Copyright (c) 2006-2012 Erin Catto http://www.box2d.org
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
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked as such, and must not be
15
+ * misrepresented as being the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+ #include <Box2D/Dynamics/Joints/b2MotorJoint.h>
20
+ #include <Box2D/Dynamics/b2Body.h>
21
+ #include <Box2D/Dynamics/b2TimeStep.h>
22
+
23
+ // Point-to-point constraint
24
+ // Cdot = v2 - v1
25
+ // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
26
+ // J = [-I -r1_skew I r2_skew ]
27
+ // Identity used:
28
+ // w k % (rx i + ry j) = w * (-ry i + rx j)
29
+
30
+ // Angle constraint
31
+ // Cdot = w2 - w1
32
+ // J = [0 0 -1 0 0 1]
33
+ // K = invI1 + invI2
34
+
35
+ void b2MotorJointDef::Initialize(b2Body* bA, b2Body* bB)
36
+ {
37
+ bodyA = bA;
38
+ bodyB = bB;
39
+ b2Vec2 xB = bodyB->GetPosition();
40
+ linearOffset = bodyA->GetLocalPoint(xB);
41
+
42
+ float32 angleA = bodyA->GetAngle();
43
+ float32 angleB = bodyB->GetAngle();
44
+ angularOffset = angleB - angleA;
45
+ }
46
+
47
+ b2MotorJoint::b2MotorJoint(const b2MotorJointDef* def)
48
+ : b2Joint(def)
49
+ {
50
+ m_linearOffset = def->linearOffset;
51
+ m_angularOffset = def->angularOffset;
52
+
53
+ m_linearImpulse.SetZero();
54
+ m_angularImpulse = 0.0f;
55
+
56
+ m_maxForce = def->maxForce;
57
+ m_maxTorque = def->maxTorque;
58
+ m_correctionFactor = def->correctionFactor;
59
+ }
60
+
61
+ void b2MotorJoint::InitVelocityConstraints(const b2SolverData& data)
62
+ {
63
+ m_indexA = m_bodyA->m_islandIndex;
64
+ m_indexB = m_bodyB->m_islandIndex;
65
+ m_localCenterA = m_bodyA->m_sweep.localCenter;
66
+ m_localCenterB = m_bodyB->m_sweep.localCenter;
67
+ m_invMassA = m_bodyA->m_invMass;
68
+ m_invMassB = m_bodyB->m_invMass;
69
+ m_invIA = m_bodyA->m_invI;
70
+ m_invIB = m_bodyB->m_invI;
71
+
72
+ b2Vec2 cA = data.positions[m_indexA].c;
73
+ float32 aA = data.positions[m_indexA].a;
74
+ b2Vec2 vA = data.velocities[m_indexA].v;
75
+ float32 wA = data.velocities[m_indexA].w;
76
+
77
+ b2Vec2 cB = data.positions[m_indexB].c;
78
+ float32 aB = data.positions[m_indexB].a;
79
+ b2Vec2 vB = data.velocities[m_indexB].v;
80
+ float32 wB = data.velocities[m_indexB].w;
81
+
82
+ b2Rot qA(aA), qB(aB);
83
+
84
+ // Compute the effective mass matrix.
85
+ m_rA = b2Mul(qA, -m_localCenterA);
86
+ m_rB = b2Mul(qB, -m_localCenterB);
87
+
88
+ // J = [-I -r1_skew I r2_skew]
89
+ // [ 0 -1 0 1]
90
+ // r_skew = [-ry; rx]
91
+
92
+ // Matlab
93
+ // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
94
+ // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
95
+ // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
96
+
97
+ float32 mA = m_invMassA, mB = m_invMassB;
98
+ float32 iA = m_invIA, iB = m_invIB;
99
+
100
+ b2Mat22 K;
101
+ K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
102
+ K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
103
+ K.ey.x = K.ex.y;
104
+ K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
105
+
106
+ m_linearMass = K.GetInverse();
107
+
108
+ m_angularMass = iA + iB;
109
+ if (m_angularMass > 0.0f)
110
+ {
111
+ m_angularMass = 1.0f / m_angularMass;
112
+ }
113
+
114
+ m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset);
115
+ m_angularError = aB - aA - m_angularOffset;
116
+
117
+ if (data.step.warmStarting)
118
+ {
119
+ // Scale impulses to support a variable time step.
120
+ m_linearImpulse *= data.step.dtRatio;
121
+ m_angularImpulse *= data.step.dtRatio;
122
+
123
+ b2Vec2 P(m_linearImpulse.x, m_linearImpulse.y);
124
+ vA -= mA * P;
125
+ wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
126
+ vB += mB * P;
127
+ wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
128
+ }
129
+ else
130
+ {
131
+ m_linearImpulse.SetZero();
132
+ m_angularImpulse = 0.0f;
133
+ }
134
+
135
+ data.velocities[m_indexA].v = vA;
136
+ data.velocities[m_indexA].w = wA;
137
+ data.velocities[m_indexB].v = vB;
138
+ data.velocities[m_indexB].w = wB;
139
+ }
140
+
141
+ void b2MotorJoint::SolveVelocityConstraints(const b2SolverData& data)
142
+ {
143
+ b2Vec2 vA = data.velocities[m_indexA].v;
144
+ float32 wA = data.velocities[m_indexA].w;
145
+ b2Vec2 vB = data.velocities[m_indexB].v;
146
+ float32 wB = data.velocities[m_indexB].w;
147
+
148
+ float32 mA = m_invMassA, mB = m_invMassB;
149
+ float32 iA = m_invIA, iB = m_invIB;
150
+
151
+ float32 h = data.step.dt;
152
+ float32 inv_h = data.step.inv_dt;
153
+
154
+ // Solve angular friction
155
+ {
156
+ float32 Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError;
157
+ float32 impulse = -m_angularMass * Cdot;
158
+
159
+ float32 oldImpulse = m_angularImpulse;
160
+ float32 maxImpulse = h * m_maxTorque;
161
+ m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
162
+ impulse = m_angularImpulse - oldImpulse;
163
+
164
+ wA -= iA * impulse;
165
+ wB += iB * impulse;
166
+ }
167
+
168
+ // Solve linear friction
169
+ {
170
+ b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor * m_linearError;
171
+
172
+ b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
173
+ b2Vec2 oldImpulse = m_linearImpulse;
174
+ m_linearImpulse += impulse;
175
+
176
+ float32 maxImpulse = h * m_maxForce;
177
+
178
+ if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
179
+ {
180
+ m_linearImpulse.Normalize();
181
+ m_linearImpulse *= maxImpulse;
182
+ }
183
+
184
+ impulse = m_linearImpulse - oldImpulse;
185
+
186
+ vA -= mA * impulse;
187
+ wA -= iA * b2Cross(m_rA, impulse);
188
+
189
+ vB += mB * impulse;
190
+ wB += iB * b2Cross(m_rB, impulse);
191
+ }
192
+
193
+ data.velocities[m_indexA].v = vA;
194
+ data.velocities[m_indexA].w = wA;
195
+ data.velocities[m_indexB].v = vB;
196
+ data.velocities[m_indexB].w = wB;
197
+ }
198
+
199
+ bool b2MotorJoint::SolvePositionConstraints(const b2SolverData& data)
200
+ {
201
+ B2_NOT_USED(data);
202
+
203
+ return true;
204
+ }
205
+
206
+ b2Vec2 b2MotorJoint::GetAnchorA() const
207
+ {
208
+ return m_bodyA->GetPosition();
209
+ }
210
+
211
+ b2Vec2 b2MotorJoint::GetAnchorB() const
212
+ {
213
+ return m_bodyB->GetPosition();
214
+ }
215
+
216
+ b2Vec2 b2MotorJoint::GetReactionForce(float32 inv_dt) const
217
+ {
218
+ return inv_dt * m_linearImpulse;
219
+ }
220
+
221
+ float32 b2MotorJoint::GetReactionTorque(float32 inv_dt) const
222
+ {
223
+ return inv_dt * m_angularImpulse;
224
+ }
225
+
226
+ void b2MotorJoint::SetMaxForce(float32 force)
227
+ {
228
+ b2Assert(b2IsValid(force) && force >= 0.0f);
229
+ m_maxForce = force;
230
+ }
231
+
232
+ float32 b2MotorJoint::GetMaxForce() const
233
+ {
234
+ return m_maxForce;
235
+ }
236
+
237
+ void b2MotorJoint::SetMaxTorque(float32 torque)
238
+ {
239
+ b2Assert(b2IsValid(torque) && torque >= 0.0f);
240
+ m_maxTorque = torque;
241
+ }
242
+
243
+ float32 b2MotorJoint::GetMaxTorque() const
244
+ {
245
+ return m_maxTorque;
246
+ }
247
+
248
+ void b2MotorJoint::SetCorrectionFactor(float32 factor)
249
+ {
250
+ b2Assert(b2IsValid(factor) && 0.0f <= factor && factor <= 1.0f);
251
+ m_correctionFactor = factor;
252
+ }
253
+
254
+ float32 b2MotorJoint::GetCorrectionFactor() const
255
+ {
256
+ return m_correctionFactor;
257
+ }
258
+
259
+ void b2MotorJoint::SetLinearOffset(const b2Vec2& linearOffset)
260
+ {
261
+ if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y)
262
+ {
263
+ m_bodyA->SetAwake(true);
264
+ m_bodyB->SetAwake(true);
265
+ m_linearOffset = linearOffset;
266
+ }
267
+ }
268
+
269
+ const b2Vec2& b2MotorJoint::GetLinearOffset() const
270
+ {
271
+ return m_linearOffset;
272
+ }
273
+
274
+ void b2MotorJoint::SetAngularOffset(float32 angularOffset)
275
+ {
276
+ if (angularOffset != m_angularOffset)
277
+ {
278
+ m_bodyA->SetAwake(true);
279
+ m_bodyB->SetAwake(true);
280
+ m_angularOffset = angularOffset;
281
+ }
282
+ }
283
+
284
+ float32 b2MotorJoint::GetAngularOffset() const
285
+ {
286
+ return m_angularOffset;
287
+ }
288
+
289
+ void b2MotorJoint::Dump()
290
+ {
291
+ int32 indexA = m_bodyA->m_islandIndex;
292
+ int32 indexB = m_bodyB->m_islandIndex;
293
+
294
+ b2Log(" b2MotorJointDef jd;\n");
295
+ b2Log(" jd.bodyA = bodies[%d];\n", indexA);
296
+ b2Log(" jd.bodyB = bodies[%d];\n", indexB);
297
+ b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
298
+ b2Log(" jd.linearOffset.Set(%.15lef, %.15lef);\n", m_linearOffset.x, m_linearOffset.y);
299
+ b2Log(" jd.angularOffset = %.15lef;\n", m_angularOffset);
300
+ b2Log(" jd.maxForce = %.15lef;\n", m_maxForce);
301
+ b2Log(" jd.maxTorque = %.15lef;\n", m_maxTorque);
302
+ b2Log(" jd.correctionFactor = %.15lef;\n", m_correctionFactor);
303
+ b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
304
+ }
@@ -0,0 +1,133 @@
1
+ /*
2
+ * Copyright (c) 2006-2012 Erin Catto http://www.box2d.org
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
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked as such, and must not be
15
+ * misrepresented as being the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+ #ifndef B2_MOTOR_JOINT_H
20
+ #define B2_MOTOR_JOINT_H
21
+
22
+ #include <Box2D/Dynamics/Joints/b2Joint.h>
23
+
24
+ /// Motor joint definition.
25
+ struct b2MotorJointDef : public b2JointDef
26
+ {
27
+ b2MotorJointDef()
28
+ {
29
+ type = e_motorJoint;
30
+ linearOffset.SetZero();
31
+ angularOffset = 0.0f;
32
+ maxForce = 1.0f;
33
+ maxTorque = 1.0f;
34
+ correctionFactor = 0.3f;
35
+ }
36
+
37
+ /// Initialize the bodies and offsets using the current transforms.
38
+ void Initialize(b2Body* bodyA, b2Body* bodyB);
39
+
40
+ /// Position of bodyB minus the position of bodyA, in bodyA's frame, in meters.
41
+ b2Vec2 linearOffset;
42
+
43
+ /// The bodyB angle minus bodyA angle in radians.
44
+ float32 angularOffset;
45
+
46
+ /// The maximum motor force in N.
47
+ float32 maxForce;
48
+
49
+ /// The maximum motor torque in N-m.
50
+ float32 maxTorque;
51
+
52
+ /// Position correction factor in the range [0,1].
53
+ float32 correctionFactor;
54
+ };
55
+
56
+ /// A motor joint is used to control the relative motion
57
+ /// between two bodies. A typical usage is to control the movement
58
+ /// of a dynamic body with respect to the ground.
59
+ class b2MotorJoint : public b2Joint
60
+ {
61
+ public:
62
+ b2Vec2 GetAnchorA() const;
63
+ b2Vec2 GetAnchorB() const;
64
+
65
+ b2Vec2 GetReactionForce(float32 inv_dt) const;
66
+ float32 GetReactionTorque(float32 inv_dt) const;
67
+
68
+ /// Set/get the target linear offset, in frame A, in meters.
69
+ void SetLinearOffset(const b2Vec2& linearOffset);
70
+ const b2Vec2& GetLinearOffset() const;
71
+
72
+ /// Set/get the target angular offset, in radians.
73
+ void SetAngularOffset(float32 angularOffset);
74
+ float32 GetAngularOffset() const;
75
+
76
+ /// Set the maximum friction force in N.
77
+ void SetMaxForce(float32 force);
78
+
79
+ /// Get the maximum friction force in N.
80
+ float32 GetMaxForce() const;
81
+
82
+ /// Set the maximum friction torque in N*m.
83
+ void SetMaxTorque(float32 torque);
84
+
85
+ /// Get the maximum friction torque in N*m.
86
+ float32 GetMaxTorque() const;
87
+
88
+ /// Set the position correction factor in the range [0,1].
89
+ void SetCorrectionFactor(float32 factor);
90
+
91
+ /// Get the position correction factor in the range [0,1].
92
+ float32 GetCorrectionFactor() const;
93
+
94
+ /// Dump to b2Log
95
+ void Dump();
96
+
97
+ protected:
98
+
99
+ friend class b2Joint;
100
+
101
+ b2MotorJoint(const b2MotorJointDef* def);
102
+
103
+ void InitVelocityConstraints(const b2SolverData& data);
104
+ void SolveVelocityConstraints(const b2SolverData& data);
105
+ bool SolvePositionConstraints(const b2SolverData& data);
106
+
107
+ // Solver shared
108
+ b2Vec2 m_linearOffset;
109
+ float32 m_angularOffset;
110
+ b2Vec2 m_linearImpulse;
111
+ float32 m_angularImpulse;
112
+ float32 m_maxForce;
113
+ float32 m_maxTorque;
114
+ float32 m_correctionFactor;
115
+
116
+ // Solver temp
117
+ int32 m_indexA;
118
+ int32 m_indexB;
119
+ b2Vec2 m_rA;
120
+ b2Vec2 m_rB;
121
+ b2Vec2 m_localCenterA;
122
+ b2Vec2 m_localCenterB;
123
+ b2Vec2 m_linearError;
124
+ float32 m_angularError;
125
+ float32 m_invMassA;
126
+ float32 m_invMassB;
127
+ float32 m_invIA;
128
+ float32 m_invIB;
129
+ b2Mat22 m_linearMass;
130
+ float32 m_angularMass;
131
+ };
132
+
133
+ #endif
@@ -0,0 +1,222 @@
1
+ /*
2
+ * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
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
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked as such, and must not be
15
+ * misrepresented as being the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+ #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
20
+ #include <Box2D/Dynamics/b2Body.h>
21
+ #include <Box2D/Dynamics/b2TimeStep.h>
22
+
23
+ // p = attached point, m = mouse point
24
+ // C = p - m
25
+ // Cdot = v
26
+ // = v + cross(w, r)
27
+ // J = [I r_skew]
28
+ // Identity used:
29
+ // w k % (rx i + ry j) = w * (-ry i + rx j)
30
+
31
+ b2MouseJoint::b2MouseJoint(const b2MouseJointDef* def)
32
+ : b2Joint(def)
33
+ {
34
+ b2Assert(def->target.IsValid());
35
+ b2Assert(b2IsValid(def->maxForce) && def->maxForce >= 0.0f);
36
+ b2Assert(b2IsValid(def->frequencyHz) && def->frequencyHz >= 0.0f);
37
+ b2Assert(b2IsValid(def->dampingRatio) && def->dampingRatio >= 0.0f);
38
+
39
+ m_targetA = def->target;
40
+ m_localAnchorB = b2MulT(m_bodyB->GetTransform(), m_targetA);
41
+
42
+ m_maxForce = def->maxForce;
43
+ m_impulse.SetZero();
44
+
45
+ m_frequencyHz = def->frequencyHz;
46
+ m_dampingRatio = def->dampingRatio;
47
+
48
+ m_beta = 0.0f;
49
+ m_gamma = 0.0f;
50
+ }
51
+
52
+ void b2MouseJoint::SetTarget(const b2Vec2& target)
53
+ {
54
+ if (m_bodyB->IsAwake() == false)
55
+ {
56
+ m_bodyB->SetAwake(true);
57
+ }
58
+ m_targetA = target;
59
+ }
60
+
61
+ const b2Vec2& b2MouseJoint::GetTarget() const
62
+ {
63
+ return m_targetA;
64
+ }
65
+
66
+ void b2MouseJoint::SetMaxForce(float32 force)
67
+ {
68
+ m_maxForce = force;
69
+ }
70
+
71
+ float32 b2MouseJoint::GetMaxForce() const
72
+ {
73
+ return m_maxForce;
74
+ }
75
+
76
+ void b2MouseJoint::SetFrequency(float32 hz)
77
+ {
78
+ m_frequencyHz = hz;
79
+ }
80
+
81
+ float32 b2MouseJoint::GetFrequency() const
82
+ {
83
+ return m_frequencyHz;
84
+ }
85
+
86
+ void b2MouseJoint::SetDampingRatio(float32 ratio)
87
+ {
88
+ m_dampingRatio = ratio;
89
+ }
90
+
91
+ float32 b2MouseJoint::GetDampingRatio() const
92
+ {
93
+ return m_dampingRatio;
94
+ }
95
+
96
+ void b2MouseJoint::InitVelocityConstraints(const b2SolverData& data)
97
+ {
98
+ m_indexB = m_bodyB->m_islandIndex;
99
+ m_localCenterB = m_bodyB->m_sweep.localCenter;
100
+ m_invMassB = m_bodyB->m_invMass;
101
+ m_invIB = m_bodyB->m_invI;
102
+
103
+ b2Vec2 cB = data.positions[m_indexB].c;
104
+ float32 aB = data.positions[m_indexB].a;
105
+ b2Vec2 vB = data.velocities[m_indexB].v;
106
+ float32 wB = data.velocities[m_indexB].w;
107
+
108
+ b2Rot qB(aB);
109
+
110
+ float32 mass = m_bodyB->GetMass();
111
+
112
+ // Frequency
113
+ float32 omega = 2.0f * b2_pi * m_frequencyHz;
114
+
115
+ // Damping coefficient
116
+ float32 d = 2.0f * mass * m_dampingRatio * omega;
117
+
118
+ // Spring stiffness
119
+ float32 k = mass * (omega * omega);
120
+
121
+ // magic formulas
122
+ // gamma has units of inverse mass.
123
+ // beta has units of inverse time.
124
+ float32 h = data.step.dt;
125
+ b2Assert(d + h * k > b2_epsilon);
126
+ m_gamma = h * (d + h * k);
127
+ if (m_gamma != 0.0f)
128
+ {
129
+ m_gamma = 1.0f / m_gamma;
130
+ }
131
+ m_beta = h * k * m_gamma;
132
+
133
+ // Compute the effective mass matrix.
134
+ m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
135
+
136
+ // K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
137
+ // = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
138
+ // [ 0 1/m1+1/m2] [-r1.x*r1.y r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
139
+ b2Mat22 K;
140
+ K.ex.x = m_invMassB + m_invIB * m_rB.y * m_rB.y + m_gamma;
141
+ K.ex.y = -m_invIB * m_rB.x * m_rB.y;
142
+ K.ey.x = K.ex.y;
143
+ K.ey.y = m_invMassB + m_invIB * m_rB.x * m_rB.x + m_gamma;
144
+
145
+ m_mass = K.GetInverse();
146
+
147
+ m_C = cB + m_rB - m_targetA;
148
+ m_C *= m_beta;
149
+
150
+ // Cheat with some damping
151
+ wB *= 0.98f;
152
+
153
+ if (data.step.warmStarting)
154
+ {
155
+ m_impulse *= data.step.dtRatio;
156
+ vB += m_invMassB * m_impulse;
157
+ wB += m_invIB * b2Cross(m_rB, m_impulse);
158
+ }
159
+ else
160
+ {
161
+ m_impulse.SetZero();
162
+ }
163
+
164
+ data.velocities[m_indexB].v = vB;
165
+ data.velocities[m_indexB].w = wB;
166
+ }
167
+
168
+ void b2MouseJoint::SolveVelocityConstraints(const b2SolverData& data)
169
+ {
170
+ b2Vec2 vB = data.velocities[m_indexB].v;
171
+ float32 wB = data.velocities[m_indexB].w;
172
+
173
+ // Cdot = v + cross(w, r)
174
+ b2Vec2 Cdot = vB + b2Cross(wB, m_rB);
175
+ b2Vec2 impulse = b2Mul(m_mass, -(Cdot + m_C + m_gamma * m_impulse));
176
+
177
+ b2Vec2 oldImpulse = m_impulse;
178
+ m_impulse += impulse;
179
+ float32 maxImpulse = data.step.dt * m_maxForce;
180
+ if (m_impulse.LengthSquared() > maxImpulse * maxImpulse)
181
+ {
182
+ m_impulse *= maxImpulse / m_impulse.Length();
183
+ }
184
+ impulse = m_impulse - oldImpulse;
185
+
186
+ vB += m_invMassB * impulse;
187
+ wB += m_invIB * b2Cross(m_rB, impulse);
188
+
189
+ data.velocities[m_indexB].v = vB;
190
+ data.velocities[m_indexB].w = wB;
191
+ }
192
+
193
+ bool b2MouseJoint::SolvePositionConstraints(const b2SolverData& data)
194
+ {
195
+ B2_NOT_USED(data);
196
+ return true;
197
+ }
198
+
199
+ b2Vec2 b2MouseJoint::GetAnchorA() const
200
+ {
201
+ return m_targetA;
202
+ }
203
+
204
+ b2Vec2 b2MouseJoint::GetAnchorB() const
205
+ {
206
+ return m_bodyB->GetWorldPoint(m_localAnchorB);
207
+ }
208
+
209
+ b2Vec2 b2MouseJoint::GetReactionForce(float32 inv_dt) const
210
+ {
211
+ return inv_dt * m_impulse;
212
+ }
213
+
214
+ float32 b2MouseJoint::GetReactionTorque(float32 inv_dt) const
215
+ {
216
+ return inv_dt * 0.0f;
217
+ }
218
+
219
+ void b2MouseJoint::ShiftOrigin(const b2Vec2& newOrigin)
220
+ {
221
+ m_targetA -= newOrigin;
222
+ }