https://github.com/nature-of-code/noc-syllabus-modules/tree/main/module00-intro
Bug Scene
I wanted to create a bug-flying scene and set the following rules:
State
The bug will have three states:
- Stay - The bug remains still on the wall.
- Crawl - The bug moves slowly in a random direction.
- Fly - The bug moves randomly in the air.
Probability Control
- State transitions:
- By default:
- 60% chance to stay.
- 30% chance to crawl.
- 10% chance to fly.
- The longer the bug stays, the higher the chance to crawl or fly.
- The longer the bug crawl, the higher the chance to stay or fly.
- The longer the bug fly, the higher the chance to stay or crawl.
- ~~Cake attraction:~~
If the distance to the cake is less than 200px, the bug is 100% attracted to it.
Change the bug’s movement to fly directly to the cake.
- ~~Flyswatter avoidance:~~
If the flyswatter is closer than 100px, the bug 100% flies away.
Move the bug in the opposite direction of the flyswatter
Current Code
https://editor.p5js.org/sh7361/sketches/c_oi0tuOC
What I did
Using the code from The Nature of Code, Exercise 0.7, I make the bug movement looks nature in each state by adjusting the step size.
Unsolved questions
- [x] updateState() is a nightmare:
- [x] I tried to increase probability of change over time to reduce change state too frequently and make the behavior more natural. For now I tried grows quadratically (pow(..., 2)) to keep the probability small for a long time. But since the probability check happens every frame, the cumulative chance of change still happening rises quickly.
- [x] Looking for a more efficient way to make the Probability Control.
- [x] when bug state changes to fly, how to make the bug starts flying from its current location while make it fly within width and height.