Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Possibility to ignore hitboxes for ray casting #1863

Merged
merged 3 commits into from
Aug 24, 2022

Conversation

spydon
Copy link
Member

@spydon spydon commented Aug 22, 2022

Description

A lot of the time it is useful to be able to ignore certain hitboxes, if you are for example casting rays from within the hitbox of one of your components. This PR adds this functionality to raycast, raycastAll and raytrace.

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs: etc).
  • I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples.

Breaking Change

  • No, this is not a breaking change.

Related Issues

@ufrshubham
Copy link
Member

Did some quick testing and found this to be working as expect. But also found an issues when using CircleHitboxes. It seems that if I cast a ray from inside a circular hitbox, it automatically ignores that hitbox. Here is an example code:

class RayCastGame extends FlameGame with HasCollisionDetection {
  RaycastResult<ShapeHitbox>? result;
  late ShapeHitbox shapeHitbox1;
  late ShapeHitbox shapeHitbox2;

  @override
  Future<void>? onLoad() {
    debugMode = true;
    add(
      CircleComponent(
        radius: 20,
        position: size / 2,
        anchor: Anchor.center,
        paint: Paint()..color = Colors.red,
      )..add(
          shapeHitbox1 = CircleHitbox(), // Left 
          //shapeHitbox1 = RectangleHitbox(), // Right
        ),
    );

    add(
      CircleComponent(
        radius: 20,
        position: Vector2(size.x / 2, 30),
        anchor: Anchor.center,
        paint: Paint()..color = Colors.blue,
      )..add(
          shapeHitbox2 = CircleHitbox(),
        ),
    );

    return super.onLoad();
  }

  @override
  void update(double dt) {
    final ray = Ray2(
      origin: size / 2,
      direction: Vector2(0, -1),
    );

    result = collisionDetection.raycast(
      ray,
    );

    super.update(dt);
  }

  @override
  void render(Canvas canvas) {
    if (result != null) {
      canvas.drawLine(
        (size / 2).toOffset(),
        (result!.intersectionPoint)!.toOffset(),
        Paint()
          ..color = Colors.yellow
          ..strokeWidth = 5,
      );
    }
    super.render(canvas);
  }
}

And here are the 2 outputs when using hitbox for red circle as CircleHitbox and RectangleHitbox. Left one uses a circular hitbox and right one uses rectangular hitbox. For some reason the ray cast passes through the hitbox of red circle in first example.

image

Let me know if this should be reported as a separate issue. Hope that I am not missing something very obvious.

@spydon
Copy link
Member Author

spydon commented Aug 23, 2022

Thanks @ufrshubham for checking! This behaviour seems unrelated to this PR, maybe you could just copy your comment and open an issue with it?
Strange that this faulty behaviour doesn't present itself in the examples on https//examples.flame-engine.org...

@spydon spydon changed the title feat: Possibility to ignore hitboxes feat: Possibility to ignore hitboxes for ray casting Aug 23, 2022
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
packages/flame/lib/src/collisions/collision_detection.dart Outdated Show resolved Hide resolved
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
@spydon spydon enabled auto-merge (squash) August 24, 2022 07:50
@spydon spydon merged commit b22bc64 into main Aug 24, 2022
@spydon spydon deleted the spydon/ignore-hitboxes branch August 24, 2022 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants