UNPKG

bird-oid

Version:

A 3D boid system with accompanying emergent behaviors. Implementation mostly based on Craig Reynolds paper Steering Behaviors For Autonomous Characters.

121 lines (120 loc) 11.9 kB
/** * "Seek (or pursuit of a static target) acts to steer the character towards a specified position in global space." * @param {import("../types.js").BasicBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function seek({ position, target, velocity, maxSpeed }: import("../types.js").BasicBehaviorOptions): import("gl-matrix").vec3; /** * "Flee is simply the inverse of seek and acts to steer the character so that its velocity is radially aligned away from the target. The desired velocity points in the opposite direction." * @param {import("../types.js").BasicBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function flee(options: import("../types.js").BasicBehaviorOptions): import("gl-matrix").vec3; /** * "Pursuit is similar to seek except that the quarry (target) is another moving character. [...] The position of a character T units of time in the future (assuming it does not maneuver) can be obtained by scaling its velocity by T and adding that offset to its current position. Steering for pursuit is then simply the result of applying the seek steering behavior to the predicted target location." * @param {import("../types.js").ExtendedBasicBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function pursue({ position, target, velocity, targetVelocity, maxSpeed, }: import("../types.js").ExtendedBasicBehaviorOptions): import("gl-matrix").vec3; /** * "Evasion is analogous to pursuit, except that flee is used to steer away from the predicted future position of the target character." * @param {import("../types.js").ExtendedBasicBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function evade({ position, target, velocity, targetVelocity, maxSpeed, }: import("../types.js").ExtendedBasicBehaviorOptions): import("gl-matrix").vec3; /** * "Arrival behavior is identical to seek while the character is far from its target. But instead of moving through the target at full speed, this behavior causes the character to slow down as it approaches the target, eventually slowing to a stop coincident with the target" * @param {BasicWithRadiusBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function arrive({ position, target, velocity, maxSpeed, radius }: BasicWithRadiusBehaviorOptions): import("gl-matrix").vec3; /** * "The implementation of obstacle avoidance behavior described here will make a simplifying assumption that both the character and obstacle can be reasonably approximated as spheres, although the basic concept can be easily extend to more precise shape models. [...] It is convenient to consider the geometrical situation from the character’s local coordinate system. The goal of the behavior is to keep an imaginary cylinder of free space in front of the character. The cylinder lies along the character’s forward axis, has a diameter equal to the character’s bounding sphere, and extends from the character’s center for a distance based on the character’s speed and agility. An obstacle further than this distance away is not an immediate threat. The obstacle avoidance behavior considers each obstacle in turn (perhaps using a spatial portioning scheme to cull out distance obstacles) and determines if they intersect with the cylinder. By localizing the center of each spherical obstacle, the test for non-intersection with the cylinder is very fast. The local obstacle center is projected onto the side-up plane (by setting its forward coordinate to zero) if the 2D distance from that point to the local origin is greater than the sum of the radii of the obstacle and the character, then there is no potential collision. Similarly obstacles which are fully behind the character, or fully ahead of the cylinder, can be quickly rejected. For any remaining obstacles a line-sphere intersection calculation is performed. The obstacle which intersects the forward axis nearest the character is selected as the “most threatening.” Steering to avoid this obstacle is computed by negating the (lateral) side-up projection of the obstacle’s center. * @param {ObstacleBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function avoidObstacles({ position, velocity, obstacles, maxSpeed, maxAvoidForce, fixedDistance, }: ObstacleBehaviorOptions): import("gl-matrix").vec3; /** * "The steering force takes a “random walk” from one direction to another. This idea [...] is to constrain the steering force to the surface of a sphere located slightly ahead of the character. To produce the steering force for the next frame: a random displacement is added to the previous value, and the sum is constrained again to the sphere’s surface. The sphere’s radius [...] determines the maximum wandering “strength” and the magnitude of the random displacement [...] determines the wander “rate.”" * Note: Don't forget to update theta and phi angles: angle += Math.random() * WANDER_SPEED - WANDER_SPEED * 0.5; * @param {WanderBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function wander({ velocity, distance, radius, theta, phi }: WanderBehaviorOptions): import("gl-matrix").vec3; /** * "Path following behavior enables a character to steer along a predetermined path, such as a roadway, corridor or tunnel." * @param {FollowPathBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function followPath({ path, position, velocity, maxSpeed, fixedDistance, }: FollowPathBehaviorOptions): import("gl-matrix").vec3; /** * "In flow field following behavior the character steers to align its motion with the local tangent of a flow field (also known as a force field or a vector field). [...] The future position of a character is estimated and the flow field is sampled at that location. This flow direction [...] is the “desired velocity” and the steering direction (vector S) is simply the difference between the current velocity (vector V) and the desired velocity." * @param {FollowFlowFieldSimpleBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function followFlowFieldSimple({ flowField, position, velocity, maxSpeed, }: FollowFlowFieldSimpleBehaviorOptions): import("gl-matrix").vec3; /** * @param {FollowFlowFieldBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function followFlowField({ flowField, position, velocity, maxSpeed, fixedDistance, }: FollowFlowFieldBehaviorOptions): import("gl-matrix").vec3; /** * "Leader following behavior causes one or more character to follow another moving character designated as the leader. Generally the followers want to stay near the leader, without crowding the leader, and taking care to stay out of the leader’s way (in case they happen to find them selves in front of the leader). In addition, if there is more than one follower, they want to avoid bumping each other. The implementation of leader following relies on arrival behavior (see above) a desire to move towards a point, slowing as it draws near. The arrival target is a point offset slightly behind the leader. (The offset distance might optionally increases with speed.) If a follower finds itself in a rectangular region in front of the leader, it will steer laterally away from the leader’s path before resuming arrival behavior. In addition the followers use separation behavior to prevent crowding each other." * @param {FollowLeaderSimpleBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function followLeaderSimple({ leader, position, velocity, maxSpeed, distance, radius, }: FollowLeaderSimpleBehaviorOptions): import("gl-matrix").vec3; /** * Notes: next behaviour should be a separation * @param {FollowLeaderBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function followLeader({ leader, position, velocity, maxSpeed, distance, radius, evadeScale, arriveScale, }: FollowLeaderBehaviorOptions): import("gl-matrix").vec3; /** * "To compute steering for separation, first a search is made to find other characters within the specified neighborhood. [...] For each nearby character, a repulsive force is computed by subtracting the positions of our character and the nearby character, normalizing, and then applying a 1/r weighting. (That is, the position offset vector is scaled by 1/r 2.) Note that 1/r is just a setting that has worked well, not a fundamental value. These repulsive forces for each nearby character are summed together to produce the overall steering force." * @param {GroupsBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function separate({ boids, maxDistance, position, velocity, maxSpeed }: GroupsBehaviorOptions): import("gl-matrix").vec3; /** * "Cohesion steering behavior gives an character the ability to cohere with (approach and form a group with) other nearby characters. [...] Steering for cohesion can be computed by finding all characters in the local neighborhood (as described above for separation), computing the “average position” (or “center of gravity”) of the nearby characters. The steering force can applied in the direction of that “average position” (subtracting our character position from the average position, as in the original boids model), or it can be used as the target for seek steering behavior." * @param {GroupsBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function cohere({ boids, maxDistance, position, velocity, maxSpeed }: GroupsBehaviorOptions): import("gl-matrix").vec3; /** * "Alignment steering behavior gives an character the ability to align itself with (that is, head in the same direction and/or speed as) other nearby characters [...]. Steering for alignment can be computed by finding all characters in the local neighborhood (as described above for separation), averaging together the velocity (or alternately, the unit forward vector) of the nearby characters. This average is the “desired velocity,” and so the steering vector is the difference between the average and our character’s current velocity (or alternately, its unit forward vector)." * @param {GroupsBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function align({ boids, maxDistance, position, velocity, maxSpeed }: GroupsBehaviorOptions): import("gl-matrix").vec3; /** * "[...] in addition to other applications, the separation, cohesion and alignment behaviors can be combined to produce the boids model of flocks, herds and schools" * @param {GroupsBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function flock({ boids, maxDistance, position, velocity, maxSpeed }: GroupsBehaviorOptions): import("gl-matrix").vec3; /** * Constraint in system bounds. * @param {BoundsConstraintBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function boundsConstrain({ center, bounds, position, velocity, maxSpeed, maxForce, fixedDistance, }: BoundsConstraintBehaviorOptions): import("gl-matrix").vec3; /** * Constraint in sphere bounds. * @param {SphereConstraintBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function sphereConstrain({ center, radius, position, velocity, maxSpeed, maxForce, fixedDistance, }: SphereConstraintBehaviorOptions): import("gl-matrix").vec3; /** * Wrap to opposite bound (no velocity change). * @param {BoundsWrapConstraintBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function boundsWrapConstrain({ position, center, bounds }: BoundsWrapConstraintBehaviorOptions): import("gl-matrix").vec3; /** * Wrap to opposite bound (no velocity change). * @param {SphereWrapConstraintBehaviorOptions} options * @returns {import("gl-matrix").vec3} */ export function sphereWrapConstrain({ position, center, radius }: SphereWrapConstraintBehaviorOptions): import("gl-matrix").vec3;