111 lines
3.6 KiB
Kotlin
Raw Normal View History

import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.css.*
2021-11-24 12:28:41 +03:00
import react.Props
2021-12-29 20:00:03 +03:00
import react.fc
2021-08-18 23:02:17 +03:00
import space.kscience.plotly.layout
import space.kscience.plotly.models.Trace
2022-01-27 12:10:00 +03:00
import space.kscience.visionforge.Colors
import space.kscience.visionforge.markup.VisionOfMarkup
import space.kscience.visionforge.react.flexRow
import space.kscience.visionforge.ring.ThreeCanvasWithControls
import space.kscience.visionforge.ring.solid
import space.kscience.visionforge.solid.*
import styled.css
import styled.styledDiv
import kotlin.math.sqrt
2021-11-24 12:28:41 +03:00
external interface DemoProps : Props {
2022-08-14 12:52:18 +03:00
var solids: Solids
}
2021-12-29 20:00:03 +03:00
val GravityDemo = fc<DemoProps> { props ->
2022-01-27 12:10:00 +03:00
val velocityTrace = Trace {
name = "velocity"
}
2022-01-27 12:10:00 +03:00
val energyTrace = Trace {
name = "energy"
}
val markup = VisionOfMarkup()
styledDiv {
css {
height = 100.vh - 50.pt
}
styledDiv {
css {
height = 50.vh
}
child(ThreeCanvasWithControls) {
attrs {
2022-08-14 12:52:18 +03:00
solids = props.solids
solid {
2022-01-27 12:10:00 +03:00
pointLight(200, 200, 200, name = "light"){
2023-07-25 13:35:55 +03:00
color(Colors.white)
2022-01-27 12:10:00 +03:00
}
ambientLight()
sphere(5.0, "ball") {
detail = 16
2023-07-25 13:35:55 +03:00
color("red")
val h = 100.0
y = h
2022-08-14 12:52:18 +03:00
solids.context.launch {
val g = 10.0
val dt = 0.1
var time = 0.0
var velocity = 0.0
while (isActive) {
delay(20)
time += dt
velocity -= g * dt
val energy = g * y.toDouble() + velocity * velocity / 2
y = y.toDouble() + velocity * dt
velocityTrace.appendXYLatest(time, y)
energyTrace.appendXYLatest(time, energy)
if (y.toDouble() <= 2.5) {
//conservation of energy
velocity = sqrt(2 * g * h)
}
markup.content = """
## Bouncing sphere parameters
**velocity** = $velocity
**energy** = $energy
""".trimIndent()
}
}
}
box(200, 5, 200, name = "floor") {
y = -2.5
}
}
}
}
}
flexRow {
css {
alignContent = Align.stretch
alignItems = Align.stretch
height = 50.vh - 50.pt
}
plotly {
2022-01-27 12:10:00 +03:00
traces(velocityTrace, energyTrace)
2021-08-18 23:02:17 +03:00
layout {
xaxis.title = "time"
}
}
Markup {
attrs {
this.markup = markup
}
}
}
}
}