flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.23k stars 902 forks source link

PolygonHitbox is not working properly #2827

Closed WilkoThomassen closed 12 months ago

WilkoThomassen commented 12 months ago

Current bug behavior

When a hitbox is defined, the onHoverEnter will not be fired based on the coordinates of the polygon

HoverEnter is fired but it does not follow the path of the polygon hitbox.

Test sample below on web or macOS app and it will be visible when you hover the polygon.

Expected behavior

When the mousepointer enters the rendered shape, onHoverEnter should be fired

Steps to reproduce

import 'dart:async';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    GameWidget(
      game: MyGame(),
    ),
  );
}

class MyGame extends FlameGame {
  @override
  FutureOr<void> onLoad() {
    add(HoverablePolygon());
    return super.onLoad();
  }
}

class HoverablePolygon extends PositionComponent with HoverCallbacks, GestureHitboxes {
  late ShapeHitbox hitbox;
  @override
  Future<void> onLoad() async {
    super.onLoad();

    final points = [
      Vector2(0.9, 0.1),
      Vector2(0.2, -0.8),
      Vector2(-0.7, 0.2),
      Vector2(0.1, 0.6),
    ];
    hitbox = PolygonHitbox.relative(points, parentSize: Vector2(300, 300));
    hitbox.paint.color = Colors.red;
    hitbox.renderShape = true;
    add(hitbox);
  }

  @override
  void onHoverEnter() {
    hitbox.paint.color = Colors.green;
  }

  @override
  void onHoverExit() {
    hitbox.paint.color = Colors.red;
  }
}

Flutter doctor output

[✓] Flutter (Channel stable, 3.13.6, on macOS 14.0 23A344 darwin-arm64, locale en-NL)
    • Flutter version 3.13.6 on channel stable at /Users/wilkothomassen/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ead455963c (4 weeks ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/wilkothomassen/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A240d
    • CocoaPods version 1.13.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.83.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554             • android-arm64  • Android 13 (API 33) (emulator)
    • Wilko’s iPhone (mobile)     • 00008130-00024C8C2E40001C • ios            • iOS 17.0.3 21A360
    • macOS (desktop)             • macos                     • darwin-arm64   • macOS 14.0 23A344 darwin-arm64
    • Chrome (web)                • chrome                    • web-javascript • Google Chrome 118.0.5993.88

[✓] Network resources
    • All expected network resources are available.

• No issues found!

More environment information

More information

spydon commented 12 months ago

Duplicate of https://github.com/flame-engine/flame/issues/2758, thanks for the well written issue though! Hopefully I'll have time to look into it after the Flutter LatAm conference.

WilkoThomassen commented 12 months ago

I'm sorry and thank you!