88 Commits

Author SHA1 Message Date
62feb6f313 Fix: separation() system needs smaller view range
I figured out why the new flocking behavior is different...

The previous version used 1/4th the view range for boid separation and I
didn't preserve that.
2025-12-25 22:58:03 -06:00
1846b7065e Convert flocking systems to use Avian2d 2025-12-24 16:02:46 -06:00
e252e3385c (autoformat) 2025-12-24 15:44:54 -06:00
5f0b428811 Debug scanner uses shape_intersections
That was really easy. I feel dumb for making a checkpoint commit before.
Oh well, onwards to the next step: Swap over the flocking systems.
2025-12-24 15:01:03 -06:00
119d7acf09 WIP: Debug tool scans using Avian2d shape casting
Working demonstration, but I'm not sure shape casting is the action I
want to be doing. I've just found a "shape_intersections" method in the
docs, but I'm saving my progress before making further changes.
2025-12-24 14:54:49 -06:00
ba01d8137f Mark v0.7.0
Mark new version, update the lockfile, and autoformat the project.
2025-12-22 11:07:56 -06:00
b8c28529e6 Remove the Player Boid
The player-controlled boid was for testing the flocking behavior. It has
outlived this purpose and I never even bothered to make it larger like
the others. It's finally going away completely.
2025-12-22 10:56:47 -06:00
fd161dc26b Wire in the magic physics vars as a Resource
I don't know what to call these, so they're "misc. parameters" for now.
2025-12-22 10:45:02 -06:00
c2cf100c05 Flocking params as a resource & egui-inspector
The birdoid flocking parameters are now a resource named
`FlockingParameters`. Adjustments can be made using the Egui inspector
widget, although I plan to make a custom UI in the future.
2025-12-21 16:33:04 -06:00
0828518963 Begin 0.7.0-dev, small upgrade to behavior params
I'm not happy with the flocking behavior so I fiddled with the program
to find these new behavior parameters. It's annoying to constantly
rebuild the program, though, and I'd like for the end-user to be able to
fiddle with it at runtime. To that end, v0.7.0 shall be the UI update.
2025-12-21 15:31:08 -06:00
5ef91ec88c Remove unused print_gizmo_config() function 2025-12-20 09:42:14 -06:00
7482130ac2 Don't have Bevy resize to full-screen
Now that I have a custom canvas, Bevy should adopt that as it's render
target rather than further modifying it.
2025-10-28 16:23:17 -05:00
e16c0e2e32 Add and use a custom canvas element on the page
Tell bevy to find the canvas with ID "#boids-canvas" instead of creating
it's own.
2025-10-28 16:12:09 -05:00
2d568455d8 Use whole browser window
Taken from
https://johanhelsing.studio/posts/extreme-bevy
2025-09-03 16:14:38 -05:00
ccd8a12b79 Autoformat 2025-09-03 14:27:28 -05:00
f2a71e712a Put the boid separation range back to 1/4 not 1/8
With the 1.0 radius boids (instead of whatever `Circle::default()`
uses), a separation activation of 1/4 the range looks more visually
appealing (imo).
2025-09-03 14:25:16 -05:00
dca8bcdaa7 Add speed_controller to maintain system energy
The starting velocities going outwards in a circle means that the sum of
energy in the system is 0. As the boids come back together, they will
slow down to fall into a lattice. This is interesting, but not the
flocking behavior Boids are supposed to have.

I've replaced the `space_brakes` function with this new speed controller
to keep boids moving between two speeds. Anything too slow will speed up
along it's current vector, and anything too fast will slow down.
2025-09-03 14:20:17 -05:00
e6e56506f8 Fix: alignment should align to vel, not impulse
I need to apply an impulse to match the velocities, but the previous
version was trying to match the forces.
2025-09-03 14:11:18 -05:00
bc0630b4ae Fix: off-by-one error in alignment averaging
The "length" is actually the `enumerate()` index, which is one less than
the item count. The previous version was not using the average, and may
have been deviding by 0 for boids with exactly one neighbor.
2025-09-03 14:09:40 -05:00
76a6b4f999 New cohesion rule function 2025-09-03 14:07:50 -05:00
451311126d New separation rule function 2025-09-03 14:07:34 -05:00
7b380196a5 Filter-map the entities to avoid self-alignment
The boids shouldn't try to align with themselves. That doesn't really
make any sense.
2025-09-03 12:23:53 -05:00
8b61d38636 Make keyboard control forces, not velocities
The keyboard input should apply a force, not modify the velocity. Right
now, there is no Mass component, but in the future there might be. I've
just fixed a broken physics integrator made by bad assumptions, too, so
I'm goig to do this for consistency if nothing else.
2025-09-03 12:23:53 -05:00
b3cf47e684 Remove BoidBundle, use required-components instead 2025-09-03 12:23:53 -05:00
69403bc6ca Fix the physics integrator
The velocity component never got updated. Instead, the system was only
calculating the current frame's deltaV. That instantaneous dV and any
velocity that *did* get assigned (bundle insertion, keyboard control)
would be added to the position.

Now forces are integrated into velocity, and velocity is integrated into
position. You know... like a real integration function.
2025-09-03 12:23:53 -05:00
a6240c844a Make another new alignment system 2025-09-03 12:23:53 -05:00
3eb23fb4bf Move physics parts to a physics module 2025-09-03 12:23:53 -05:00
4150f85ccc Rename & restructure birdoids module
The module isn't the plugin, so it's going to be called simply
"birdoids" going forward.

I've turned it into a folder and a `mod.rs` so I can slap down a small,
custom physics system.
2025-08-30 15:39:41 -05:00
214da65db5 Switch off deprecated functions, fix clippy lints 2025-08-30 15:39:41 -05:00
325e515a7b Autoformat 2025-08-30 15:39:41 -05:00
e85114c4c8 Adjust code for Bevy 0.14 -> 0.16 upgrade
Now the program builds again.
2025-08-30 15:39:41 -05:00
b75c05940d Run cargo fmt before merging branch 2024-07-16 09:31:15 -05:00
e09399cc23 Update force functions to use Option<Force> 2024-07-16 09:02:09 -05:00
abd107d04a Update tests: Force return needs an Option<>
The force functions are discontinuous at (0,0), and as such need to
return an optional. Without this, I'll be feeding NaNs back into the
program, which will propogate and cause a total simulation collapse.
2024-07-16 08:50:26 -05:00
519482aaf3 Rename separation_force parameters 2024-07-15 14:42:39 -05:00
a75704fb49 Placeholder for separation on div-0 error
This calculation should produce an infinity, or NaN. Either way, it's an
error that definitely still exists. I'll make a test for it, and then
fix it at some point. This is so that I don't forget.
2024-07-15 14:41:46 -05:00
60a0357b0d Tests were missing the BOID_VIEW_RANGE
I love it when I need to test my tests. It's just so easy to do.
2024-07-15 14:40:18 -05:00
82dabcfabe Fix: Cohesion function was accepting bad input
The cohesion function would accept inputs over the range (-1, 1) when it
has been designed to work over (0, 1). This changes the math to
calculate the magnitude separately and re-apply it to the pointing
vector.
2024-07-15 13:48:13 -05:00
c48fe1ed2e Add unit tests for force functions
Finally time to just use the unit testing tools Cargo gives me. These
are all a bunch of simple tests to verify that the functions work as
expected.

... They don't. I made the assertion that the input ranges are (0, 1)
when, in fact, they are not. The boid's view distance is scaled into
a unit vector space. The trouble with this is that a valid coordinate
is (-1, 0). The force functions don't have the correct shape at this
range.

The fix is to get the absolute value -- the vector's magnitude -- and
feed that through the force calculation function. This way it *is* in
the range (0, 1). The magnitude can then be multiplied back over the
deviation vector to get a proper force application.

This unit vector can then optionally be multiplied back up to have more
exaggerated effects. The {COHESION,SEPARATION,ALIGNMENT}_FACTOR's can
pick up that role again.
2024-07-15 11:38:39 -05:00
651ed774c3 Move separation_force down with cohesive_force 2024-07-15 10:14:13 -05:00
283a0bdde3 Add separation_force function 2024-07-15 10:10:56 -05:00
6c38083e6d Fix: Return Force instead of Vec2 2024-07-15 10:04:41 -05:00
930aa2e75d Add cohesive_force function 2024-07-15 10:01:43 -05:00
18885d9b94 Rename Acceleration to Force
Closes #2

Acceleration is mass * force.
This doesn't materially change anything, but it will make it easier to
give the boids a mass component. The change will not require updates to
most of the systems, only the physics integrator and the boids bundle.
2024-07-12 16:47:27 -05:00
092f0a25dd Give space_brakes a const coefficient 2024-07-12 14:49:17 -05:00
27cf0f390b Drop unneeded use line 2024-07-12 10:10:33 -05:00
f9ce96bbce cargo fmt again 2024-07-12 10:00:27 -05:00
a161aacaca Crank up the forces to see the boids move!
The Boids algorithm functions need a lot of work, but the cohesion and
separation functions appear to nominally perform their stated goal.

Both are constant forces across their whole range, so there is a jerking
motion as things cross in and out of the activation boundaries.

I think the alignment function breaks the laws of thermodynamics, as
well. Oh well, that's what the spacebrakes system is for.
2024-07-11 17:00:12 -05:00
32991d3e37 Fix: Acceleration needs to be reset to 0
The physics integrator is real scuffed. Nothing has mass, and no forces
are applied. Instead, objects affect each other's acceleration. It acts
functionally like a force accumulator where everything has a mass of 1.

Under this model, it must be cleared each frame so that it can be
re-accumulated during the next physics calculation cycle.

This bug was spotted because of anomolous behavior of the velocity
pointer. Nearly stationary boids would have a rather large (especially
after the coefficient reduction earlier) velocity. I noticed this
earlier when building the debug tool and thought it was an error in
the calculations. There might still be bugs, I still need to test, but
that particular one seems to have been a position integrator error.
2024-07-11 16:56:47 -05:00
bb33fda018 Fix: spatial tree update frequency is too low
The reubild frequency of the spatial tree was causing calculations to
operate on old information. This is not the intended behavior. The
default schedule seems to be to run every frame.

I also lowered the debug pointer coefficient. Boids moving at a normal
velocity are actually fast enough to draw a meaningful line. Maybe I'll
switch it into some kind of exponential space so small changes are
visible, and big changes don't draw a line all the way across the
screen.
2024-07-11 16:53:38 -05:00