Commit Graph

45 Commits

Author SHA1 Message Date
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
8001bbceb9 Refactor alignment() to use velocity_of_boids 2024-07-11 16:23:53 -05:00
d2a9d1214a Refactor cohesion() to use center_of_boids()
I made the center_of_boids() and velocity_of_boids() functions so that
they can be reused in the debug overlay operations. I should probably
use them in the main system, too.
2024-07-11 16:13:34 -05:00
e38df137a8 Move util functions below systems functions
I'm trying to group the kinds of parts together so that they're easier
to locate.
2024-07-11 15:24:57 -05:00
336992573e Completed velocity pointer gizmo
Right-clicking toggles velocity sensing mode and the cursor gets a line
that indicates the average velocity of the targeted points.

There is a coefficient of 50.0 on the pointer magnitude. The boids move
so slowly that the line is very short without this magnification.

There is also something wrong with the math. The boids expand and slow
down, but the velocity vector does not reflect this. It *grows* slightly
and then stays at that size as the boids slowly approach a stop.

When writing the average velocity function, I realized that it is
**exactly** the same as the center of mass function. They're both just
averaging a bunch of points. Both functions are implemented as pass-
through calls to a general purpose Vec2 averaging function.

The `impl Iterator<...>...` *is* actually necessary because of the use
of `map()`s to filter out the boids parts from the spatial tree.
2024-07-11 14:46:36 -05:00
ac25a41f77 Fix: off-by-one in the boid count enumeration
`enumerate()` begins counting at 0, so I was dividing the sum by one
less than the count of boids. Thus the over-expanded result.

I should do unit testing.

I should rewrite this function to take the vector directly. I *know*
that the input is a vector at all times. Mmh.
2024-07-11 14:13:07 -05:00
a0d2e2b467 Debug draw CoM is working!... CoM has error
The debug tool can draw a center-of-mass dot, but it doesn't look like
a sensible location. Time to check on the math, I guess.
2024-07-11 14:02:55 -05:00
d99326d849 Braking function to slow all boids
This will help during debugging to prevent things from screaming around
the play area. Especially when the acceleration routines are unbounded
and can infinitely add energy to the system.
2024-07-10 16:27:21 -05:00
7eb65df7cf Alignment function complete, seems off... 2024-07-10 16:26:56 -05:00
1fc79ae7bb Spawn boids in larger ring so they don't pop apart
The separation function causes the boids to rush away from each other
when the simulation starts because they're all stacked at the origin.

Spawn them out in a ringso that this doesn't happen nearly as
aggressively.
2024-07-10 15:03:49 -05:00
806c23c620 Use 1.0 for steering, and small init velocity 2024-07-10 14:53:10 -05:00
8fb11fe800 Make cohesion() affect the acceleration 2024-07-10 14:38:41 -05:00
eb6d4b2e32 Further rewrite of cohesion to match separation 2024-07-10 14:35:46 -05:00
95cdd90485 Fix over-application of cohesion forces
The cohesion function was erroneously applying forces to boids that
shouldn't be experiencing them. It shouldn't be iterating over all
boids-with-velocity and adding speed. It should be applying that force
only to the one boid that the loop is examining at that iteration.

I also cleaned up the signature to have just one query struct. I had it
split as two because I was getting issues with mutability. The fix was
to make the query mutable, and then keep the Transforms as immutable
references. This way I can reach through and update the Velocity alone.
2024-07-10 14:03:38 -05:00
a0323af081 Fix: Put the cohesion function back in 2024-07-10 10:59:18 -05:00
13cacb558e Fix: force direction & magnitude for separation
The clumping was because the function was moving the boids together,
not apart. The chaotic movement was the result of the large coefficient
on the force.
2024-07-10 10:53:12 -05:00
368d6c9201 Boids clumping (try for separation function)
I've updated the position integration function to account for
acceleration. I need to change it once more to represent force, instead.
The idea is that the steering functions can all do their thing and apply
a force to the boid. Then, all mobs have their acceleration (forces)
integrated up through velocity and into their new position.

This version produces clumps of fast moving boids. Boids out on their
own will slowly come to a stop. I'm not sure why either of these is the
case.
2024-07-10 10:34:16 -05:00
38c1d779c1 Just use a for loop in the cohesion function
I was trying to work out a way to run the loop using iterators, but
that doesn't really matter. I had an empty loop to consume the results,
which means I just have a for loop anyway.
2024-07-10 08:47:30 -05:00
3293706328 Iterate over all boids instead of only the player
I understand what Daniel was talking about when they referred to the
movement being very rigid.

https://github.com/danieldidiobalsamo/boids/blob/master/src/boids_plugin.rs#L289

Their implementation has a different main loop than mine so that the
acceleration can be delayed by one frame. This way things can choose
their movements without being able to observe the ones that have already
completed theirs for the current frame. I haven't done that, and things
are weird.

I'm going to try to resolve this by moving down into an acceleration
value. Apply a force, integrate at the beginning or end of the frame.
2024-07-09 17:02:14 -05:00
5d41cd57d3 Fix cohesion force vector
Found it! The force was going the wrong way.
2024-07-09 16:42:19 -05:00
4cf591f937 Cohesion function... sorta. Cohesion *no* function
I got the cohesion function to act on the Boids, but there's something
wrong with the math. I already had to dance around the accidental
introduction of NaN's into the system.
2024-07-09 16:37:46 -05:00
3a680a4ea2 Added bevy_spatial for boid interactions
I'm finally starting on the actual Boids flocking algorithm parts.
I don't want to iterate over all the things, and Bevy doesn't seem to
have fast collision testing yet, so I've reached for bevy_spatial to
track my Boids.
2024-07-09 11:56:52 -05:00
84fa15bd77 cargo-fmt 2024-07-08 16:53:02 -05:00
8f52ad3954 Border avoidance system 2024-07-08 16:10:31 -05:00
7224199a94 Delete unused entity spawn, add player color const 2024-07-08 15:18:18 -05:00
20d12bb056 Slightly better signature for keyboard handler 2024-07-08 14:20:54 -05:00
2e5c34da3d Basic player character steering
This is basically useless as an actual feature, but it was helpful to
demonstrate that I'm beginning to understand how to do this stuff.
2024-07-08 14:18:50 -05:00
38a252c59c Add keyboard event handler. "Q" to quit program 2024-07-08 14:00:07 -05:00
188b571be8 Starting the Boids impl. Dot explosion
The Boids entities are spawned and given a velocity that takes them
away from each other. Now to do the actual Bird-oids algorithm so that
they flock.
2024-07-08 13:32:54 -05:00