Files
flame/test/components/position_component_test.dart
Lukas Klingsbo bde4585fa0 Collision detection (#633)
* Move out collision detection methods

* Add possibility to define a hull for PositionComponents

* Add example of how to use hull with tapable

* Update contains point comment

* Fix contains point

* Hull should be based on center position

* Remove collision detection parts

* Use percentage of size instead of absolute size

* Separate hull from PositionComponent

* Clarify hull example

* Fix formatting

* Change to relative import

* Use mixin for hitbox

* Update changelog

* Rename HasHitbox to Hitbox

* Clarified names

* Add spaces within braces

* Removed extra spaces in the braces

* Moved point rotation to Vector2 extension

* Render hitbox within extension

* Added collision detection

* Add tests

* Separate classes into files

* Fix formatting

* Move geometry files into geometry directory

* Use relative import for mixin

* Begin intersections between different shapes

* Add shape class

* Align with rebase

* Fix CHANGELOG

* Fix children positioning

* New polygon intersection algorithm

* No anchor for shape in PoC

* Remove unused imports

* Smarter bounding rectangle comparisons

* Formatting

* Add Circle to Circle collision

* Circle-polygon intersections

* Explanation of circle-circle intersections

* Properly render circle circle collisions

* Fix formatting

* Better example

* Update docs for collision detection

* Fix formatting

* Add polygon definition example

* Update documentation about the shapes

* Moved premature rc6 changelog line

* Added a cache system for shape calculations

* Fix formatting

* Fix formatting

* Fix imports

* Add collidable polygon to example

* Use anchorPosition for PositionComponent containsPoint

* Fix angle problem for Rectangle

* collisionCallback -> onCollision

* Fixed Erick's comments

* Improve collision detection example

* Fix #662, zero size doesn't contain any points

* Fix formatting

* Can't contain point if x or y is 0

* Fix formatting

* Fix test

* Remove unnecessary collidable example part

* Align with Draggable overhaul

* Updated collision detection docs

* Fix PR comments

* Have more sensible Circle constructor

* Clarify shape fields

* Need ensureInitialized

* Update docs to conform with switched constructors

* Fix new definitions

* Fix formatting

* Update documentation

* Fix formatting

* Fix formatting

* Exclude metrics check for test files

* Add another simpler example of collision detection

* Updated according to comments

* Fix comments

* Fix more comments

* Fix more comments

* Fix relative import

* Fix comments

* Moved export of geometry

* Fix comments

* Remove unused import

* Fix assert for shape.component

* Fix comments

* Expect instead of assert in test
2021-02-22 00:44:11 +01:00

137 lines
4.2 KiB
Dart

import 'dart:math' as math;
import 'package:flame/components.dart';
import 'package:flame/src/geometry/polygon.dart';
import 'package:test/test.dart';
class MyComponent extends PositionComponent {}
class MyHitboxComponent extends PositionComponent with Hitbox {}
void main() {
group('PositionComponent overlap test', () {
test('overlap', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(4.0, 4.0);
component.angle = 0.0;
component.anchor = Anchor.center;
final point = Vector2(2.0, 2.0);
expect(component.containsPoint(point), true);
});
test('overlap on edge', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(2.0, 2.0);
component.angle = 0.0;
component.anchor = Anchor.center;
final point = Vector2(1.0, 1.0);
expect(component.containsPoint(point), true);
});
test('not overlapping with x', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(2.0, 2.0);
component.angle = 0.0;
component.anchor = Anchor.center;
final point = Vector2(4.0, 1.0);
expect(component.containsPoint(point), false);
});
test('not overlapping with y', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(2.0, 2.0);
component.angle = 0.0;
component.anchor = Anchor.center;
final point = Vector2(1.0, 4.0);
expect(component.containsPoint(point), false);
});
test('overlapping with angle', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(2.0, 2.0);
component.angle = math.pi / 4;
component.anchor = Anchor.center;
final point = Vector2(3.1, 2.0);
expect(component.containsPoint(point), true);
});
test('not overlapping with angle', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(2.0, 2.0);
component.angle = math.pi / 4;
component.anchor = Anchor.center;
final point = Vector2(1.0, 0.1);
expect(component.containsPoint(point), false);
});
test('overlapping with angle and topLeft anchor', () {
final PositionComponent component = MyComponent();
component.position = Vector2(1.0, 1.0);
component.size = Vector2(2.0, 2.0);
component.angle = math.pi / 4;
component.anchor = Anchor.topLeft;
final point = Vector2(1.0, 3.1);
expect(component.containsPoint(point), true);
});
test('component with hitbox contains point', () {
final size = Vector2(2.0, 2.0);
final Hitbox component = MyHitboxComponent();
component.position = Vector2(1.0, 1.0);
component.anchor = Anchor.topLeft;
component.size = size;
final hitbox = HitboxPolygon([
Vector2(1, 0),
Vector2(0, -1),
Vector2(-1, 0),
Vector2(0, 1),
]);
component.addShape(hitbox);
final point = component.position + component.size / 4;
expect(component.containsPoint(point), true);
});
test('component with hitbox does not contains point', () {
final size = Vector2(2.0, 2.0);
final Hitbox component = MyHitboxComponent();
component.position = Vector2(1.0, 1.0);
component.anchor = Anchor.topLeft;
component.size = size;
component.addShape(HitboxPolygon([
Vector2(1, 0),
Vector2(0, -1),
Vector2(-1, 0),
Vector2(0, 1),
]));
final point = Vector2(1.1, 1.1);
expect(component.containsPoint(point), false);
});
test('component with zero size does not contain point', () {
final PositionComponent component = MyComponent();
component.position = Vector2(2.0, 2.0);
component.size = Vector2(0.0, 0.0);
component.angle = 0.0;
component.anchor = Anchor.center;
final point = Vector2(2.0, 2.0);
expect(component.containsPoint(point), false);
});
});
}