Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min/max cohesion contribution for more controllable flocking #50

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add min/max cohesion contribution for more controllable flocking
sinbad committed Dec 19, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7014a91a71db397fefbafe5a75b053d02b7dd5a9
7 changes: 5 additions & 2 deletions 2D/Behaviors/SteerForCohesion2D.cs
Original file line number Diff line number Diff line change
@@ -11,14 +11,17 @@ namespace UnitySteer2D.Behaviors
[RequireComponent(typeof (SteerForNeighborGroup2D))]
public class SteerForCohesion2D : SteerForNeighbors2D
{
[SerializeField] private float _minContribution = 0f;
[SerializeField] private float _maxContribution = 1f;

public override Vector2 CalculateNeighborContribution(Vehicle2D other)
{
// accumulate sum of forces leading us towards neighbor's positions
var distance = other.Position - Vehicle.Position;
var sqrMag = distance.sqrMagnitude;
// Provide some contribution, but diminished by the distance to
// Provide some contribution, but diminished by the distance to
// the vehicle.
distance *= 1 / sqrMag;
distance *= Mathf.Clamp(1 / sqrMag, _minContribution, _maxContribution);
return distance;
}