12using namespace world3d_detail;
14World3D::EventShape WorldBackend::do_eventShapeFrom(b3ShapeId
shapeId)
const {
15 auto found = owner_->shapeRecords_.find(b3StoreShapeId(
shapeId));
16 if (
found != owner_->shapeRecords_.end())
return found->second;
24 if (maxPoints < 1 || maxPoints > 4096)
34 constexpr int maxContactPairs = 4096;
36 const int capacity = std::min(availablePairs, maxContactPairs + 1);
38 std::vector<b3ContactData> contacts(
static_cast<size_t>(
capacity));
41 points.reserve(
static_cast<size_t>(std::min(maxPoints, contactCount * 2)));
43 for (
int contactIndex = 0; contactIndex < std::min(contactCount, maxContactPairs); ++contactIndex) {
44 const b3ContactData &
contact = contacts[
static_cast<size_t>(contactIndex)];
49 if (!shapeA || !shapeB || !bodyA || !bodyB) {
53 const bool targetIsA = bodyA ==
target;
54 if (!targetIsA && bodyB !=
target)
continue;
55 Shape3D *ownShape = targetIsA ? shapeA : shapeB;
56 Shape3D *otherShape = targetIsA ? shapeB : shapeA;
57 Body3D *otherBody = targetIsA ? bodyB : bodyA;
60 for (
int manifoldIndex = 0; manifoldIndex <
contact.manifoldCount; ++manifoldIndex) {
61 const b3Manifold &manifold =
contact.manifolds[manifoldIndex];
62 const b3Vec3
normal = targetIsA ? manifold.normal : -manifold.normal;
63 for (
int pointIndex = 0; pointIndex < manifold.pointCount; ++pointIndex) {
64 if (
static_cast<int>(
points.size()) >= maxContactPairs) {
68 const b3ManifoldPoint &
point = manifold.points[pointIndex];
69 const b3Pos worldPoint = b3OffsetPos(centerOfMass, targetIsA ?
point.anchorA :
point.
anchorB);
70 points.push_back({ownShape->getId(), ownShape->getTag(), otherBody->getId(), otherShape->getId(),
71 otherShape->getTag(),
point.featureId,
static_cast<float>(worldPoint.x),
72 static_cast<float>(worldPoint.y),
static_cast<float>(worldPoint.z),
normal.x,
79 std::sort(
points.begin(),
points.end(), [](
const ContactPointResult &
a,
const ContactPointResult &
b) {
80 if (a.shapeId != b.shapeId) return a.shapeId < b.shapeId;
81 if (a.otherShapeId != b.otherShapeId) return a.otherShapeId < b.otherShapeId;
82 if (a.featureId != b.featureId) return a.featureId < b.featureId;
83 if (a.x != b.x) return a.x < b.x;
84 if (a.y != b.y) return a.y < b.y;
87 if (
static_cast<int>(
points.size()) > maxPoints) {
88 points.resize(
static_cast<size_t>(maxPoints));
95 return do_queryBodyContactsOwned(
body, maxPoints);
104 b3ContactEvents contacts = b3World_GetContactEvents(worldId_);
105 for (
int i = 0; i < contacts.beginCount; ++i) {
106 EventShape a = do_eventShapeFrom(contacts.beginEvents[i].shapeIdA);
107 EventShape b = do_eventShapeFrom(contacts.beginEvents[i].shapeIdB);
108 if (
a.shapeId == 0 ||
b.shapeId == 0)
continue;
109 if (
a.shapeId >
b.shapeId) std::swap(
a,
b);
110 owner_->beginContacts_.push_back({
a,
b});
112 for (
int i = 0; i < contacts.endCount; ++i) {
113 EventShape a = do_eventShapeFrom(contacts.endEvents[i].shapeIdA);
114 EventShape b = do_eventShapeFrom(contacts.endEvents[i].shapeIdB);
115 if (
a.shapeId == 0 ||
b.shapeId == 0)
continue;
116 if (
a.shapeId >
b.shapeId) std::swap(
a,
b);
117 owner_->endContacts_.push_back({
a,
b});
120 for (
int i = 0; i < contacts.hitCount; ++i) {
121 const b3ContactHitEvent &
source = contacts.hitEvents[i];
124 if (
a.shapeId == 0 ||
b.shapeId == 0)
continue;
126 float normalImpulse = 0.f;
127 if (b3Contact_IsValid(
source.contactId)) {
128 const b3ContactData data = b3Contact_GetData(
source.contactId);
129 for (
int manifoldIndex = 0; manifoldIndex < data.manifoldCount; ++manifoldIndex) {
130 const b3Manifold &manifold = data.manifolds[manifoldIndex];
131 for (
int pointIndex = 0; pointIndex < manifold.pointCount; ++pointIndex)
132 normalImpulse += manifold.points[pointIndex].totalNormalImpulse;
137 if (
a.shapeId >
b.shapeId) {
141 owner_->hits_.push_back({
a,
b,
static_cast<float>(
source.point.x),
static_cast<float>(
source.point.y),
146 b3SensorEvents sensors = b3World_GetSensorEvents(worldId_);
147 for (
int i = 0; i < sensors.beginCount; ++i) {
148 EventShape sensor = do_eventShapeFrom(sensors.beginEvents[i].sensorShapeId);
149 EventShape visitor = do_eventShapeFrom(sensors.beginEvents[i].visitorShapeId);
151 owner_->beginTriggers_.push_back({sensor, visitor});
153 for (
int i = 0; i < sensors.endCount; ++i) {
154 EventShape sensor = do_eventShapeFrom(sensors.endEvents[i].sensorShapeId);
155 EventShape visitor = do_eventShapeFrom(sensors.endEvents[i].visitorShapeId);
157 owner_->endTriggers_.push_back({sensor, visitor});
160 const b3JointEvents jointEvents = b3World_GetJointEvents(worldId_);
161 for (
int i = 0; i < jointEvents.count; ++i) {
162 const b3JointEvent &
source = jointEvents.jointEvents[i];
164 if (!joint || owner_->joints_.find(joint) == owner_->joints_.end() || !joint->isValid() ||
165 !b3Joint_IsValid(
source.jointId) || b3Joint_GetUserData(
source.jointId) != joint)
167 const b3Vec3
force = b3Joint_GetConstraintForce(
source.jointId);
168 const b3Vec3
torque = b3Joint_GetConstraintTorque(
source.jointId);
169 owner_->jointStressEvents_.push_back({joint->getId(), joint->getBodyAId(), joint->getBodyBId(),
176 for (
auto it = owner_->shapeRecords_.begin(); it != owner_->shapeRecords_.end();) {
177 if (!b3Shape_IsValid(b3LoadShapeId(it->first)))
178 it = owner_->shapeRecords_.erase(it);
182 for (
auto it = owner_->shapeRawHandles_.begin(); it != owner_->shapeRawHandles_.end();) {
183 if (!b3Shape_IsValid(b3LoadShapeId(it->first)))
184 it = owner_->shapeRawHandles_.erase(it);
188 for (
auto it = owner_->shapeHandles_.begin(); it != owner_->shapeHandles_.end();) {
189 if (!it->second || !it->second->isValid())
190 it = owner_->shapeHandles_.erase(it);
std::shared_ptr< const std::vector< glm::vec2 > > points
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
Script-facing joint owned by a World3D. Legacy scalar/void methods adapt the checked backend contract...
Body3D * findBody(PhysicsBodyHandle handle) const
Borrows a live body; null for stale/foreign handles. Never delete lookup results.
bool isValid() const noexcept override
Native world liveness.
eve::Result< BodyContacts3D > queryBodyContactsOwned(PhysicsBodyHandle body, int maxPoints) const override
queryBodyContactsOwned; uses the class ownership, thread and failure contract.
eve::Result< void > collectEvents() override
Collects solver events into the owner's queues without publishing callbacks.
Body3D * bodyFromShape(b3ShapeId shapeId)
Resolves a native Box3D user-data observer for an immediate internal call.
Shape3D * shapeFromShape(b3ShapeId shapeId)
Resolves a native Box3D user-data observer for an immediate internal call.
eve::RuntimeHandle< PhysicsBodyHandleTag > PhysicsBodyHandle
Generation-qualified runtime identity for a physics body.
Stable snapshot describing one side of a 3D collision event.
static b3BodyId bodyId(const Body3D &body)
Borrows the native identifier synchronously; facade must belong to a Box3D world.