Forces - Wind simulation 4

import io.data2viz.color.* import io.data2viz.geom.* import io.data2viz.math.* import io.data2viz.timer.* import io.data2viz.force.* import io.data2viz.viz.* import io.data2viz.random.* import kotlin.math.atan import kotlin.math.abs fun main() { // FEEL FREE TO EXPERIMENT BY CHANGING THESE VALUES ************************ val frictionRate = 4.pct // friction rate val defaultIntensity = 70.pct // default fixed intensity (no intensity decay) val linkForceIterations = 20 // the more iterations = the more "rigid" the curtains val collisionForceStrength = 1.pct // less strength = wind pass through the "curtains" val singleCurtainWidth = 1 // width of a curtain val curtainsNumber = 35 // # of curtains drawn val curtainsLength = 30 // length of a curtain val stitchSpace = 10.0 // size between nodes val stitchRigidity = 0.4 // lower value = more "elastic" (don't go over 2.0) val windRadius = 100.0 // size of the "wind" circles for collision val windSpeed = 8 // speed of the "wind" val windAngle = 0.deg // angle of the "wind" val gravityValue = 0.15.pct // higher values = heavier "curtains" // ************************************************************************* val width = 800 val height = 400 val curtainsWidth = curtainsNumber * singleCurtainWidth val totalStitches = curtainsWidth * curtainsLength val movement = Vector(windSpeed * windAngle.cos, windSpeed * windAngle.sin) val randPos = RandomDistribution.uniform(100.0, 300.0) // our domain object, storing a default starting position and if it is "fixed" or not data class Stitch(val position:Point, val fixed:Boolean = false) data class StitchLink(val source:ForceNode<Stitch>, val target:ForceNode<Stitch>) // creating the objects, only the top line is "fixed" val stitches = (0 until totalStitches).map { val col = it % curtainsWidth val row = it / curtainsWidth Stitch( point(80.0 + ((col + row) * stitchSpace), 50.0 + (row * 3) - col), it < curtainsWidth || it >= totalStitches - curtainsWidth ) }.toMutableList() // adding 3 more nodes to the simulation, these nodes are used to simulate the wind // these nodes will have a very different behavior stitches += (Stitch(point(0, 350), false)) stitches += (Stitch(point(-450, 600), false)) stitches += (Stitch(point(-200, 400), false)) lateinit var viz:Viz // keeping a reference to our ForceLink, this will allow easy access to the Links lateinit var forceLinks:ForceLink<Stitch> val simulation = forceSimulation<Stitch> { friction = frictionRate intensity = defaultIntensity intensityDecay = 0.pct // if the Stitch is "fixed", we use its current position has a fixed one (node won't move) initForceNode = { position = domain.position fixedX = if (domain.fixed) domain.position.x else null fixedY = if (domain.fixed) domain.position.y else null } // the force that creates links between the nodes // each node is linked to the next one on the right and next one below forceLinks = forceLink { linkGet = { val links = mutableListOf<Link<Stitch>>() val currentCol = index % singleCurtainWidth val wholeCol = index % curtainsWidth val row = index / curtainsWidth // only "link" the stitches, not the 3 nodes used for simulating the wind if (index < totalStitches) { // check if we had the right-next node if (currentCol != (singleCurtainWidth - 1) && wholeCol < curtainsWidth - 1) { links += Link(this, nodes[index + 1], stitchSpace, stitchRigidity) } // check if we had the bottom-next node if (row < curtainsLength - 1) { links += Link(this, nodes[index + curtainsWidth], stitchSpace, stitchRigidity) } } // return the list of links links } iterations = linkForceIterations } // create a collision force, only the 3 "wind" nodes have a radius forceCollision { radiusGet = { if (index < totalStitches) .0 else windRadius } strength = collisionForceStrength iterations = 1 } // create a "gravity" force, only applies to the "stiches" not the "wind" forceY { yGet = { height.toDouble() } strengthGet = { if (index < totalStitches) gravityValue else 0.pct } } domainObjects = stitches } val stitchLinks = mutableListOf<StitchLink>() simulation.nodes.forEach { if (it.index < totalStitches) { val col = it.index % curtainsWidth val row = it.index / curtainsWidth if (col < curtainsWidth - 1) stitchLinks += StitchLink(it, simulation.nodes[it.index + 1]) if (row < curtainsLength - 1) stitchLinks += StitchLink(it, simulation.nodes[it.index + curtainsWidth]) } } // storing the visuals of links and wind particles val links = mutableListOf<LineNode>() val winds = mutableListOf<CircleNode>() viz = viz { size = size(width, height) (totalStitches .. totalStitches+2).forEach { winds += circle { radius = windRadius fill = Colors.Web.lightblue.withAlpha(60.pct) x = -600.0 y = -600.0 } } stitchLinks.forEach { links += line { stroke = Colors.Web.black strokeWidth = 2.0 } } animation { // force move the "wind" particles (totalStitches .. totalStitches+2).forEach { val windNode = simulation.nodes[it] windNode.position += movement if (windNode.x > width) { windNode.x = -50.0 windNode.y = randPos() } // uncomment these 4 lines to visualize the "wind" particles /*winds[it - totalStitches].apply { x = windNode.x y = windNode.y }*/ } // show the new coordinates of each links to visualize the wind effect forceLinks.links.forEachIndexed { index, stitchLink -> links[index].apply { x1 = stitchLink.source.x x2 = stitchLink.target.x y1 = stitchLink.source.y y2 = stitchLink.target.y // uncomment these 2 lines to tint each link depending on its angle... // val angle = (atan(((y1 - y2) / (x1 - x2))) * 2).rad // stroke = Colors.hsl(angle, 100.pct, 40.pct) } } } } viz.bindRendererOnNewCanvas() }
pierre avatar

Sketch created by

pierre

comments

Gaetan Zoritchak