-
Notifications
You must be signed in to change notification settings - Fork 37
/
DemoFollowing01.kt
59 lines (54 loc) · 1.93 KB
/
DemoFollowing01.kt
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
49
50
51
52
53
54
55
56
57
58
59
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.delegatemagic.smoothing.following
import org.openrndr.extra.delegatemagic.smoothing.smoothing
import org.openrndr.math.Vector2
import kotlin.random.Random
/**
* Demonstrates using delegate-magic tools with
* [Double] and [Vector2].
*
* The white circle's position uses [following].
* The red circle's position uses [smoothing].
*
* `following` uses physics (velocity and acceleration).
* `smoothing` eases values towards the target.
*
* Variables using delegates (`by`) interpolate
* toward target values, shown as gray lines.
*
* The behavior of the delegate-magic functions can be configured
* via arguments that affect their output.
*
* The arguments come in pairs of similar name:
* The first one, often of type [Double], is constant,
* The second one contains `Property` in its name and can be
* modified after its creation and even be linked to a UI
* to modify the behavior of the delegate function in real time.
* The `Property` argument overrides the other.
*/
fun main() = application {
program {
val target = object {
var pos = drawer.bounds.center
}
val spos by smoothing(target::pos)
val fpos by following(target::pos)
extend {
if (frameCount % 90 == 0) {
target.pos = Vector2(
Random.nextDouble(0.0, width.toDouble()),
Random.nextDouble(10.0, height.toDouble())
)
}
drawer.fill = ColorRGBa.WHITE
drawer.circle(fpos, 15.0)
drawer.fill = ColorRGBa.RED
drawer.circle(spos, 10.0)
drawer.fill = null
drawer.stroke = ColorRGBa.GRAY.opacify(0.5)
drawer.lineSegment(0.0, target.pos.y, width.toDouble(), target.pos.y)
drawer.lineSegment(target.pos.x, 0.0, target.pos.x, height.toDouble())
}
}
}