2020-10-03 22:55:14 +02:00
|
|
|
#ifndef RBDLSIM_RBDLSIM_H
|
|
|
|
#define RBDLSIM_RBDLSIM_H
|
|
|
|
|
|
|
|
#include <rbdl/Model.h>
|
|
|
|
#include <rbdl/rbdl_math.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace RBDLSim {
|
|
|
|
|
|
|
|
using namespace RigidBodyDynamics;
|
|
|
|
using namespace RigidBodyDynamics::Math;
|
|
|
|
|
2020-10-04 17:28:02 +02:00
|
|
|
struct SimShape;
|
|
|
|
struct SimBody;
|
|
|
|
struct CollisionInfo;
|
|
|
|
|
2020-11-20 14:17:15 +01:00
|
|
|
const double cCollisionEps = 1.0e-5;
|
2020-10-05 21:47:25 +02:00
|
|
|
|
2020-10-03 22:55:14 +02:00
|
|
|
struct SimShape {
|
2020-10-06 20:22:27 +02:00
|
|
|
enum ShapeType { Box = 0, Sphere = 1, Plane = 2 };
|
2020-10-03 22:55:14 +02:00
|
|
|
ShapeType mType;
|
|
|
|
Vector3d pos;
|
|
|
|
Quaternion orientation;
|
|
|
|
Vector3d scale;
|
2020-11-05 23:12:05 +01:00
|
|
|
double restitution = 1.0;
|
2020-10-03 22:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SimBody {
|
|
|
|
VectorNd q, qdot, qddot, tau;
|
|
|
|
Model mModel;
|
2020-10-09 14:01:36 +02:00
|
|
|
bool mIsStatic = false;
|
2020-10-06 20:22:27 +02:00
|
|
|
|
2020-10-03 22:55:14 +02:00
|
|
|
typedef std::pair<unsigned int, SimShape> BodyCollisionInfo;
|
|
|
|
std::vector<BodyCollisionInfo> mCollisionShapes;
|
|
|
|
|
|
|
|
void step(double ts);
|
2020-10-06 20:22:27 +02:00
|
|
|
void resolveCollisions(double dt, std::vector<CollisionInfo>& collisions);
|
|
|
|
void calcNextPositions(double dt, const VectorNd& in_qdot, VectorNd& out_q);
|
2020-10-03 22:55:14 +02:00
|
|
|
void updateCollisionShapes();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CollisionInfo {
|
2020-11-05 23:12:05 +01:00
|
|
|
SimBody* mBodyA = nullptr;
|
|
|
|
SimBody* mBodyB = nullptr;
|
2020-10-09 14:01:36 +02:00
|
|
|
const SimShape* mShapeA = nullptr;
|
|
|
|
const SimShape* mShapeB = nullptr;
|
2020-10-04 17:28:02 +02:00
|
|
|
int mBodyAIndex;
|
|
|
|
int mBodyBIndex;
|
2020-11-20 23:16:37 +01:00
|
|
|
Vector3d mManifoldPoints[8];
|
2020-11-20 14:17:15 +01:00
|
|
|
int mNumManifoldPoints = 0;
|
2020-10-09 14:01:36 +02:00
|
|
|
Vector3d posA = Vector3d::Zero();
|
|
|
|
Vector3d posB = Vector3d::Zero();
|
2020-11-15 21:47:17 +01:00
|
|
|
double biasVelocityA = 0.;
|
|
|
|
double biasVelocityB = 0.;
|
2020-11-13 01:23:13 +01:00
|
|
|
double accumImpulse = 0.;
|
|
|
|
double deltaImpulse = 0.;
|
2020-10-09 14:01:36 +02:00
|
|
|
Vector3d dir = Vector3d::Zero();
|
|
|
|
VectorNd jacA = VectorNd::Zero(1);
|
|
|
|
VectorNd jacB = VectorNd::Zero(1);
|
|
|
|
MatrixNd MInvA = MatrixNd::Zero(1, 1);
|
|
|
|
MatrixNd MInvB = MatrixNd::Zero(1, 1);
|
|
|
|
double GMInvGTA = 0.;
|
|
|
|
double GMInvGTB = 0.;
|
2020-11-05 23:12:05 +01:00
|
|
|
double effectiveRestitution = 1.0;
|
2020-10-09 14:01:36 +02:00
|
|
|
|
|
|
|
double depth = 0.;
|
2020-10-03 22:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct World {
|
|
|
|
double mSimTime = 0.;
|
|
|
|
std::vector<SimBody> mBodies;
|
|
|
|
std::vector<SimShape> mStaticShapes;
|
|
|
|
std::vector<CollisionInfo> mContactPoints;
|
|
|
|
|
2020-11-05 23:12:05 +01:00
|
|
|
void calcUnconstrainedVelUpdate(double dt);
|
2020-10-03 22:55:14 +02:00
|
|
|
void updateCollisionShapes();
|
|
|
|
void detectCollisions();
|
2020-10-04 17:28:02 +02:00
|
|
|
void resolveCollisions(double dt);
|
|
|
|
bool integrateWorld(double dt);
|
2020-10-03 22:55:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool CheckPenetration(
|
|
|
|
const SimShape& shape_a,
|
|
|
|
const SimShape& shape_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
2020-10-06 20:22:27 +02:00
|
|
|
bool CheckPenetrationSphereVsSphere(
|
|
|
|
const SimShape& shape_a,
|
|
|
|
const SimShape& shape_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
2020-10-05 21:47:25 +02:00
|
|
|
bool CheckPenetrationSphereVsPlane(
|
|
|
|
const SimShape& shape_a,
|
|
|
|
const SimShape& shape_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
2020-11-20 14:17:15 +01:00
|
|
|
bool CheckPenetrationBoxVsPlane (
|
|
|
|
const SimShape& shape_a,
|
|
|
|
const SimShape& shape_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
|
|
|
bool CheckPenetrationAABBVsPlane(
|
|
|
|
const SimShape& shape_a,
|
|
|
|
const SimShape& shape_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
2020-10-03 22:55:14 +02:00
|
|
|
SimBody CreateSphereBody(
|
|
|
|
double mass,
|
|
|
|
double radius,
|
2020-11-08 12:49:43 +01:00
|
|
|
double restitution,
|
2020-10-03 22:55:14 +02:00
|
|
|
const Vector3d& pos,
|
|
|
|
const Vector3d& vel);
|
|
|
|
|
|
|
|
SimBody CreateBoxBody(
|
|
|
|
double mass,
|
|
|
|
const Vector3d& size,
|
2020-11-20 22:53:30 +01:00
|
|
|
double restitution,
|
2020-10-03 22:55:14 +02:00
|
|
|
const Vector3d& pos,
|
|
|
|
const Vector3d& vel);
|
|
|
|
|
2020-10-09 14:01:36 +02:00
|
|
|
void PrepareConstraintImpulse(
|
|
|
|
SimBody* body_a,
|
|
|
|
SimBody* body_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
void CalcCollisions(
|
2020-11-08 12:49:43 +01:00
|
|
|
SimBody& body_a,
|
|
|
|
SimBody& body_b,
|
2020-10-09 14:01:36 +02:00
|
|
|
std::vector<CollisionInfo>& collisions);
|
|
|
|
void CalcConstraintImpulse(
|
|
|
|
SimBody* body_a,
|
|
|
|
SimBody* body_b,
|
|
|
|
CollisionInfo& cinfo,
|
|
|
|
const double dt);
|
|
|
|
void ApplyConstraintImpulse(
|
|
|
|
SimBody* body_a,
|
|
|
|
SimBody* body_b,
|
|
|
|
CollisionInfo& cinfo);
|
|
|
|
|
2020-10-03 22:55:14 +02:00
|
|
|
} // namespace RBDLSim
|
|
|
|
|
|
|
|
#endif //RBDLSIM_RBDLSIM_H
|