13template<
class TileData>
19enum class BodyType : uint8_t {
31 template<
class TileData>
33 friend class ResolveMethods;
82 m_flags[NEEDS_REINSERT] =
true;
83 m_transform.pos += amount;
92 m_flags[NEEDS_REINSERT] =
true;
93 m_transform.rot += amount;
103 m_flags[NEEDS_REINSERT] =
true;
104 m_transform.pos = pos;
113 m_flags[NEEDS_REINSERT] =
true;
114 m_transform.rot = rot;
115 m_transform.update();
136 return m_flags[IS_STATIC];
145 m_flags[IS_STATIC] = static_;
154 return m_transform.getWorldPoint(localPoint);
163 return m_transform.getLocalPoint(worldPoint);
172 return m_transform.pos;
181 m_linearVelocity += vel * (Float)!m_flags[IS_STATIC];
199 return m_inverseMass;
216 vec2 relPos = spaceTransform.getLocalPoint(
getWorldPoint(localAABB.midpoint()), localOffset);
217 return AABB<Float>(relPos, localAABB.width() * 0.5f, localAABB.height() * 0.5f).rotate(spaceTransform.sincos - m_transform.sincos);
223 Float m_inverseMass = 0;
224 vec2 m_linearVelocity = { 0.0f, 0.0f };
227 std::bitset<4> m_flags;
228 BodyType m_bodyType = BodyType::Invalid;
240 template<
class TileData>
250 Body(uint32_t
id, BodyType type)
261 m_linearVelocity = m_linearVelocity + m_force * m_inverseMass * dt;
262 vec2 newPos = m_transform.pos + m_linearVelocity * dt;
263 m_force = { 0.0f, 0.0f };
265 m_angularVelocity = m_angularVelocity + m_torque * m_inverseI * dt;
266 Float rot = m_transform.rot + m_angularVelocity * dt;
269 m_transform.update();
271 if(!nearlyEqual(newPos, m_transform.pos, 0.1f) || !nearlyEqual(rot, m_transform.rot, 0.05f))
272 m_flags[NEEDS_REINSERT] =
true;
274 m_transform.pos = newPos;
275 m_transform.rot = rot;
279 return m_transform.getWorldPoint(localPoint, m_com);
283 return m_transform.getLocalPoint(worldPoint, m_com);
287 return m_transform.pos + m_com;
296 m_linearVelocity = vec2(0);
297 m_angularVelocity = 0;
301 m_inverseI = (Float)1 / m_I;
302 m_inverseMass = (Float)1 / m_mass;
303 m_com = m_unweightedCom * m_inverseMass;
313 m_angularVelocity = vel * (Float)!m_flags[IS_STATIC];
344 Float m_inverseI = 0;
346 vec2 m_centroid = { 0.0f, 0.0f };
347 vec2 m_com = { 0.0f, 0.0f };
349 vec2 m_unweightedCom = { 0.0f, 0.0f };
350 vec2 m_unweightedCentroid = { 0.0f, 0.0f };
352 vec2 m_force = { 0.0f, 0.0f };
353 Float m_angularVelocity = 0.0f;
354 Float m_torque = 0.0f;
360 :
WorldBody(
id, BodyType::Bullet), m_radius(radius) {}
363 m_transform.pos = m_transform.pos + m_linearVelocity * dt;
367 const Float m_aabbRadius = m_radius * sqrt<Float>(2.0f);
369 return AABB<Float>(m_transform.pos, m_aabbRadius, m_aabbRadius);
Extends the functionality of WorldBody to allow for things such as angular velocity.
Definition body.hpp:238
virtual vec2 getLocalPoint(const vec2 &worldPoint) const override
Gets a world position thats space of this body and transforms into the local space of this body.
Definition body.hpp:282
const vec2 & com() const
Returns the center of mass of this body.
Definition body.hpp:321
const vec2 & centroid() const
Returns the centroid of this body.
Definition body.hpp:330
virtual void integrate(Float dt) override
Integerates this body, meaning the linear velocity and angular velocity will be integrated into the p...
Definition body.hpp:253
Body(uint32_t id, BodyType type)
constructs a new WorldBody
Definition body.hpp:250
virtual vec2 getWorldPos() override
Returns the world position of this body.
Definition body.hpp:286
void addAngularVel(const Float &vel)
Applies an angular velocity to this body.
Definition body.hpp:312
virtual vec2 getWorldPoint(const vec2 &localPoint) const override
Gets a local position thats within the local space of this body and transforms into the world space.
Definition body.hpp:278
virtual void setStatic(bool static_) override
Either make the body static or dynamic.
Definition body.hpp:290
Float rotationalInertia() const
Returns rotational inertia scalar of this body.
Definition body.hpp:339
virtual AABB< Float > getAABB() const
Returns the AABB of this body.
Definition body.hpp:366
virtual void integrate(Float dt) override
Integerates this body, meaning the linear velocity and angular velocity will be integrated into the p...
Definition body.hpp:362
The abstract base class of all Rigid Bodies that defines essential methods and members to be used in ...
Definition body.hpp:30
virtual void setStatic(bool static_)
Either make the body static or dynamic.
Definition body.hpp:144
void addLinearVel(const vec2 &vel)
applies a linear velocity to this body
Definition body.hpp:180
virtual vec2 getLocalPoint(const vec2 &worldPoint) const
Gets a world position thats space of this body and transforms into the local space of this body.
Definition body.hpp:162
Transform & transform()
Returns the REFERENCE transform of this body.
Definition body.hpp:64
const Transform & transform() const
Returns the CONST REFERENCE transform of this body.
Definition body.hpp:72
Float inverseMass() const
Returns the inverse mass of this body.
Definition body.hpp:198
void rotateBy(Float amount)
Rotates the body by amount.
Definition body.hpp:91
virtual vec2 getWorldPoint(const vec2 &localPoint) const
Gets a local position thats within the local space of this body and transforms into the world space.
Definition body.hpp:153
void setPos(const vec2 &pos)
Sets the position of this body to pos.
Definition body.hpp:102
BodyType bodyType() const
Returns the bodyType of this body, see enum BodyType.
Definition body.hpp:56
WorldBody(uint32_t id, BodyType bodyType)
constructs a new WorldBody
Definition body.hpp:41
virtual AABB< Float > getAABB() const =0
Returns the AABB of this body.
bool isStatic() const
Returns if the body is static, meaning it cannot be moved by forces or other bodies.
Definition body.hpp:135
virtual void integrate(Float dt)=0
Integerates this body, meaning the linear velocity and angular velocity will be integrated into the p...
void moveBy(const vec2 &amount)
Moves the body by amount.
Definition body.hpp:81
uint32_t id() const
Returns the id assigned to this body the physics world.
Definition body.hpp:48
void setRot(Float rot)
Sets the rotation of this body to rot.
Definition body.hpp:112
Float mass() const
Returns the mass of this body.
Definition body.hpp:189
virtual vec2 getWorldPos()
Returns the world position of this body.
Definition body.hpp:171
Allows collisions between different bodies to occur.
Definition world.hpp:188
contains various functions and classes that are used for both convience and/or functionality that may...
Resolves the collision between two bodies using the impulse method.
Definition collide.hpp:397