r/processing • • Nov 23 '19

Looking for a partner, while avoiding exes.

396 Upvotes

8 comments sorted by

51

u/[deleted] Nov 23 '19

These things couple up, then after a while break up, from then on they avoid their former partners. Eventually there'll be no possible couples left and they'll all be avoiding each other.

13

u/[deleted] Nov 23 '19

wow.

10

u/[deleted] Nov 24 '19

Here's the code: https://github.com/fiskurgit/ProcKtTemplate/blob/master/src/prockt/sketches/Sketch030.kt

package prockt.sketches

import prockt.KApplet
import prockt.api.KVector

/*

    Attracted to nearest neighbour, then breaking up after a while

 */
class Sketch030: KApplet() {

    private val motes = mutableListOf<Mote>()

    private val count = 200

    private val startColor = color("#9d2f4d")
    private val endColor = color("#4973a1")

    override fun settings() {
        size(800, 600)
    }

    override fun setup() {
        repeat(count){index ->
            motes.add(Mote(index))
        }
        noStroke()
        noCursor()
    }

    override fun draw() {
        motes.forEach { mote ->
            mote.update().draw()
            mote.draw()
        }

        fill(BLACK, 30)
        rect(0, 0, width, height)
    }

    override fun mousePressed() {
        motes.clear()
        repeat(count){ index ->
            motes.add(Mote(index))
        }
    }

    inner class Mote(val id: Int){
        private var location = KVector(random(width), random(height))
        private var velocity = KVector(0f, 0f)
        private var acceleration: KVector? = null
        private var maxSpeed = 3f
        private var relationshipLength = random(100, 500)
        var closestDistance = Float.MAX_VALUE

        val exes = mutableListOf<Int>()

        var currentCompanion = -1
        var cyclesAttached = 0

        fun update(): Mote{
            var closestMote: Mote? = null
            closestDistance = Float.MAX_VALUE
            motes.forEach {mote ->
                val distance = location.distance(mote.location)
                if(distance > 1 && distance < closestDistance){
                    closestMote = mote
                    closestDistance = distance
                }
            }

            var directionToMote = closestMote!!.location - location
            directionToMote.normalize()

            if(closestMote!!.id == currentCompanion){
                cyclesAttached++
            }
            currentCompanion = closestMote!!.id

            if(cyclesAttached > relationshipLength){
                exes.add(currentCompanion)

                currentCompanion = -1
                cyclesAttached = 0
                relationshipLength = random(100, 500)
            }

            directionToMote *= when {
                exes.contains(closestMote!!.id) -> -0.6f
                else -> 0.2f
            }

            acceleration = directionToMote

            velocity += acceleration!!
            velocity.limit(maxSpeed)
            location += velocity

            checkBounds()

            return this
        }

        fun draw() {
            val lerpAmount = map(closestDistance, 0, 20, 0f, 1f)
            fill(lerpColor(startColor, endColor , lerpAmount))
            ellipse(location.x, location.y, 4, 4)
        }

        private fun checkBounds() {
            when {
                location.x > width -> location.x = 0f
                location.x < 0 -> location.x = width.toFloat()
            }

            when {
                location.y > height -> location.y = 0f
                location.y < 0 -> location.y = height.toFloat()
            }
        }
    }
}

(If this isn't formatted correctly for you it's because there's a bug with Reddit's Markdown rendering - no need to complain about it and suggest 'using 4 spaces')

13

u/Flannakis Nov 23 '19

This needs more votes... can you also create offspring for a % of courtships? Also blue pink, is male female? Then add in death and see what happens?

16

u/[deleted] Nov 23 '19

Colour is proximity to nearest neighbour. When you see something red traveling in a straight line that's a pair that have coupled up. Blues are single. Have just coded steering away from a threat (if coupled up they move away from nearest non-coupled neighbour).

Next I'm going to code some basic vision; rather than just be attracted to the nearest neighbour, they'll be only be attracted to others ahead of them within a limited distance and viewing angle. So many possibilities!

2

u/abhikapahi Nov 24 '19

The movement is so natural. Would love to see the code

1

u/[deleted] Nov 24 '19

Posted above/below

2

u/Flannakis Nov 23 '19

Cool, just followed you. Waiting for the next iteration :)