소스 검색

do huge movements in steps instead of at once

opengl-renderer-broken
Martin Dørum 4 년 전
부모
커밋
9817aae87d
2개의 변경된 파일31개의 추가작업 그리고 10개의 파일을 삭제
  1. 4
    0
      libswan/include/swan/Vector2.h
  2. 27
    10
      libswan/src/traits/BodyTrait.cc

+ 4
- 0
libswan/include/swan/Vector2.h 파일 보기

return (T)std::sqrt((double)(this->x * this->x + this->y * this->y)); return (T)std::sqrt((double)(this->x * this->x + this->y * this->y));
} }


constexpr Vector2<T> sign() {
return Vector2<T>(x > 0 ? 1 : -1, y > 0 ? 1 : -1);
}

constexpr operator std::pair<T, T>() const { constexpr operator std::pair<T, T>() const {
return std::pair<T, T>(x, y); return std::pair<T, T>(x, y);
} }

+ 27
- 10
libswan/src/traits/BodyTrait.cc 파일 보기

on_ground_ = false; on_ground_ = false;


for (int x = (int)floor(bounds.left() + epsilon); x <= (int)floor(bounds.right() - epsilon); ++x) { for (int x = (int)floor(bounds.left() + epsilon); x <= (int)floor(bounds.right() - epsilon); ++x) {
int ty = (int)floor(bounds.top() + epsilon);
Tile &top = plane.getTile({ x, ty });
if (top.is_solid_) {
bounds.pos.y = (float)ty + 1.0;
collided = true;
break;
}

int by = (int)floor(bounds.bottom() - epsilon); int by = (int)floor(bounds.bottom() - epsilon);
Tile &bottom = plane.getTile({ x, by }); Tile &bottom = plane.getTile({ x, by });
if (bottom.is_solid_) { if (bottom.is_solid_) {
on_ground_ = true; on_ground_ = true;
break; break;
} }

int ty = (int)floor(bounds.top() + epsilon);
Tile &top = plane.getTile({ x, ty });
if (top.is_solid_) {
bounds.pos.y = (float)ty + 1.0;
collided = true;
break;
}
} }


if (collided) { if (collided) {
vel_ += (force_ / mass_) * dt; vel_ += (force_ / mass_) * dt;
force_ = { 0, 0 }; force_ = { 0, 0 };


pos_.x += vel_.x * dt;
Vec2 dist = vel_ * dt;
Vec2 dir = dist.sign();
Vec2 step = dir * 0.4;

// Move in increments of at most 'step', on the X axis
while (abs(dist.x) > abs(step.x)) {
pos_.x += step.x;
collideX(plane);
dist.x -= step.x;
}
pos_.x += dist.x;
collideX(plane); collideX(plane);
pos_.y += vel_.y * dt;

// Move in increments of at most 'step', on the Y axis
while (abs(dist.y) > abs(step.y)) {
pos_.y += step.y;
collideY(plane);
dist.y -= step.y;
}
pos_.y += dist.y;
collideY(plane); collideY(plane);
} }



Loading…
취소
저장