载入中...
搜索中...
未找到
World3DContacts.cpp
浏览该文件的文档.
1// eve-hot-path
2#include "physics/Joint3D.h"
4
5#include <algorithm>
6#include <cfloat>
7#include <cmath>
8#include <utility>
9
10namespace eve::physics {
11using namespace world3d_detail;
12
13
15 beginContacts_.clear();
16 endContacts_.clear();
17 beginTriggers_.clear();
18 endTriggers_.clear();
19 hits_.clear();
20 jointStressEvents_.clear();
21 contactPoints_.clear();
22}
23
24int World3D::queryBodyContacts(int bodyId, int maxPoints) {
25 contactPoints_.clear();
26 if (bodyId <= 0) throw eve::Exception("World3D.queryBodyContacts: bodyId must be a positive stable id");
27 if (maxPoints < 1 || maxPoints > 4096)
28 throw eve::Exception("World3D.queryBodyContacts: maxPoints must be in [1, 4096]");
29 const auto *body = findBodyById(bodyId);
30 if (!body) return 0;
31 auto result = queryBodyContactsOwned(body->runtimeHandle(), maxPoints);
32 if (!result) throw eve::Exception("World3D.queryBodyContacts: %s", result.status().describe().c_str());
33 contactPoints_ = std::move(result).takeValue().points;
34 return static_cast<int>(contactPoints_.size());
35}
36
38 return rigidBackend_->queryBodyContactsOwned(body, maxPoints);
39}
40
41
42const World3D::ContactEvent &World3D::contactEventAt(const std::vector<ContactEvent> &events, int index,
43 const char *operation) const {
44 if (index < 0 || index >= static_cast<int>(events.size()))
45 throw eve::Exception("World3D.%s: index out of range", operation);
46 return events[static_cast<size_t>(index)];
47}
48
49const World3D::TriggerEvent &World3D::triggerEventAt(const std::vector<TriggerEvent> &events, int index,
50 const char *operation) const {
51 if (index < 0 || index >= static_cast<int>(events.size()))
52 throw eve::Exception("World3D.%s: index out of range", operation);
53 return events[static_cast<size_t>(index)];
54}
55
56const World3D::HitEvent &World3D::hitEventAt(int index, const char *operation) const {
57 if (index < 0 || index >= static_cast<int>(hits_.size()))
58 throw eve::Exception("World3D.%s: index out of range", operation);
59 return hits_[static_cast<size_t>(index)];
60}
61
62const World3D::JointStressEvent &World3D::jointStressEventAt(int index, const char *operation) const {
63 if (index < 0 || index >= static_cast<int>(jointStressEvents_.size()))
64 throw eve::Exception("World3D.%s: index out of range", operation);
65 return jointStressEvents_[static_cast<size_t>(index)];
66}
67
68#define EV_CONTACT_GETTER(name, events, member) \
69 int World3D::name(int index) const { return contactEventAt(events, index, #name).member; }
70
71EV_CONTACT_GETTER(getBeginContactBodyAId, beginContacts_, shapeA.bodyId)
72EV_CONTACT_GETTER(getBeginContactBodyBId, beginContacts_, shapeB.bodyId)
73EV_CONTACT_GETTER(getBeginContactShapeAId, beginContacts_, shapeA.shapeId)
74EV_CONTACT_GETTER(getBeginContactShapeBId, beginContacts_, shapeB.shapeId)
75EV_CONTACT_GETTER(getBeginContactShapeATag, beginContacts_, shapeA.shapeTag)
76EV_CONTACT_GETTER(getBeginContactShapeBTag, beginContacts_, shapeB.shapeTag)
77EV_CONTACT_GETTER(getEndContactBodyAId, endContacts_, shapeA.bodyId)
78EV_CONTACT_GETTER(getEndContactBodyBId, endContacts_, shapeB.bodyId)
79EV_CONTACT_GETTER(getEndContactShapeAId, endContacts_, shapeA.shapeId)
80EV_CONTACT_GETTER(getEndContactShapeBId, endContacts_, shapeB.shapeId)
81EV_CONTACT_GETTER(getEndContactShapeATag, endContacts_, shapeA.shapeTag)
82EV_CONTACT_GETTER(getEndContactShapeBTag, endContacts_, shapeB.shapeTag)
83
84#undef EV_CONTACT_GETTER
85
86#define EV_TRIGGER_GETTER(name, events, member) \
87 int World3D::name(int index) const { return triggerEventAt(events, index, #name).member; }
88
89EV_TRIGGER_GETTER(getBeginTriggerSensorBodyId, beginTriggers_, sensor.bodyId)
90EV_TRIGGER_GETTER(getBeginTriggerVisitorBodyId, beginTriggers_, visitor.bodyId)
91EV_TRIGGER_GETTER(getBeginTriggerSensorShapeId, beginTriggers_, sensor.shapeId)
92EV_TRIGGER_GETTER(getBeginTriggerVisitorShapeId, beginTriggers_, visitor.shapeId)
93EV_TRIGGER_GETTER(getBeginTriggerSensorShapeTag, beginTriggers_, sensor.shapeTag)
94EV_TRIGGER_GETTER(getBeginTriggerVisitorShapeTag, beginTriggers_, visitor.shapeTag)
95EV_TRIGGER_GETTER(getEndTriggerSensorBodyId, endTriggers_, sensor.bodyId)
96EV_TRIGGER_GETTER(getEndTriggerVisitorBodyId, endTriggers_, visitor.bodyId)
97EV_TRIGGER_GETTER(getEndTriggerSensorShapeId, endTriggers_, sensor.shapeId)
98EV_TRIGGER_GETTER(getEndTriggerVisitorShapeId, endTriggers_, visitor.shapeId)
99EV_TRIGGER_GETTER(getEndTriggerSensorShapeTag, endTriggers_, sensor.shapeTag)
100EV_TRIGGER_GETTER(getEndTriggerVisitorShapeTag, endTriggers_, visitor.shapeTag)
101
102#undef EV_TRIGGER_GETTER
103
104#define EV_HIT_INT_GETTER(name, member) \
105 int World3D::name(int index) const { return hitEventAt(index, #name).member; }
106
107EV_HIT_INT_GETTER(getHitBodyAId, shapeA.bodyId)
108EV_HIT_INT_GETTER(getHitBodyBId, shapeB.bodyId)
109EV_HIT_INT_GETTER(getHitShapeAId, shapeA.shapeId)
110EV_HIT_INT_GETTER(getHitShapeBId, shapeB.shapeId)
111EV_HIT_INT_GETTER(getHitShapeATag, shapeA.shapeTag)
112EV_HIT_INT_GETTER(getHitShapeBTag, shapeB.shapeTag)
113
114#undef EV_HIT_INT_GETTER
115
116#define EV_HIT_FLOAT_GETTER(name, member) \
117 float World3D::name(int index) const { return hitEventAt(index, #name).member; }
118
119EV_HIT_FLOAT_GETTER(getHitPointX, pointX)
120EV_HIT_FLOAT_GETTER(getHitPointY, pointY)
121EV_HIT_FLOAT_GETTER(getHitPointZ, pointZ)
122EV_HIT_FLOAT_GETTER(getHitNormalX, normalX)
123EV_HIT_FLOAT_GETTER(getHitNormalY, normalY)
124EV_HIT_FLOAT_GETTER(getHitNormalZ, normalZ)
125EV_HIT_FLOAT_GETTER(getHitApproachSpeed, approachSpeed)
126EV_HIT_FLOAT_GETTER(getHitNormalImpulse, normalImpulse)
127
128#undef EV_HIT_FLOAT_GETTER
129
130#define EV_JOINT_STRESS_INT_GETTER(name, member) \
131 int World3D::name(int index) const { return jointStressEventAt(index, #name).member; }
132EV_JOINT_STRESS_INT_GETTER(getJointStressJointId, jointId)
133EV_JOINT_STRESS_INT_GETTER(getJointStressBodyAId, bodyAId)
134EV_JOINT_STRESS_INT_GETTER(getJointStressBodyBId, bodyBId)
135EV_JOINT_STRESS_INT_GETTER(getJointStressKind, kind)
136#undef EV_JOINT_STRESS_INT_GETTER
137
138#define EV_JOINT_STRESS_FLOAT_GETTER(name, member) \
139 float World3D::name(int index) const { return jointStressEventAt(index, #name).member; }
140EV_JOINT_STRESS_FLOAT_GETTER(getJointStressForceX, forceX)
141EV_JOINT_STRESS_FLOAT_GETTER(getJointStressForceY, forceY)
142EV_JOINT_STRESS_FLOAT_GETTER(getJointStressForceZ, forceZ)
143EV_JOINT_STRESS_FLOAT_GETTER(getJointStressTorqueX, torqueX)
144EV_JOINT_STRESS_FLOAT_GETTER(getJointStressTorqueY, torqueY)
145EV_JOINT_STRESS_FLOAT_GETTER(getJointStressTorqueZ, torqueZ)
146#undef EV_JOINT_STRESS_FLOAT_GETTER
147
148const World3D::ContactPointResult &World3D::contactPointAt(int index, const char *operation) const {
149 if (index < 0 || index >= static_cast<int>(contactPoints_.size()))
150 throw eve::Exception("World3D.%s: index out of range", operation);
151 return contactPoints_[static_cast<size_t>(index)];
152}
153
154#define EV_CONTACT_POINT_INT_GETTER(name, member) \
155 int World3D::name(int index) const { return contactPointAt(index, #name).member; }
156
157EV_CONTACT_POINT_INT_GETTER(getContactPointShapeId, shapeId)
158EV_CONTACT_POINT_INT_GETTER(getContactPointShapeTag, shapeTag)
159EV_CONTACT_POINT_INT_GETTER(getContactPointOtherBodyId, otherBodyId)
160EV_CONTACT_POINT_INT_GETTER(getContactPointOtherShapeId, otherShapeId)
161EV_CONTACT_POINT_INT_GETTER(getContactPointOtherShapeTag, otherShapeTag)
162
163#undef EV_CONTACT_POINT_INT_GETTER
164
165#define EV_CONTACT_POINT_FLOAT_GETTER(name, member) \
166 float World3D::name(int index) const { return contactPointAt(index, #name).member; }
167
168EV_CONTACT_POINT_FLOAT_GETTER(getContactPointX, x)
169EV_CONTACT_POINT_FLOAT_GETTER(getContactPointY, y)
170EV_CONTACT_POINT_FLOAT_GETTER(getContactPointZ, z)
171EV_CONTACT_POINT_FLOAT_GETTER(getContactPointNormalX, normalX)
172EV_CONTACT_POINT_FLOAT_GETTER(getContactPointNormalY, normalY)
173EV_CONTACT_POINT_FLOAT_GETTER(getContactPointNormalZ, normalZ)
174EV_CONTACT_POINT_FLOAT_GETTER(getContactPointSeparation, separation)
175EV_CONTACT_POINT_FLOAT_GETTER(getContactPointNormalImpulse, normalImpulse)
176EV_CONTACT_POINT_FLOAT_GETTER(getContactPointTotalNormalImpulse, totalNormalImpulse)
177EV_CONTACT_POINT_FLOAT_GETTER(getContactPointNormalVelocity, normalVelocity)
178
179#undef EV_CONTACT_POINT_FLOAT_GETTER
180
182 return contactPointAt(index, "isContactPointPersisted").persisted;
183}
184
185} // namespace eve::physics
double z
double y
double x
ActionParameterOperation operation
Poco::JSON::Object::Ptr result
std::vector< particles::ParticleRecordEvent > events
std::string body
std::string kind
float separation
Definition TreeMesh.cpp:161
uint32_t index
int shapeId
#define EV_CONTACT_GETTER(name, events, member)
#define EV_HIT_FLOAT_GETTER(name, member)
#define EV_CONTACT_POINT_INT_GETTER(name, member)
#define EV_CONTACT_POINT_FLOAT_GETTER(name, member)
#define EV_TRIGGER_GETTER(name, events, member)
#define EV_HIT_INT_GETTER(name, member)
#define EV_JOINT_STRESS_INT_GETTER(name, member)
#define EV_JOINT_STRESS_FLOAT_GETTER(name, member)
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
eve::Result< BodyContacts3D > queryBodyContactsOwned(PhysicsBodyHandle body, int maxPoints) const
Reads bounded contact data without changing the legacy query cache.
int queryBodyContacts(int bodyId, int maxPoints)
Compatibility cache adapter over queryBodyContactsOwned for script callers. Results are oriented from...
Body3D * findBodyById(int bodyId) const
Borrows a live body by stable event/query id; never delete lookup results.
Definition World3D.cpp:439
void clearContactEvents()
Clears all contact and trigger buffers before the next step.
bool isContactPointPersisted(int index) const
Whether this manifold point existed in the previous step.
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Begin/end event for two non-sensor shapes.
Definition World3D.h:57