semi working sweeping collision along velocity

This commit is contained in:
jacob 2024-09-06 16:00:44 -05:00
parent 11759dc6cd
commit 14163d2a09
5 changed files with 1054 additions and 579 deletions

View File

@ -97,8 +97,15 @@ struct entity {
struct entity_handle colliding_with;
struct v2 point;
struct gjk_simplex simplex;
struct gjk_prototype prototype;
struct v2 pendir;
b32 velocity_intersects;
struct xform xf0;
struct xform xf1;
b32 solved;
struct xform predicted_xform;

View File

@ -124,7 +124,8 @@ INTERNAL void spawn_test_entities(void)
{
//struct v2 pos = V2(-1, -1);
//struct v2 pos = V2(1.1230469346046448864129274625156, -1); /* Touching right side of box */
struct v2 pos = V2(1.1230469346046448864129274625156 - 0.0001, -1); /* Touching right side of box */
//struct v2 pos = V2(1.1230469346046448864129274625156 - 0.0001, -1); /* Touching right side of box */
struct v2 pos = V2(0.374142020941, -0.246118023992); /* Touching glitch spot */
struct v2 size = V2(1, 1);
f32 r = PI / 4;
//f32 r = 0;
@ -558,7 +559,8 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
bullet->bullet_src = ent->handle;
bullet->bullet_src_pos = rel_pos;
bullet->bullet_src_dir = rel_dir;
bullet->bullet_impulse = 0.25;
//bullet->bullet_impulse = 0.25;
bullet->bullet_impulse = 100;
bullet->mass_unscaled = 0.04;
entity_enable_prop(bullet, ENTITY_PROP_BULLET);
@ -649,8 +651,9 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
struct v2 tick_velocity = v2_sub(xf.og, ent->verlet_xform.og);
tick_velocity = v2_add(tick_velocity, v2_mul(acceleration, dt));
ent->verlet_xform = xf;
xf.og = v2_add(xf.og, tick_velocity);
entity_set_xform(ent, xf);
ent->predicted_xform = xf;
}
/* ========================== *
@ -671,78 +674,6 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
* ========================== */
#if 0
for (u64 e0_index = 0; e0_index < store->reserved; ++e0_index) {
struct entity *e0 = &store->entities[e0_index];
if (!(e0->valid && entity_has_prop(e0, ENTITY_PROP_ACTIVE))) continue;
if (!entity_has_prop(e0, ENTITY_PROP_PHYSICAL)) continue;
if (!entity_has_prop(e0, ENTITY_PROP_PLAYER_CONTROLLED)) continue;
struct xform e0_xf = entity_get_xform(e0);
struct quad e0_quad;
struct v2_array e0_poly;
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, e0->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), e0->animation_frame);
e0_quad = xform_mul_quad(e0->sprite_local_xform, quad_from_rect(slice.rect));
e0_quad = xform_mul_quad(e0_xf, e0_quad);
e0_poly = (struct v2_array) {
.count = ARRAY_COUNT(e0_quad.e),
.points = e0_quad.e
};
}
b32 colliding = false;
struct gjk_extended_simplex simplex = { 0 };
struct v2 pen = V2(0, 0);
struct v2 spot = V2(0, 0);
struct entity_handle colliding_with = { 0 };
for (u64 e1_index = 0; e1_index < store->reserved; ++e1_index) {
struct entity *e1 = &store->entities[e1_index];
if (e1 == e0) continue;
if (!(e1->valid && entity_has_prop(e1, ENTITY_PROP_ACTIVE))) continue;
if (!entity_has_prop(e1, ENTITY_PROP_PHYSICAL)) continue;
struct xform e1_xf = entity_get_xform(e1);
struct quad e1_quad;
struct v2_array e1_poly;
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, e1->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), e1->animation_frame);
e1_quad = xform_mul_quad(e1->sprite_local_xform, quad_from_rect(slice.rect));
e1_quad = xform_mul_quad(e1_xf, e1_quad);
e1_poly = (struct v2_array) {
.count = ARRAY_COUNT(e1_quad.e),
.points = e1_quad.e
};
}
colliding = gjk_boolean(e0_poly, e1_poly, G.gjk_steps);
colliding_with = e1->handle;
//simplex = res.final_simplex;
if (colliding) {
//pen = epa_colliding_pen(e0_poly, e1_poly, simplex);
#if 0
/* Pen movement test */
{
struct xform xf = e1_xf;
xf.og = v2_add(xf.og, pen);
entity_set_xform(e1, xf);
//e1->verlet_xform.og = v2_add(e1->verlet_xform.og, pen);
}
#endif
} else {
spot = e0->spot;
}
}
e0->colliding = colliding;
e0->colliding_with = colliding_with;
e0->simplex = simplex;
e0->pen = pen;
e0->spot = spot;
}
#else
for (u64 e0_index = 0; e0_index < store->reserved; ++e0_index) {
struct entity *e0 = &store->entities[e0_index];
if (!(e0->valid && entity_has_prop(e0, ENTITY_PROP_ACTIVE))) continue;
@ -790,15 +721,15 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
};
}
#if 0
#if 1
struct v2 e0_velocity = v2_sub(e0_xf.og, e0->verlet_xform.og);
struct v2 e1_velocity = v2_sub(e1_xf.og, e1->verlet_xform.og);
pendir = v2_sub(e0_velocity, e1_velocity); /* TODO: What if both point in opposite directions? */
pendir = v2_sub(e0_velocity, e1_velocity);
//pendir = v2_norm(pendir);
#else
//pendir = V2(0, 0);
pendir = V2(0, 0);
//pendir = V2(-0.25, -1);
pendir = v2_mul(v2_norm(V2(-0.25, -1)), 0.2);
//pendir = v2_mul(v2_norm(V2(-0.25, -1)), 0.2);
//pendir = V2(0, 99999);
//pendir = V2(-99999999999, -99999999999);
//pendir = V2(-0.0000794729349, -1.00000);
@ -845,6 +776,136 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
colliding_with->point = point1;
}
}
#else
for (u64 e0_index = 0; e0_index < store->reserved; ++e0_index) {
struct entity *e0 = &store->entities[e0_index];
if (!(e0->valid && entity_has_prop(e0, ENTITY_PROP_ACTIVE))) continue;
if (!entity_has_prop(e0, ENTITY_PROP_PHYSICAL)) continue;
if (!entity_has_prop(e0, ENTITY_PROP_PLAYER_CONTROLLED) && !entity_has_prop(e0, ENTITY_PROP_BULLET)) continue;
struct xform xf0 = entity_get_xform(e0);
struct xform xf1 = e0->predicted_xform;
struct quad e0_quad;
struct v2_array e0_poly;
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, e0->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), e0->animation_frame);
e0_quad = xform_mul_quad(e0->sprite_local_xform, quad_from_rect(slice.rect));
e0_quad = xform_mul_quad(xf0, e0_quad);
e0_poly = (struct v2_array) {
.count = ARRAY_COUNT(e0_quad.e),
.points = e0_quad.e
};
}
b32 any_collision = false;
b32 colliding = false;
struct v2 point0 = V2(0, 0);
struct v2 point1 = V2(0, 0);
struct entity *colliding_with = entity_nil();
struct gjk_simplex simplex = { 0 };
struct gjk_prototype prototype = { 0 };
struct v2 velocity = V2(0, 0);
struct v2 pen = V2(0, 0);
b32 solved = false;
b32 velocity_intersects = false;
(UNUSED)pen;
for (u64 e1_index = 0; e1_index < store->reserved; ++e1_index) {
struct entity *e1 = &store->entities[e1_index];
if (e1 == e0) continue;
if (!(e1->valid && entity_has_prop(e1, ENTITY_PROP_ACTIVE))) continue;
if (!entity_has_prop(e1, ENTITY_PROP_PHYSICAL)) continue;
if (entity_has_prop(e1, ENTITY_PROP_PLAYER_CONTROLLED)) continue;
struct xform e1_xf = entity_get_xform(e1);
struct quad e1_quad;
struct v2_array e1_poly;
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, e1->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), e1->animation_frame);
e1_quad = xform_mul_quad(e1->sprite_local_xform, quad_from_rect(slice.rect));
e1_quad = xform_mul_quad(e1_xf, e1_quad);
e1_poly = (struct v2_array) {
.count = ARRAY_COUNT(e1_quad.e),
.points = e1_quad.e
};
}
/* Testing */
{
/* TODO: Continuity gen check */
velocity = v2_sub(xf1.og, xf0.og);
//velocity = V2(0.014992147684, -0.000010356307);
//velocity = V2(0.005, 0);
//velocity = V2(0.005, 0.005);
//velocity = V2(-1, -1);
//velocity = v2_neg(velocity);
//xf0.og = v2_sub(xf1.og, velocity);
xf1.og = v2_add(xf0.og, velocity);
}
struct gjk_extended_result res = gjk_extended(e0_poly, e1_poly, xf0, xf1);
colliding = res.colliding;
point0 = res.p0;
point1 = res.p1;
colliding_with = e1;
simplex = res.simplex;
prototype = res.prototype;
velocity_intersects = res.velocity_intersects;
solved = res.solved;
if (colliding) {
#if 1
any_collision = true;
pen = v2_sub(point1, point0);
/* Pen movement test */ #if 0
f32 epsilon = 0.000100; pen = v2_add(pen, v2_mul(v2_norm(pen), epsilon));
#endif
//xf0.og = v2_add(xf0.og, pen);
//e0->verlet_xform.og = v2_add(e0->verlet_xform.og, pen);
//e0->verlet_xform.og = v2_add(e0->verlet_xform.og, pen);
#endif
}
}
e0->colliding = colliding;
e0->colliding_with = colliding_with->handle;
e0->point = point0;
e0->simplex = simplex;
e0->prototype = prototype;
e0->xf0 = xf0;
e0->xf1 = xf1;
e0->pendir = velocity;
e0->velocity_intersects = velocity_intersects;
e0->solved = solved;
if (colliding_with->valid) {
colliding_with->colliding = colliding;
colliding_with->colliding_with = e0->handle;
colliding_with->point = point1;
}
(UNUSED)any_collision;
#if 1
{
//if (!v2_eq(pen, V2(0, 0))) {
if (any_collision) {
xf1.og = v2_add(xf0.og, pen);
//e0->verlet_xform.og = xf0.og;
e0->verlet_xform.og = v2_sub(xf1.og, velocity);
}
entity_set_xform(e0, xf1);
}
#else
{
entity_set_xform(e0, e0->predicted_xform);
}
#endif
}
#endif

1216
src/gjk.c

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,11 @@ struct gjk_simplex {
struct gjk_menkowski_point a, b, c;
};
/* Returns simple true or false indicating shape collision */
b32 gjk_boolean(struct v2_array shape0, struct v2_array shape1);
#if 0
struct gjk_extended_result {
b32 colliding;
struct v2 p0, p1; /* Closest points (or penetrating points if colliding) on each shape */
@ -27,9 +32,6 @@ struct gjk_extended_result {
b32 velocity_intersects;
};
/* Returns simple true or false indicating shape collision */
b32 gjk_boolean(struct v2_array shape0, struct v2_array shape1);
/* Returns shape whether shapes are colliding well as closest / penetrating points on each shape.
*
* If shapes are colliding and `penetration_dir` is non-zero, the shortest
@ -41,4 +43,31 @@ struct gjk_extended_result gjk_extended(struct v2_array shape0, struct v2_array
struct v2_array menkowski(struct arena *arena, struct v2_array poly0, struct v2_array poly1);
#else
/* TODO: Remove this (debugging) */
struct gjk_prototype {
struct v2 points[256];
u32 len;
};
struct gjk_extended_result {
b32 colliding;
struct v2 p0, p1; /* Closest points on each shape */
/* For debugging */
struct gjk_simplex simplex;
b32 velocity_intersects;
b32 solved;
struct gjk_prototype prototype;
};
struct gjk_extended_result gjk_extended(struct v2_array shape0, struct v2_array shape1, struct xform xf0, struct xform xf1);
struct v2_array menkowski(struct arena *arena, struct v2_array poly0, struct v2_array poly1, struct xform xf0, struct xform xf1);
struct v2_array cloud(struct arena *arena, struct v2_array poly0, struct v2_array poly1, struct xform xf0, struct xform xf1);
#endif
#endif

View File

@ -991,21 +991,32 @@ INTERNAL void user_update(void)
(UNUSED)colliding;
#if 1
/* Draw menkowski */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
struct quad ent_quad;
struct v2_array ent_poly;
/* Create shapes */
struct quad ent_quad_xf0;
struct v2_array ent_poly_xf0;
struct quad ent_quad_xf1;
struct v2_array ent_poly_xf1;
struct quad e1_quad;
struct v2_array e1_poly;
{
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), ent->animation_frame);
ent_quad = xform_mul_quad(ent->sprite_local_xform, quad_from_rect(slice.rect));
ent_quad = xform_mul_quad(entity_get_xform(ent), ent_quad);
ent_poly = (struct v2_array) {
.count = ARRAY_COUNT(ent_quad.e),
.points = ent_quad.e
ent_quad_xf0 = xform_mul_quad(ent->sprite_local_xform, quad_from_rect(slice.rect));
ent_quad_xf0 = xform_mul_quad(ent->xf0, ent_quad_xf0);
ent_poly_xf0 = (struct v2_array) {
.count = ARRAY_COUNT(ent_quad_xf0.e),
.points = ent_quad_xf0.e
};
}
{
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("shape"), ent->animation_frame);
ent_quad_xf1 = xform_mul_quad(ent->sprite_local_xform, quad_from_rect(slice.rect));
ent_quad_xf1 = xform_mul_quad(ent->xf1, ent_quad_xf1);
ent_poly_xf1 = (struct v2_array) {
.count = ARRAY_COUNT(ent_quad_xf1.e),
.points = ent_quad_xf1.e
};
}
{
@ -1020,16 +1031,54 @@ INTERNAL void user_update(void)
}
}
u32 color = RGBA_32_F(0, 0, 0.25, 1);
/* Draw collision shapes */
{
(UNUSED)ent_poly_xf0;
(UNUSED)ent_poly_xf1;
f32 thickness = 2;
{
u32 color = RGBA_32_F(1, 0, 0, 0.25);
struct quad quad = xform_mul_quad(G.world_view, ent_quad_xf0);
draw_solid_quad_line(G.viewport_canvas, quad, thickness, color);
}
{
u32 color = RGBA_32_F(0, 1, 0, 0.25);
struct quad quad = xform_mul_quad(G.world_view, ent_quad_xf1);
draw_solid_quad_line(G.viewport_canvas, quad, thickness, color);
}
}
/* Draw menkowski */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
u32 color = ent->solved ? RGBA_32_F(0, 0, 0.25, 1) : RGBA_32_F(0, 0.25, 0.25, 1);
f32 thickness = 2;
(UNUSED)thickness;
struct v2_array m = menkowski(temp.arena, ent_poly, e1_poly);
//struct v2_array m = menkowski(temp.arena, ent_poly, e1_poly);
struct v2_array m = menkowski(temp.arena, ent_poly_xf0, e1_poly, ent->xf0, ent->xf1);
for (u64 i = 0; i < m.count; ++i) m.points[i] = xform_mul_v2(G.world_view, m.points[i]);
draw_solid_poly_line(G.viewport_canvas, m, true, thickness, color);
//draw_solid_poly(G.viewport_canvas, m, color);
}
/* Draw cloud */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
u32 color = RGBA_32_F(1, 1, 1, 1);
f32 radius = 2;
//struct v2_array m = menkowski(temp.arena, ent_poly, e1_poly);
struct v2_array m = cloud(temp.arena, ent_poly_xf0, e1_poly, ent->xf0, ent->xf1);
for (u64 i = 0; i < m.count; ++i) {
struct v2 p = xform_mul_v2(G.world_view, m.points[i]);;
draw_solid_circle(G.viewport_canvas, p, radius, color, 10);
}
}
/* Draw pendir */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
f32 thickness = 2;
@ -1041,6 +1090,19 @@ INTERNAL void user_update(void)
draw_solid_arrow_ray(G.viewport_canvas, start, ray, thickness, thickness * 4, color);
}
/* Draw prototype */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
f32 thickness = 2;
u32 color = RGBA_32_F(1, 1, 1, 0.25);
struct v2_array m = {
.points = ent->prototype.points,
.count = ent->prototype.len
};
for (u64 i = 0; i < m.count; ++i) m.points[i] = xform_mul_v2(G.world_view, m.points[i]);
draw_solid_poly_line(G.viewport_canvas, m, true, thickness, color);
}
/* Draw simplex */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
f32 thickness = 2;
@ -1074,10 +1136,10 @@ INTERNAL void user_update(void)
/* Draw point */
{
u32 color = entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED) ? COLOR_PURPLE : COLOR_TURQOISE;
f32 thickness = 5;
u32 color = entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED) ? RGBA_32_F(1, 0, 0, 0.5) : RGBA_32_F(0, 1, 1, 0.5);
f32 radius = 5;
struct v2 point = xform_mul_v2(G.world_view, ent->point);
draw_solid_circle(G.viewport_canvas, point, thickness, color, 10);
draw_solid_circle(G.viewport_canvas, point, radius, color, 10);
}
}
@ -1256,6 +1318,18 @@ INTERNAL void user_update(void)
draw_text(G.viewport_canvas, font, pos, string_format(temp.arena, STR("debug_camera: %F"), FMT_STR(G.debug_camera ? STR("true") : STR("false"))));
pos.y += spacing;
struct v2 player_collision_vel = entity_find_first_match_one(store, ENTITY_PROP_PLAYER_CONTROLLED)->pendir;
draw_text(G.viewport_canvas, font, pos, string_format(temp.arena, STR("player collision velocity: (%F, %F)"), FMT_FLOAT_P((f64)player_collision_vel.x, 12), FMT_FLOAT_P((f64)player_collision_vel.y, 12)));
pos.y += spacing;
struct v2 xf0_pos = entity_find_first_match_one(store, ENTITY_PROP_PLAYER_CONTROLLED)->xf0.og;
draw_text(G.viewport_canvas, font, pos, string_format(temp.arena, STR("player collision xf0 pos: (%F, %F)"), FMT_FLOAT_P((f64)xf0_pos.x, 12), FMT_FLOAT_P((f64)xf0_pos.y, 12)));
pos.y += spacing;
struct v2 xf1_pos = entity_find_first_match_one(store, ENTITY_PROP_PLAYER_CONTROLLED)->xf1.og;
draw_text(G.viewport_canvas, font, pos, string_format(temp.arena, STR("player collision xf1 pos: (%F, %F)"), FMT_FLOAT_P((f64)xf1_pos.x, 12), FMT_FLOAT_P((f64)xf1_pos.y, 12)));
pos.y += spacing;
#if RTC
draw_text(G.viewport_canvas, font, pos, string_format(temp.arena, STR("gjk steps: %F"), FMT_UINT(gjk_debug_steps)));
pos.y += spacing;