Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/PhysicsScene.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/Core/Reference.h>
8
#include <Jolt/Physics/Body/BodyCreationSettings.h>
9
#include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
10
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
11
12
JPH_NAMESPACE_BEGIN
13
14
class PhysicsSystem;
15
16
/// Contains the creation settings of a set of bodies
17
class JPH_EXPORT PhysicsScene : public RefTarget<PhysicsScene>
18
{
19
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, PhysicsScene)
20
21
public:
22
/// Add a body to the scene
23
void AddBody(const BodyCreationSettings &inBody);
24
25
/// Body constant to use to indicate that the constraint is attached to the fixed world
26
static constexpr uint32 cFixedToWorld = 0xffffffff;
27
28
/// Add a constraint to the scene
29
/// @param inConstraint Constraint settings
30
/// @param inBody1 Index in the bodies list of first body to attach constraint to
31
/// @param inBody2 Index in the bodies list of the second body to attach constraint to
32
void AddConstraint(const TwoBodyConstraintSettings *inConstraint, uint32 inBody1, uint32 inBody2);
33
34
/// Add a soft body to the scene
35
void AddSoftBody(const SoftBodyCreationSettings &inSoftBody);
36
37
/// Get number of bodies in this scene
38
size_t GetNumBodies() const { return mBodies.size(); }
39
40
/// Access to the body settings for this scene
41
const Array<BodyCreationSettings> & GetBodies() const { return mBodies; }
42
Array<BodyCreationSettings> & GetBodies() { return mBodies; }
43
44
/// A constraint and how it is connected to the bodies in the scene
45
class ConnectedConstraint
46
{
47
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, ConnectedConstraint)
48
49
public:
50
ConnectedConstraint() = default;
51
ConnectedConstraint(const TwoBodyConstraintSettings *inSettings, uint inBody1, uint inBody2) : mSettings(inSettings), mBody1(inBody1), mBody2(inBody2) { }
52
53
RefConst<TwoBodyConstraintSettings> mSettings; ///< Constraint settings
54
uint32 mBody1; ///< Index of first body (in mBodies)
55
uint32 mBody2; ///< Index of second body (in mBodies)
56
};
57
58
/// Get number of constraints in this scene
59
size_t GetNumConstraints() const { return mConstraints.size(); }
60
61
/// Access to the constraints for this scene
62
const Array<ConnectedConstraint> & GetConstraints() const { return mConstraints; }
63
Array<ConnectedConstraint> & GetConstraints() { return mConstraints; }
64
65
/// Get number of bodies in this scene
66
size_t GetNumSoftBodies() const { return mSoftBodies.size(); }
67
68
/// Access to the soft body settings for this scene
69
const Array<SoftBodyCreationSettings> & GetSoftBodies() const { return mSoftBodies; }
70
Array<SoftBodyCreationSettings> & GetSoftBodies() { return mSoftBodies; }
71
72
/// Instantiate all bodies, returns false if not all bodies could be created
73
bool CreateBodies(PhysicsSystem *inSystem) const;
74
75
/// Go through all body creation settings and fix shapes that are scaled incorrectly (note this will change the scene a bit).
76
/// @return False when not all scales could be fixed.
77
bool FixInvalidScales();
78
79
/// Saves the state of this object in binary form to inStream.
80
/// @param inStream The stream to save the state to
81
/// @param inSaveShapes If the shapes should be saved as well (these could be shared between physics scenes, in which case the calling application may want to write custom code to restore them)
82
/// @param inSaveGroupFilter If the group filter should be saved as well (these could be shared)
83
void SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const;
84
85
using PhysicsSceneResult = Result<Ref<PhysicsScene>>;
86
87
/// Restore a saved scene from inStream
88
static PhysicsSceneResult sRestoreFromBinaryState(StreamIn &inStream);
89
90
/// For debugging purposes: Construct a scene from the current state of the physics system
91
void FromPhysicsSystem(const PhysicsSystem *inSystem);
92
93
private:
94
/// The bodies that are part of this scene
95
Array<BodyCreationSettings> mBodies;
96
97
/// Constraints that are part of this scene
98
Array<ConnectedConstraint> mConstraints;
99
100
/// Soft bodies that are part of this scene
101
Array<SoftBodyCreationSettings> mSoftBodies;
102
};
103
104
JPH_NAMESPACE_END
105
106