For Flame_Forge2D there could exist a mixin that helps users to actually see the Ray2
in the screen.
This could help people debug HitBox
es easier.
I don't believe that anything is a risk here.
Here is an example using the code below.
Here is an example of what could be written (copied from raycast_example.dart):
mixin RayCastToMouse on Forge2DGame implements RayCast, MouseMovementDetector {
late Vector2 _direction = Vector2(1, 0);
@override
void onMouseMove(PointerHoverInfo info) {
final vector = info.eventPosition.game;
final tempDirection = Vector2(
vector.x - rayStartPosition.x,
vector.y - rayStartPosition.y,
);
_direction = Vector2(
tempDirection.x / tempDirection.length.abs(),
tempDirection.y / tempDirection.length.abs(),
);
}
@override
Vector2 get rayDirection => _direction;
}
mixin RayCast on Forge2DGame implements HasCollisionDetection {
final _points = <Vector2>[];
@override
void update(double dt) {
super.update(dt);
final ray = Ray2(
origin: Vector2(rayStartPosition.x, rayStartPosition.y),
direction: rayDirection,
);
final result = collisionDetection.raycast(ray);
if ((result != null) && (result.intersectionPoint != null)) {
_fireRay(ray.origin, result.intersectionPoint!);
}
}
@override
void render(Canvas canvas) {
super.render(canvas);
for (int i = 0; i < _points.length - 1; i++) {
canvas.drawLine(
_points[i].toOffset(),
_points[i + 1].toOffset(),
Paint()
..color = Colors.red
..strokeWidth = 4,
);
}
}
void _fireRay(
Vector2 rayStart,
Vector2 rayTarget,
) {
_points.clear();
_points.add(worldToScreen(rayStart));
final nearestCallback = _NearestBoxRayCastCallback();
world.raycast(nearestCallback, rayStart, rayTarget);
if (nearestCallback.nearestPoint != null) {
_points.add(worldToScreen(nearestCallback.nearestPoint!));
} else {
_points.add(worldToScreen(rayTarget));
}
}
Vector2 get rayDirection => Vector2(1, 0);
Vector2 get rayStartPosition;
}
class _NearestBoxRayCastCallback extends RayCastCallback {
Vector2? nearestPoint;
double nearestFraction = 0;
@override
double reportFixture(
Fixture fixture,
Vector2 point,
Vector2 normal,
double fraction,
) {
if ((nearestPoint == null) || (fraction < nearestFraction)) {
nearestPoint = point;
nearestFraction = fraction;
}
return 1;
}
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too