Fx2D
A C++20 2D rigid-body physics engine
Loading...
Searching...
No Matches
Solver.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <memory>
6#include <vector>
7
8#include "Fx2D/Math.h"
9
10// Forward declaration
11class FxEntity;
12
13// Result of a single-sided SAT overlap query (A's edges tested against B).
14// gap < 0: bodies overlapping, |gap| = penetration depth on that axis.
15// has_sep: true if a separating axis was found (no overlap from A's side).
17 FxVec2f normal = {1.0f, 0.0f}; // edge normal of the min-penetration axis
18 float gap = -FxInfinityf; // maximum B_min_val seen (signed separation)
19 bool has_sep = false; // true if any edge gave gap > 0
20 size_t ref_edge_index = 0; // index of that edge in A's vertex list
21 FxVec2f ref_edge_dir = {0.0f, 0.0f}; // normalized direction along that edge
22 size_t pen_vertex_index = 0; // index of B's deepest vertex on that edge
23};
24
25// Solver-local body state, structure of arrays, one entry per registry packed index. Gathered
26// once per substep so the sweeps address bodies by index rather than chasing shared_ptrs, and
27// the layout the batched solve reads. See docs/roadmap/simd.md.
29 std::vector<float> vx, vy, w; // linear and angular velocity
30 std::vector<float> inv_m, inv_i; // effective inverse mass and inertia (0 when immovable)
31
32 void resize(std::size_t n) {
33 vx.resize(n);
34 vy.resize(n);
35 w.resize(n);
36 inv_m.resize(n);
37 inv_i.resize(n);
38 }
39
40 std::size_t size() const { return vx.size(); }
41
42 // Velocity of the material point at body-relative offset r. Same expression as
43 // FxEntity::velocity_at_local_point, kept identical so the two cannot drift apart.
44 FxVec2f velocity_at(int32_t body, const FxVec2f& r) const {
45 const std::size_t i = static_cast<std::size_t>(body);
46 return FxVec2f(vx[i] - w[i] * r.y(), vy[i] + w[i] * r.x());
47 }
48};
49
50// Per-substep sweep scratch for one contact: lever arms, their cross products with the contact
51// basis, the effective masses, the restitution target and the mixed material constants. Kept
52// beside the contact, not inside it, so copying a contact does not carry it.
54 FxVec2f rA[2]{{0.0f, 0.0f}, {0.0f, 0.0f}};
55 FxVec2f rB[2]{{0.0f, 0.0f}, {0.0f, 0.0f}};
56 float ra_n[2] = {0.0f, 0.0f}, rb_n[2] = {0.0f, 0.0f};
57 float ra_t[2] = {0.0f, 0.0f}, rb_t[2] = {0.0f, 0.0f};
58 float K_n[2] = {0.0f, 0.0f}, K_t[2] = {0.0f, 0.0f};
59 float wA = 0.0f, wB = 0.0f, IA = 0.0f, IB = 0.0f;
60
61 // Closing speed at substep start — fixes the restitution target for every sweep.
62 float vn_pre[2] = {0.0f, 0.0f};
63
64 // Pair material constants, mixed once per substep so the sweeps never touch an entity to
65 // find out how bouncy or how rough the pair is.
66 float restitution = 0.0f;
67 float mu_static = 0.0f;
68 float mu_kinetic = 0.0f;
69};
70
71struct FxContact {
72 // Sentinel for cache_slot below: this contact has not been registered with a scene.
73 static constexpr uint32_t kNoCacheSlot = 0xffffffffu;
74
75 private:
76 bool m_is_valid = false;
77
78 public:
79 size_t count = 0; // True if contact is valid
80 FxVec2f position[2] = {{0.0f, 0.0f}, {0.0f, 0.0f}}; // up to 2 contact points in world
81 // coordinates
82 FxVec2f normal{0.0f, 0.0f}; // Contact normal (unit vector)
83 float penetration_depth = FxInfinityf; // Penetration depth (positive if overlapping)
84 // Each point's own depth where it is known (two-point manifolds, held contacts), else
85 // infinite: penetration_depth, the deepest, is all there is. Points apart are not solved.
86 float point_depth[2] = {FxInfinityf, FxInfinityf};
87
88 // The two colliding bodies, borrowed not owned: FxScene pins a shared_ptr to every entity
89 // its contact buffers name, so one deleted between steps still survives to be reported.
90 // Valid until the next step(); do not keep a contact past the frame that produced it.
91 FxEntity* entity1 = nullptr; // First entity in collision
92 FxEntity* entity2 = nullptr; // Second entity in collision
93
94 // Impulse applied this substep (may be released; never exceeds what was applied).
95 float jn_accumulated[2] = {0.0f, 0.0f};
96 float jt_accumulated[2] = {0.0f, 0.0f};
97
98 // Previous substep impulse used as the warm-start guess.
99 float jn_warm[2] = {0.0f, 0.0f};
100 float jt_warm[2] = {0.0f, 0.0f};
101
102 // Packed indices of the two bodies into FxSolverBodies, filled when the contact is detected.
103 // The shared_ptrs above stay for the public contact buffer; the solver uses these.
104 int32_t body1 = -1;
105 int32_t body2 = -1;
106
107 // Solver-internal pair bookkeeping: the key identifies the entity pair, the slot is where
108 // its warm-start impulses and step-buffer position live. Exposed through contacts() only
109 // because the whole struct is; nothing outside the solver should read them.
110 uint64_t pair_key = 0;
112
113 // Constructor overloads
114 FxContact() = default;
115 FxContact(bool valid) : m_is_valid(valid) {}
116
117 // method to check validity
118 bool is_valid(bool full_check = true) const {
119 return m_is_valid &&
120 (!full_check || (entity1 != nullptr && entity2 != nullptr && count != 0 &&
121 std::isfinite(penetration_depth) && normal.norm() > 1e-3f));
122 }
123 void set_valid(bool valid) { m_is_valid = valid; }
124};
125
126// A contact carried through the substeps of one step: the manifold as the narrow phase found it,
127// plus where its points sit on each body, so later substeps re-measure it from the current poses
128// rather than detect it again (Macklin et al., "Small Steps in Physics Simulation", 2019).
131 FxVec2f local1[2] = {{0.0f, 0.0f}, {0.0f, 0.0f}}; // contact points in entity1's frame
132 FxVec2f local2[2] = {{0.0f, 0.0f}, {0.0f, 0.0f}}; // and in entity2's
133 float detect_depth[2] = {0.0f, 0.0f}; // each point's depth at detection
134 float detect_theta1 = 0.0f;
135 float detect_theta2 = 0.0f;
136 bool held = false; // false until the pair is detected this step, and again once released
137};
138
139// Position-based constraint base class for XPBD solver
141 // Joints rename the constraints they own; see FxJoint::namespace_constraints.
142 friend class FxJoint;
143
144 protected:
145 std::string m_name; // "id1_id2_constraint-name"
146 double compliance = 1e-7; // XPBD alpha = compliance / dt^2
147 // When set, prev_pose moves with pose so the correction registers no velocity. Velocity is
148 // derived as (pose - prev_pose)/h, so a constraint that only moves pose is a spring that
149 // converts every pull into momentum -- right for a joint, wrong for dragging a body.
150 bool carries_velocity = true;
151 std::shared_ptr<FxEntity> entity1;
152 std::shared_ptr<FxEntity> entity2;
153
154 public:
155 // bool entities_collide = false; // Whether connected entities should collide
156 // Set stiffness (converts to compliance internally)
157 void set_stiffness(double k) {
158 if (k <= 0.0) return;
159 compliance = 1.0 / k;
160 }
161 void setCompliance(double c) { compliance = std::max(0.0, c); }
162 // Evaluate C and gradients; set active=false to skip
163 virtual void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
164 bool& active) const = 0;
165 // One-iteration XPBD/PBD correction (no lambda term in numerator)
166 void resolve(double dt);
167 // Accessor method for name (required by FxNamedRegistry)
168 const std::string& get_name() const { return m_name; }
169
170 // Entity accessor methods (read-only)
171 const std::shared_ptr<FxEntity>& get_entity1() const { return entity1; }
172 const std::shared_ptr<FxEntity>& get_entity2() const { return entity2; }
173
174 // Entity name accessor methods
175 std::string get_entity1_name() const;
176 std::string get_entity2_name() const;
177
178 virtual ~FxConstraint() = default;
179};
180
181// Angular limit constraint that restricts the relative angle between two entities
182// to be within a specified range [lower, upper]. The constraint is only active when violated
184 public:
185 float lower_limit = 0.0f; // Lower angle limit (degrees)
186 float upper_limit = FxPif; // Upper angle limit (degrees)
187 float slop = 0.01f; // Tolerance zone around limits
188 bool enabled = true; // Whether this constraint is active
189 FxAngularLimitConstraint(const std::shared_ptr<FxEntity>& e1,
190 const std::shared_ptr<FxEntity>& e2);
191 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
192 bool& active) const override;
193};
194
195// Constraint that locks relative angle between two entities to a target value
197 public:
198 float target = 0.0f; // Target relative angle
199 bool enabled = true; // Whether this constraint is active
200 FxAngleLockConstraint(const std::shared_ptr<FxEntity>& e1, const std::shared_ptr<FxEntity>& e2,
201 float tgt = 0.0f);
202 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
203 bool& active) const override;
204};
205
206// Constraint that projects the separation between two anchor points onto a specified world axis
208 private:
209 FxVec2f m_anchor1; // Local anchor point on entity1
210 FxVec2f m_anchor2; // Local anchor point on entity2
211 public:
212 bool enabled = true; // Whether this constraint is active
213 FxAnchorConstraint(const std::shared_ptr<FxEntity>& e1, const std::shared_ptr<FxEntity>& e2,
214 const FxVec2f& anchor, bool anchor_is_local = true);
215 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
216 bool& active) const override;
217};
218
219// Linear limit constraint that restricts the projection of separation between two entities onto a
220// specified axis
222 private:
223 FxVec2f m_axis; // Axis direction (normalized)
224 bool m_axis_is_local; // Whether axis is local to entity1 or in world coordinates
225 float m_initial_projection;
226
227 public:
228 float lower_limit = 0; // Lower limit for projection
229 float upper_limit = 10; // Upper limit for projection
230 float slop = 0.0001f; // Tolerance zone around limits
231 bool enabled = true; // Whether this constraint is active
232 FxSeparationConstraint(const std::shared_ptr<FxEntity>& e1, const std::shared_ptr<FxEntity>& e2,
233 const FxVec2f& axis, bool axis_is_local = true);
234 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
235 bool& active) const override;
236};
237
238// Keeps anchor separation within [min_length, max_length], solving only violated bounds.
239// Equal limits give a rigid link; a zero minimum gives a rope that allows slack.
241 private:
242 FxVec2f m_anchor1; // Local anchor point on entity1
243 FxVec2f m_anchor2; // Local anchor point on entity2
244
245 public:
246 float min_length = 0.0f; // Anchors are pushed apart below this
247 float max_length = 1.0f; // Anchors are pulled together above this
248 float slop = 0.0f; // Tolerance zone outside both limits
249 bool enabled = true; // Whether this constraint is active
250
251 FxDistanceConstraint(const std::shared_ptr<FxEntity>& e1, const std::shared_ptr<FxEntity>& e2,
252 const FxVec2f& anchor1, const FxVec2f& anchor2);
253 // Current anchor separation in world units, whatever the limits are.
254 float length() const;
255 // World position of each anchor at the bodies' current poses.
258 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
259 bool& active) const override;
260};
261
262// Constraint that forces motion along a specified axis
264 private:
265 FxVec2f m_axis; // Axis direction (normalized)
266 bool m_axis_is_local; // Whether axis is local to entity1 or in world coordinates
267 float m_initial_projection; // Initial perpendicular distance projection
268 public:
269 bool enabled = true; // Whether this constraint is active
270 FxMotionAlongAxisConstraint(const std::shared_ptr<FxEntity>& e1,
271 const std::shared_ptr<FxEntity>& e2, const FxVec2f& axis,
272 bool axis_is_local = true);
273 void evaluate(float& C, FxVec2f& g1, FxVec2f& g2, float& gth1, float& gth2,
274 bool& active) const override;
275};
276
277// Partitions the broad-phase pair list into colors, where no two pairs in a color touch the
278// same movable body -- so a color's contacts write to disjoint bodies and can be solved
279// together. Greedy in pair order, so deterministic. See docs/concepts/collisions.md.
281 public:
282 // Colors are capped so the per-color bookkeeping stays a fixed, small cost. Contacts that
283 // find no free color land in an overflow group, which is solved one contact at a time.
284 static constexpr uint32_t kMaxColors = 12;
285
286 // Color `pairs`, leaving the list itself alone -- reordering it costs the narrow phase its
287 // locality. `entities` is the registry packed storage, indexed by the pair entries.
288 // Buffers are reused, so this allocates nothing after the first call.
289 void color_pairs(const std::vector<std::pair<size_t, size_t>>& pairs,
290 const std::vector<std::shared_ptr<FxEntity>>& entities);
291
292 // The color assigned to pair i, as an index the contact carries forward. kMaxColors means
293 // the pair overflowed and must be solved on its own.
294 uint32_t color_of_pair(size_t i) const { return m_color_of[i]; }
295
296 // Turn per-contact colors into a sweep order: `order` comes back holding contact indices
297 // grouped by color. Cheap -- it sorts four-byte indices, not contacts.
298 void group_contacts(const std::vector<uint32_t>& contact_colors, std::vector<uint32_t>& order);
299
300 // Group g spans [group_start(g), group_start(g + 1)) in that order.
301 size_t group_count() const { return m_group_starts.empty() ? 0 : m_group_starts.size() - 1; }
302 uint32_t group_start(size_t g) const { return m_group_starts[g]; }
303
304 // True when group g holds the contacts whose pairs did not fit a color. They may share
305 // bodies with each other and must be solved sequentially, never batched.
306 bool is_overflow_group(size_t g) const { return g == m_overflow_group; }
307
308 // Pairs placed into a color, and those that overflowed. Diagnostics only.
309 size_t colored_count() const { return m_colored_count; }
310 size_t overflow_count() const { return m_overflow_count; }
311
312 private:
313 std::vector<uint32_t> m_group_starts; // group_count() + 1 entries
314 std::vector<uint32_t> m_color_of; // per pair: color index, or kMaxColors for overflow
315 std::vector<uint64_t> m_used; // kMaxColors bitsets, one bit per body
316 std::vector<uint32_t> m_counts; // pairs per group, then reused as write cursors
317 std::vector<uint8_t> m_movable; // per body: can an impulse move it at all
318 std::vector<uint32_t> m_group_of_color; // color -> sweep group, or kUncolored if unused
319 std::vector<uint32_t> m_group_counts; // counting-sort cursors for group_contacts
320 size_t m_overflow_group = static_cast<size_t>(-1); // sweep group holding overflow, if any
321 size_t m_colored_count = 0;
322 size_t m_overflow_count = 0;
323 bool m_has_overflow = false;
324};
325
326// One color's contacts transposed into columns, built once per substep and swept
327// velocity_passes times. Always two slots per contact: a one-point manifold leaves slot 1 with
328// zero effective mass, read as inactive, so no lane has to branch on manifold size.
330 // Per contact.
331 std::vector<int32_t> ia, ib;
332 std::vector<float> nx, ny, tx, ty;
333 std::vector<float> wA, wB, IA, IB;
334 std::vector<float> restitution, mu_s, mu_k;
335 std::vector<uint32_t> contact_index; // where to write the impulses back
336 std::vector<float> jn_sum;
337
338 // The two bodies' velocities, gathered in and scattered out once per colour per pass. Held
339 // in columns so everything between the gather and the scatter is contiguous.
340 std::vector<float> vax, vay, wav, vbx, vby, wbv;
341
342 // Per contact, per slot.
343 std::vector<float> rAx[2], rAy[2], rBx[2], rBy[2];
344 std::vector<float> ra_n[2], rb_n[2], ra_t[2], rb_t[2];
345 std::vector<float> K_n[2], K_t[2], vn_pre[2];
346 std::vector<float> jn[2], jt[2];
347
348 std::size_t size() const { return ia.size(); }
349
350 // Every column, visited once. The single place that knows the full list, so adding a column
351 // cannot leave one uncleared and carrying last substep's data.
352 template<typename Fn>
353 void for_each_column(Fn fn) {
354 fn(ia);
355 fn(ib);
356 fn(nx);
357 fn(ny);
358 fn(tx);
359 fn(ty);
360 fn(wA);
361 fn(wB);
362 fn(IA);
363 fn(IB);
364 fn(restitution);
365 fn(mu_s);
366 fn(mu_k);
367 fn(contact_index);
368 fn(jn_sum);
369 fn(vax);
370 fn(vay);
371 fn(wav);
372 fn(vbx);
373 fn(vby);
374 fn(wbv);
375 for (int s = 0; s < 2; ++s) {
376 fn(rAx[s]);
377 fn(rAy[s]);
378 fn(rBx[s]);
379 fn(rBy[s]);
380 fn(ra_n[s]);
381 fn(rb_n[s]);
382 fn(ra_t[s]);
383 fn(rb_t[s]);
384 fn(K_n[s]);
385 fn(K_t[s]);
386 fn(vn_pre[s]);
387 fn(jn[s]);
388 fn(jt[s]);
389 }
390 }
391
392 void clear() {
393 for_each_column([](auto& column) { column.clear(); });
394 }
395
396 // Grow the velocity columns to match the contact columns after a build.
398 vax.resize(ia.size());
399 vay.resize(ia.size());
400 wav.resize(ia.size());
401 vbx.resize(ia.size());
402 vby.resize(ia.size());
403 wbv.resize(ia.size());
404 jn_sum.resize(ia.size());
405 }
406};
407
408namespace FxSolver {
409// True when a contact is one the velocity solver will actually act on. Shared so the contact
410// graph colors exactly the set the sweeps visit -- coloring a contact the solver then skips
411// would waste a color slot and could push a real contact into the overflow group.
412inline bool contact_is_solvable(const FxContact& contact) {
413 if (!contact.is_valid() || contact.penetration_depth <= 0.0f) return false;
414 if (!contact.entity1 || !contact.entity2) return false;
415 return contact.body1 >= 0 && contact.body2 >= 0;
416}
417
418// False for a manifold point that has come apart: a held manifold keeps a corner that lifts
419// off, and solving it would hold that corner up, or bounce it before it lands.
420inline constexpr float kContactPointApart = 1e-4f;
421inline bool contact_point_touches(const FxContact& contact, size_t i) {
422 return !(contact.point_depth[i] < -kContactPointApart);
423}
424
425// AABB overlap check methods
426bool aabb_overlap_check(const FxEntity& entity1, const FxEntity& entity2);
427bool aabb_overlap_check(const std::shared_ptr<FxEntity>& entity1,
428 const std::shared_ptr<FxEntity>& entity2);
429
430// Main collision detection method using SAT
431const FxContact collision_check(const FxEntity& entity1, const FxEntity& entity2);
432const FxContact collision_check(const std::shared_ptr<FxEntity>& entity1,
433 const std::shared_ptr<FxEntity>& entity2);
434
435// Speculative contact for CCD bodies: generates a pre-contact when bodies are separated but
436// approaching fast enough to close the gap within this substep.
437FxContact speculative_contact_check(const std::shared_ptr<FxEntity>& entity1,
438 const std::shared_ptr<FxEntity>& entity2, float substep_dt);
439
440// Holds a contact the narrow phase just found, recording where its points sit on each body.
441void hold_contact(FxHeldContact& held, const FxContact& contact);
442// Moves a held contact's points with their bodies and re-takes its depth at the deepest point.
443// False once either body has turned or a point has slid too far for the manifold to stand, or
444// the bodies have come apart; the pair is then tested by the narrow phase again.
446
447// Main collision resolution method
448void resolve_penetration(const FxContact& contact, double dt = 0.016f);
449// Capture closing speeds once per substep before impulses (shared restitution target), and
450// resolve the per-substep constants the sweeps need. Reads pose and material properties from the
451// entities, velocity from the columns.
453 const FxSolverBodies& bodies);
454// Velocity-level solver: restitution and dynamic friction impulses. Columns only.
455// Append one contact to the batch. Caller guarantees contact_is_solvable(contact).
456void batch_append(FxContactBatch& batch, const FxContact& contact, const FxContactSolverData& data,
457 uint32_t contact_index);
458// Solve batch entries [begin, end) -- one colour and one manifold size, so no two entries
459// share a movable body and every lane runs the same number of contact points.
460void resolve_velocities_batched(FxContactBatch& batch, std::size_t begin, std::size_t end,
461 FxSolverBodies& bodies, int slots);
462// Copy the accumulated impulses back onto the contacts, for the warm-start cache.
463void batch_write_back(const FxContactBatch& batch, std::vector<FxContact>& contacts);
464// Re-apply cached impulses from the previous solve before computing new ones. Columns only.
465void warm_start(FxContact& contact, const FxContactSolverData& data, FxSolverBodies& bodies);
466
467} // namespace FxSolver
@ C
Definition Input.h:15
FxVec< float, 2 > FxVec2f
Definition Math.h:416
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
FxAnchorConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2, const FxVec2f &anchor, bool anchor_is_local=true)
FxAngleLockConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2, float tgt=0.0f)
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
FxAngularLimitConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2)
Base type for solver constraints.
Definition Solver.h:140
void resolve(double dt)
const std::shared_ptr< FxEntity > & get_entity2() const
Definition Solver.h:172
void set_stiffness(double k)
Definition Solver.h:157
const std::shared_ptr< FxEntity > & get_entity1() const
Definition Solver.h:171
friend class FxJoint
Definition Solver.h:142
bool carries_velocity
Definition Solver.h:150
std::shared_ptr< FxEntity > entity1
Definition Solver.h:151
std::string get_entity2_name() const
const std::string & get_name() const
Definition Solver.h:168
double compliance
Definition Solver.h:146
void setCompliance(double c)
Definition Solver.h:161
virtual void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const =0
std::shared_ptr< FxEntity > entity2
Definition Solver.h:152
virtual ~FxConstraint()=default
std::string get_entity1_name() const
std::string m_name
Definition Solver.h:145
bool is_overflow_group(size_t g) const
Definition Solver.h:306
void group_contacts(const std::vector< uint32_t > &contact_colors, std::vector< uint32_t > &order)
size_t group_count() const
Definition Solver.h:301
size_t colored_count() const
Definition Solver.h:309
uint32_t group_start(size_t g) const
Definition Solver.h:302
static constexpr uint32_t kMaxColors
Definition Solver.h:284
void color_pairs(const std::vector< std::pair< size_t, size_t > > &pairs, const std::vector< std::shared_ptr< FxEntity > > &entities)
size_t overflow_count() const
Definition Solver.h:310
uint32_t color_of_pair(size_t i) const
Definition Solver.h:294
FxDistanceConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2, const FxVec2f &anchor1, const FxVec2f &anchor2)
float length() const
FxVec2f anchor2_world() const
FxVec2f anchor1_world() const
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
A named rigid body with state, material properties, geometry, and forces.
Definition Entity.h:49
FxMotionAlongAxisConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2, const FxVec2f &axis, bool axis_is_local=true)
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
FxSeparationConstraint(const std::shared_ptr< FxEntity > &e1, const std::shared_ptr< FxEntity > &e2, const FxVec2f &axis, bool axis_is_local=true)
void evaluate(float &C, FxVec2f &g1, FxVec2f &g2, float &gth1, float &gth2, bool &active) const override
Single-precision 2D vector.
const FxContact collision_check(const FxEntity &entity1, const FxEntity &entity2)
void warm_start(FxContact &contact, const FxContactSolverData &data, FxSolverBodies &bodies)
void hold_contact(FxHeldContact &held, const FxContact &contact)
bool contact_point_touches(const FxContact &contact, size_t i)
Definition Solver.h:421
void resolve_penetration(const FxContact &contact, double dt=0.016f)
void batch_append(FxContactBatch &batch, const FxContact &contact, const FxContactSolverData &data, uint32_t contact_index)
bool aabb_overlap_check(const FxEntity &entity1, const FxEntity &entity2)
void resolve_velocities_batched(FxContactBatch &batch, std::size_t begin, std::size_t end, FxSolverBodies &bodies, int slots)
FxContact speculative_contact_check(const std::shared_ptr< FxEntity > &entity1, const std::shared_ptr< FxEntity > &entity2, float substep_dt)
constexpr float kContactPointApart
Definition Solver.h:420
bool contact_is_solvable(const FxContact &contact)
Definition Solver.h:412
void batch_write_back(const FxContactBatch &batch, std::vector< FxContact > &contacts)
void init_velocity_pass(FxContact &contact, FxContactSolverData &data, const FxSolverBodies &bodies)
bool remeasure_contact(FxHeldContact &held)
std::vector< float > ty
Definition Solver.h:332
void clear()
Definition Solver.h:392
std::vector< float > jn_sum
Definition Solver.h:336
std::vector< float > vax
Definition Solver.h:340
std::vector< float > vbx
Definition Solver.h:340
std::vector< float > IA
Definition Solver.h:333
std::vector< float > wA
Definition Solver.h:333
std::vector< float > rAy[2]
Definition Solver.h:343
std::vector< float > rb_n[2]
Definition Solver.h:344
std::vector< float > wbv
Definition Solver.h:340
std::vector< float > rb_t[2]
Definition Solver.h:344
std::vector< float > vby
Definition Solver.h:340
std::vector< float > K_t[2]
Definition Solver.h:345
std::vector< int32_t > ia
Definition Solver.h:331
std::vector< float > mu_k
Definition Solver.h:334
std::vector< float > wB
Definition Solver.h:333
std::vector< float > nx
Definition Solver.h:332
std::vector< float > K_n[2]
Definition Solver.h:345
std::vector< float > vay
Definition Solver.h:340
std::vector< float > jn[2]
Definition Solver.h:346
std::vector< int32_t > ib
Definition Solver.h:331
std::vector< float > ra_n[2]
Definition Solver.h:344
std::vector< float > ra_t[2]
Definition Solver.h:344
std::vector< float > mu_s
Definition Solver.h:334
std::vector< float > wav
Definition Solver.h:340
std::size_t size() const
Definition Solver.h:348
std::vector< float > rBx[2]
Definition Solver.h:343
std::vector< uint32_t > contact_index
Definition Solver.h:335
std::vector< float > rAx[2]
Definition Solver.h:343
void for_each_column(Fn fn)
Definition Solver.h:353
std::vector< float > IB
Definition Solver.h:333
void size_velocity_columns()
Definition Solver.h:397
std::vector< float > rBy[2]
Definition Solver.h:343
std::vector< float > ny
Definition Solver.h:332
std::vector< float > tx
Definition Solver.h:332
std::vector< float > jt[2]
Definition Solver.h:346
std::vector< float > restitution
Definition Solver.h:334
std::vector< float > vn_pre[2]
Definition Solver.h:345
FxVec2f rB[2]
Definition Solver.h:55
FxVec2f rA[2]
Definition Solver.h:54
float rb_n[2]
Definition Solver.h:56
float ra_n[2]
Definition Solver.h:56
float vn_pre[2]
Definition Solver.h:62
float ra_t[2]
Definition Solver.h:57
float rb_t[2]
Definition Solver.h:57
Contact data produced by the narrow phase and solver.
Definition Solver.h:71
bool is_valid(bool full_check=true) const
Definition Solver.h:118
int32_t body1
Definition Solver.h:104
float point_depth[2]
Definition Solver.h:86
FxEntity * entity1
Definition Solver.h:91
FxVec2f normal
Definition Solver.h:82
FxEntity * entity2
Definition Solver.h:92
float jn_accumulated[2]
Definition Solver.h:95
float jn_warm[2]
Definition Solver.h:99
FxVec2f position[2]
Definition Solver.h:80
FxContact(bool valid)
Definition Solver.h:115
uint32_t cache_slot
Definition Solver.h:111
FxContact()=default
float jt_accumulated[2]
Definition Solver.h:96
float penetration_depth
Definition Solver.h:83
void set_valid(bool valid)
Definition Solver.h:123
int32_t body2
Definition Solver.h:105
static constexpr uint32_t kNoCacheSlot
Definition Solver.h:73
uint64_t pair_key
Definition Solver.h:110
size_t count
Definition Solver.h:79
float jt_warm[2]
Definition Solver.h:100
float detect_theta1
Definition Solver.h:134
FxVec2f local2[2]
Definition Solver.h:132
float detect_theta2
Definition Solver.h:135
FxVec2f local1[2]
Definition Solver.h:131
float detect_depth[2]
Definition Solver.h:133
FxContact contact
Definition Solver.h:130
size_t ref_edge_index
Definition Solver.h:20
FxVec2f ref_edge_dir
Definition Solver.h:21
bool has_sep
Definition Solver.h:19
size_t pen_vertex_index
Definition Solver.h:22
FxVec2f normal
Definition Solver.h:17
float gap
Definition Solver.h:18
std::vector< float > inv_i
Definition Solver.h:30
std::vector< float > vx
Definition Solver.h:29
std::vector< float > vy
Definition Solver.h:29
std::size_t size() const
Definition Solver.h:40
std::vector< float > w
Definition Solver.h:29
std::vector< float > inv_m
Definition Solver.h:30
FxVec2f velocity_at(int32_t body, const FxVec2f &r) const
Definition Solver.h:44
void resize(std::size_t n)
Definition Solver.h:32