Files
vibe2_modules/source/modules/physics.cppm
Sergio Valor d2648d2328 Experimento parcial de migración a C++20 modules
- Creados módulos core, themes, physics, rendering, input
- Integrados core y themes exitosamente en main.cpp
- Physics, rendering, input comentados por conflictos SDL
- Aplicación funcional con módulos parciales
- Experimento archivado para futuras referencias

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-17 21:55:54 +02:00

122 lines
3.8 KiB
C++

export module vibe2.physics;
import vibe2.core;
// Declaraciones forward en lugar de includes pesados
struct SDL_FRect;
export namespace vibe2::physics {
// Entidad física básica
class PhysicsEntity {
private:
float x_, y_, w_, h_; // Posición y tamaño
float vx_, vy_; // Velocidad
float gravity_force_; // Gravedad
bool on_floor_; // En el suelo
bool stopped_; // Detenido
float bounce_loss_; // Pérdida de rebote
public:
PhysicsEntity(float x, float y, float w, float h, float vx = 0.0f, float vy = 0.0f)
: x_(x), y_(y), w_(w), h_(h),
vx_(vx * vibe2::physics::CONVERSION_FACTOR),
vy_(vy * vibe2::physics::CONVERSION_FACTOR),
gravity_force_(vibe2::GRAVITY_FORCE * vibe2::physics::CONVERSION_FACTOR * vibe2::physics::CONVERSION_FACTOR),
on_floor_(false), stopped_(false), bounce_loss_(0.7f) {}
void update(float deltaTime) {
if (stopped_) return;
// Aplicar gravedad
if (!on_floor_) {
vy_ += gravity_force_ * deltaTime;
}
// Actualizar posición
x_ += vx_ * deltaTime;
if (!on_floor_) {
y_ += vy_ * deltaTime;
} else {
y_ = vibe2::SCREEN_HEIGHT - h_;
}
handleCollisions();
}
void handleCollisions() {
// Colisiones laterales
if (x_ < 0) {
x_ = 0;
vx_ = -vx_;
}
if (x_ + w_ > vibe2::SCREEN_WIDTH) {
x_ = vibe2::SCREEN_WIDTH - w_;
vx_ = -vx_;
}
// Colisión superior
if (y_ < 0) {
y_ = 0;
vy_ = -vy_;
}
// Colisión inferior (suelo)
if (y_ + h_ > vibe2::SCREEN_HEIGHT) {
y_ = vibe2::SCREEN_HEIGHT - h_;
vy_ = -vy_ * bounce_loss_;
if (vy_ > -vibe2::physics::VELOCITY_THRESHOLD && vy_ < vibe2::physics::VELOCITY_THRESHOLD) {
vy_ = 0.0f;
on_floor_ = true;
}
}
// Fricción en el suelo
if (on_floor_) {
float friction = 1.0f - (1.0f - vibe2::physics::FRICTION_FACTOR) * deltaTime * 60.0f;
vx_ *= friction;
if (vx_ > -vibe2::physics::VELOCITY_THRESHOLD && vx_ < vibe2::physics::VELOCITY_THRESHOLD) {
vx_ = 0.0f;
stopped_ = true;
}
}
}
// Modificar velocidad
void addVelocity(float vx, float vy) {
if (stopped_) {
vx_ += vx * vibe2::physics::CONVERSION_FACTOR;
}
vy_ += vy * vibe2::physics::CONVERSION_FACTOR;
on_floor_ = false;
stopped_ = false;
}
// Cambiar gravedad
void toggleGravity() {
gravity_force_ = (gravity_force_ == 0.0f) ?
(vibe2::GRAVITY_FORCE * vibe2::physics::CONVERSION_FACTOR * vibe2::physics::CONVERSION_FACTOR) : 0.0f;
}
// Getters
float getX() const { return x_; }
float getY() const { return y_; }
float getW() const { return w_; }
float getH() const { return h_; }
float getVX() const { return vx_; }
float getVY() const { return vy_; }
float getGravityForce() const { return gravity_force_; }
bool isOnFloor() const { return on_floor_; }
bool isStopped() const { return stopped_; }
void setPosition(float x, float y) {
x_ = x;
y_ = y;
}
private:
float deltaTime = 0.016f; // Para cálculos internos
};
}