Skip to content

Commit

Permalink
refactor!: isBullet getter and setter (#64)
Browse files Browse the repository at this point in the history
Removes `setBullet` and `isBullet` in favour of setter and getters.

```dart
// Before:
body.setBullet(true);
body.isBullet(); // true

// After:
body.isBullet = true;
body.isBullet; // true
```
  • Loading branch information
alestiago committed Oct 30, 2023
1 parent 8e1b6bb commit ee5d4bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/forge2d/example/web/circle_stress.dart
Expand Up @@ -122,7 +122,7 @@ class CircleStress extends Demo {
body.createFixture(fixtureDef);
}

body.setBullet(false);
body.isBullet = false;

// Create an empty ground body.
final bodyDef = BodyDef();
Expand Down
23 changes: 17 additions & 6 deletions packages/forge2d/lib/src/dynamics/body.dart
Expand Up @@ -598,21 +598,32 @@ class Body {
}
}

/// Is this body treated like a bullet for continuous collision detection?
bool isBullet() {
return (flags & bulletFlag) == bulletFlag;
}

/// {@template isBullet}
/// Whether this body should be treated like a bullet for continuous collision
/// detection.
void setBullet(bool flag) {
///
/// Fast moving dynamic bodies should be labeled as bullets so that they
/// do not tunnel through other moving objects.
///
/// **Warning:** You should use this flag sparingly since it increases
/// processing time.
///
/// See also:
///
/// * [Box2D bullets documentation](https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_dynamics.html)
/// * [Box2D bullet member data definition](https://box2d.org/documentation/structb2_body_def.html#a7c0047c9a98a1d20614eeddcdbce7586)
/// {@endtemplate}
set isBullet(bool flag) {
if (flag) {
flags |= bulletFlag;
} else {
flags &= ~bulletFlag;
}
}

/// {@macro isBullet}
bool get isBullet => (flags & bulletFlag) == bulletFlag;

/// You can disable sleeping on this body. If you disable sleeping, the body
/// will be woken.
void setSleepingAllowed(bool flag) {
Expand Down
8 changes: 4 additions & 4 deletions packages/forge2d/lib/src/dynamics/world.dart
Expand Up @@ -723,8 +723,8 @@ class World {
continue;
}

final collideA = bodyA.isBullet() || typeA != BodyType.dynamic;
final collideB = bodyB.isBullet() || typeB != BodyType.dynamic;
final collideA = bodyA.isBullet || typeA != BodyType.dynamic;
final collideB = bodyB.isBullet || typeB != BodyType.dynamic;

// Are these two non-bullet dynamic bodies?
if (collideA == false && collideB == false) {
Expand Down Expand Up @@ -838,8 +838,8 @@ class World {
// Only add static, kinematic, or bullet bodies.
final other = contact.getOtherBody(body);
if (other.bodyType == BodyType.dynamic &&
body.isBullet() == false &&
other.isBullet() == false) {
!body.isBullet &&
!other.isBullet) {
continue;
}

Expand Down
17 changes: 17 additions & 0 deletions packages/forge2d/test/dynamics/body_test.dart
Expand Up @@ -4,6 +4,23 @@ import 'package:test/scaffolding.dart';

void main() {
group('Body', () {
group('isBullet', () {
test('is false by default', () {
final body = Body(BodyDef(), World());
expect(body.isBullet, equals(false));
});

test('can change', () {
final body = Body(BodyDef(), World());
const newBulletValue = true;
expect(body.isBullet, isNot(equals(newBulletValue)));

body.isBullet = newBulletValue;

expect(body.isBullet, equals(newBulletValue));
});
});

group('gravityOverride', () {
test("body doesn't move when is zero", () {
final gravity = Vector2(10, 10);
Expand Down

0 comments on commit ee5d4bf

Please sign in to comment.