Ejemplo n.º 1
0
        public int addAgent(Vector3 position, RVOLayer layer, RVOLayer collideWith, int priority = 1)
        {
            if (defaultAgent == null)
            {
                return(-1);
            }

            Agent agent = new Agent();

            agent.id = stotalID;
            stotalID++;
            agent.maxNeighbors    = defaultAgent.maxNeighbors;
            agent.maxSpeed        = defaultAgent.maxSpeed * priority;
            agent.neighborDist    = defaultAgent.neighborDist;
            agent.position        = position;
            agent.radius          = defaultAgent.radius;
            agent.timeHorizon     = defaultAgent.timeHorizon;
            agent.timeHorizonObst = defaultAgent.timeHorizonObst;
            agent.velocity        = defaultAgent.velocity * priority;
            agent.layer           = layer;
            agent.colliderWith    = collideWith;
            agent.priority        = priority;
            agents.Add(agent);
            onAddAgent();
            return(agent.id);
        }
 public void OnGet()
 {
     this.radius              = 400;
     this.maxSpeed            = 10000;
     this.height              = 2000;
     this.locked              = false;
     this.lockWhenNotMoving   = false;
     this.agentTimeHorizon    = 2000;
     this.obstacleTimeHorizon = 2000;
     this.neighbourDist       = 2000;
     this.maxNeighbours       = 6;
     this.mask             = -1;
     this.layer            = RVOLayer.DefaultAgent;
     this.collidesWith     = (RVOLayer)(-1);
     this.wallAvoidForce   = 1f;
     this.wallAvoidFalloff = 1f;
     this.center           = new VInt3(0, 1000, 0);
     this.enableRotation   = false;
     this.rotationSpeed    = 30f;
     this.simulator        = null;
     this.adjustedY        = 0;
     this.actor.Release();
     this.desiredVelocity = VInt3.zero;
     this.checkNavNode    = false;
     this.lastPosition    = VInt3.zero;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads public properties and stores them in internal fields.
        /// This is required because multithreading is used and if another script
        /// updated the fields at the same time as this class used them in another thread
        /// weird things could happen.
        ///
        /// Will also set CalculatedTargetPoint and CalculatedSpeed to the result
        /// which was last calculated.
        /// </summary>
        public void BufferSwitch()
        {
            // <== Read public properties
            radius              = Radius;
            height              = Height;
            maxSpeed            = nextMaxSpeed;
            desiredSpeed        = nextDesiredSpeed;
            agentTimeHorizon    = AgentTimeHorizon;
            obstacleTimeHorizon = ObstacleTimeHorizon;
            maxNeighbours       = MaxNeighbours;
            // Manually controlled overrides the agent being locked
            // (if one for some reason uses them at the same time)
            locked              = Locked && !manuallyControlled;
            position            = Position;
            elevationCoordinate = ElevationCoordinate;
            collidesWith        = CollidesWith;
            layer = Layer;

            if (locked)
            {
                // Locked agents do not move at all
                desiredTargetPointInVelocitySpace = position;
                desiredVelocity = currentVelocity = Vector2.zero;
            }
            else
            {
                desiredTargetPointInVelocitySpace = nextTargetPoint - position;

                // Estimate our current velocity
                // This is necessary because other agents need to know
                // how this agent is moving to be able to avoid it
                currentVelocity = (CalculatedTargetPoint - position).normalized * CalculatedSpeed;

                // Calculate the desired velocity from the point we want to reach
                desiredVelocity = desiredTargetPointInVelocitySpace.normalized * desiredSpeed;

                if (collisionNormal != Vector2.zero)
                {
                    collisionNormal.Normalize();
                    var dot = Vector2.Dot(currentVelocity, collisionNormal);

                    // Check if the velocity is going into the wall
                    if (dot < 0)
                    {
                        // If so: remove that component from the velocity
                        currentVelocity -= collisionNormal * dot;
                    }

                    // Clear the normal
                    collisionNormal = Vector2.zero;
                }
            }
        }
Ejemplo n.º 4
0
    void CreatAgent(RVOLayer layer, RVOLayer collideWith, int priority = 1)
    {
        int sid = Simulator.Instance.addAgent(mousePosition, layer, collideWith, priority);

        // Simulator.Instance.setAgentMaxSpeed(sid, Simulator.Instance.getAgentMaxSpeed(sid) * priority);
        if (sid >= 0)
        {
            GameObject go = LeanPool.Spawn(agentPrefab, new Vector3(mousePosition.x, 0, mousePosition.y), Quaternion.identity);
            GameAgent  ga = go.GetComponent <GameAgent>();
            go.GetComponentInChildren <MeshRenderer>().material.color = priority == 1 ? Color.white : Color.red;
            Assert.IsNotNull(ga);
            ga.sid = sid;
            magentMap.Add(sid, ga);
        }
    }
Ejemplo n.º 5
0
        public void BufferSwitch()
        {
            this.radius              = this.Radius;
            this.height              = this.Height;
            this.maxSpeed            = this.MaxSpeed;
            this.neighbourDist       = this.NeighbourDist;
            this.agentTimeHorizon    = this.AgentTimeHorizon;
            this.obstacleTimeHorizon = this.ObstacleTimeHorizon;
            this.maxNeighbours       = this.MaxNeighbours;
            this.desiredVelocity     = this.DesiredVelocity;
            this.locked              = this.Locked;
            this.collidesWith        = this.CollidesWith;
            this.layer    = this.Layer;
            this.Velocity = this.velocity;
            List <ObstacleVertex> list = this.obstaclesBuffered;

            this.obstaclesBuffered = this.obstacles;
            this.obstacles         = list;
        }
Ejemplo n.º 6
0
        /** Reads public properties and stores them in internal fields.
         * This is required because multithreading is used and if another script
         * updated the fields at the same time as this class used them in another thread
         * weird things could happen.
         *
         * Will also set CalculatedTargetPoint and CalculatedSpeed to the result
         * which was last calculated.
         */
        public void BufferSwitch()
        {
            // <== Read public properties
            radius              = Radius;
            height              = Height;
            maxSpeed            = nextMaxSpeed;
            desiredSpeed        = nextDesiredSpeed;
            neighbourDist       = NeighbourDist;
            agentTimeHorizon    = AgentTimeHorizon;
            obstacleTimeHorizon = ObstacleTimeHorizon;
            maxNeighbours       = MaxNeighbours;
            locked              = Locked;
            position            = Position;
            elevationCoordinate = ElevationCoordinate;
            collidesWith        = CollidesWith;
            layer = Layer;

            desiredTargetPointInVelocitySpace = nextTargetPoint - position;

            // Estimate our current velocity
            // This is necessary because other agents need to know
            // how this agent is moving to be able to avoid it
            currentVelocity = (CalculatedTargetPoint - position).normalized * CalculatedSpeed;

            if (collisionNormal != Vector2.zero)
            {
                collisionNormal.Normalize();
                var dot = Vector2.Dot(currentVelocity, collisionNormal);

                // Check if the velocity is going into the wall
                if (dot < 0)
                {
                    currentVelocity -= collisionNormal * dot;
                }

                // Clear the normal
                collisionNormal = Vector2.zero;
            }
        }
Ejemplo n.º 7
0
        public void BufferSwitch()
        {
            // <==
            radius              = Radius;
            height              = Height;
            maxSpeed            = MaxSpeed;
            neighbourDist       = NeighbourDist;
            agentTimeHorizon    = AgentTimeHorizon;
            obstacleTimeHorizon = ObstacleTimeHorizon;
            maxNeighbours       = MaxNeighbours;
            desiredVelocity     = DesiredVelocity;
            locked              = Locked;
            collidesWith        = CollidesWith;
            layer = Layer;

            //position = Position;

            // ==>
            Velocity = velocity;
            List <ObstacleVertex> tmp = obstaclesBuffered;

            obstaclesBuffered = obstacles;
            obstacles         = tmp;
        }
Ejemplo n.º 8
0
		/** Adds an obstacle described by the vertices.
		 * 
		 * \see RemoveObstacle
		 */
		public ObstacleVertex AddObstacle (Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle) {
			
			if (vertices == null) throw new System.ArgumentNullException ("Vertices must not be null");
			
			if (vertices.Length < 2) throw new System.ArgumentException ("Less than 2 vertices in an obstacle");
			
			ObstacleVertex first = null;
			ObstacleVertex prev = null;
			
			bool identity = matrix == Matrix4x4.identity;
			
			//Don't interfere with ongoing calculations
			if (Multithreading && doubleBuffering) for (int j=0;j<workers.Length;j++) workers[j].WaitOne();
			
			for (int i=0;i<vertices.Length;i++) {
				ObstacleVertex v = new ObstacleVertex();
				if (first == null) first = v;
				else prev.next = v;
				
				v.prev = prev;
				v.layer = layer;

				//Premature optimization ftw!
				v.position = identity ? vertices[i] : matrix.MultiplyPoint3x4(vertices[i]);
				
				//v.thin = thin;
				v.height = height;
				
				prev = v;
			}
			
			prev.next = first;
			first.prev = prev;
			
			ObstacleVertex c = first;
			do {
				Vector3 dir = c.next.position - c.position;
				c.dir = new Vector2 (dir.x,dir.z).normalized;


				c = c.next;
			} while (c != first);
			
			obstacles.Add (first);
			
			UpdateObstacles ();
			return first;
		}
Ejemplo n.º 9
0
        /// <summary>
        /// Adds an obstacle described by the vertices.
        ///
        /// See: RemoveObstacle
        /// </summary>
        public ObstacleVertex AddObstacle(Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle, bool cycle = true)
        {
            if (vertices == null)
            {
                throw new System.ArgumentNullException("Vertices must not be null");
            }
            if (vertices.Length < 2)
            {
                throw new System.ArgumentException("Less than 2 vertices in an obstacle");
            }

            ObstacleVertex first = null;
            ObstacleVertex prev  = null;

            // Don't interfere with ongoing calculations
            BlockUntilSimulationStepIsDone();

            for (int i = 0; i < vertices.Length; i++)
            {
                var v = new ObstacleVertex
                {
                    prev   = prev,
                    layer  = layer,
                    height = height
                };

                if (first == null)
                {
                    first = v;
                }
                else
                {
                    prev.next = v;
                }

                prev = v;
            }

            if (cycle)
            {
                prev.next  = first;
                first.prev = prev;
            }

            UpdateObstacle(first, vertices, matrix);
            obstacles.Add(first);
            return(first);
        }
        // Token: 0x06002860 RID: 10336 RVA: 0x001B7880 File Offset: 0x001B5A80
        public ObstacleVertex AddObstacle(Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle, bool cycle = true)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("Vertices must not be null");
            }
            if (vertices.Length < 2)
            {
                throw new ArgumentException("Less than 2 vertices in an obstacle");
            }
            ObstacleVertex obstacleVertex  = null;
            ObstacleVertex obstacleVertex2 = null;

            this.BlockUntilSimulationStepIsDone();
            for (int i = 0; i < vertices.Length; i++)
            {
                ObstacleVertex obstacleVertex3 = new ObstacleVertex
                {
                    prev   = obstacleVertex2,
                    layer  = layer,
                    height = height
                };
                if (obstacleVertex == null)
                {
                    obstacleVertex = obstacleVertex3;
                }
                else
                {
                    obstacleVertex2.next = obstacleVertex3;
                }
                obstacleVertex2 = obstacleVertex3;
            }
            if (cycle)
            {
                obstacleVertex2.next = obstacleVertex;
                obstacleVertex.prev  = obstacleVertex2;
            }
            this.UpdateObstacle(obstacleVertex, vertices, matrix);
            this.obstacles.Add(obstacleVertex);
            return(obstacleVertex);
        }
Ejemplo n.º 11
0
 public void BufferSwitch()
 {
     this.radius = this.Radius;
     this.height = this.Height;
     this.maxSpeed = this.MaxSpeed;
     this.neighbourDist = this.NeighbourDist;
     this.agentTimeHorizon = this.AgentTimeHorizon;
     this.obstacleTimeHorizon = this.ObstacleTimeHorizon;
     this.maxNeighbours = this.MaxNeighbours;
     this.desiredVelocity = this.DesiredVelocity;
     this.locked = this.Locked;
     this.collidesWith = this.CollidesWith;
     this.layer = this.Layer;
     this.Velocity = this.velocity;
     List<ObstacleVertex> list = this.obstaclesBuffered;
     this.obstaclesBuffered = this.obstacles;
     this.obstacles = list;
 }
Ejemplo n.º 12
0
        public ObstacleVertex AddObstacle(VInt3[] vertices, int height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("Vertices must not be null");
            }
            if (vertices.Length < 2)
            {
                throw new ArgumentException("Less than 2 vertices in an obstacle");
            }
            ObstacleVertex obstacleVertex  = null;
            ObstacleVertex obstacleVertex2 = null;
            bool           flag            = matrix == Matrix4x4.identity;

            if (this.Multithreading && this.doubleBuffering)
            {
                for (int i = 0; i < this.workers.Length; i++)
                {
                    this.workers[i].WaitOne();
                }
            }
            for (int j = 0; j < vertices.Length; j++)
            {
                ObstacleVertex obstacleVertex3 = new ObstacleVertex();
                if (obstacleVertex == null)
                {
                    obstacleVertex = obstacleVertex3;
                }
                else
                {
                    obstacleVertex2.next = obstacleVertex3;
                }
                obstacleVertex3.prev     = obstacleVertex2;
                obstacleVertex3.layer    = layer;
                obstacleVertex3.position = ((!flag) ? ((VInt3)matrix.MultiplyPoint3x4((Vector3)vertices[j])) : vertices[j]);
                obstacleVertex3.height   = height;
                obstacleVertex2          = obstacleVertex3;
            }
            obstacleVertex2.next = obstacleVertex;
            obstacleVertex.prev  = obstacleVertex2;
            ObstacleVertex obstacleVertex4 = obstacleVertex;

            do
            {
                obstacleVertex4.dir = (obstacleVertex4.next.position - obstacleVertex4.position).xz.normalized;
                obstacleVertex4     = obstacleVertex4.next;
            }while (obstacleVertex4 != obstacleVertex);
            this.obstacles.Add(obstacleVertex);
            this.UpdateObstacles();
            return(obstacleVertex);
        }
Ejemplo n.º 13
0
 public ObstacleVertex AddObstacle(Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle)
 {
     if (vertices == null)
     {
         throw new ArgumentNullException("Vertices must not be null");
     }
     if (vertices.Length < 2)
     {
         throw new ArgumentException("Less than 2 vertices in an obstacle");
     }
     ObstacleVertex obstacleVertex = null;
     ObstacleVertex obstacleVertex2 = null;
     bool flag = matrix == Matrix4x4.identity;
     if (this.Multithreading && this.doubleBuffering)
     {
         for (int i = 0; i < this.workers.Length; i++)
         {
             this.workers[i].WaitOne();
         }
     }
     for (int j = 0; j < vertices.Length; j++)
     {
         ObstacleVertex obstacleVertex3 = new ObstacleVertex();
         if (obstacleVertex == null)
         {
             obstacleVertex = obstacleVertex3;
         }
         else
         {
             obstacleVertex2.next = obstacleVertex3;
         }
         obstacleVertex3.prev = obstacleVertex2;
         obstacleVertex3.layer = layer;
         obstacleVertex3.position = ((!flag) ? matrix.MultiplyPoint3x4(vertices[j]) : vertices[j]);
         obstacleVertex3.height = height;
         obstacleVertex2 = obstacleVertex3;
     }
     obstacleVertex2.next = obstacleVertex;
     obstacleVertex.prev = obstacleVertex2;
     ObstacleVertex obstacleVertex4 = obstacleVertex;
     do
     {
         Vector3 vector = obstacleVertex4.next.position - obstacleVertex4.position;
         ObstacleVertex arg_13E_0 = obstacleVertex4;
         Vector2 vector2 = new Vector2(vector.x, vector.z);
         arg_13E_0.dir = vector2.normalized;
         obstacleVertex4 = obstacleVertex4.next;
     }
     while (obstacleVertex4 != obstacleVertex);
     this.obstacles.Add(obstacleVertex);
     this.UpdateObstacles();
     return obstacleVertex;
 }
Ejemplo n.º 14
0
        /** Adds an obstacle described by the vertices.
         *
         * \see RemoveObstacle
         */
        public ObstacleVertex AddObstacle(Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle)
        {
            if (vertices == null)
            {
                throw new System.ArgumentNullException("Vertices must not be null");
            }

            if (vertices.Length < 2)
            {
                throw new System.ArgumentException("Less than 2 vertices in an obstacle");
            }

            ObstacleVertex first = null;
            ObstacleVertex prev  = null;

            bool identity = matrix == Matrix4x4.identity;

            //Don't interfere with ongoing calculations
            if (Multithreading && doubleBuffering)
            {
                for (int j = 0; j < workers.Length; j++)
                {
                    workers[j].WaitOne();
                }
            }

            for (int i = 0; i < vertices.Length; i++)
            {
                ObstacleVertex v = new ObstacleVertex();
                if (first == null)
                {
                    first = v;
                }
                else
                {
                    prev.next = v;
                }

                v.prev  = prev;
                v.layer = layer;

                //Premature optimization ftw!
                v.position = identity ? vertices[i] : matrix.MultiplyPoint3x4(vertices[i]);

                //v.thin = thin;
                v.height = height;

                prev = v;
            }

            prev.next  = first;
            first.prev = prev;

            ObstacleVertex c = first;

            do
            {
                Vector3 dir = c.next.position - c.position;
                c.dir = new Vector2(dir.x, dir.z).normalized;


                c = c.next;
            } while (c != first);

            obstacles.Add(first);

            UpdateObstacles();
            return(first);
        }
Ejemplo n.º 15
0
        public void BufferSwitch()
        {
            // <==
            radius = Radius;
            height = Height;
            maxSpeed = MaxSpeed;
            neighbourDist = NeighbourDist;
            agentTimeHorizon = AgentTimeHorizon;
            obstacleTimeHorizon = ObstacleTimeHorizon;
            maxNeighbours = MaxNeighbours;
            desiredVelocity = DesiredVelocity;
            locked = Locked;
            collidesWith = CollidesWith;
            layer = Layer;

            //position = Position;

            // ==>
            Velocity = velocity;
            List<ObstacleVertex> tmp = obstaclesBuffered;
            obstaclesBuffered = obstacles;
            obstacles = tmp;
        }
Ejemplo n.º 16
0
        public ObstacleVertex AddObstacle(VInt3[] vertices, int height, Matrix4x4 matrix, RVOLayer layer = 2)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("Vertices must not be null");
            }
            if (vertices.Length < 2)
            {
                throw new ArgumentException("Less than 2 vertices in an obstacle");
            }
            ObstacleVertex item    = null;
            ObstacleVertex vertex2 = null;
            bool           flag    = matrix == Matrix4x4.identity;

            if (this.Multithreading && this.doubleBuffering)
            {
                for (int j = 0; j < this.workers.Length; j++)
                {
                    this.workers[j].WaitOne();
                }
            }
            for (int i = 0; i < vertices.Length; i++)
            {
                ObstacleVertex vertex3 = new ObstacleVertex();
                if (item == null)
                {
                    item = vertex3;
                }
                else
                {
                    vertex2.next = vertex3;
                }
                vertex3.prev     = vertex2;
                vertex3.layer    = layer;
                vertex3.position = !flag ? ((VInt3)matrix.MultiplyPoint3x4((Vector3)vertices[i])) : vertices[i];
                vertex3.height   = height;
                vertex2          = vertex3;
            }
            vertex2.next = item;
            item.prev    = vertex2;
            ObstacleVertex next = item;

            do
            {
                VInt3 num3 = next.next.position - next.position;
                next.dir = num3.xz.normalized;
                next     = next.next;
            }while (next != item);
            this.obstacles.Add(item);
            this.UpdateObstacles();
            return(item);
        }