예제 #1
0
    Vector3 Calculate()
    {
        Vector3 steeringBehaviourForce = Vector3.zero;

        steeringBehaviourForce += steeringBehaviour.Calculate() * steeringBehaviour.weight;
        return(steeringBehaviourForce);
    }
예제 #2
0
파일: Vehicle.cs 프로젝트: gjtqiyue/AI
    //IEnumerator Calculate()
    //{
    //    while (true)
    //    {

    //        yield return new WaitForFixedUpdate();
    //    }
    //}

    // Update the physics and velocity of the vehicle
    void FixedUpdate()
    {
        //SteeringBehaviour.instance.SetDestination(target.position);

        // calculate the combine force form each steering behavior in the
        // vehicle's list
        my_SteeringForce = my_SteeringBehaviour.Calculate();
        Vector3 steeringForce = my_SteeringForce;

        Debug.DrawLine(Position(), Position() + steeringForce, Color.red);

        // calculate the acceleration
        Vector3 acceleration = steeringForce / rig.mass;

        // update the velocity
        velocity += acceleration * Time.deltaTime;

        // make sure the velocity doesn't exceed the max velocity
        Vector3 normalized = velocity.normalized;

        if (velocity.magnitude >= maxSpeed)
        {
            velocity = normalized * maxSpeed;
        }

        // update the position
        Vector3 previousPosition = transform.position;

        transform.position += velocity * Time.deltaTime;
        //Debug.Log("velocity2: " + velocity);
        //Debug.DrawLine(Position(), Position() + velocity, Color.green);
        // replaceable method
        //rig.AddForce(steeringForce);

        // update the rotation
        if (transform.position - previousPosition != Vector3.zero)
        {
            transform.rotation = Quaternion.LookRotation(transform.position - previousPosition);
        }


        //Debug.Break();
        // update the heading and side if the velocity is greater than 0
        if (velocity.sqrMagnitude > 0.00000001)
        {
            heading = normalized;
            side    = new Vector3(velocity.x, -velocity.y, velocity.z);
        }
    }
예제 #3
0
        public override void Update(float timeElapsed)
        {
            if (Target != null)
            {
                //calculate the combined force from each steering behavior in the vehicle's list
                Vector2D steeringForce = SB.Calculate();

                //Acceleration = Force/Mass
                Vector2D acceleration = steeringForce / Mass;

                //update velocity
                Velocity += (acceleration * timeElapsed);

                //make sure vehicle does not exceed maximum velocity
                Velocity = Velocity.Truncate(MaxSpeed);

                //update the position
                Pos += (Velocity * timeElapsed);

                //update the heading if the vehicle has a non zero velocity
                if (Velocity.LengthSquared() > 0.00000001)
                {
                    Heading = Velocity.Normalize();
                    Side    = Heading.Perp();
                }
            }
            else if (Velocity.LengthSquared() > 0.00000001)
            {
                Velocity *= 0;
            }
            if (this.Pos.X > MyWorld.Width)
            {
                this.Pos = new Vector2D(1, Pos.Y);
            }
            else if (this.Pos.X < 0)
            {
                this.Pos = new Vector2D(MyWorld.Width - 1, Pos.Y);
            }
            if (this.Pos.Y > MyWorld.Height)
            {
                this.Pos = new Vector2D(Pos.X, 1);
            }
            else if (this.Pos.Y < 0)
            {
                this.Pos = new Vector2D(Pos.X, MyWorld.Height - 1);
            }
        }
        public override Vector2D Calculate()
        {
            Vertex followPath = path.bestPath;

            if (followPath == null)
            {
                return(new Vector2D());
            }

            if (followPath.adj.Count == 0 && movingEntity.Pos.DistanceSqrt(followPath.position) <= 50f)
            {
                movingEntity.Velocity = new Vector2D();
                return(new Vector2D());
            }

            Vector2D force = sb.Calculate(followPath.position);

            followPath = setNextTarget(followPath);

            path.bestPath = followPath;

            return(force);
        }
예제 #5
0
        public override void Update(float timeElapsed)
        {
            // Calculate the combined force from each steering behaviour.
            Vector2D steeringForce = SteeringBehaviour.Calculate();

            // Acceleration = Force / Mass (Newton's laws of physics).
            Vector2D acceleration = steeringForce.Clone().Divide(Mass);

            // Update velocity.
            Velocity.Add(acceleration.Clone().Multiply(timeElapsed));

            // Make sure the moving entity does not exceed maximum speed.
            Velocity.Truncate(MaxSpeed);

            // Update position.
            Pos.Add(Velocity.Clone().Multiply(timeElapsed));

            // Update heading if the velocity is bigger than 0
            if (Velocity.LengthSquared() > 0)
            {
                Heading = Velocity.Clone().Normalize();
                Side    = Heading.Perpendicular();
            }
        }
예제 #6
0
    void FixedUpdate()
    {
        elapsedTime = Time.fixedDeltaTime;

        Vector2 resultanteForces = new Vector2(0, 0);

        setDirection();

        //Combiner les forces ici
        resultanteForces += steeringBehaviour.Calculate();

        //Finalement appliquer la resultante
        acceleration = resultanteForces;
        Vector2 vel = rb.velocity + resultanteForces * elapsedTime;

        if (vel.magnitude > maxSpeed)
        {
            rb.velocity = vel.normalized * maxSpeed;
        }
        else
        {
            rb.velocity = vel;
        }
    }
예제 #7
0
    public void AIUpdate()
    {
        //update the time elapsed

        //keep a record of its old position so we can update its cell later
        //in this method
        Vector3 OldVelocity = aiAgent.Velocity();

        Vector3 SteeringForce = Vector3.zero;

        //calculate the combined force from each steering behavior in the
        //vehicle's list
        SteeringForce = steeringBehaviour.Calculate();

        //Acceleration = Force/Mass
        Vector3 acceleration = SteeringForce / aiAgent.Mass();
        Vector3 gravityaccel = steeringBehaviour.GravityAccel();

        grounded = (gravityaccel.magnitude > 0) ? true : false;

        aiAgent.Update(acceleration, gravityaccel, useWrapAround, Time.deltaTime);

        Vector3 vPos = aiAgent.VPos();

        vPos.y = steeringBehaviour.GravityTruncate(vPos.y);

        aiAgent.SetVPos(vPos);

        //EnforceNonPenetrationConstraint(this, World()->Agents());

        //update the vehicle's current cell if space partitioning is turned on
        if (isSmoothingOn())
        {
            m_vSmoothedHeading = m_pHeadingSmoother.Update(aiAgent.Heading());
        }

        //moveState
        if (steeringBehaviour.IsSteering())
        {
            float epsillon = 0.01f;

            if (MathUtil.Equal(aiAgent.Velocity(), Vector3.zero, epsillon) &&
                OldVelocity.magnitude >= epsillon)
            {
                if (onMoveComplete != null)
                {
                    onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Reached);
                }
            }
            else if (gravityaccel.magnitude <= 0 &&
                     gravityaccel_old.magnitude > 0)
            {
                if (onMoveComplete != null)
                {
                    onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Landed);
                }
            }
        }

        gameObject.transform.position = aiAgent.VPos();

        if (useRotation)
        {
            Vector3 vDir = aiAgent.Heading();

            if (vDir.magnitude > 0)
            {
                gameObject.transform.rotation = Quaternion.LookRotation(vDir);
            }
        }

        Vector3 target = gameObject.transform.position + gameObject.transform.rotation * Vector3.forward * aiAgent.BRadius();

        gravityaccel_old = gravityaccel;

        DebugExtension.DebugCircle(target, 0.5f);
    }