void IListenerErg.OnErgDataEvent(ErgDataEvent ergDataEvent)
    {
        // currentErgData = ergDataEvent.ergData;
        if (playerref.activeInHierarchy)
        {
            for (int i = 0; i < physicsManagers.Length; ++i)
            {
                Rigidbody rigidBody = players[i].GetComponent <Rigidbody>();

                float v = rigidBody.velocity.x;
                float x = rigidBody.position.x;

                InteroBody1D body = null;
                if (currentSegment != null)
                {
                    ErgData e = new ErgData();
                    e.Copy(ergDataEvent.ergData);
                    e.distance = currentSegment.getProgressedDistance(ergDataEvent.ergData);
                    body       = physicsManagers[i].UpdateLocation(x, v, e);
                    //hud.DisplayCurrentSegment(currentSegment, ergDataEvent.ergData);
                }
                else
                {
                    body = physicsManagers[i].UpdateLocation(x, v, ergDataEvent.ergData);
                }

                rigidBody.velocity = new Vector3(body.velocity, rigidBody.velocity.y, rigidBody.velocity.z);
                rigidBody.position = new Vector3(body.distance, rigidBody.position.y, rigidBody.position.z);
                // print(i+ "update " + body.distance);
            }
        }
    }
    public void UpdateRival(OSCErgDataEvent ergEvent)
    {
        if (rivals == null)
        {
            return;
        }
        print("UpdateRival index1 " + ergEvent.senderId);
        print("UpdateRival index2 " + rivals);
        GameObject     rival          = rivals[ergEvent.senderId];
        PhysicsManager physicsManager = physicsManagers[ergEvent.senderId];
        Rigidbody      rigidBody      = rival.GetComponent <Rigidbody>();
        float          v = rigidBody.velocity.x;
        float          x = rigidBody.position.x;
        // float z = ergEvent.senderId * 2.0f;

        ErgData e = new ErgData();

        e.Copy(ergEvent.ergData);
        e.distance = ergEvent.segment.getProgressedDistance(ergEvent.ergData);
        print("UpdateRival p Distance " + e.distance);
        if (e.distance < 0.1 && e.distance > -0.1)
        {
            physicsManager.ResetLocation();
        }

        InteroBody1D body = physicsManager.UpdateLocation(x, v, e);

        rigidBody.velocity = new Vector3(body.velocity, rigidBody.velocity.y, rigidBody.velocity.z);
        rigidBody.position = new Vector3(body.distance, rigidBody.position.y, rigidBody.position.z);
    }
Пример #3
0
    void IListenerErg.OnErgDataEvent(ErgDataEvent ergDataEvent)
    {
        // currentErgData = ergDataEvent.ergData;
        if (player.activeInHierarchy)
        {
            Rigidbody rigidBody = player.GetComponent <Rigidbody>();
            float     v         = rigidBody.velocity.x;
            float     x         = rigidBody.position.x;

            InteroBody1D body = null;

            /*
             *
             *  // update hud
             *  if (currentSegment != null)
             *  {
             *      ErgData e = new ErgData();
             *      e.Copy(ergDataEvent.ergData);
             *      e.distance = currentSegment.getProgressedDistance(ergDataEvent.ergData);
             *      // $"Hello, {name}! Today is {date.DayOfWeek},
             *
             *      print($"Progress d {e.distance}\t{x}\t{v}");
             *      // float d = currentSegment.getProgressedDistance(ergDataEvent.ergData);
             *      // print("loc xx erg " + ergDataEvent.ergData + "|"+d);
             *      // print(ergDataEvent.ergData);
             *
             *      body = physicsManager.UpdateLocation(x, v, e);
             *      hud.DisplayCurrentSegment(currentSegment, ergDataEvent.ergData);
             *      // physicsManager.se
             *
             *  }
             *  else*/
            if (currentSegment != null)
            {
                ErgData e = new ErgData();
                e.Copy(ergDataEvent.ergData);
                e.distance = currentSegment.getProgressedDistance(ergDataEvent.ergData);
                body       = physicsManager.UpdateLocation(x, v, e);
                hud.DisplayCurrentSegment(currentSegment, ergDataEvent.ergData);
            }
            else
            {
                body = physicsManager.UpdateLocation(x, v, ergDataEvent.ergData);
            }


            rigidBody.velocity = new Vector3(body.velocity, rigidBody.velocity.y, rigidBody.velocity.z);
            rigidBody.position = new Vector3(body.distance, rigidBody.position.y, rigidBody.position.z);
        }
    }
    public override InteroBody1D UpdateLocation(float realDistance, float realSpeed, ErgData e)
    {
        // UnityEngine.Debug.Log("Jojo");
        InteroBody1D targetLocation = new InteroBody1D(e);

        //return targetLocation;

        // InteroBody1D newLocation = new InteroBody1D(e);
        // first time?
        if (previousLocation == null)
        {
            // teleport to new location
            previousLocation = targetLocation;
            return(new InteroBody1D(e.distance, 0));
        }
        else
        {
            float dT = targetLocation.time - previousLocation.time;

            if (dT < 0.0001f)
            {
                previousLocation.copy(targetLocation);
                // mantain the old speed
                return(new InteroBody1D(realDistance, realSpeed));
            }

            float deltaDistance = targetLocation.distance - realDistance;
            float speed         = deltaDistance / dT;
            // Debug.Log($"delta\t{e.distance}\t{deltaDistance}\t{dT}");
            if (System.Math.Abs(deltaDistance) > 15)
            {
                // teleport to new location
                previousLocation.copy(targetLocation);
                return(new InteroBody1D(e.distance, realSpeed));
            }
            // PID controller
            float deltaForce = kD * speed + kP * (targetLocation.distance - realDistance);
            previousLocation.copy(targetLocation);

            if (deltaForce > 0.0f)
            {
                deltaForce = 0.0f;
            }

            InteroBody1D body = new InteroBody1D(realDistance, deltaForce);

            return(body);
        }
    }
Пример #5
0
    public override InteroBody1D UpdateLocation(float realDistance, float realSpeed, ErgData e)
    {
        // UnityEngine.Debug.Log("Jojo");
        InteroBody1D targetLocation = new InteroBody1D(e);

        return(targetLocation);

        /*
         * // InteroBody1D newLocation = new InteroBody1D(e);
         * // first time?
         * if (previousLocation == null)
         * {
         *  // teleport to new location
         *  previousLocation = targetLocation;
         *  return new InteroBody1D(e.distance, 0);
         * }
         * else
         * {
         *  float dT = targetLocation.time - previousLocation.time;
         *
         *  if (dT < 0.0001f)
         *  {
         *      previousLocation.copy(targetLocation);
         *      // mantain the old speed
         *      return new InteroBody1D(realDistance, realSpeed);
         *  }
         *
         *  float deltaDistance = targetLocation.distance - realDistance;
         *  float speed = deltaDistance / dT;
         *  // Debug.Log($"delta\t{e.distance}\t{deltaDistance}\t{dT}");
         *  if (System.Math.Abs(deltaDistance) > 15)
         *  {
         *      // teleport to new location
         *      previousLocation.copy(targetLocation);
         *      return new InteroBody1D(e.distance, realSpeed);
         *  }
         *  // PID controller
         *  float deltaForce = kD * speed + kP * (targetLocation.distance - realDistance);
         *  previousLocation.copy(targetLocation);
         *  return new InteroBody1D(realDistance, deltaForce);
         * }
         */
    }
 // this functions returns the new location
 // returns the new velocity of the object
 public override void ResetLocation()
 {
     previousLocation = null;
 }