Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2017 08:19
What would you like to do?
Bullet API in dart
import 'dart:io';
import 'dart-ext:phys';
import 'dart:typed_data';
import 'package:vector_math/vector_math.dart';
class World {
int _p;
World() {
_p = _n_construct();
void setGravity(Vector3 vec) {
_n_set_gravity(_p, vec.x, vec.y, vec.z);
void addRigidBody(RigidBody r) {
_n_add_rigid_body(_p, r._p);
void step(double dt, [int substeps = 1]) {
_n_step(_p, dt, substeps);
int _n_construct() native "world_construct";
void _n_init_lazy(int p) native "world_init_lazy";
void _n_set_gravity(int p, double x, double y, double z) native "world_set_gravity";
void _n_add_rigid_body(int p, int pr) native "world_add_rigid_body";
void _n_step(int p, double dt, int substeps) native "world_step";
abstract class CollisionShape {
int _p;
Vector3 calculateInertia(double mass) {
List<double> out = new List<double>(3);
_n_calc_inertia(_p, mass, out);
return new Vector3(out[0], out[1], out[2]);
int _n_calc_inertia(int p, double mass, List<double> data) native "shape_calc_inertia";
class EmptyShape extends CollisionShape {
EmptyShape() {
_p = _n_construct();
int _n_construct() native "shape_construct";
class StaticPlaneShape extends CollisionShape {
StaticPlaneShape([Vector3 planeNormal, double planeConstant = 1.0]) {
planeNormal ??= new Vector3(0.0, 0.0, 0.0);
_p = _n_construct(planeNormal.x, planeNormal.y, planeNormal.z, planeConstant);
int _n_construct(double x, double y, double z, double pc) native "shape_static_plane_construct";
class SphereShape extends CollisionShape {
SphereShape(double radius) {
_p = _n_construct(radius);
int _n_construct(double rad) native "shape_sphere_construct";
class MotionState {
int _p;
MotionState([Vector3 pos, Quaternion angle]) {
pos ??= new Vector3(0.0, 0.0, 0.0);
angle ??= new Quaternion.identity();
_p = _n_construct(pos.x, pos.y, pos.z, angle.x, angle.y, angle.z, angle.w);
Vector3 get pos {
List<double> data = new List<double>(3);
_n_get_pos(_p, data);
return new Vector3(data[0], data[1], data[2]);
set pos(Vector3 v) {
_n_set_pos(_p, v.x, v.y, v.z);
Quaternion get angle {
List<double> data = new List<double>(4);
_n_get_angle(_p, data);
return new Quaternion(data[0], data[1], data[2], data[3]);
set angle(Quaternion v) {
_n_set_angle(_p, v.x, v.y, v.z, v.w);
void _n_get_pos(int p, List<double> data) native "motion_state_get_pos";
void _n_set_pos(int p, double x, double y, double z) native "motion_state_set_pos";
void _n_get_angle(int p, List<double> data) native "motion_state_get_angle";
void _n_set_angle(int p, double x, double y, double z, double w) native "motion_state_set_angle";
int _n_construct(double x, double y, double z, double qx, double qy, double qz, double qw) native "motion_state_construct";
class RigidBody {
MotionState motionState;
CollisionShape shape;
int _p;
RigidBody({double mass, this.motionState, this.shape, Vector3 inertia, double restitution, double friction}) {
restitution ??= 0.1;
friction ??= 0.5;
mass ??= 1.0;
motionState ??= new MotionState();
shape ??= new EmptyShape();
inertia ??= shape.calculateInertia(mass);
_p = _n_construct(mass, motionState._p, shape._p, inertia.x, inertia.y, inertia.z, restitution, friction);
int _n_construct(double mass, int pMotionState, int pCollisionShape, double x, double y, double z, double restitution, double friction) native "rigid_body_construct";
void main() {
var w = new World();
w.setGravity(new Vector3(0.0, -10.0, 0.0));
var planeShape = new StaticPlaneShape(new Vector3(0.0, 1.0, 0.0));
var planeBody = new RigidBody(shape: planeShape, mass: 0.0, restitution: 0.9);
var ballShape = new SphereShape(1.0);
var ballMotionState = new MotionState(new Vector3(0.0, 30.0, 0.0));
var ballBody = new RigidBody(shape: ballShape, motionState: ballMotionState, mass: 1.0, restitution: 0.5);
for (int i = 0; i < 300; i++) {
w.step(1 / 60, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment