forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerated_particle.dart
48 lines (40 loc) · 1.15 KB
/
accelerated_particle.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import 'dart:math';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import '../components/mixins/single_child_particle.dart';
import '../particle.dart';
import 'curved_particle.dart';
/// A particle serves as a container for basic
/// acceleration physics.
/// [Offset] unit is logical px per second.
/// speed = Offset(0, 100) is 100 logical pixels per second, down
/// acceleration = Offset(-40, 0) will accelerate to left at rate of 40 px/s
class AcceleratedParticle extends CurvedParticle with SingleChildParticle {
@override
Particle child;
final Offset acceleration;
Offset speed;
Offset position;
AcceleratedParticle({
@required this.child,
this.acceleration = Offset.zero,
this.speed = Offset.zero,
this.position = Offset.zero,
double lifespan,
}) : super(
lifespan: lifespan,
);
@override
void render(Canvas canvas) {
canvas.save();
canvas.translate(position.dx, position.dy);
super.render(canvas);
canvas.restore();
}
@override
void update(double t) {
speed += acceleration * t;
position += speed * t - (acceleration * pow(t, 2)) / 2;
super.update(t);
}
}