~vladh/peony

ba1aa4929bdc3e8e26ac390b7c38e3abc318781b — Vlad-Stefan Harbuz 1 year, 7 months ago a1736d2
refactor physics state
M src/behavior_functions.cpp => src/behavior_functions.cpp +1 -2
@@ 29,8 29,7 @@ behavior_functions::char_movement_test(entities::Handle entity_handle)
        return;
    }

    physics::Component *physics_component =
        engine::get_physics_component(entity_handle);
    physics::Component *physics_component = physics::get_component(entity_handle);
    if (!physics_component) {
        logs::error("Could not get physics::Component for behavior::Component");
        return;

M src/core.cpp => src/core.cpp +1 -0
@@ 134,6 134,7 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)
    drawable::init(&state->drawable_state, asset_memory_pool);
    lights::init(&state->lights_state, asset_memory_pool);
    anim::init(&state->anim_state, asset_memory_pool);
    physics::init(&state->physics_state, asset_memory_pool);
    behavior::init(
        &state->behavior_state,
        asset_memory_pool,

M src/engine.cpp => src/engine.cpp +0 -18
@@ 64,13 64,6 @@ engine::get_entities()
}


Array<physics::Component> *
engine::get_physics_components()
{
    return &engine::state->physics_component_set.components;
}


entities::Entity *
engine::get_entity(entities::Handle entity_handle)
{


@@ 78,13 71,6 @@ engine::get_entity(entities::Handle entity_handle)
}


physics::Component *
engine::get_physics_component(entities::Handle entity_handle)
{
    return engine::state->physics_component_set.components[entity_handle];
}


f64
engine::get_t()
{


@@ 158,10 144,6 @@ void engine::init(engine::State *engine_state, memory::Pool *asset_memory_pool) 
        .entities = Array<entities::Entity>(
            asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1)
    };
    engine::state->physics_component_set = {
        .components = Array<physics::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "physics_components", true, 1)
    };
}



M src/engine.hpp => src/engine.hpp +0 -3
@@ 58,7 58,6 @@ public:
        Array<models::ModelLoader> model_loaders;
        models::EntityLoaderSet entity_loader_set;
        entities::Set entity_set;
        physics::ComponentSet physics_component_set;
    };

    static engine::State * debug_get_engine_state();


@@ 67,9 66,7 @@ public:
    static void mark_first_non_internal_handle();
    static entities::Set * get_entity_set();
    static Array<entities::Entity> * get_entities();
    static Array<physics::Component> * get_physics_components();
    static entities::Entity * get_entity(entities::Handle entity_handle);
    static physics::Component * get_physics_component(entities::Handle entity_handle);
    static f64 get_t();
    static f64 get_dt();
    static void run_main_loop(GLFWwindow *window);

M src/models.cpp => src/models.cpp +1 -1
@@ 98,7 98,7 @@ models::prepare_entity_loader_and_check_if_done(
        *animation_component = model_loader->animation_component;
        animation_component->entity_handle = entity_loader->entity_handle;

        physics::Component *physics_component = engine::get_physics_component(entity_loader->entity_handle);
        physics::Component *physics_component = physics::get_component(entity_loader->entity_handle);
        *physics_component = entity_loader->physics_component;
        physics_component->entity_handle = entity_loader->entity_handle;


M src/physics.cpp => src/physics.cpp +29 -3
@@ 7,13 7,16 @@
#include "intrinsics.hpp"


physics::State *physics::state = nullptr;


physics::RayCollisionResult
physics::find_ray_collision(
    spatial::Ray *ray,
    // TODO: Replace this with some kind of collision layers.
    physics::Component *physics_component_to_ignore_or_nullptr
) {
    each (candidate, *engine::get_physics_components()) {
    each (candidate, *get_components()) {
        if (!is_component_valid(candidate)) {
            continue;
        }


@@ 41,7 44,7 @@ physics::find_collision(
    physics::Component *self_physics,
    spatial::Component *self_spatial
) {
    each (candidate_physics, *engine::get_physics_components()) {
    each (candidate_physics, *get_components()) {
        if (!is_component_valid(candidate_physics)) {
            continue;
        }


@@ 70,7 73,7 @@ physics::find_collision(
void
physics::update()
{
    each (physics_component, *engine::get_physics_components()) {
    each (physics_component, *get_components()) {
        if (!is_component_valid(physics_component)) {
            continue;
        }


@@ 87,6 90,29 @@ physics::update()
}


Array<physics::Component> *
physics::get_components()
{
    return &physics::state->components;
}


physics::Component *
physics::get_component(entities::Handle entity_handle)
{
    return physics::state->components[entity_handle];
}


void
physics::init(physics::State *physics_state, memory::Pool *asset_memory_pool)
{
    physics::state = physics_state;
    physics::state->components = Array<physics::Component>(
        asset_memory_pool, MAX_N_ENTITIES, "physics_components", true, 1);
}


spatial::Obb
physics::transform_obb(spatial::Obb obb, spatial::Component *spatial)
{

M src/physics.hpp => src/physics.hpp +6 -1
@@ 18,7 18,7 @@ public:
        spatial::Obb transformed_obb;
    };

    struct ComponentSet {
    struct State {
        Array<Component> components;
    };



@@ 50,6 50,9 @@ public:
        spatial::Component *self_spatial
    );
    static void update();
    static Array<physics::Component> * get_components();
    static physics::Component * get_component(entities::Handle entity_handle);
    static void init(physics::State *physics_state, memory::Pool *asset_memory_pool);

private:
    static spatial::Obb transform_obb(spatial::Obb obb, spatial::Component *spatial);


@@ 95,4 98,6 @@ private:
        spatial::Component *spatial_a,
        spatial::Component *spatial_b
    );

    static physics::State *state;
};

M src/state.hpp => src/state.hpp +4 -3
@@ 24,17 24,18 @@ struct State {
    WindowSize window_size;
    spatial::State spatial_state;
    drawable::State drawable_state;
    lights::State lights_state;
    anim::State anim_state;
    physics::State physics_state;
    behavior::State behavior_state;
    engine::State engine_state;
    renderer::State renderer_state;
    cameras::State cameras_state;
    input::State input_state;
    gui::State gui_state;
    lights::State lights_state;
    anim::State anim_state;
    mats::State materials_state;
    tasks::State tasks_state;
    debugdraw::State debug_draw_state;
    behavior::State behavior_state;
    memory::Pool *asset_memory_pool;
};