Fase 6c: migrar Ship al sistema de fisica vectorial
Primera entidad migrada. La nave del jugador ya NO mantiene su propio estado cinemático ad-hoc — toda la física vive en Entity::body_ y el movimiento lo realiza Physics::PhysicsWorld. Cambios en ship.hpp: - Eliminado: float velocity_ (escalar, polar) - Eliminado: void applyPhysics() (lo hace el world) - Añadido: override postUpdate() para sincronizar center_/angle_ - getVelocityVector() ahora devuelve body_.velocity (Vec2 cartesiano) - Nuevo getter getSpeed() = body_.velocity.length() - setCenter() actualiza tanto el mirror como body_.position - markHit() detiene el body_ (velocity = 0) Cambios en ship.cpp: - Constructor configura el body_: * mass = 10.0 (referencia para impulsos en choques) * radius = SHIP_RADIUS (12.0) * restitution = 0.6 (rebote moderado en paredes) * linear_damping = 1.5 s⁻¹ (fricción exponencial) * angular_damping = 0.0 (la rotación es por input, no inercial) - init() resetea body_ a la posición/orientación nueva, velocity = 0 - processInput() ahora: * Rotación: modifica body_.angle directamente (no física) * Thrust: applyForce(direction * mass * ACCELERATION) - update() solo gestiona timer de invulnerabilidad y aplica el cap de MAX_VELOCITY (el thrust acumula fuerza sin tope; clampamos body_.velocity) - postUpdate() copia body_.position -> center_ y body_.angle -> angle_ - draw() sin cambios funcionales (usa getSpeed() en lugar de velocity_) Cambios en GameScene: - En init(): physics_world_.addBody(&ship.getBody()) por cada nave activa - En update(): physics_world_.update(dt) + ship.postUpdate(dt) al inicio del frame (las fuerzas del frame N-1 se integran en el frame N; 1 frame de latencia ~16ms, imperceptible a 60fps) Cambios de comportamiento visibles esperados: - La nave ahora rebota contra las paredes del PLAYAREA con restitution=0.6 (antes: clipping silencioso). PRIMERA muestra de la nueva física. - Inercia: tras soltar THRUST, la nave conserva velocidad y se decelera exponencialmente con linear_damping. Sensación más espacial. - Velocidad limitada en magnitud vectorial (antes: escalar). El cap preserva el feel arcade aproximado de MAX_VELOCITY = 120 px/s. Edge case pendiente para tuning: - Naves muertas siguen en el world como obstáculos físicos (radius=12). No es crítico mientras los enemies/bullets no estén migrados. Smoke test xvfb: arranca correctamente. Validación de feeling requiere test del usuario en vivo. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
05740775c2
commit
2fe22ff911
3 files changed
+113
-150
No files matched your search
@@ -5,13 +5,11 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/entities/entity.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
class Ship : public Entities::Entity {
|
||||
public:
|
||||
@@ -23,40 +21,43 @@ class Ship : public Entities::Entity {
|
||||
void init(const Vec2* spawn_point, bool activar_invulnerabilitat = false);
|
||||
void processInput(float delta_time, uint8_t player_id);
|
||||
void update(float delta_time) override;
|
||||
void postUpdate(float delta_time) override;
|
||||
void draw() const override;
|
||||
|
||||
// Override: Interfície d'Entity
|
||||
[[nodiscard]] bool isActive() const override { return !is_hit_; }
|
||||
// Override: Interfaz de Entity
|
||||
[[nodiscard]] auto isActive() const -> bool override { return !is_hit_; }
|
||||
|
||||
// Override: Interfície de colisión
|
||||
[[nodiscard]] float getCollisionRadius() const override {
|
||||
// Override: Interfaz de colisión
|
||||
[[nodiscard]] auto getCollisionRadius() const -> float override {
|
||||
return Defaults::Entities::SHIP_RADIUS;
|
||||
}
|
||||
[[nodiscard]] bool isCollidable() const override {
|
||||
[[nodiscard]] auto isCollidable() const -> bool override {
|
||||
return !is_hit_ && invulnerable_timer_ <= 0.0F;
|
||||
}
|
||||
|
||||
// Getters (API pública sin canvis)
|
||||
[[nodiscard]] bool isAlive() const { return !is_hit_; }
|
||||
[[nodiscard]] bool isHit() const { return is_hit_; }
|
||||
[[nodiscard]] bool isInvulnerable() const { return invulnerable_timer_ > 0.0F; }
|
||||
[[nodiscard]] Vec2 getVelocityVector() const {
|
||||
return {
|
||||
.x = velocity_ * std::cos(angle_ - (Constants::PI / 2.0F)),
|
||||
.y = velocity_ * std::sin(angle_ - (Constants::PI / 2.0F))};
|
||||
}
|
||||
// Getters (API pública sin cambios)
|
||||
[[nodiscard]] auto isAlive() const -> bool { return !is_hit_; }
|
||||
[[nodiscard]] auto isHit() const -> bool { return is_hit_; }
|
||||
[[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_timer_ > 0.0F; }
|
||||
// Velocidad como vector cartesiano (ahora viene directa del body_).
|
||||
[[nodiscard]] auto getVelocityVector() const -> Vec2 { return body_.velocity; }
|
||||
// Velocidad escalar (utilidad para draw y debugging).
|
||||
[[nodiscard]] auto getSpeed() const -> float { return body_.velocity.length(); }
|
||||
|
||||
// Setters
|
||||
void setCenter(const Vec2& nou_centre) { center_ = nou_centre; }
|
||||
void setCenter(const Vec2& nou_centre) {
|
||||
center_ = nou_centre;
|
||||
body_.position = nou_centre;
|
||||
}
|
||||
|
||||
// Colisiones (Fase 10)
|
||||
void markHit() { is_hit_ = true; }
|
||||
// Colisiones
|
||||
void markHit() {
|
||||
is_hit_ = true;
|
||||
body_.velocity = Vec2{}; // Detener al morir
|
||||
}
|
||||
|
||||
private:
|
||||
// Membres específics de Ship (heretats: renderer_, shape_, center_, angle_, brightness_)
|
||||
float velocity_; // Velocidad (px/s)
|
||||
// Miembros específicos de Ship (heredados: renderer_, shape_, center_, angle_, brightness_, body_)
|
||||
bool is_hit_;
|
||||
float invulnerable_timer_; // 0.0f = vulnerable, >0.0f = invulnerable
|
||||
|
||||
void applyPhysics(float delta_time);
|
||||
};
|
||||
Reference in new issue
Block a user