2021-07-26 15:53:18 +00:00
|
|
|
|
2021-05-10 06:28:56 +00:00
|
|
|
#include "zpl.h"
|
2021-05-04 17:41:30 +00:00
|
|
|
#include "modules/physics.h"
|
2021-05-07 14:43:54 +00:00
|
|
|
#include "world/world.h"
|
2021-07-26 15:53:18 +00:00
|
|
|
#include "world/blocks.h"
|
|
|
|
#include "profiler.h"
|
2021-05-04 17:41:30 +00:00
|
|
|
|
2021-07-26 15:53:18 +00:00
|
|
|
#define PHY_BLOCK_COLLISION 1
|
2021-05-08 15:48:09 +00:00
|
|
|
#define PHY_WALK_DRAG 0.12
|
2021-07-26 15:53:18 +00:00
|
|
|
#define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f)
|
|
|
|
#define PHY_CORRECTION(x) ((zpl_max(0.0f, (WORLD_BLOCK_SIZE/2.0f) - zpl_abs(x))*zpl_sign(x)))*(WORLD_BLOCK_SIZE/2.0f)
|
2021-05-07 15:24:22 +00:00
|
|
|
|
2021-05-04 17:41:30 +00:00
|
|
|
void MoveWalk(ecs_iter_t *it) {
|
2021-07-26 15:53:18 +00:00
|
|
|
Velocity *v = ecs_column(it, Velocity, 1);
|
2021-07-18 11:23:59 +00:00
|
|
|
|
2021-05-04 17:41:30 +00:00
|
|
|
for (int i = 0; i < it->count; i++) {
|
2021-05-07 15:24:22 +00:00
|
|
|
v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG);
|
|
|
|
v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG);
|
2021-05-08 09:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 15:53:18 +00:00
|
|
|
void IntegratePositions(ecs_iter_t *it) {
|
|
|
|
profile(PROF_INTEGRATE_POS) {
|
|
|
|
Position *p = ecs_column(it, Position, 1);
|
|
|
|
Velocity *v = ecs_column(it, Velocity, 2);
|
|
|
|
|
|
|
|
for (int i = 0; i < it->count; i++) {
|
|
|
|
// NOTE(zaklaus): world bounds
|
|
|
|
{
|
|
|
|
double w = (double)world_dim();
|
|
|
|
p[i].x = zpl_clamp(p[i].x, 0, w-1);
|
|
|
|
p[i].y = zpl_clamp(p[i].y, 0, w-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PHY_BLOCK_COLLISION==1
|
|
|
|
// NOTE(zaklaus): X axis
|
|
|
|
{
|
|
|
|
world_block_lookup lookup = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y);
|
|
|
|
uint32_t flags = blocks_get_flags(lookup.block_id);
|
|
|
|
if (flags & BLOCK_FLAG_COLLISION) {
|
|
|
|
v[i].x = PHY_CORRECTION(lookup.ox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(zaklaus): Y axis
|
|
|
|
{
|
|
|
|
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y+PHY_LOOKAHEAD(v[i].y));
|
|
|
|
uint32_t flags = blocks_get_flags(lookup.block_id);
|
|
|
|
if (flags & BLOCK_FLAG_COLLISION) {
|
|
|
|
v[i].y = PHY_CORRECTION(lookup.oy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p[i].x += v[i].x * it->delta_time;
|
|
|
|
p[i].y += v[i].y * it->delta_time;
|
2021-05-08 07:18:50 +00:00
|
|
|
}
|
2021-05-08 08:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTrackerPos(ecs_iter_t *it) {
|
|
|
|
Position *p = ecs_column(it, Position, 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < it->count; i++){
|
2021-05-07 14:43:54 +00:00
|
|
|
librg_entity_chunk_set(world_tracker(), it->entities[i], librg_chunk_from_realpos(world_tracker(), p[i].x, p[i].y, 0));
|
2021-05-04 17:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsImport(ecs_world_t *ecs) {
|
|
|
|
ECS_MODULE(ecs, Physics);
|
|
|
|
ecs_set_name_prefix(ecs, "Physics");
|
2021-05-07 14:43:54 +00:00
|
|
|
|
2021-05-04 17:41:30 +00:00
|
|
|
ECS_TAG(ecs, Walking);
|
|
|
|
ECS_TAG(ecs, Flying);
|
|
|
|
ECS_TYPE(ecs, Movement, Walking, Flying);
|
2021-07-18 11:23:59 +00:00
|
|
|
|
2021-05-07 14:43:54 +00:00
|
|
|
ECS_META(ecs, Velocity);
|
2021-07-18 11:23:59 +00:00
|
|
|
|
2021-07-26 15:53:18 +00:00
|
|
|
ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, Velocity);
|
|
|
|
ECS_SYSTEM(ecs, IntegratePositions, EcsOnValidate, general.Position, Velocity);
|
2021-05-08 08:08:37 +00:00
|
|
|
ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, general.Position);
|
2021-07-18 11:23:59 +00:00
|
|
|
|
2021-05-04 17:41:30 +00:00
|
|
|
ECS_SET_TYPE(Movement);
|
|
|
|
ECS_SET_ENTITY(Walking);
|
|
|
|
ECS_SET_ENTITY(Flying);
|
|
|
|
ECS_SET_COMPONENT(Velocity);
|
|
|
|
ECS_SET_ENTITY(MoveWalk);
|
2021-05-08 09:22:25 +00:00
|
|
|
ECS_SET_ENTITY(UpdateTrackerPos);
|
2021-07-26 15:53:18 +00:00
|
|
|
ECS_SET_ENTITY(IntegratePositions);
|
2021-05-04 17:41:30 +00:00
|
|
|
}
|