ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
types.hpp
1
10
16
17#ifndef TOYMAKERENGINE_PHYSICSTYPES_H
18#define TOYMAKERENGINE_PHYSICSTYPES_H
19
20#include <array>
21#include <string>
22#include <utility>
23
24#include <glm/glm.hpp>
25#include <nlohmann/json.hpp>
26
28
29namespace ToyMaker {
30 struct PhysicsState;
31
47 const ObjectBounds& object,
48 const PhysicsState& physics,
49 const glm::vec3& correctionPoint,
50 const glm::vec3& correctionGradient
51 );
52
67 const ObjectBounds& object,
68 const PhysicsState& physics,
69 const glm::vec3& correctionRotational
70 );
71
79 glm::mat3 computeInertiaRotationalWorld(const glm::vec3& rotationalInertiaLocal, const glm::quat& orientation);
80
89 ObjectBounds object,
90 const PhysicsState& physics,
91 const glm::vec3& impulsePositional,
92 const glm::vec3& impulsePoint
93 );
94
103 ObjectBounds object,
104 const PhysicsState& physics,
105 const glm::vec3& impulseRotational
106 );
107
115 const ObjectBounds& object,
116 PhysicsState physics,
117 const glm::vec3& impulsePositional,
118 const glm::vec3& impulsePoint
119 );
120
127 const ObjectBounds& object,
128 PhysicsState physics,
129 const glm::vec3& impulseRotational
130 );
131
143 using Traits = uint8_t;
144
152 enum Mode: Traits {
159
169
175 };
176
182 static const Traits MaskMode;
183
203
209 static const Traits MaskCollisionResponse;
210
216 inline static std::string getComponentTypeName() { return "PhysicsState"; }
217
225 glm::vec3 mForce { 0.f };
226
235 glm::vec3 mTorque { 0.f };
236
243 glm::vec3 mVelocity { 0.f };
244
251 glm::vec3 mAngularVelocity { 0.f };
252
260 glm::vec3 mRotationalInertiaInverse { 1.f };
261
266 float mMassInverse { 1.f };
267
274
281
290
296 float mVelocityBleed { 0.01f };
297
303 float mVelocityBleedAngular { 0.01f };
304
309 float mVelocityCutoff { 0.001f };
310
315 float mVelocityCutoffAngular { 0.0005f };
316
321 Traits mTraits { static_cast<Traits>(COLLISION_SEPARATE) | static_cast<Traits>(MODE_DYNAMIC) };
322
332 void applyForceGlobal(const glm::vec3& force, const glm::vec3& atPosition, const ObjectBounds& bounds);
333
345 void applyForceLocal(const glm::vec3& force, const glm::vec3& atPosition, const ObjectBounds& bounds);
346
351 inline float getMass() const {
352 if(mMassInverse == 0.f) {
353 return std::numeric_limits<float>::max();
354 }
355 return 1.f / mMassInverse;
356 }
357
362 inline void setMass(float mass) {
363 assert(isNumber(mass) && isPositiveStrict(mass) && "Mass must be a valid positive number");
364 if(mass == std::numeric_limits<float>::max()) {
365 mMassInverse = 0.f;
366 return;
367 }
368 mMassInverse = 1.f / mass;
369 }
370
375 inline glm::vec3 getRotationalInertia() const {
376 return glm::vec3 {
377 mRotationalInertiaInverse.x == 0.f? std::numeric_limits<float>::max(): 1.f / mRotationalInertiaInverse.x,
378 mRotationalInertiaInverse.y == 0.f? std::numeric_limits<float>::max(): 1.f / mRotationalInertiaInverse.y,
379 mRotationalInertiaInverse.z == 0.f? std::numeric_limits<float>::max(): 1.f / mRotationalInertiaInverse.z,
380 };
381 }
382
387 inline void setRotationalInertia(const glm::vec3& rotationalInertia) {
388 assert(
389 isNumber(rotationalInertia) && isPositiveStrict(rotationalInertia)
390 && "Rotational inertia must be valid positive number for each axis"
391 );
392 mRotationalInertiaInverse = glm::vec3 {
393 rotationalInertia.x == std::numeric_limits<float>::max()? 0.f: 1.f / rotationalInertia.x,
394 rotationalInertia.y == std::numeric_limits<float>::max()? 0.f: 1.f / rotationalInertia.y,
395 rotationalInertia.z == std::numeric_limits<float>::max()? 0.f: 1.f / rotationalInertia.z,
396 };
397 }
398
403 inline void setCoefficientRestitution(float newCoefficient) {
404 assert(
405 isNonNegative(newCoefficient) && newCoefficient <= 1.f
406 && "Restitution coefficient must be non negative and cannot exceed 1"
407 );
408 mCoefficientRestitution = newCoefficient;
409 }
410
415 inline Mode getMode() const {
416 switch(mTraits&MaskMode) {
417 case MODE_DYNAMIC:
418 return MODE_DYNAMIC;
419 case MODE_KINEMATIC:
420 return MODE_KINEMATIC;
421 case MODE_STATIC:
422 return MODE_STATIC;
423 default:
424 return MODE_STATIC;
425 assert(false && "Unrecognized physics type specified");
426 }
427 }
428
433 inline void setMode(Mode mode) {
434 switch(mode&MaskMode) {
435 case MODE_DYNAMIC:
436 case MODE_KINEMATIC:
437 case MODE_STATIC:
438 mTraits = (mTraits&(~MaskMode)) | mode;
439 return;
440 default:
441 assert(false && "Unrecognized physics type specified");
442 }
443 }
444
454 inline bool separatesOnCollision() const {
455 return mTraits&static_cast<Traits>(COLLISION_SEPARATE);
456 }
457
467 inline bool signalsOnCollision() const {
468 return mTraits&static_cast<Traits>(COLLISION_SIGNAL);
469 }
470
476 mTraits |= response;
477 }
478
484 mTraits &= ~response;
485 }
486 };
487
488 inline const PhysicsState::Traits PhysicsState::MaskMode { 0x3 };
489 inline const PhysicsState::Traits PhysicsState::MaskCollisionResponse { 0xC };
490
491 template<uint8_t LagrangeCount>
492 class Constraint;
493
494 template <typename TConfig, typename TParameter, uint8_t LagrangeCount>
496
507 private:
517 float mCompliance { 0.f };
518
519 protected:
524 BaseConstraint(float compliance) { setCompliance(compliance); }
525
526 public:
527 using ParticipantID = std::size_t;
528
529 using ParticipantTable = std::unordered_map<
530 ParticipantID,
531 std::pair<
532 std::reference_wrapper<ObjectBounds>,
533 std::reference_wrapper<PhysicsState>
534 >
535 >;
536
541 float getCompliance() const;
542
551 const ParticipantTable& states,
552 float substepSeconds
553 ) {}
554
563 const ParticipantTable& states,
564 float substepSeconds
565 ) {}
566
572 virtual void resetLagrange() {}
573
578 void setCompliance(float newCompliance);
579
584 template <typename TConstraint,
585 std::enable_if_t<
586 std::is_base_of<
588 TConstraint
589 >::value, bool
590 > = true
591 >
592 void setParameter(ParticipantID participant, const typename TConstraint::Parameter& parameter);
593
598 template <typename TConstraint,
599 std::enable_if_t<
600 std::is_base_of<
602 TConstraint
603 >::value, bool
604 > = true
605 >
606 typename TConstraint::Parameter getParameter(ParticipantID participant) const;
607
612 template <typename TConstraint,
613
614 std::enable_if_t<
615 std::is_base_of<
617 TConstraint
618 >::value, bool
619 > = true
620 >
621 void setConfig(const typename TConstraint::Config& config);
622
627 template <typename TConstraint,
628 std::enable_if_t<
629 std::is_base_of<
631 TConstraint
632 >::value, bool
633 > = true
634 >
635 typename TConstraint::Config getConfig() const;
636
637 virtual ~BaseConstraint() {};
638 };
639
645 template<uint8_t LagrangeCount>
647 private:
653 template<uint8_t... ints>
654 void resetLagrange(std::integer_sequence<uint8_t, ints...> index);
655
665 std::array<float, LagrangeCount> mLagrangeMultipliers { 0.f };
666
671 std::array<float, LagrangeCount> mLagrangeDeltas { 0.f };
672
673 protected:
678 Constraint(float compliance): BaseConstraint { compliance } {}
679
680 public:
685 void applyLagrangeDelta(float delta, uint8_t index);
686
691 const std::array<float, LagrangeCount>& getLagrange() const;
692
697 const std::array<float, LagrangeCount>& getLagrangeDelta() const;
698
702 void resetLagrange() override;
703 };
704
715 template <typename TConfig, typename TParameter, uint8_t LagrangeCount>
716 class ConstraintParametrized: public Constraint<LagrangeCount> {
717 private:
724 std::unordered_map<BaseConstraint::ParticipantID, TParameter> mParameters {};
725
730 TConfig mConfig {};
731
732 public:
733 using Config = TConfig;
734 using Parameter = TParameter;
735 static const uint8_t NLagrange = LagrangeCount;
736
742 const TConfig& config,
743 const std::vector<TParameter>& constraintParameters,
744 float compliance
745 );
746
751 void setConfig(const TConfig& config);
752
757 TConfig getConfig() const;
758
763 void setParameter(BaseConstraint::ParticipantID participant, const TParameter& parameter);
764
769 inline TParameter getParameter(BaseConstraint::ParticipantID participant) const {
770 return mParameters.at(participant);
771 }
772
777 void removeParameter(BaseConstraint::ParticipantID participant);
778
783 const std::unordered_map<BaseConstraint::ParticipantID, TParameter>& getParameters() const;
784 };
785
793 struct ConstraintContact: public Constraint<2> {
794 public:
800 bool mCollided { false };
801
806 glm::vec3 mPreviousA { 0.f };
807
812 glm::vec3 mCurrentA { 0.f };
813
818 glm::vec3 mRelativeA { 0.f };
819
824 glm::vec3 mPreviousB { 0.f };
825
830 glm::vec3 mCurrentB { 0.f };
831
836 glm::vec3 mRelativeB { 0.f };
837
843 glm::vec3 mContactNormal { 0.f };
844
850 float mCollisionVelocity { 0.f };
851
857 float mPenetration { 0.f };
858
864
870 const Collision& collision,
871 const PhysicsState& physicsA,
872 const ObjectBounds& boundsA,
873 const ObjectBounds& boundsAPrev,
874 const PhysicsState& physicsB,
875 const ObjectBounds& boundsB,
876 const ObjectBounds& boundsBPrev
877 );
878
884 const ParticipantTable& states,
885 float substepSeconds
886 ) override;
887
893 const ParticipantTable& states,
894 float substepSeconds
895 ) override;
896 };
897
905 public:
911
917 void addContact(const Collision& collision,
918 const PhysicsState& physicsA, const ObjectBounds& boundsA, const ObjectBounds& boundsAPrev,
919 const PhysicsState& physicsB, const ObjectBounds& boundsB, const ObjectBounds& boundsBPrev
920 );
921
927 const ParticipantTable& states,
928 float substepSeconds
929 ) override;
930
936 const ParticipantTable& states,
937 float substepSeconds
938 ) override;
939
944 void resetLagrange() override;
945
950 inline uint8_t getNContacts() const { return mNContacts; }
951
956 inline void clear() { mNContacts = 0; }
957
958 private:
963 uint8_t mNContacts { 0 };
964
969 std::array<ConstraintContact, 5> mContacts {};
970
976 void trim(const ObjectBounds& boundsA, const ObjectBounds& boundsB);
977 };
978
987 class ConstraintDampingRigidbody: public ConstraintParametrized<float, PhysicsState, 1> {
988 public:
994
1000 const ParticipantTable& states,
1001 float substepSeconds
1002 ) override;
1003 };
1004
1005
1018 glm::vec3 mAxis { 1.f, 0.f, 0.f };
1019
1028 float mBoundLower { 0.f };
1029
1038 float mBoundUpper { 0.f };
1039
1044 bool isActive { true };
1045
1050 inline bool isSensible(bool isRotation=false) const {
1051 return (
1053 && isNumber(mAxis)
1054 && squareDistance(mAxis) != 0.f
1058 && (!isRotation || (
1059 mBoundLower < glm::pi<float>() && mBoundUpper < glm::pi<float>()
1060 && mBoundLower > -glm::pi<float>() && mBoundUpper > -glm::pi<float>()
1061 ))
1062 );
1063 }
1064 };
1065
1075 glm::quat mRotateToLocal { 1.f, 0.f, 0.f, 0.f };
1076
1082 glm::vec3 mVector { 0.f, 0.f, 1.f };
1083
1084 inline bool isSensible(bool isRotation=false) const {
1085 return (
1087 && glm::length(mRotateToLocal) == 1.f
1088 );
1089 }
1090 };
1091
1092
1098 class ConstraintRotation1D: public ConstraintParametrized<Constraint1DOFConfig, Constraint1DOFParam, 1> {
1099 public:
1101 void applyConstraintPosition(const ParticipantTable& states, float substepSeconds);
1102 };
1103
1109 class ConstraintDistance1D: public ConstraintParametrized<Constraint1DOFConfig, Constraint1DOFParam, 1> {
1110 public:
1112 void applyConstraintPosition(const ParticipantTable& states, float substepSeconds);
1113 };
1114
1115 NLOHMANN_JSON_SERIALIZE_ENUM(PhysicsState::Mode, {
1116 { PhysicsState::MODE_DYNAMIC, "dynamic" },
1117 { PhysicsState::MODE_KINEMATIC, "kinematic" },
1118 { PhysicsState::MODE_STATIC, "static" },
1119 });
1120
1121 NLOHMANN_JSON_SERIALIZE_ENUM(PhysicsState::CollisionResponse, {
1122 { PhysicsState::COLLISION_SEPARATE, "separate" },
1123 { PhysicsState::COLLISION_SIGNAL, "signal" },
1124 });
1125
1126 inline void from_json(
1127 const nlohmann::json& json,
1128 PhysicsState& physics
1129 ) {
1130 assert(json.at("type") == PhysicsState::getComponentTypeName() && "Incorrect type property for an physics property component");
1131 physics = {};
1132
1133 float mass;
1134 if(json.at("mass").is_string() && json.at("mass") == "infinity") {
1135 mass = std::numeric_limits<float>::max();
1136 } else {
1137 mass = json.at("mass");
1138 }
1139
1140 physics.setMass(mass);
1141 physics.setMode(json.at("mode"));
1142
1143 if(json.find("collision_response") != json.end()) {
1144 physics.mTraits &= ~PhysicsState::MaskCollisionResponse;
1145 for(const PhysicsState::CollisionResponse response: json.at("collision_response")) {
1146 physics.setCollisionResponse(response);
1147 }
1148 }
1149
1150 if(json.find("velocity") != json.end()) {
1151 physics.mVelocity = glm::vec3 {
1152 json.at("velocity")[0],
1153 json.at("velocity")[1],
1154 json.at("velocity")[2],
1155 };
1156 assert(isNumber(physics.mVelocity) && isFinite(physics.mVelocity) && "Velocity must be sensible");
1157 }
1158 if(json.find("velocity_bleed") != json.end()) {
1159 physics.mVelocityBleed = json.at("velocity_bleed");
1160 assert(
1161 isNumber(physics.mVelocityBleed)
1162 && physics.mVelocityBleed >= 0.f
1163 && physics.mVelocityBleed <= 1.f
1164 &&"Velocity bleed must be a finite positive number in range [0, 1]."
1165 );
1166 }
1167 if(json.find("velocity_cutoff") != json.end()) {
1168 physics.mVelocityCutoff = json.at("velocity_cutoff");
1169 assert(
1170 isNumber(physics.mVelocityCutoff)
1171 && isFinite(physics.mVelocityCutoff)
1172 && isNonNegative(physics.mVelocityCutoff)
1173 && "Velocity cutoff must be a finite non-negative number"
1174 );
1175 }
1176
1177 if(json.find("angular_velocity") != json.end()) {
1178 physics.mAngularVelocity = glm::vec3 {
1179 json.at("angular_velocity")[0],
1180 json.at("angular_velocity")[1],
1181 json.at("angular_velocity")[2],
1182 };
1183 assert(isNumber(physics.mAngularVelocity) && isFinite(physics.mAngularVelocity) && "Angular velocity must be sensible");
1184 }
1185 if(json.find("angular_velocity_bleed") != json.end()) {
1186 physics.mVelocityBleedAngular = json.at("angular_velocity_bleed");
1187 assert(
1188 isNumber(physics.mVelocityBleedAngular)
1189 && physics.mVelocityBleedAngular >= 0.f
1190 && physics.mVelocityBleedAngular <= 1.f
1191 && "Velocity bleed must be a finite positive number in range [0, 1]."
1192 );
1193 }
1194 if(json.find("angular_velocity_cutoff") != json.end()) {
1195 physics.mVelocityCutoffAngular = json.at("angular_velocity_cutoff");
1196 assert(
1197 isNumber(physics.mVelocityCutoffAngular)
1198 && isFinite(physics.mVelocityCutoffAngular)
1199 && isNonNegative(physics.mVelocityCutoffAngular)
1200 && "Velocity cutoff must be a finite positive number"
1201 );
1202 }
1203
1204 if(json.find("force") != json.end()) {
1205 physics.mForce = glm::vec3 {
1206 json.at("force")[0],
1207 json.at("force")[1],
1208 json.at("force")[2],
1209 };
1210 assert(isNumber(physics.mForce) && isFinite(physics.mForce) && "Force must be sensible");
1211 }
1212
1213 if(json.find("torque") != json.end()) {
1214 physics.mForce = glm::vec3 {
1215 json.at("torque")[0],
1216 json.at("torque")[1],
1217 json.at("torque")[2],
1218 };
1219 assert(isNumber(physics.mTorque) && isFinite(physics.mTorque) && "Torque must be sensible");
1220 }
1221
1222 if(json.find("coefficient_friction_static") != json.end()) {
1223 physics.mCoefficientFrictionStatic = json.at("coefficient_friction_static");
1224 assert(physics.mCoefficientFrictionStatic >= 0.f && "Coefficient friction must be non-negative");
1225 }
1226
1227 if(json.find("coefficient_friction_dynamic") != json.end()) {
1228 physics.mCoefficientFrictionDynamic = json.at("coefficient_friction_dynamic");
1229 assert(physics.mCoefficientFrictionDynamic >= 0.f && "Coefficient friction must be non-negative");
1230 }
1231
1232 if(json.find("coefficient_restitution") != json.end()) {
1233 physics.setCoefficientRestitution(json.at("coefficient_restitution"));
1234 }
1235 }
1236
1237 inline void to_json(
1238 nlohmann::json& json,
1239 const PhysicsState& physics
1240 ) {
1241 const float mass { physics.getMass() };
1242 std::vector<PhysicsState::CollisionResponse> collisionResponse {};
1243 if(physics.mTraits&PhysicsState::COLLISION_SEPARATE) {
1244 collisionResponse.push_back(PhysicsState::COLLISION_SEPARATE);
1245 }
1246 if(physics.mTraits&PhysicsState::COLLISION_SIGNAL) {
1247 collisionResponse.push_back(PhysicsState::COLLISION_SIGNAL);
1248 }
1249 json = {
1251 mass != std::numeric_limits<float>::max()?
1252 nlohmann::json::object({ "mass", mass }):
1253 nlohmann::json::object({ "mass", "infinity" }),
1254 { "mode", physics.getMode() },
1255 { "collision_response", collisionResponse },
1256 { "coefficient_friction_static", physics.mCoefficientFrictionStatic },
1257 { "coefficient_friction_dynamic", physics.mCoefficientFrictionDynamic },
1258 { "coefficient_restitution", physics.mCoefficientRestitution },
1259 { "velocity_bleed", physics.mVelocityBleed },
1260 { "angular_velocity_bleed", physics.mVelocityBleedAngular },
1261 { "velocity_cutoff", physics.mVelocityCutoff },
1262 { "velocity_cutoff_angular", physics.mVelocityCutoffAngular },
1263 };
1264 }
1265
1266 template <uint8_t LagrangeCount>
1267 inline const std::array<float, LagrangeCount>& Constraint<LagrangeCount>::getLagrange() const {
1268 return mLagrangeMultipliers;
1269 }
1270
1271 template <uint8_t LagrangeCount>
1272 inline const std::array<float, LagrangeCount>& Constraint<LagrangeCount>::getLagrangeDelta() const {
1273 return mLagrangeMultipliers;
1274 }
1275
1276 template <uint8_t LagrangeCount>
1277 inline void Constraint<LagrangeCount>::applyLagrangeDelta(float delta, uint8_t index) {
1278 mLagrangeDeltas[index] = delta;
1279 mLagrangeMultipliers[index] += delta;
1280 }
1281
1282 template <uint8_t LagrangeCount>
1284 resetLagrange(std::make_integer_sequence<uint8_t, LagrangeCount>());
1285 }
1286
1287 template<uint8_t LagrangeCount>
1288 template<uint8_t ...indices>
1289 inline void Constraint<LagrangeCount>::resetLagrange(std::integer_sequence<uint8_t, indices...> sequence) {
1290 ((mLagrangeMultipliers[indices] = mLagrangeDeltas[indices] = 0.f), ...);
1291 }
1292
1293 template<typename TConfig, typename TParameter, uint8_t LagrangeCount>
1294 inline void ConstraintParametrized<TConfig, TParameter, LagrangeCount>::setParameter(BaseConstraint::ParticipantID participant, const TParameter& parameter) {
1295 mParameters[participant] = parameter;
1296 }
1297
1298 template <typename TConstraint,
1299 std::enable_if_t<
1300 std::is_base_of<
1302 TConstraint
1303 >::value, bool
1304 >
1305 >
1306 inline void BaseConstraint::setParameter(ParticipantID participant, const typename TConstraint::Parameter& parameter) {
1307 static_cast<TConstraint&>(*this).setParameter(participant, parameter);
1308 }
1309
1310 template <typename TConstraint,
1311 std::enable_if_t<
1312 std::is_base_of<
1314 TConstraint
1315 >::value, bool
1316 >
1317 >
1318 inline typename TConstraint::Parameter BaseConstraint::getParameter(ParticipantID participant) const {
1319 return static_cast<TConstraint&>(*this).getParameter(participant);
1320 }
1321
1322
1323 template<typename TConfig, typename TParameter, uint8_t LagrangeCount>
1325 mConfig = config;
1326 }
1327
1328
1329 template<typename TConfig, typename TParameter, uint8_t LagrangeCount>
1333
1334 template <typename TConstraint,
1335 std::enable_if_t<
1336 std::is_base_of<
1338 TConstraint
1339 >::value, bool
1340 >
1341 >
1342 inline void BaseConstraint::setConfig(const typename TConstraint::Config& config) {
1343 static_cast<TConstraint&>(*this).setConfig(config);
1344 }
1345
1346 template <typename TConstraint,
1347 std::enable_if_t<
1348 std::is_base_of<
1350 TConstraint
1351 >::value, bool
1352 >
1353 >
1354 inline typename TConstraint::Config BaseConstraint::getConfig() const {
1355 return static_cast<TConstraint&>(*this).getConfig();
1356 }
1357
1358 template<typename TConfig, typename TParameter, uint8_t LagrangeCount>
1359 inline void ConstraintParametrized<TConfig, TParameter, LagrangeCount>::removeParameter(BaseConstraint::ParticipantID participant) {
1360 mParameters.erase(participant);
1361 }
1362
1363 template <typename TConfig, typename TParameter, uint8_t LagrangeCount>
1364 inline const std::unordered_map<BaseConstraint::ParticipantID, TParameter>& ConstraintParametrized<TConfig, TParameter, LagrangeCount>::getParameters() const {
1365 return mParameters;
1366 }
1367
1368 template <typename TConfig, typename TParameter, uint8_t LagrangeCount>
1369 inline ConstraintParametrized<TConfig, TParameter, LagrangeCount>::ConstraintParametrized(const TConfig& config, const std::vector<TParameter>& constraintParameters, float compliance): Constraint<LagrangeCount> { compliance }, mConfig { config } {
1370 for(auto i { 0 }; i < constraintParameters.size(); ++i) {
1371 setParameter(i, constraintParameters[i]);
1372 }
1373 }
1374}
1375
1376#endif
1377
Base class for constraints.
Definition types.hpp:506
void setConfig(const typename TConstraint::Config &config)
Sets the configuration of a constraint.
Definition types.hpp:1342
TConstraint::Config getConfig() const
Gets the configuration of a constraint.
Definition types.hpp:1354
TConstraint::Parameter getParameter(ParticipantID participant) const
Gets a parameter associated with a particular constraint participant.
Definition types.hpp:1318
virtual void applyConstraintVelocity(const ParticipantTable &states, float substepSeconds)
Applies velocity-based constraint to current set of bounds and physics states.
Definition types.hpp:562
BaseConstraint(float compliance)
Initializes this constraint.
Definition types.hpp:524
virtual void resetLagrange()
Resets all lagrange multipliers, preparing them for a new sequence of constraint solve substeps.
Definition types.hpp:572
float mCompliance
Value greater than equal to zero, inverse of the physical stiffness of this constraint.
Definition types.hpp:517
void setCompliance(float newCompliance)
Sets the compliance value for this constraint.
Definition types.cpp:42
void setParameter(ParticipantID participant, const typename TConstraint::Parameter &parameter)
Sets a parameter belonging to a particular constraint participant.
Definition types.hpp:1306
virtual void applyConstraintPosition(const ParticipantTable &states, float substepSeconds)
Applies positional constraint to current set of bounds and physics states.
Definition types.hpp:550
float getCompliance() const
Gets the current compliance value for this constraint.
Definition types.cpp:47
void trim(const ObjectBounds &boundsA, const ObjectBounds &boundsB)
Culls contacts that are no longer considered colliding, or have moved too far from their original loc...
Definition types.cpp:575
void resetLagrange() override
Resets all lagrange values associated with this manifold.
Definition types.cpp:398
void applyConstraintVelocity(const ParticipantTable &states, float substepSeconds) override
Applies dynamic friction.
Definition types.cpp:404
void addContact(const Collision &collision, const PhysicsState &physicsA, const ObjectBounds &boundsA, const ObjectBounds &boundsAPrev, const PhysicsState &physicsB, const ObjectBounds &boundsB, const ObjectBounds &boundsBPrev)
Attempts to add a contact to the manifold, succeeding when the new contact is not close to any existi...
Definition types.cpp:416
std::array< ConstraintContact, 5 > mContacts
Collection of contact constraints tracked by this manifold.
Definition types.hpp:969
void applyConstraintPosition(const ParticipantTable &states, float substepSeconds) override
Separates intersecting/colliding objects and applies static friction.
Definition types.cpp:410
void clear()
Clears all contacts.
Definition types.hpp:956
uint8_t getNContacts() const
Returns the number of contacts currently held by this manifold.
Definition types.hpp:950
ConstraintContactManifold()
Constraint constructor with a compliance of 0, since our collision constraints are perfectly stiff.
Definition types.hpp:910
uint8_t mNContacts
The number of contacts stored by this manifold.
Definition types.hpp:963
The velocity constraint responsible for applying a velocity-dependent damping force on all dynamic ob...
Definition types.hpp:987
void applyConstraintVelocity(const ParticipantTable &states, float substepSeconds) override
Slow down dynamic objects moving at a constant speed so that they eventually come to a stop.
Definition types.cpp:608
Constraint where the distance between a pair of points, one from each participant,...
Definition types.hpp:1109
void applyConstraintPosition(const ParticipantTable &states, float substepSeconds)
Applies positional constraint to current set of bounds and physics states.
Definition types.cpp:740
Subclass implementation for any constraint which takes data of type TParameter.
Definition types.hpp:716
const std::unordered_map< BaseConstraint::ParticipantID, TParameter > & getParameters() const
Returns all parameters known to this constraint.
Definition types.hpp:1364
ConstraintParametrized(const TConfig &config, const std::vector< TParameter > &constraintParameters, float compliance)
Initializes this constraint with some initial compliance value and constraint parameters.
Definition types.hpp:1369
TParameter getParameter(BaseConstraint::ParticipantID participant) const
Gets parameter belonging to a particular constraint participant.
Definition types.hpp:769
TConfig mConfig
The configuration of the constraint as a whole, specific to this constraint.
Definition types.hpp:730
void setParameter(BaseConstraint::ParticipantID participant, const TParameter &parameter)
Adds a parameter for this constraint.
Definition types.hpp:1294
std::unordered_map< BaseConstraint::ParticipantID, TParameter > mParameters
A set of parameters associated with each entity.
Definition types.hpp:724
void setConfig(const TConfig &config)
Sets configuration for this constraint.
Definition types.hpp:1324
void removeParameter(BaseConstraint::ParticipantID participant)
Removes a parameter belonging to a particular constraint participant.
Definition types.hpp:1359
TConfig getConfig() const
Gets the current configuration of this constraint.
Definition types.hpp:1330
Restricts angle between 2 vectors from 2 participants around an axis defined relative to participant ...
Definition types.hpp:1098
void applyConstraintPosition(const ParticipantTable &states, float substepSeconds)
Applies positional constraint to current set of bounds and physics states.
Definition types.cpp:659
Any constraint storing LagrangeCount correction multipliers.
Definition types.hpp:646
void applyLagrangeDelta(float delta, uint8_t index)
Adds a delta to a lagrange value located at index.
Definition types.hpp:1277
const std::array< float, LagrangeCount > & getLagrangeDelta() const
Gets the latest lagrange delta applied to a constraint.
Definition types.hpp:1272
const std::array< float, LagrangeCount > & getLagrange() const
Gets the current Lagrange multiplier values for this constraint.
Definition types.hpp:1267
Constraint(float compliance)
Initializes this constraint with some initial compliance value.
Definition types.hpp:678
void resetLagrange(std::integer_sequence< uint8_t, ints... > index)
Private implementation for each lagrange multiplier index in need of a reset.
std::array< float, LagrangeCount > mLagrangeDeltas
The last delta applied to the Lagrange multiplier for this constraint.
Definition types.hpp:671
void resetLagrange() override
Sets all lagrange multipliers to 0 in preparation for the next physics update.
Definition types.hpp:1283
std::array< float, LagrangeCount > mLagrangeMultipliers
The Lagrange multiplier, computed every substep since the start of the physics simulation till the cu...
Definition types.hpp:665
bool isPositiveStrict(float number)
Tests whether a number is strictly positive.
Definition util.hpp:71
bool isNumber(float number)
Tests whether a float is really a number (as opposed to a special error representation).
Definition util.hpp:92
bool isNonNegative(float number)
Tests whether a number is non-negative.
Definition util.hpp:113
float squareDistance(const glm::vec3 &vector)
Returns the square of the length of a 3 component vector.
Definition util.hpp:35
bool isFinite(float number)
Tests whether a given number is finite.
Definition util.hpp:47
bool isSensible(const glm::mat3 &matrix)
Determines whether a particular matrix is valid and finite.
Definition math.cpp:775
CollisionResponse
Defines how the object this component is attached to responds to collisions.
Definition types.hpp:190
float computeGeneralizedInverseMassPositional(const ObjectBounds &object, const PhysicsState &physics, const glm::vec3 &correctionPoint, const glm::vec3 &correctionGradient)
Computes the generalized inverse mass used for positional corrections applied by constraints.
Definition types.cpp:821
glm::mat3 computeInertiaRotationalWorld(const glm::vec3 &rotationalInertiaLocal, const glm::quat &orientation)
Returns an objects rotation tensor in the global frame given its tensor in the local frame according ...
ObjectBounds applyImpulseObject(ObjectBounds object, const PhysicsState &physics, const glm::vec3 &impulsePositional, const glm::vec3 &impulsePoint)
Returns object bounds in state it would be post application of a positional impulse.
Definition types.cpp:854
float computeGeneralizedInverseMassRotational(const ObjectBounds &object, const PhysicsState &physics, const glm::vec3 &correctionRotational)
Computes the generalized inverse mass used by constraints to apply strictly rotational corrections.
Definition types.cpp:841
Mode
Defines how the object this component is attached to responds to physics updates.
Definition types.hpp:152
PhysicsState applyImpulsePhysics(const ObjectBounds &object, PhysicsState physics, const glm::vec3 &impulsePositional, const glm::vec3 &impulsePoint)
Returns new physics state after application of global impulse.
Definition types.cpp:878
@ COLLISION_SIGNAL
Whether this object's collision events should be reported (via signal).
Definition types.hpp:201
@ COLLISION_SEPARATE
Whether this object should be separated from the object it collides with.
Definition types.hpp:195
@ MODE_KINEMATIC
Trait indicating that this object will undergo position and orientation updates according to its tran...
Definition types.hpp:168
@ MODE_STATIC
Trait indicating that this object won't undergo any physics system updates whatsoever.
Definition types.hpp:174
@ MODE_DYNAMIC
Trait indicating that this object responds to external forces, including those set externally,...
Definition types.hpp:158
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:26
Classes and structs representing data related to the engine's spatial query system (the precursor to ...
Data representing everything about a collision.
Definition types.hpp:816
A collection of values defining an orientation or position constraint for a single degree of freedom.
Definition types.hpp:1012
glm::vec3 mAxis
A non-zero vector relative to participant 0 along or about which rotation or position is constrained ...
Definition types.hpp:1018
bool isSensible(bool isRotation=false) const
Tests invariants for the constraint this block of data is associated with.
Definition types.hpp:1050
float mBoundUpper
The highest permissible angle between the pair of vectors from participant 0 and 1.
Definition types.hpp:1038
bool isActive
Whether the constraint associated with this configuration is in effect.
Definition types.hpp:1044
float mBoundLower
The lowest permissible angle or distance between the pair of vectors or points from participants 0 an...
Definition types.hpp:1028
Parameters defining a body participating in a 2-body constraint.
Definition types.hpp:1070
glm::quat mRotateToLocal
The rotation taking the parameter from constraint space to object-local space.
Definition types.hpp:1075
glm::vec3 mVector
The constrained vector representing a position for a distance constraints, and a direction for a rota...
Definition types.hpp:1082
glm::vec3 mContactNormal
Upon collision, points in the direction object B would need to move in order to be separated from obj...
Definition types.hpp:843
void updateCollisionData(const Collision &collision, const PhysicsState &physicsA, const ObjectBounds &boundsA, const ObjectBounds &boundsAPrev, const PhysicsState &physicsB, const ObjectBounds &boundsB, const ObjectBounds &boundsBPrev)
Caches collision related information shared across position and velocity corrections.
Definition types.cpp:53
bool mCollided
Whether or not two objects are currently intersecting (and therefore whether they should be separated...
Definition types.hpp:800
void applyConstraintPosition(const ParticipantTable &states, float substepSeconds) override
Separates intersecting/colliding objects and applies static friction.
Definition types.cpp:105
float mPenetration
The shortest distance an object must move along the contact normal in order to be separated from the ...
Definition types.hpp:857
glm::vec3 mRelativeB
The contact point of object B relative to its own frame.
Definition types.hpp:836
glm::vec3 mCurrentB
The contact point of object B participating in this constraint in world space.
Definition types.hpp:830
glm::vec3 mCurrentA
The contact point of object A participating in this constraint in world space.
Definition types.hpp:812
float mCollisionVelocity
The velocity of A's point of contact relative to B at the time the collision took place.
Definition types.hpp:850
void applyConstraintVelocity(const ParticipantTable &states, float substepSeconds) override
Applies dynamic friction.
Definition types.cpp:262
glm::vec3 mPreviousB
The projected last point of contact of object B participating in this constraint.
Definition types.hpp:824
glm::vec3 mRelativeA
The contact point of object A relative to its own frame.
Definition types.hpp:818
glm::vec3 mPreviousA
The projected last point of contact of object A participating in this constraint.
Definition types.hpp:806
ConstraintContact()
Initializes constraint with collision data from two potentially intersecting objects.
Definition types.cpp:51
A component defining the true bounds of a spatially queryable object situated somewhere in the world.
Definition types.hpp:845
Component representing the physics state of the body it's attached to at some particular point in tim...
Definition types.hpp:142
float mVelocityCutoff
The linear velocity below which the object's velocity is set to 0, bringing it to a stop.
Definition types.hpp:309
glm::vec3 mAngularVelocity
The angular velocity of this object.
Definition types.hpp:251
Mode getMode() const
Returns the physics type associated with this object.
Definition types.hpp:415
void applyForceGlobal(const glm::vec3 &force, const glm::vec3 &atPosition, const ObjectBounds &bounds)
Applies a force force at position atPosition, updating the torque and central force of an object whos...
Definition types.cpp:18
float mCoefficientFrictionStatic
The friction coefficient of the force that prevents relative motion between the surface of two object...
Definition types.hpp:273
bool separatesOnCollision() const
Whether this object is configured to separate from another object it collides with.
Definition types.hpp:454
static const Traits MaskCollisionResponse
Mask used to retrieve the section of an object's physics traits that indicate its collision response ...
Definition types.hpp:209
Traits mTraits
Defines a set of flags that determines how this object responds to physics updates.
Definition types.hpp:321
void setMass(float mass)
Sets the mass of this object.
Definition types.hpp:362
void setCollisionResponse(CollisionResponse response)
Sets a flag related to this object's collision response behaviour.
Definition types.hpp:475
void unsetCollisionResponse(CollisionResponse response)
Unsets a flag related to this object's collision response behaviour.
Definition types.hpp:483
void setRotationalInertia(const glm::vec3 &rotationalInertia)
Sets the rotational inertia for this object along each axis in the object's local frame.
Definition types.hpp:387
glm::vec3 mTorque
Proportional to sum of all forces acting perpendicular to the vector going from the point at which th...
Definition types.hpp:235
void applyForceLocal(const glm::vec3 &force, const glm::vec3 &atPosition, const ObjectBounds &bounds)
Applies a force force at position atPosition, updating the torque and central force of an object whos...
Definition types.cpp:10
float mVelocityCutoffAngular
The angular velocity below which the object's velocity is set to 0, bringing it to a stop.
Definition types.hpp:315
void setCoefficientRestitution(float newCoefficient)
Sets the coefficient of restitution for this object.
Definition types.hpp:403
bool signalsOnCollision() const
Whether this object is configured to report when it makes contact with another object.
Definition types.hpp:467
glm::vec3 mForce
The sum of all the forces acting on this object's center of mass, causing it to move through space.
Definition types.hpp:225
void setMode(Mode mode)
Sets the physics type associated with this object.
Definition types.hpp:433
float mMassInverse
The inverse of this object's mass.
Definition types.hpp:266
glm::vec3 mVelocity
The velocity of this object.
Definition types.hpp:243
float mCoefficientRestitution
The fraction of the net kinetic energy prior to a collision retained by a pair of objects after the c...
Definition types.hpp:289
glm::vec3 getRotationalInertia() const
Gets the rotational inertia for this object along each axis in the object's local frame.
Definition types.hpp:375
float mCoefficientFrictionDynamic
The friction coefficient of the force that hinders motion between the surface of two objects when the...
Definition types.hpp:280
glm::vec3 mRotationalInertiaInverse
The inverse of this object's resistance to rotational change.
Definition types.hpp:260
float mVelocityBleedAngular
The approximate fraction (as a number in range [0, 1]) of angular velocity lost by a rotating object ...
Definition types.hpp:303
static std::string getComponentTypeName()
Fetches the component type string associated with this class.
Definition types.hpp:216
float getMass() const
Gets the mass of this object.
Definition types.hpp:351
static const Traits MaskMode
Mask used to retrieve the section of an object's physics traits that indicate its update mode.
Definition types.hpp:182
float mVelocityBleed
The approximate fraction (as a number in range [0, 1]) of velocity lost by a moving object not experi...
Definition types.hpp:296