Physics Subsystem Component¶
Overview¶
The Physics Subsystem provides specialized handling for different types of components. This includes physics components, collision components, and movement components.
Example with the CollisionBox Component¶
CollisionBoxComponent(Rectangle2d rectangle, Vector2d offset = Vector2d(0., 0.));
The CollisionBoxComponent class is a subclass of the Component class. It is used to represent collision components in the game. The CollisionBoxComponent class has the following function to update the component with time:
void CollisionBoxComponent::update(std::shared_ptr<Actor> actor, double delta)
here this update check collision with other actors that has also collisionbox compoents and emit an event if a collision is detected.
if (intersects) {
BBOC::engine->getSubsystem<PhysicsSubsystem>()->getEventBus()
.emit<CollisionEvent>(actor, otherActor);
}
The update() function is called every frame to update the component with time. The delta parameter is the time elapsed since the last frame.
Example with the Physics Component¶
PhysicsComponent(Vector2d velocity, double mass, double drag, bool _follow_vector)
The PhysicsComponent class is a subclass of the Component class. It is used to represent physics components in the game. The PhysicsComponent class has the following function to update the component with time:
void PhysicsComponent::update(std::shared_ptr<Actor> actor, double delta) {
_delta += delta;
_velocity *= (1.0 - _drag * (delta / 1000.));
if (this->_follow_vector){
actor->setRotation(atan2(_velocity.y(), _velocity.x()) * 180. / std::numbers::pi_v<double>);
}
actor->setWorldPosition(actor->getWorldPosition() + _velocity * (delta / 1000.));
}
The update() function is called every frame to update the component with time. The delta parameter is the time elapsed since the last frame.
We update the velocity of the actor with the drag and the time elapsed since the last frame. We also update the position of the actor with the velocity and the time elapsed since the last frame.