7#include <box3d/box3d.h>
12b3ShapeDef makeShapeDef(
float density,
float friction,
float restitution,
bool sensor) {
13 b3ShapeDef def = b3DefaultShapeDef();
14 def.density = density;
15 def.baseMaterial.friction = friction;
16 def.baseMaterial.restitution = restitution;
17 def.isSensor = sensor;
18 def.enableContactEvents = !sensor;
19 def.enableSensorEvents =
true;
27 : world_(world), body_(
body), shapeId_(shapeId), kind_(
kind), a_(
a), b_(
b), c_(
c) {}
31 b3Shape_SetUserData(shapeId_,
nullptr);
32 b3DestroyShape(shapeId_,
true);
43 if (
isValid()) b3Shape_SetUserData(shapeId_,
nullptr);
54 b3Shape_SetUserData(shapeId_,
nullptr);
55 b3DestroyShape(shapeId_,
true);
62void Shape3D::recreate(
bool sensor) {
63 if (!body_ || !body_->
isValid())
66 float density =
isValid() ? b3Shape_GetDensity(shapeId_) : 1.f;
67 float friction =
isValid() ? b3Shape_GetFriction(shapeId_) : 0.2f;
68 float restitution =
isValid() ? b3Shape_GetRestitution(shapeId_) : 0.f;
71 b3Shape_SetUserData(shapeId_,
nullptr);
72 b3DestroyShape(shapeId_,
false);
76 b3ShapeDef def = makeShapeDef(density, friction, restitution, sensor);
79 b3BoxHull box = b3MakeBoxHull(a_, b_, c_);
80 shapeId_ = b3CreateHullShape(body_->
raw(), &def, &box.base);
85 sphere.center = b3Vec3_zero;
87 shapeId_ = b3CreateSphereShape(body_->
raw(), &def, &sphere);
92 capsule.center1 = b3Vec3{0.f, -a_, 0.f};
93 capsule.center2 = b3Vec3{0.f, a_, 0.f};
95 shapeId_ = b3CreateCapsuleShape(body_->
raw(), &def, &capsule);
99 b3Shape_SetUserData(shapeId_,
this);
103 if (!body_ || !body_->
isValid())
return;
104 if (
isValid() && b3Shape_IsSensor(shapeId_) == sensor)
return;
112 b3Shape_SetFriction(shapeId_, friction);
119 b3Shape_SetRestitution(shapeId_, restitution);
123 return isValid() ? b3Shape_GetRestitution(shapeId_) : 0.f;
128 b3Shape_SetDensity(shapeId_, density,
true);
136 b3Vec3 point{
x,
y,
z};
138 proxy.points = &point;
142 b3WorldTransform wt = b3Body_GetTransform(body_->
raw());
144 xf.p = b3Vec3{
static_cast<float>(wt.p.x),
static_cast<float>(wt.p.y),
145 static_cast<float>(wt.p.z)};
150 const b3HullData *hull = b3Shape_GetHull(shapeId_);
151 if (!hull)
return false;
152 return b3OverlapHull(hull, xf, &proxy);
155 b3Sphere sphere = b3Shape_GetSphere(shapeId_);
156 return b3OverlapSphere(&sphere, xf, &proxy);
159 b3Capsule capsule = b3Shape_GetCapsule(shapeId_);
160 return b3OverlapCapsule(&capsule, xf, &proxy);
3D rigid body (Box3D) in meter-space coordinates (+Y up by convention). Owned by a World3D; create sh...
bool isValid() const
True while the underlying Box3D body is still alive.
void setDensity(float density)
void invalidate()
Internal: marks the wrapper invalid after destruction.
bool testPoint(float x, float y, float z) const
Point-in-shape test in world meters.
Shape3D(World3D *world, Body3D *body, b3ShapeId shapeId, Kind kind, float a, float b, float c)
Internal: wraps a Box3D shape (use Body3D::new*Shape).
void setRestitution(float restitution)
float getFriction() const
float getRestitution() const
void destroy()
Destroys the shape inside its world.
void setSensor(bool sensor)
Sensor shapes report contacts but never collide.
void setFriction(float friction)
Material properties.
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
void forgetShape(Shape3D *shape)