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

refactor!: bullet getter and setter #64

Merged
merged 10 commits into from
Oct 30, 2023
2 changes: 1 addition & 1 deletion packages/forge2d/example/web/circle_stress.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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