8#include <Box2D/Box2D.h>
17b2BodyType parseBodyType(
const std::string &
type) {
18 if (
type ==
"static")
return b2_staticBody;
19 if (
type ==
"kinematic")
return b2_kinematicBody;
20 if (
type ==
"dynamic")
return b2_dynamicBody;
24const char *bodyTypeName(b2BodyType
t) {
26 case b2_staticBody:
return "static";
27 case b2_kinematicBody:
return "kinematic";
28 case b2_dynamicBody:
return "dynamic";
29 default:
return "static";
36 : world_(world), body_(
body), id_(
id), runtimeHandle_(runtimeHandle) {}
39 if (body_ && world_ && world_->
raw()) {
40 std::vector<Joint2D *> joints(world_->joints_.begin(), world_->joints_.end());
42 if (joint && joint->isValid() && (joint->getBodyAId() ==
getId() || joint->getBodyBId() ==
getId()))
46 b2Fixture *
f = body_->GetFixtureList();
48 b2Fixture *next =
f->GetNext();
49 auto *wrap =
static_cast<Fixture *
>(
f->GetUserData());
56 body_->SetUserData(
nullptr);
57 world_->releaseBodyJoints2D(*
this);
58 world_->
raw()->DestroyBody(body_);
67 if (body_) body_->SetUserData(
nullptr);
74 if (!body_ || !world_ || !world_->
raw()) {
78 std::vector<Joint2D *> joints(world_->joints_.begin(), world_->joints_.end());
80 if (joint && joint->isValid() && (joint->getBodyAId() ==
getId() || joint->getBodyBId() ==
getId()))
83 b2Fixture *
f = body_->GetFixtureList();
85 b2Fixture *next =
f->GetNext();
86 auto *wrap =
static_cast<Fixture *
>(
f->GetUserData());
93 body_->SetUserData(
nullptr);
94 world_->releaseBodyJoints2D(*
this);
95 world_->
raw()->DestroyBody(body_);
103 if (!body_ || !world_)
return;
104 body_->SetTransform(b2Vec2(world_->
toMeters(
x), world_->
toMeters(
y)), body_->GetAngle());
108 if (!body_ || !world_)
return 0.f;
109 return world_->
toPixels(body_->GetPosition().x);
113 if (!body_ || !world_)
return 0.f;
114 return world_->
toPixels(body_->GetPosition().y);
119 body_->SetTransform(body_->GetPosition(), radians);
123 if (!body_)
return 0.f;
124 return body_->GetAngle();
128 if (!body_ || !world_)
return;
133 if (!body_ || !world_)
return 0.f;
134 return world_->
toPixels(body_->GetLinearVelocity().x);
138 if (!body_ || !world_)
return 0.f;
139 return world_->
toPixels(body_->GetLinearVelocity().y);
143 if (!body_ || !world_)
return 0.f;
144 return world_->
toPixels(body_->GetLinearVelocity().Length());
150 if (!body_ || !world_)
return 0.f;
151 return world_->
toPixels(body_->GetWorldCenter().x);
155 if (!body_ || !world_)
return 0.f;
156 return world_->
toPixels(body_->GetWorldCenter().y);
161 body_->SetAngularVelocity(omega);
165 if (!body_)
return 0.f;
166 return body_->GetAngularVelocity();
170 if (!body_ || !world_)
return;
172 body_->ApplyForceToCenter(b2Vec2(world_->
toMeters(fx), world_->
toMeters(fy)),
true);
176 if (!body_ || !world_)
return;
182 if (!body_ || !world_)
return;
183 body_->ApplyLinearImpulse(b2Vec2(world_->
toMeters(ix), world_->
toMeters(iy)),
184 body_->GetWorldCenter(),
true);
189 body_->ApplyAngularImpulse(
impulse,
true);
194 body_->SetType(parseBodyType(bodyType));
198 if (!body_)
return "static";
199 return bodyTypeName(body_->GetType());
204 body_->SetFixedRotation(fixed);
225 body_->SetAwake(
awake);
237 if (!body_ || !world_)
throw eve::Exception(
"Body.newRectangleFixture: body destroyed");
239 throw eve::Exception(
"Body.newRectangleFixture: width/height must be > 0");
241 b2PolygonShape
shape;
251 b2Fixture *
raw = body_->CreateFixture(&def);
253 raw->SetUserData(fx);
254 world_->fixtures_.insert(fx);
259 if (!body_ || !world_)
throw eve::Exception(
"Body.newCircleFixture: body destroyed");
271 b2Fixture *
raw = body_->CreateFixture(&def);
273 raw->SetUserData(fx);
274 world_->fixtures_.insert(fx);
280 if (!body_ || !world_)
throw eve::Exception(
"Body.newPolygonFixture: body destroyed");
282 throw eve::Exception(
"Body.newPolygonFixture: expected 3..8 packed XY vertices");
283 std::vector<b2Vec2>
points;
287 throw eve::Exception(
"Body.newPolygonFixture: vertices must be finite");
290 b2PolygonShape
shape;
292 if (
shape.GetVertexCount() < 3)
293 throw eve::Exception(
"Body.newPolygonFixture: vertices do not form a convex polygon");
299 b2Fixture *
raw = body_->CreateFixture(&def);
300 if (!
raw)
throw eve::Exception(
"Body.newPolygonFixture: Box2D rejected the fixture");
301 auto *fixture =
new Fixture(world_,
this,
raw);
302 raw->SetUserData(fixture);
303 world_->fixtures_.insert(fixture);
309 if (!body_ || !world_)
throw eve::Exception(
"Body.newChainFixture: body destroyed");
312 throw eve::Exception(
"Body.newChainFixture: invalid packed XY vertex count");
313 std::vector<b2Vec2>
points;
317 throw eve::Exception(
"Body.newChainFixture: vertices must be finite");
320 throw eve::Exception(
"Body.newChainFixture: consecutive vertices must be distinct");
324 throw eve::Exception(
"Body.newChainFixture: loop endpoint is implicit and must not be repeated");
334 b2Fixture *
raw = body_->CreateFixture(&def);
335 if (!
raw)
throw eve::Exception(
"Body.newChainFixture: Box2D rejected the fixture");
336 auto *fixture =
new Fixture(world_,
this,
raw);
337 raw->SetUserData(fixture);
338 world_->fixtures_.insert(fixture);
std::shared_ptr< const std::vector< glm::vec2 > > points
std::array< double, 2 > vertices[3]
std::array< double, 3 > impulse
EVENGINE_API_FOUNDATION public API.
static constexpr RuntimeHandle invalid() noexcept
Returns the canonical invalid handle.
Fixture * newRectangleFixture(float width, float height, float density=1.f, float friction=0.2f, float restitution=0.f)
Creates a rectangle fixture in pixel-space units.
float getLinearVelocityX() const
Returns the linear velocity x.
void applyForceAt(float fx, float fy, float x, float y)
Force applied at a world pixel position.
void setBullet(bool bullet)
CCD bullet mode (recommended for fast small bodies).
float getX() const
Returns the x.
void setAwake(bool awake)
Wakes / sleeps the body manually.
void setType(const std::string &bodyType)
"static" | "kinematic" | "dynamic".
void setAngle(float radians)
Rotation in radians.
float getLinearVelocityY() const
Returns the linear velocity y.
bool isActive() const
True when active.
void applyLinearImpulse(float ix, float iy)
Instantaneous linear impulse.
Fixture * newPolygonFixture(const std::vector< float > &vertices, float density=1.f, float friction=0.2f, float restitution=0.f)
Create a bounded convex polygon fixture from packed local pixel-space XY vertices.
Fixture * newCircleFixture(float radius, float density=1.f, float friction=0.2f, float restitution=0.f)
Creates a circular fixture in pixel-space units.
void setFixedRotation(bool fixed)
Locks rotation so the body cannot spin.
Fixture * newChainFixture(const std::vector< float > &vertices, bool loop=false, float friction=0.2f, float restitution=0.f)
Create an open chain or closed loop from packed local pixel-space XY vertices.
void applyForce(float fx, float fy)
Force (pixels/s² * kg) applied at the center of mass.
bool isAwake() const
True when awake.
bool isFixedRotation() const
True when fixed rotation.
bool isBullet() const
True when bullet.
void setAngularVelocity(float omega)
Angular velocity in radians/s.
b2Body * raw()
Exposes the underlying Box2D body for tightly-scoped backend integration.
float getAngularVelocity() const
Returns the angular velocity.
void destroy()
Destroys the body inside its world.
void applyAngularImpulse(float impulse)
Instantaneous angular impulse.
float getWorldCenterY() const
Returns the world center y.
float getLinearSpeed() const
Magnitude of the linear velocity.
float getAngle() const
Returns the angle.
std::string getType() const
Returns the type.
int getId() const
Stable id used by contact/impact events.
void invalidate()
Internal: marks the wrapper invalid after world destruction.
void setPosition(float x, float y)
Position in pixels.
float getWorldCenterX() const
World center of mass in pixels.
Body(World *world, b2Body *body, int id, PhysicsBodyHandle runtimeHandle)
Internal: wraps a Box2D body (use World::newBody).
Fixture * newRectangleFixtureAt(float width, float height, float offsetX, float offsetY, float density=1.f, float friction=0.2f, float restitution=0.f)
Creates an offset rectangle fixture in pixel-space units.
float getMass() const
Mass in kilograms (Box2D units).
void setActive(bool active)
Disables/enables the body and its fixtures.
void setLinearVelocity(float vx, float vy)
Linear velocity in pixels/s.
float getY() const
Returns the y.
2D fixture: a shape attached to a Body with material + filter settings. Also carries a string tag use...
Owning planar joint wrapper observing World-owned backend state. @ownership Caller/VM owns factory wr...
Box2D world wrapper (2D physics) with pixel-space coordinates. Handles stepping, gravity,...
void forgetBody(Body *body)
void forgetFixture(Fixture *fixture)
float toPixels(float meters) const
Converts a meter-space length to pixels.
b2World * raw()
Exposes the underlying Box2D world for tightly-scoped backend integration.
float toMeters(float pixels) const
Converts a pixel-space length to meters.
Optional physics backend for vehicle mobility and body attach.