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!: gravity getter and setter #66

Merged
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
25 changes: 20 additions & 5 deletions packages/forge2d/lib/src/dynamics/world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@ class World {
final List<Joint> joints = <Joint>[];

final Vector2 _gravity;

/// The current [World]'s [gravity].
///
/// {@template World.gravity}
/// All [bodies] are affected by [gravity]; unless the [Body] has a specified
/// [Body.gravityOverride]. If so, [Body.gravityOverride] is used instead of
/// [gravity].
///
/// See also:
///
/// * [Body.gravityScale], to multipy [gravity] for a [Body].
/// * [Body.gravityOverride], to change how the world treats the gravity for
/// a [Body].
/// {@endtemplate}
Vector2 get gravity => _gravity;

/// Changes the [World]'s gravity.
///
/// {@macro World.gravity}
set gravity(Vector2 gravity) => this.gravity.setFrom(gravity);
spydon marked this conversation as resolved.
Show resolved Hide resolved

bool _allowSleep = false;

DestroyListener? destroyListener;
Expand Down Expand Up @@ -477,11 +497,6 @@ class World {
/// Gets the quality of the dynamic tree
double getTreeQuality() => contactManager.broadPhase.getTreeQuality();

/// Change the global gravity vector.
void setGravity(Vector2 gravity) {
_gravity.setFrom(gravity);
}

/// Is the world locked (in the middle of a time step).
bool get isLocked => (flags & locked) == locked;

Expand Down
11 changes: 11 additions & 0 deletions packages/forge2d/test/dynamics/world_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ class AnotherRevoluteJoint extends RevoluteJoint {
}

void main() {
group('gravity', () {
test('can change', () {
final world = World();

final newGravity = world.gravity.clone()..x += 1;
world.gravity = newGravity;

expect(world.gravity, equals(newGravity));
});
});

group(
'createJoint',
() {
Expand Down