physics_body.dart 9.21 KB
Newer Older
1
part of flutter_sprites;
2 3 4 5 6 7 8 9 10 11

enum PhysicsBodyType {
    static,
    dynamic
}

class PhysicsBody {
  PhysicsBody(this.shape, {
    this.tag: null,
    this.type: PhysicsBodyType.dynamic,
12 13 14 15
    double density: 1.0,
    double friction: 0.0,
    double restitution: 0.0,
    bool isSensor: false,
16 17
    Offset linearVelocity: Offset.zero,
    double angularVelocity: 0.0,
18
    this.linearDampening: 0.0,
19
    double angularDampening: 0.0,
20 21 22 23 24
    bool allowSleep: true,
    bool awake: true,
    bool fixedRotation: false,
    bool bullet: false,
    bool active: true,
25
    this.gravityScale: 1.0
26 27 28 29 30
  }) {
    this.density = density;
    this.friction = friction;
    this.restitution = restitution;
    this.isSensor = isSensor;
31 32 33 34

    this.linearVelocity = linearVelocity;
    this.angularVelocity = angularVelocity;
    this.angularDampening = angularDampening;
35 36 37 38 39 40

    this.allowSleep = allowSleep;
    this.awake = awake;
    this.fixedRotation = fixedRotation;
    this.bullet = bullet;
    this.active = active;
41
  }
42

43 44
  Vector2 _lastPosition;
  double _lastRotation;
45 46
  Vector2 _targetPosition;
  double _targetAngle;
47

48 49
  double _scale;

50 51
  Object tag;

52
  final PhysicsShape shape;
53 54 55

  PhysicsBodyType type;

56 57 58 59 60 61 62 63 64
  double _density;

  double get density => _density;

  set density(double density) {
    _density = density;

    if (_body == null)
      return;
65
    for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) {
66 67 68 69 70 71 72 73 74 75 76 77 78
      f.setDensity(density);
    }
  }

  double _friction;

  double get friction => _friction;

  set friction(double friction) {
    _friction = friction;

    if (_body == null)
      return;
79
    for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) {
80 81 82 83 84 85 86 87 88 89 90 91 92
      f.setFriction(friction);
    }
  }

  double _restitution;

  double get restitution => _restitution;

  set restitution(double restitution) {
    _restitution = restitution;

    if (_body == null)
      return;
93
    for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) {
94 95 96 97 98 99 100 101 102 103 104 105 106
      f.setRestitution(restitution);
    }
  }

  bool _isSensor;

  bool get isSensor => _isSensor;

  set isSensor(bool isSensor) {
    _isSensor = isSensor;

    if (_body == null)
      return;
107
    for (box2d.Fixture f = _body.getFixtureList(); f != null; f = f.getNext()) {
108 109 110
      f.setSensor(isSensor);
    }
  }
111

112 113 114 115 116 117 118 119 120 121 122
  Offset _linearVelocity;

  Offset get linearVelocity {
    if (_body == null)
      return _linearVelocity;
    else {
      double dx = _body.linearVelocity.x * _physicsNode.b2WorldToNodeConversionFactor;
      double dy = _body.linearVelocity.y * _physicsNode.b2WorldToNodeConversionFactor;
      return new Offset(dx, dy);
    }
  }
123

124 125
  set linearVelocity(Offset linearVelocity) {
    _linearVelocity = linearVelocity;
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    if (_body != null) {
      Vector2 vec = new Vector2(
        linearVelocity.dx / _physicsNode.b2WorldToNodeConversionFactor,
        linearVelocity.dy / _physicsNode.b2WorldToNodeConversionFactor
      );
      _body.linearVelocity = vec;
    }
  }

  double _angularVelocity;

  double get angularVelocity {
    if (_body == null)
      return _angularVelocity;
    else
      return _body.angularVelocity;
  }

  set angularVelocity(double angularVelocity) {
    _angularVelocity = angularVelocity;

    if (_body != null) {
      _body.angularVelocity = angularVelocity;
    }
  }

  // TODO: Should this be editable in box2d.Body ?
  final double linearDampening;

  double _angularDampening;

  double get angularDampening => _angularDampening;

  set angularDampening(double angularDampening) {
    _angularDampening = angularDampening;

    if (_body != null)
      _body.angularDamping = angularDampening;
  }
166

167 168 169 170 171 172
  bool _allowSleep;

  bool get allowSleep => _allowSleep;

  set allowSleep(bool allowSleep) {
    _allowSleep = allowSleep;
173

174 175 176
    if (_body != null)
      _body.setSleepingAllowed(allowSleep);
  }
177

178 179 180 181 182 183 184 185
  bool _awake;

  bool get awake {
    if (_body != null)
      return _body.isAwake();
    else
      return _awake;
  }
186

187 188
  set awake(bool awake) {
    _awake = awake;
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    if (_body != null)
      _body.setAwake(awake);
  }

  bool _fixedRotation;

  bool get fixedRotation => _fixedRotation;

  set fixedRotation(bool fixedRotation) {
    _fixedRotation = fixedRotation;

    if (_body != null)
      _body.setFixedRotation(fixedRotation);
  }

  bool _bullet;

  bool get bullet => _bullet;

  set bullet(bool bullet) {
    _bullet = bullet;

    if (_body != null) {
      _body.setBullet(bullet);
    }
  }

  bool _active;

  bool get active {
    if (_body != null)
      return _body.isActive();
    else
      return _active;
  }

  set active(bool active) {
    _active = active;

    if (_body != null)
      _body.setActive(active);
  }
232 233 234

  double gravityScale;

235
  PhysicsWorld _physicsNode;
236 237 238 239
  Node _node;

  box2d.Body _body;

240 241
  List<PhysicsJoint> _joints = [];

242 243
  bool _attached = false;

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  void applyForce(Offset force, Point worldPoint) {
    assert(_body != null);

    Vector2 b2Force = new Vector2(
      force.dx / _physicsNode.b2WorldToNodeConversionFactor,
      force.dy / _physicsNode.b2WorldToNodeConversionFactor);

    Vector2 b2Point = new Vector2(
      worldPoint.x / _physicsNode.b2WorldToNodeConversionFactor,
      worldPoint.y / _physicsNode.b2WorldToNodeConversionFactor
    );

    _body.applyForce(b2Force, b2Point);
  }

  void applyForceToCenter(Offset force) {
    assert(_body != null);

    Vector2 b2Force = new Vector2(
      force.dx / _physicsNode.b2WorldToNodeConversionFactor,
      force.dy / _physicsNode.b2WorldToNodeConversionFactor);

    _body.applyForceToCenter(b2Force);
  }

  void applyTorque(double torque) {
    assert(_body != null);

    _body.applyTorque(torque / _physicsNode.b2WorldToNodeConversionFactor);
  }

  void applyLinearImpulse(Offset impulse, Point worldPoint, [bool wake = true]) {
    assert(_body != null);

    Vector2 b2Impulse = new Vector2(
      impulse.dx / _physicsNode.b2WorldToNodeConversionFactor,
      impulse.dy / _physicsNode.b2WorldToNodeConversionFactor);

    Vector2 b2Point = new Vector2(
      worldPoint.x / _physicsNode.b2WorldToNodeConversionFactor,
      worldPoint.y / _physicsNode.b2WorldToNodeConversionFactor
    );

    _body.applyLinearImpulse(b2Impulse, b2Point, wake);
  }

  void applyAngularImpulse(double impulse) {
    assert(_body != null);

    _body.applyAngularImpulse(impulse / _physicsNode.b2WorldToNodeConversionFactor);
  }

296
  void _attach(PhysicsWorld physicsNode, Node node) {
297 298
    assert(_attached == false);

299 300 301
    // Update scale
    _scale = node.scale;

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    // Create BodyDef
    box2d.BodyDef bodyDef = new box2d.BodyDef();
    bodyDef.linearVelocity = new Vector2(linearVelocity.dx, linearVelocity.dy);
    bodyDef.angularVelocity = angularVelocity;
    bodyDef.linearDamping = linearDampening;
    bodyDef.angularDamping = angularDampening;
    bodyDef.allowSleep = allowSleep;
    bodyDef.awake = awake;
    bodyDef.fixedRotation = fixedRotation;
    bodyDef.bullet = bullet;
    bodyDef.active = active;
    bodyDef.gravityScale = gravityScale;
    if (type == PhysicsBodyType.dynamic)
      bodyDef.type = box2d.BodyType.DYNAMIC;
    else
      bodyDef.type = box2d.BodyType.STATIC;

    double conv = physicsNode.b2WorldToNodeConversionFactor;
    bodyDef.position = new Vector2(node.position.x / conv, node.position.y / conv);
    bodyDef.angle = radians(node.rotation);

    // Create Body
    _body = physicsNode.b2World.createBody(bodyDef);

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    _createFixtures(physicsNode);

    _body.userData = this;

    _physicsNode = physicsNode;
    _node = node;

    _attached = true;

    // Attach any joints
    for (PhysicsJoint joint in _joints) {
      if (joint.bodyA._attached && joint.bodyB._attached) {
        joint._attach(physicsNode);
      }
    }
  }

343
  void _createFixtures(PhysicsWorld physicsNode) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    // Create FixtureDef
    box2d.FixtureDef fixtureDef = new box2d.FixtureDef();
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;
    fixtureDef.density = density;
    fixtureDef.isSensor = isSensor;

    // Get shapes
    List<box2d.Shape> b2Shapes = [];
    List<PhysicsShape> physicsShapes = [];
    _addB2Shapes(physicsNode, shape, b2Shapes, physicsShapes);

    // Create fixtures
    for (int i = 0; i < b2Shapes.length; i++) {
      box2d.Shape b2Shape = b2Shapes[i];
      PhysicsShape physicsShape = physicsShapes[i];

      fixtureDef.shape = b2Shape;
      box2d.Fixture fixture = _body.createFixtureFromFixtureDef(fixtureDef);
      fixture.userData = physicsShape;
    }
  }

  void _detach() {
    if (_attached) {
369
      _physicsNode._bodiesScheduledForDestruction.add(_body);
370 371 372 373
      _attached = false;
    }
  }

374
  void _updateScale(PhysicsWorld physicsNode) {
375 376 377 378 379 380 381 382 383 384 385 386
    // Destroy old fixtures
    for (box2d.Fixture fixture = _body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
      _body.destroyFixture(fixture);
    }

    // Make sure we create new b2Shapes
    shape._invalidate();

    // Create new fixtures
    _createFixtures(physicsNode);
  }

387
  void _addB2Shapes(PhysicsWorld physicsNode, PhysicsShape shape, List<box2d.Shape> b2Shapes, List<PhysicsShape> physicsShapes) {
388 389 390 391 392
    if (shape is PhysicsShapeGroup) {
      for (PhysicsShape child in shape.shapes) {
        _addB2Shapes(physicsNode, child, b2Shapes, physicsShapes);
      }
    } else {
393
      b2Shapes.add(shape.getB2Shape(physicsNode, _scale));
394 395 396 397
      physicsShapes.add(shape);
    }
  }
}