Skip to content

Commit

Permalink
Merge branch 'main' into devkage/can-see-world-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Jul 21, 2023
2 parents 5bd0251 + 8e0a787 commit 2ee0282
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math' as math;

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/geometry.dart';
Expand Down Expand Up @@ -64,6 +66,8 @@ class StandardCollisionDetection<B extends Broadphase<ShapeHitbox>>

static final _temporaryRaycastResult = RaycastResult<ShapeHitbox>();

static final _temporaryRayAabb = Aabb2();

@override
RaycastResult<ShapeHitbox>? raycast(
Ray2 ray, {
Expand All @@ -72,17 +76,21 @@ class StandardCollisionDetection<B extends Broadphase<ShapeHitbox>>
RaycastResult<ShapeHitbox>? out,
}) {
var finalResult = out?..reset();
_updateRayAabb(ray, maxDistance);
for (final item in items) {
if (ignoreHitboxes?.contains(item) ?? false) {
continue;
}
if (!item.aabb.intersectsWithAabb2(_temporaryRayAabb)) {
continue;
}
final currentResult =
item.rayIntersection(ray, out: _temporaryRaycastResult);
final possiblyFirstResult = !(finalResult?.isActive ?? false);
if (currentResult != null &&
(possiblyFirstResult ||
currentResult.distance! < finalResult!.distance!) &&
(currentResult.distance! <= (maxDistance ?? double.infinity))) {
currentResult.distance! <= (maxDistance ?? double.infinity)) {
if (finalResult == null) {
finalResult = currentResult.clone();
} else {
Expand Down Expand Up @@ -172,4 +180,29 @@ class StandardCollisionDetection<B extends Broadphase<ShapeHitbox>>
}
}
}

/// Computes an axis-aligned bounding box for a [ray].
///
/// When [maxDistance] is provided, this will be the bounding box around
/// the origin of the ray and its ending point. When [maxDistance]
/// is `null`, the bounding box will encompass the whole quadrant
/// of space, from the ray's origin to infinity.
void _updateRayAabb(Ray2 ray, double? maxDistance) {
final x1 = ray.origin.x;
final y1 = ray.origin.y;
double x2;
double y2;

if (maxDistance != null) {
x2 = ray.origin.x + ray.direction.x * maxDistance;
y2 = ray.origin.y + ray.direction.y * maxDistance;
} else {
x2 = ray.direction.x > 0 ? double.infinity : double.negativeInfinity;
y2 = ray.direction.y > 0 ? double.infinity : double.negativeInfinity;
}

_temporaryRayAabb
..min.setValues(math.min(x1, x2), math.min(y1, y2))
..max.setValues(math.max(x1, x2), math.max(y1, y2));
}
}

0 comments on commit 2ee0282

Please sign in to comment.