improve collision detection algorithm

This commit is contained in:
Tony Crisci 2017-08-17 18:46:05 -04:00 committed by Drew DeVault
parent 59a71f59b9
commit 854a9381ca
1 changed files with 27 additions and 15 deletions

View File

@ -91,22 +91,34 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
sample->x_offs, sample->y_offs + 128);
bool lr_collision = !wlr_output_layout_output_at(sample->layout,
sample->x_offs + 128, sample->y_offs + 128);
bool has_double_collision = false;
if ((ur_collision && ul_collision) || (lr_collision && ll_collision)) {
sample->y_vel *= -1;
has_double_collision = true;
if (ur_collision && ul_collision && ll_collision && lr_collision) {
// oops we went off the screen somehow
struct wlr_output_layout_output *main_l_output;
main_l_output = wlr_output_layout_get(sample->layout, sample->main_output);
sample->x_offs = main_l_output->x + 20;
sample->y_offs = main_l_output->y + 20;
} else if (ur_collision && ul_collision) {
sample->y_vel = fabs(sample->y_vel);
} else if (lr_collision && ll_collision) {
sample->y_vel = -fabs(sample->y_vel);
} else if (ll_collision && ul_collision) {
sample->x_vel = fabs(sample->x_vel);
} else if (ur_collision && lr_collision) {
sample->x_vel = -fabs(sample->x_vel);
} else {
if (ur_collision || lr_collision) {
sample->x_vel = -fabs(sample->x_vel);
}
if ((ll_collision && ul_collision) || (ur_collision && lr_collision)) {
sample->x_vel *= -1;
has_double_collision = true;
if (ul_collision || ll_collision) {
sample->x_vel = fabs(sample->x_vel);
}
if (ul_collision || ur_collision) {
sample->y_vel = fabs(sample->y_vel);
}
if (ll_collision || lr_collision) {
sample->y_vel = -fabs(sample->y_vel);
}
if (!has_double_collision &&
(ur_collision || ul_collision || lr_collision || ll_collision)) {
sample->x_vel *= -1;
sample->y_vel *= -1;
}
sample->x_offs += sample->x_vel * seconds;