1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef B2_CONTACT_SOLVER_H
24 #define B2_CONTACT_SOLVER_H
25 
26 #include "box2d/b2_collision.h"
27 #include "box2d/b2_math.h"
28 #include "box2d/b2_time_step.h"
29 
30 class b2Contact;
31 class b2Body;
32 class b2StackAllocator;
33 struct b2ContactPositionConstraint;
34 
35 struct b2VelocityConstraintPoint
36 {
37 	b2Vec2 rA;
38 	b2Vec2 rB;
39 	float normalImpulse;
40 	float tangentImpulse;
41 	float normalMass;
42 	float tangentMass;
43 	float velocityBias;
44 };
45 
46 struct b2ContactVelocityConstraint
47 {
48 	b2VelocityConstraintPoint points[b2_maxManifoldPoints];
49 	b2Vec2 normal;
50 	b2Mat22 normalMass;
51 	b2Mat22 K;
52 	int32 indexA;
53 	int32 indexB;
54 	float invMassA, invMassB;
55 	float invIA, invIB;
56 	float friction;
57 	float restitution;
58 	float threshold;
59 	float tangentSpeed;
60 	int32 pointCount;
61 	int32 contactIndex;
62 };
63 
64 struct b2ContactSolverDef
65 {
66 	b2TimeStep step;
67 	b2Contact** contacts;
68 	int32 count;
69 	b2Position* positions;
70 	b2Velocity* velocities;
71 	b2StackAllocator* allocator;
72 };
73 
74 class b2ContactSolver
75 {
76 public:
77 	b2ContactSolver(b2ContactSolverDef* def);
78 	~b2ContactSolver();
79 
80 	void InitializeVelocityConstraints();
81 
82 	void WarmStart();
83 	void SolveVelocityConstraints();
84 	void StoreImpulses();
85 
86 	bool SolvePositionConstraints();
87 	bool SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB);
88 
89 	b2TimeStep m_step;
90 	b2Position* m_positions;
91 	b2Velocity* m_velocities;
92 	b2StackAllocator* m_allocator;
93 	b2ContactPositionConstraint* m_positionConstraints;
94 	b2ContactVelocityConstraint* m_velocityConstraints;
95 	b2Contact** m_contacts;
96 	int m_count;
97 };
98 
99 #endif
100 
101