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

fix: Fix collision detection comments and typo #1422

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions doc/flame/collision_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,26 @@ on top of components, see more regarding the latter in the section about the
[GestureHitboxes](inputs/gesture-input.md#GestureHitboxes) mixin.


### CollidableType
### CollisionType

The hitboxes have a field called `collidableType` which defines when a hitbox should collide with
another. Usually you want to set as many hitboxes as possible to `CollidableType.passive` to make
the collision detection more performant. By default the `CollidableType` is `active`.
The hitboxes have a field called `collisionType` which defines when a hitbox should collide with
another. Usually you want to set as many hitboxes as possible to `CollisionType.passive` to make
the collision detection more performant. By default the `CollisionType` is `active`.

The `CollidableType` enum contains the following values:
The `CollisionType` enum contains the following values:

- `active` collides with other `Collidable`s of type active or passive
- `passive` collides with other `Collidable`s of type active
- `inactive` will not collide with any other `Collidable`s

So if you have hitboxes that you don't need to check collisions against each other you can mark
them as passive by setting `collidableType = CollidableType.passive`, this could for example be
them as passive by setting `collisionType = CollisionType.passive`, this could for example be
ground components or maybe your enemies don't need to check collisions between each other, then they
could be marked as `passive` too.

Imagine a game where there are a lot of bullets, that can't collide with each other, flying towards
the player, then the player would be set to `CollidableType.active` and the bullets would be set to
`CollidableType.passive`.
the player, then the player would be set to `CollisionType.active` and the bullets would be set to
`CollisionType.passive`.

Then we have the `inactive` type which simply doesn't get checked at all in the collision detection.
This could be used for example if you have components outside of the screen that you don't care
Expand Down Expand Up @@ -267,6 +267,8 @@ Since the hitboxes now are `Component`s you add them to your component with `add
- `HitboxRectangle` -> `RectangleHitbox`
- `HitboxPolygon` -> `PolygonHitbox`
- `Collidable` -> `CollisionCallbacks` (Only needed when you want to receive the callbacks)
- `HasHitboxes` -> `GestureHitboxes` (Only when you need hitboxes for gestures)
- `CollidableType` -> `CollisionType`


## Examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AnimatedComponent extends SpriteAnimationComponent
final hitboxPaint = BasicPalette.white.paint()
..style = PaintingStyle.stroke;
add(
PolygonHitbox.relatve(
PolygonHitbox.relative(
[
Vector2(0.0, -1.0),
Vector2(-1.0, -0.1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class CollidablePolygon extends MyCollidable {
Vector2 velocity,
ScreenHitbox screenHitbox,
) : super(position, size, velocity, screenHitbox) {
hitbox = PolygonHitbox.relatve(
hitbox = PolygonHitbox.relative(
[
Vector2(-1.0, 0.0),
Vector2(-0.8, 0.6),
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/stories/input/gesture_hitboxes_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GestureHitboxesExample extends FlameGame
Vector2.random(_rng),
Vector2.random(_rng)..y *= -1,
];
return PolygonHitbox.relatve(points, parentSize: shapeSize);
return PolygonHitbox.relative(points, parentSize: shapeSize);
}
}();
return MyShapeComponent(
Expand Down
10 changes: 5 additions & 5 deletions packages/flame/lib/src/collisions/collision_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import 'package:meta/meta.dart';
import '../../collisions.dart';
import '../../components.dart';

/// The [CollidableType] is used to determine which other collidables that it
/// The [CollisionType] is used to determine which other hitboxes that it
/// should collide with.
enum CollidableType {
/// Collides with other collidables of type active or passive.
enum CollisionType {
/// Collides with other hitboxes of type active or passive.
active,

/// Collides with other collidables of type active.
/// Collides with other hitboxes of type active.
passive,

/// Will not collide with any other collidables.
/// Will not collide with any other hitboxes.
inactive,
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/collisions/collision_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class CollisionDetection<T extends Hitbox<T>> {

/// Removes the [item] from the collision detection, if you just want
/// to temporarily inactivate it you can set
/// `collidableType = CollidableType.inactive;` instead.
/// `collisionType = CollisionType.inactive;` instead.
void remove(T item) => items.remove(item);

/// Removes all [items] from the collision detection, see [remove].
Expand Down
8 changes: 4 additions & 4 deletions packages/flame/lib/src/collisions/hitboxes/hitbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import '../../../extensions.dart';
abstract class Hitbox<T extends Hitbox<T>>
implements GenericCollisionCallbacks<T> {
/// Whether the hitbox should:
/// * [CollidableType.active] - actively collide with other hitboxes.
/// * [CollidableType.passive] - passively collide with other hitboxes (only
/// * [CollisionType.active] - actively collide with other hitboxes.
/// * [CollisionType.passive] - passively collide with other hitboxes (only
/// collide with hitboxes that are active, but not other passive ones).
/// * [CollidableType.inactive] - not collide with any other hitboxes.
CollidableType get collidableType;
/// * [CollisionType.inactive] - not collide with any other hitboxes.
CollisionType get collisionType;

/// The axis-aligned bounding box of the [Hitbox], this is used to make a
/// rough estimation of whether two hitboxes can possibly collide or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PolygonHitbox extends PolygonComponent with ShapeHitbox {
/// This will form a diamond shape within the bounding size box.
/// NOTE: Always define your shape in a counter-clockwise fashion (in the
/// screen coordinate system)
PolygonHitbox.relatve(
PolygonHitbox.relative(
List<Vector2> relation, {
Vector2? position,
required Vector2 parentSize,
Expand Down
7 changes: 6 additions & 1 deletion packages/flame/lib/src/collisions/hitboxes/shape_hitbox.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:meta/meta.dart';

import '../../../collisions.dart';
Expand All @@ -10,7 +12,10 @@ import '../../geometry/shape_intersections.dart' as intersection_system;
/// [PolygonHitbox].
mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
@override
CollidableType collidableType = CollidableType.active;
CollisionType collisionType = CollisionType.active;

@override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional?

Copy link
Member Author

@spydon spydon Mar 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To override the paint? Yes, because otherwise the hitboxes just became big blocks when they were rendered in debugMode. I guess it could override the debug render method too, I'll have a think about it.

Paint get paint => debugPaint;

/// Whether the hitbox is allowed to collide with another hitbox that is
/// added to the same parent.
Expand Down
6 changes: 3 additions & 3 deletions packages/flame/lib/src/collisions/sweep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Sweep<T extends Hitbox<T>> extends Broadphase<T> {
_potentials.clear();
items.sort((a, b) => (a.aabb.min.x - b.aabb.min.x).ceil());
for (final item in items) {
if (item.collidableType == CollidableType.inactive) {
if (item.collisionType == CollisionType.inactive) {
continue;
}
if (_active.isEmpty) {
Expand All @@ -27,8 +27,8 @@ class Sweep<T extends Hitbox<T>> extends Broadphase<T> {
final activeItem = _active[i];
final activeBox = activeItem.aabb;
if (activeBox.max.x >= currentMin) {
if (item.collidableType == CollidableType.active ||
activeItem.collidableType == CollidableType.active) {
if (item.collisionType == CollisionType.active ||
activeItem.collisionType == CollisionType.active) {
_potentials.add(CollisionProspect<T>(item, activeItem));
}
} else {
Expand Down
32 changes: 16 additions & 16 deletions packages/flame/test/collision_detection/collidable_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -48,12 +48,12 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -69,7 +69,7 @@ void main() {
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -83,7 +83,7 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
final blockB = TestBlock(
Vector2.all(1),
Expand All @@ -102,12 +102,12 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -119,12 +119,12 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.passive,
type: CollisionType.passive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -140,7 +140,7 @@ void main() {
final blockB = TestBlock(
Vector2.all(1),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
await game.ensureAddAll([blockA, blockB]);
game.update(0);
Expand All @@ -152,7 +152,7 @@ void main() {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
type: CollidableType.inactive,
type: CollisionType.inactive,
);
final blockB = TestBlock(
Vector2.all(1),
Expand All @@ -168,7 +168,7 @@ void main() {
'correct collisions with many involved collidables',
(game) async {
final rng = Random(0);
List<TestBlock> generateBlocks(CollidableType type) {
List<TestBlock> generateBlocks(CollisionType type) {
return List.generate(
100,
(_) => TestBlock(
Expand All @@ -179,9 +179,9 @@ void main() {
);
}

final actives = generateBlocks(CollidableType.active);
final passives = generateBlocks(CollidableType.passive);
final inactives = generateBlocks(CollidableType.inactive);
final actives = generateBlocks(CollisionType.active);
final passives = generateBlocks(CollisionType.passive);
final inactives = generateBlocks(CollisionType.inactive);
await game.ensureAddAll((actives + passives + inactives)..shuffle());
game.update(0);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestBlock extends PositionComponent with CollisionCallbacks {
TestBlock(
Vector2 position,
Vector2 size, {
CollidableType type = CollidableType.active,
CollisionType type = CollisionType.active,
bool addTestHitbox = true,
this.name,
}) : super(
Expand All @@ -44,7 +44,7 @@ class TestBlock extends PositionComponent with CollisionCallbacks {
) {
children.register<ShapeHitbox>();
if (addTestHitbox) {
add(hitbox..collidableType = type);
add(hitbox..collisionType = type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EnemyComponent extends SpriteAnimationComponent

EnemyComponent(double x, double y)
: super(position: Vector2(x, y), size: Vector2.all(25)) {
add(RectangleHitbox()..collidableType = CollidableType.passive);
add(RectangleHitbox()..collisionType = CollisionType.passive);
}

@override
Expand Down