Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/TwoBodyConstraint.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/Constraints/Constraint.h>
8
#include <Jolt/Physics/Body/Body.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
class TwoBodyConstraint;
13
14
/// Base class for settings for all constraints that involve 2 bodies
15
class JPH_EXPORT TwoBodyConstraintSettings : public ConstraintSettings
16
{
17
JPH_DECLARE_SERIALIZABLE_ABSTRACT(JPH_EXPORT, TwoBodyConstraintSettings)
18
19
public:
20
/// Create an instance of this constraint
21
/// You can use Body::sFixedToWorld for inBody1 if you want to attach inBody2 to the world
22
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const = 0;
23
};
24
25
/// Base class for all constraints that involve 2 bodies. Body1 is usually considered the parent, Body2 the child.
26
class JPH_EXPORT TwoBodyConstraint : public Constraint
27
{
28
public:
29
JPH_OVERRIDE_NEW_DELETE
30
31
/// Constructor
32
TwoBodyConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings) : Constraint(inSettings), mBody1(&inBody1), mBody2(&inBody2) { }
33
34
/// Get the type of a constraint
35
virtual EConstraintType GetType() const override { return EConstraintType::TwoBodyConstraint; }
36
37
/// Solver interface
38
virtual bool IsActive() const override { return Constraint::IsActive() && (mBody1->IsActive() || mBody2->IsActive()) && (mBody2->IsDynamic() || mBody1->IsDynamic()); }
39
#ifdef JPH_DEBUG_RENDERER
40
virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const override;
41
#endif // JPH_DEBUG_RENDERER
42
43
/// Access to the connected bodies
44
Body * GetBody1() const { return mBody1; }
45
Body * GetBody2() const { return mBody2; }
46
47
/// Calculates the transform that transforms from constraint space to body 1 space. The first column of the matrix is the primary constraint axis (e.g. the hinge axis / slider direction), second column the secondary etc.
48
virtual Mat44 GetConstraintToBody1Matrix() const = 0;
49
50
/// Calculates the transform that transforms from constraint space to body 2 space. The first column of the matrix is the primary constraint axis (e.g. the hinge axis / slider direction), second column the secondary etc.
51
virtual Mat44 GetConstraintToBody2Matrix() const = 0;
52
53
/// Link bodies that are connected by this constraint in the island builder
54
virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
55
56
/// Link bodies that are connected by this constraint in the same split. Returns the split index.
57
virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override;
58
59
protected:
60
/// The two bodies involved
61
Body * mBody1;
62
Body * mBody2;
63
};
64
65
JPH_NAMESPACE_END
66
67