{"id":279704,"date":"2016-05-06T14:35:03","date_gmt":"2016-05-06T10:35:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=279704"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=279704","title":{"rendered":"\u041f\u0442\u0438\u0447\u043a\u0438 \u0438 Unity3D, \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438"},"content":{"rendered":"<p>       \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u043f\u043e\u0441\u0442 \u043f\u0440\u043e <a href=\"http:\/\/habrahabr.ru\/post\/182382\/\">\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0442\u0438\u0447\u0435\u043a \u043d\u0430 Unity3D<\/a>, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u0412 Unity \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0438\u0445 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e, \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u043b\u0430\u0433\u0438. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u0435\u0448\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 Boid.cs \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 Boid:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code>public class Boid {     public Vector3 velocity;     public Vector3 position;\/\/\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0442\u0438\u0446\u044b \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435      private float cohesionRadius = 10;     private float separationDistance = 5;     private Boid[] boids;     private Vector3 cohesion;     private Vector3 separation;     private int separationCount;     private Vector3 alignment;     private float maxSpeed = 15;      public Boid(Vector3 pos)     {         position = pos;     } } <\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u0440\u0430\u0441\u0447\u0451\u0442\u044b. \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c \u043e\u0442 \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e Physics.OverlapSphere:  <\/p>\n<pre><code>    public static Boid[] boids;     static public Boid[] Sphere(Vector3 position, float radius)     {         List&lt;Boid&gt; mids = new List&lt;Boid&gt;();         for (int i = 0; i &lt; boids.Length; i++)         {             if (Vector3.Distance(position, boids[i].position) &lt; radius)                 mids.Add(boids[i]);         }         return mids.ToArray();     } <\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435.<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0430\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0440\u0430\u0441\u0447\u0451\u0442\u043e\u0432:  <\/p>\n<pre><code>public void CalculateVelocity()     {         Vector3 newVelocity = Vector3.zero;         cohesion = Vector3.zero;         separation = Vector3.zero;         separationCount = 0;         alignment = Vector3.zero;         boids = Boids.Sphere(position, cohesionRadius); \/\/\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Physics.OverlapSphere \u043d\u0430 \u0441\u0432\u043e\u044e         foreach (Boid boid in boids)         {             alignment += boid.velocity;\/\/\u041d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430             cohesion += boid.position;\/\/\u0411\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0431\u0440\u0430\u0448\u0430\u0435\u043c\u0441\u044f \u043a Transfom             if (boid != this && (position - boid.position).magnitude &lt; separationDistance)             {                 separation += (position - boid.position) \/ (position - boid.position).magnitude;                 separationCount++;             }         }         cohesion = cohesion \/ boids.Length;         cohesion = cohesion - position;         cohesion = Vector3.ClampMagnitude(cohesion, maxSpeed);          if (separationCount &gt; 0)             separation = separation \/ separationCount;         separation = Vector3.ClampMagnitude(separation, maxSpeed);          alignment = alignment \/ boids.Length;         alignment = Vector3.ClampMagnitude(alignment, maxSpeed);          newVelocity += cohesion * 0.5f + separation * 15f + alignment * 1.5f;         newVelocity = Vector3.ClampMagnitude(newVelocity, maxSpeed);         velocity = (velocity * 2f + newVelocity) \/ 3f;\/\/\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0433\u043b\u0430\u0434\u043a\u043e\u0441\u0442\u0438 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f         if (position.magnitude &gt; 40)         {             velocity += -position.normalized;         }     } <\/code><\/pre>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c velocity. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0434\u0432\u0438\u0433\u0430\u0442\u044c \u043f\u0442\u0438\u0446\u0443, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e Update.  <\/p>\n<pre><code>public void Update()     {         position += velocity * Time.deltaTime;          Debug.DrawRay(position, separation, Color.green);\/\/\u0417\u0434\u0435\u0441\u044c \u0436\u0435 \u043c\u044b \u0440\u0438\u0441\u0443\u0435\u043c \u043b\u0443\u0447\u0438 \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438         Debug.DrawRay(position, cohesion, Color.magenta);         Debug.DrawRay(position, alignment, Color.blue);     } <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u043a\u043b\u0430\u0441\u0441 Boid \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d.<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a \u043d\u0438 \u0431\u0443\u0434\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u043f\u0442\u0438\u0446.<br \/>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u043b \u043d\u0430 \u0441\u0446\u0435\u043d\u0435. \u0412\u0441\u0451 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u043b\u043e\u0441\u044c \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 Boids.cs:  <\/p>\n<pre><code>using UnityEngine; using System.Collections; using System.Collections.Generic;  public class Boids : MonoBehaviour {      GameObject[] boidsObjects;  \t\/\/ Use this for initialization \tvoid Start () {         boidsObjects = new GameObject[1500];\/\/\u0421\u043e\u0437\u0434\u0430\u0451\u043c 1500 \u043f\u0442\u0438\u0446         boids = new Boid[boidsObjects.Length];         for (int i = 0; i &lt; boids.Length; i++)         {             GameObject boid = GameObject.CreatePrimitive(PrimitiveType.Cube);\/\/\u041f\u0442\u0438\u0446\u044b \u0431\u0443\u0434\u0443\u0442 \u043a\u0443\u0431\u0430\u043c\u0438             boid.transform.position = Random.insideUnitSphere * 35f;             Destroy(boid.collider);\/\/\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u043a\u043e\u043b\u0430\u0439\u0434\u0435\u0440\u044b \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043b\u0430\u0433\u0438             boidsObjects[i] = boid;             boids[i] = new Boid(boid.transform.position);\/\/\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043e\u0431\u044a\u0435\u043a\u0442 Boid         }         StartCoroutine(UpdatePhis());\/\/\u0424\u0443\u043d\u043a\u0446\u0438\u044f InvokeRepeating \u043c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \t}     IEnumerator UpdatePhis()     {         while (true)         {             int UpdateCount = 0;             for (int i = 0; i &lt; boids.Length; i++)             {                 boids[i].CalculateVelocity();                 UpdateCount++;                 if (UpdateCount &gt; 100)\/\/\u0427\u0442\u043e\u0431\u044b \u0435\u0449\u0451 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c fps \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0435 \u0432\u0441\u0435\u0445 \u043f\u0442\u0438\u0446 \u0441\u0440\u0430\u0437\u0443                 {                     yield return null;                     UpdateCount = 0;                 }             }         }     } \t\/\/ Update is called once per frame \tvoid Update () {         for (int i = 0; i &lt; boids.Length; i++)         {             boids[i].Update();\/\/\u0414\u0432\u0438\u0433\u0430\u0435\u043c \u043f\u0442\u0438\u0446             boidsObjects[i].transform.position = boids[i].position;         } \t}  \/\/\u0417\u0434\u0435\u0441\u044c \u044f \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043b \u0437\u0430\u043c\u0435\u043d\u0443 OverlapSphere     public static Boid[] boids;     static public Boid[] Sphere(Vector3 position, float radius)     {         List&lt;Boid&gt; mids = new List&lt;Boid&gt;();         for (int i = 0; i &lt; boids.Length; i++)         {             if (Vector3.Distance(position, boids[i].position) &lt; radius)                 mids.Add(boids[i]);         }         return mids.ToArray();     } } <\/code><\/pre>\n<p>  \u0412\u0435\u0448\u0430\u0435\u043c Boids.cs \u043d\u0430 \u043a\u0430\u043c\u0435\u0440\u0443 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c. \u041f\u0440\u0438 1500 \u043f\u0442\u0438\u0446 \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b 70 fps \u043d\u0430 \u0441\u0432\u043e\u0451\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.<br \/>  WebPlayer: <a href=\"http:\/\/smtserov.tf9.ru\/Boids\/Boids.html\">\u0442\u0443\u0442<\/a><br \/>  \u041a\u043e\u0434: <a href=\"http:\/\/smtserov.tf9.ru\/Boids\/Code.zip\">\u0421\u043a\u0430\u0447\u0430\u0442\u044c<\/a>       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/283064\/\"> https:\/\/habrahabr.ru\/post\/283064\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u043f\u043e\u0441\u0442 \u043f\u0440\u043e <a href=\"http:\/\/habrahabr.ru\/post\/182382\/\">\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0442\u0438\u0447\u0435\u043a \u043d\u0430 Unity3D<\/a>, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u0412 Unity \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0438\u0445 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e, \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u043b\u0430\u0433\u0438. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u0435\u0448\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 Boid.cs \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 Boid:  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-279704","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/279704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279704"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/279704\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}