Skip to main content

just_physics_engine 1.4.0 copy "just_physics_engine: ^1.4.0" to clipboard
just_physics_engine: ^1.4.0 copied to clipboard

Standalone 2D/3D physics engine for Flutter: 2D on native Box2D v3.2 (FFI) with a pure-Dart fallback, 3D on Jolt Physics through just_jolt.

example/example.dart

// An example prints its results.
// ignore_for_file: avoid_print

import 'package:just_dart/just_dart.dart';
import 'package:just_physics_engine/just_physics_engine.dart';

void main() {
  final engine = PhysicsEngine()..initialize();

  final floor = PhysicsBody(
    position: Vector2(320, 520),
    shape: RectangleShape(640, 40),
    mass: 0.0, // Static body.
    restitution: 0.2,
    friction: 0.8,
  );

  final ball = PhysicsBody(
    position: Vector2(320, 120),
    shape: CircleShape(20),
    restitution: 0.55,
    friction: 0.35,
    drag: 0.02,
  );

  engine.addBody(floor);
  engine.addBody(ball);

  // Simulate 2 seconds at 60 Hz.
  const dt = 1.0 / 60.0;
  const steps = 120;
  for (var i = 0; i < steps; i++) {
    engine.update(dt);
  }

  final stats = engine.stats;
  print(
    'Ball position: (${ball.position.x.toStringAsFixed(2)}, '
    '${ball.position.y.toStringAsFixed(2)})',
  );
  print(
    'Ball velocity: (${ball.velocity.x.toStringAsFixed(2)}, '
    '${ball.velocity.y.toStringAsFixed(2)})',
  );
  print('Backend: ${stats['backend'] ?? 'physics_2d'}');
  print('Bodies: ${engine.bodyCount}');

  engine.dispose();
}
3
likes
160
points
561
downloads

Documentation

API reference

Publisher

verified publisherjustunknown.com

Weekly Downloads

Standalone 2D/3D physics engine for Flutter: 2D on native Box2D v3.2 (FFI) with a pure-Dart fallback, 3D on Jolt Physics through just_jolt.

Repository (GitHub)
View/report issues
Contributing

Topics

#physics #game-engine #flutter #wasm

License

BSD-3-Clause (license)

Dependencies

ffi, flutter, just_box_2d, just_dart, just_jolt, meta

More

Packages that depend on just_physics_engine