载入中...
搜索中...
未找到
Shape3D.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6
7#include <box3d/id.h>
8#include <box3d/types.h>
9
10#include <string>
11#include <vector>
12
13#include <cstdint>
14
15namespace eve::physics {
16
17class Body3D;
18class World3D;
19struct WorldSnapshotAccess;
20
26public:
28 enum class Kind { Box, Sphere, Capsule, ConvexHull, TriangleMesh, HeightField };
29
31 Shape3D(World3D *world, Body3D *body, b3ShapeId shapeId, PhysicsShapeHandle runtimeHandle, Kind kind, float a,
32 float b, float c, std::vector<float> hullVertices = {}, int hullMaxVertices = 64,
33 std::vector<float> meshVertices = {}, std::vector<int32_t> meshIndices = {}, b3MeshData *meshData = nullptr,
34 bool meshWeldVertices = true, float meshWeldTolerance = 0.001f, bool meshIdentifyEdges = true,
35 bool meshUseMedianSplit = false, std::vector<float> heightValues = {}, int heightCountX = 0,
36 int heightCountZ = 0, float heightCellSizeX = 1.f, float heightCellSizeZ = 1.f, float heightGlobalMin = 0.f,
37 float heightGlobalMax = 0.f, bool heightClockwise = false, b3HeightFieldData *heightData = nullptr);
38 ~Shape3D();
39
40 Shape3D(const Shape3D &) = delete;
41 Shape3D &operator=(const Shape3D &) = delete;
42
44 int getId() const { return id_; }
46 [[nodiscard]] PhysicsShapeHandle runtimeHandle() const noexcept { return runtimeHandle_; }
48 void setTag(int tag);
50 int getTag() const { return tag_; }
51
53 void setHitEventsEnabled(bool enabled);
55 bool areHitEventsEnabled() const;
56
58 std::string getKind() const;
60 void setBoxSize(float width, float height, float depth);
62 void setSphereRadius(float radius);
69 void setCapsuleSize(float height, float radius);
76 void setConvexHullVertices(const std::vector<float> &vertices, int maxVertices = 64);
79 return kind_ == Kind::ConvexHull ? static_cast<int>(hullVertices_.size() / 3) : 0;
80 }
83 return kind_ == Kind::ConvexHull ? hullMaxVertices_ : 0;
84 }
89 void setTriangleMeshData(const std::vector<float> &vertices,
90 const std::vector<int32_t> &indices, bool weldVertices = true,
91 float weldTolerance = 0.001f, bool identifyEdges = true,
92 bool useMedianSplit = false);
95 return kind_ == Kind::TriangleMesh ? static_cast<int>(meshVertices_.size() / 3) : 0;
96 }
99 return kind_ == Kind::TriangleMesh ? static_cast<int>(meshIndices_.size() / 3) : 0;
100 }
102 void setTriangleMeshMaterialIndices(const std::vector<int32_t> &materialIndices);
104 int getTriangleMeshMaterialIndex(int triangleIndex) const;
106 int getTriangleMeshMaterialCount() const;
120 void setTriangleMeshMaterial(int slot, float friction, float restitution,
121 float rollingResistance, float tangentX, float tangentY,
122 float tangentZ, int materialId,
123 const std::string &frictionMode,
124 const std::string &restitutionMode);
126 float getTriangleMeshMaterialFriction(int slot) const;
128 float getTriangleMeshMaterialRestitution(int slot) const;
130 float getTriangleMeshMaterialRollingResistance(int slot) const;
132 int getTriangleMeshMaterialId(int slot) const;
134 void setHeightFieldHeights(const std::vector<float> &heights);
136 void setHeightFieldRegion(int x, int z, int width, int depth,
137 const std::vector<float> &heights);
139 float getHeightFieldHeight(int x, int z) const;
141 int getHeightFieldCountX() const { return kind_ == Kind::HeightField ? heightCountX_ : 0; }
143 int getHeightFieldCountZ() const { return kind_ == Kind::HeightField ? heightCountZ_ : 0; }
146 return kind_ == Kind::HeightField ? heightCellSizeX_ : 0.f;
147 }
150 return kind_ == Kind::HeightField ? heightCellSizeZ_ : 0.f;
151 }
154 return kind_ == Kind::HeightField ? heightGlobalMin_ : 0.f;
155 }
158 return kind_ == Kind::HeightField ? heightGlobalMax_ : 0.f;
159 }
161 float getBoxWidth() const { return kind_ == Kind::Box ? 2.f * a_ : 0.f; }
163 float getBoxHeight() const { return kind_ == Kind::Box ? 2.f * b_ : 0.f; }
165 float getBoxDepth() const { return kind_ == Kind::Box ? 2.f * c_ : 0.f; }
167 float getRadius() const {
168 return kind_ == Kind::Sphere ? a_ : (kind_ == Kind::Capsule ? b_ : 0.f);
169 }
171 float getCapsuleHeight() const { return kind_ == Kind::Capsule ? 2.f * a_ : 0.f; }
172
182 void setOneWay(float nx, float ny, float nz, float planeOffset, float margin,
183 float minNormalDot);
185 void disableOneWay();
187 bool isOneWay() const { return oneWayEnabled_; }
188
199 void setLocalTransform(float px, float py, float pz, float qx, float qy, float qz, float qw);
201 void setLocalPosition(float x, float y, float z);
203 void setLocalRotation(float qx, float qy, float qz, float qw);
205 float getLocalX() const { return localX_; }
207 float getLocalY() const { return localY_; }
209 float getLocalZ() const { return localZ_; }
211 float getLocalRotX() const { return localQx_; }
213 float getLocalRotY() const { return localQy_; }
215 float getLocalRotZ() const { return localQz_; }
217 float getLocalRotW() const { return localQw_; }
218
220 void setSensor(bool sensor);
221 bool isSensor() const;
222
224 void setFriction(float friction);
225 float getFriction() const;
226
227 void setRestitution(float restitution);
228 float getRestitution() const;
229
231 void setRollingResistance(float resistance);
233 float getRollingResistance() const;
235 void setTangentVelocity(float x, float y, float z);
237 float getTangentVelocityX() const;
239 float getTangentVelocityY() const;
241 float getTangentVelocityZ() const;
243 void setMaterialId(int id);
245 int getMaterialId() const;
247 void setExplosionScale(float scale);
249 float getExplosionScale() const { return explosionScale_; }
251 void setFrictionCombineMode(const std::string &mode);
253 std::string getFrictionCombineMode() const;
255 void setRestitutionCombineMode(const std::string &mode);
257 std::string getRestitutionCombineMode() const;
258
259 void setDensity(float density);
260 float getDensity() const;
261
264 void setFilterBits(uint64_t categoryBits, uint64_t maskBits);
265 uint64_t getCategoryBits() const;
267 void setCategoryBits(uint64_t bits);
269 void setMaskBits(uint64_t bits);
271 uint64_t getMaskBits() const;
273 void setGroupIndex(int index);
274 int getGroupIndex() const;
275
284 Body3D *getBody() { return body_; }
293 const Body3D *getBody() const { return body_; }
294
296 bool testPoint(float x, float y, float z) const;
297
299 void destroy();
300
302 b3ShapeId raw() const { return shapeId_; }
303 bool isValid() const;
304
306 void invalidate();
307
308private:
309 friend class World3D;
310 friend class Body3D;
311 friend struct WorldSnapshotAccess;
312
313 void recreate(bool sensor);
314 void enableContactOverrideFiltering();
315 void refreshOneWayWorldData();
316 bool allowsOneWayContact(double pointX, double pointY, double pointZ, float normalX,
317 float normalY, float normalZ) const;
318
319 World3D *world_ = nullptr;
320 Body3D *body_ = nullptr;
321 b3ShapeId shapeId_{};
322 PhysicsShapeHandle runtimeHandle_ = PhysicsShapeHandle::invalid();
323 Kind kind_ = Kind::Box;
324 float a_ = 0.f; // box hx | sphere r | capsule half-height
325 float b_ = 0.f; // box hy | unused | capsule radius
326 float c_ = 0.f; // box hz
327 std::vector<float> hullVertices_;
328 int hullMaxVertices_ = 64;
329 std::vector<float> meshVertices_;
330 std::vector<int32_t> meshIndices_;
331 b3MeshData *meshData_ = nullptr;
332 bool meshWeldVertices_ = true;
333 float meshWeldTolerance_ = 0.001f;
334 bool meshIdentifyEdges_ = true;
335 bool meshUseMedianSplit_ = false;
336 std::vector<uint8_t> meshMaterialIndices_;
337 std::vector<b3SurfaceMaterial> meshMaterials_;
338 bool meshMaterialsDirty_ = false;
339 bool contactOverrideFiltering_ = false;
340 float explosionScale_ = 1.f;
341 std::vector<float> heightValues_;
342 int heightCountX_ = 0, heightCountZ_ = 0;
343 float heightCellSizeX_ = 1.f, heightCellSizeZ_ = 1.f;
344 float heightGlobalMin_ = 0.f, heightGlobalMax_ = 0.f;
345 bool heightClockwise_ = false;
346 b3HeightFieldData *heightData_ = nullptr;
347 int id_ = 0;
348 int tag_ = 0;
349 bool hitEventsEnabled_ = false;
350 float localX_ = 0.f, localY_ = 0.f, localZ_ = 0.f;
351 float localQx_ = 0.f, localQy_ = 0.f, localQz_ = 0.f, localQw_ = 1.f;
352 bool oneWayEnabled_ = false;
353 float oneWayLocalNormalX_ = 0.f, oneWayLocalNormalY_ = 1.f,
354 oneWayLocalNormalZ_ = 0.f;
355 float oneWayPlaneOffset_ = 0.f;
356 float oneWayMargin_ = 0.05f;
357 float oneWayMinNormalDot_ = 0.1f;
358 double oneWayWorldPointX_ = 0.0, oneWayWorldPointY_ = 0.0, oneWayWorldPointZ_ = 0.0;
359 float oneWayWorldNormalX_ = 0.f, oneWayWorldNormalY_ = 1.f,
360 oneWayWorldNormalZ_ = 0.f;
361};
362
363} // namespace eve::physics
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float py
float nx
float nz
float ny
float pz
#define EVENGINE_API_WORLD
Definition Export.h:109
std::vector< std::uint32_t > indices
std::int32_t c
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
void(* destroy)(void *)
TokenKind kind
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * tag
World3D * world
Process-local, generation-qualified handles owned by the physics domain.
std::vector< Point > vertices
float radius
double restitution
int margin
std::string body
uint32_t index
std::uint32_t depth
float qy
float qx
float qw
float qz
3D rigid body (Box3D) in meter-space coordinates (+Y up by convention). Owned by a World3D; create pr...
Definition Body3D.h:24
3D shape (box/sphere/capsule) attached to a Body3D with material settings. Created via Body3D::new*Sh...
Definition Shape3D.h:25
const Body3D * getBody() const
Returns the owning body for read-only link construction.
Definition Shape3D.h:293
int getId() const
Stable world-local id, preserved when sensor state recreates the backend shape.
Definition Shape3D.h:44
float getHeightFieldCellSizeZ() const
Height-field cell spacing along local Z, or zero.
Definition Shape3D.h:149
PhysicsShapeHandle runtimeHandle() const noexcept
Process-local generation-qualified handle owned by World3D.
Definition Shape3D.h:46
int getHeightFieldCountZ() const
Height-field sample count along local Z, or zero.
Definition Shape3D.h:143
float getLocalZ() const
Local translation Z in meters.
Definition Shape3D.h:209
int getTriangleMeshVertexCount() const
Source vertex count for a triangle mesh, or zero.
Definition Shape3D.h:94
float getLocalRotX() const
Normalized local quaternion X.
Definition Shape3D.h:211
int getTag() const
User-defined integer tag, zero by default.
Definition Shape3D.h:50
Kind
Shape geometry kind.
Definition Shape3D.h:28
float getLocalY() const
Local translation Y in meters.
Definition Shape3D.h:207
float getBoxHeight() const
Full box height, or zero for another geometry kind.
Definition Shape3D.h:163
float getLocalX() const
Local translation X in meters.
Definition Shape3D.h:205
bool isOneWay() const
Whether one-way pre-solve filtering is requested.
Definition Shape3D.h:187
Shape3D & operator=(const Shape3D &)=delete
float getBoxWidth() const
Full box width, or zero for another geometry kind.
Definition Shape3D.h:161
float getHeightFieldGlobalMin() const
Height-field shared quantization minimum, or zero.
Definition Shape3D.h:153
float getLocalRotW() const
Normalized local quaternion scalar component.
Definition Shape3D.h:217
Shape3D(const Shape3D &)=delete
float getExplosionScale() const
Current explosion impulse multiplier; zero opts this shape out.
Definition Shape3D.h:249
float getCapsuleHeight() const
Capsule hemisphere-center distance, or zero for another kind.
Definition Shape3D.h:171
float getBoxDepth() const
Full box depth, or zero for another geometry kind.
Definition Shape3D.h:165
int getHeightFieldCountX() const
Height-field sample count along local X, or zero.
Definition Shape3D.h:141
float getHeightFieldCellSizeX() const
Height-field cell spacing along local X, or zero.
Definition Shape3D.h:145
float getLocalRotZ() const
Normalized local quaternion Z.
Definition Shape3D.h:215
float getHeightFieldGlobalMax() const
Height-field shared quantization maximum, or zero.
Definition Shape3D.h:157
int getTriangleMeshTriangleCount() const
Source triangle count for a triangle mesh, or zero.
Definition Shape3D.h:98
Body3D * getBody()
Returns the owning body, or null after invalidation.
Definition Shape3D.h:284
b3ShapeId raw() const
Raw Box3D shape id / liveness.
Definition Shape3D.h:302
int getConvexHullPointCount() const
Source point count for a convex hull, or zero for another kind.
Definition Shape3D.h:78
float getLocalRotY() const
Normalized local quaternion Y.
Definition Shape3D.h:213
float getRadius() const
Sphere/capsule radius, or zero for a box.
Definition Shape3D.h:167
int getConvexHullMaxVertices() const
Current convex-hull simplification budget, or zero for another kind.
Definition Shape3D.h:82
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:63
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
bool enabled