Advertisement
GaelVanhalst

Novint Falcon Framework for Unity: ForceSystem

Jan 25th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class NovintFalconForces : MonoBehaviour
  5. {
  6.     private List<BaseForce> _forces=new List<BaseForce>();
  7.     [SerializeField] private Haptics _novintFalcon=null;
  8.     public int IndexNovintFalcon = 0;
  9.  
  10.     void LateUpdate ()
  11.     {
  12.         var time = Time.deltaTime;
  13.         var currentPos = _novintFalcon.GetServoPos(IndexNovintFalcon);
  14.         Vector3 totalForce=Vector3.zero;
  15.         for (int i = 0; i < _forces.Count;)
  16.         {
  17.             var force = _forces[i];
  18.             if (force.UpdateAndCheckForDeath(time,currentPos))
  19.             {
  20.                 _forces.RemoveAt(i);
  21.             }
  22.             else
  23.             {
  24.                 i++;
  25.             }
  26.  
  27.             totalForce += force.GetForce();
  28.         }
  29.  
  30.         if (float.IsNaN(totalForce.x)) totalForce.x = 0;
  31.         if (float.IsNaN(totalForce.y)) totalForce.y = 0;
  32.         if (float.IsNaN(totalForce.z)) totalForce.z = 0;
  33.         _novintFalcon.SetServo(totalForce, IndexNovintFalcon);
  34.     }
  35.  
  36.     public void AddNewForce(BaseForce force)
  37.     {
  38.         _forces.Add(force);
  39.     }
  40. }
  41.  
  42.  
  43. //Workspace
  44. public abstract class BaseForce
  45. {
  46.     protected BaseForceLife ForceLife;
  47.     protected BaseForceDirectionAndStrength ForceDirectionAndStrength;
  48.  
  49.     public virtual bool UpdateAndCheckForDeath(float deltaTime,Vector3 position)
  50.     {
  51.         ForceLife.Update(deltaTime);
  52.         TransformPosToWorld(ref position);
  53.         ForceDirectionAndStrength.Update(position);
  54.         return ForceLife.IsDeath();
  55.     }
  56.  
  57.     public Vector3 GetForce()
  58.     {
  59.         var force=ForceDirectionAndStrength.GetCompleteForce();
  60.         TransformForceToLocal(ref force);
  61.         return force;
  62.     }
  63.  
  64.     protected virtual void TransformForceToLocal(ref Vector3 force)
  65.     {
  66.     }
  67.  
  68.     protected virtual void TransformPosToWorld(ref Vector3 position)
  69.     {
  70.     }
  71. }
  72.  
  73. public class LocalForce : BaseForce
  74. {
  75.     public LocalForce(BaseForceLife forceLife, BaseForceDirectionAndStrength forceDirectionAndStrength)
  76.     {
  77.         ForceLife = forceLife;
  78.         ForceDirectionAndStrength = forceDirectionAndStrength;
  79.     }
  80. }
  81.  
  82. public class WorldForce : BaseForce
  83. {
  84.     public Transform Listener;
  85.     public float WorldToLocalDistanceRatio;
  86.     public WorldForce(Transform listener,float worldToLocalDistanceRatio, BaseForceLife forceLife, BaseForceDirectionAndStrength forceDirectionAndStrength)
  87.     {
  88.         Listener = listener;
  89.         WorldToLocalDistanceRatio = worldToLocalDistanceRatio;
  90.         ForceLife = forceLife;
  91.         ForceDirectionAndStrength = forceDirectionAndStrength;
  92.     }
  93.  
  94.     protected override void TransformForceToLocal(ref Vector3 force)
  95.     {
  96.         force= Listener.InverseTransformDirection(force);
  97.     }
  98.     protected override void TransformPosToWorld(ref Vector3 pos)
  99.     {
  100.         pos *= WorldToLocalDistanceRatio;
  101.         pos = Listener.TransformPoint(pos);
  102.     }
  103. }
  104.  
  105. //Death and life
  106. public abstract class BaseForceLife
  107. {
  108.     protected bool Death=false;
  109.  
  110.     public bool IsDeath()
  111.     {
  112.         return Death;
  113.     }
  114.  
  115.      public virtual void Update(float time)
  116.     {
  117.     }
  118. }
  119.  
  120. public class TimedForceLife:BaseForceLife
  121. {
  122.     private float _lifeTime;
  123.  
  124.     public TimedForceLife(float lifeTime)
  125.     {
  126.         _lifeTime = lifeTime+Time.deltaTime;
  127.     }
  128.     public override void Update(float time)
  129.     {
  130.         _lifeTime -= time;
  131.         if (_lifeTime <= 0) Death = true;
  132.     }
  133. }
  134.  
  135. public class UserControlledForceLife : BaseForceLife
  136. {
  137.     public void KillForce()
  138.     {
  139.         Death = true;
  140.     }
  141. }
  142.  
  143. //Direction and Strength
  144. public abstract class BaseForceDirectionAndStrength
  145. {
  146.     protected Vector3 Direction = new Vector3();
  147.     protected float Strength;
  148.  
  149.     public Vector3 GetDirection()
  150.     {
  151.         return Direction.normalized;
  152.     }
  153.  
  154.     public float GetStrength()
  155.     {
  156.         return Strength;
  157.     }
  158.  
  159.     public Vector3 GetCompleteForce()
  160.     {
  161.         return Direction.normalized * Strength;
  162.     }
  163.  
  164.     public virtual void SetStrength(float strength)
  165.     {
  166.         Strength = strength;
  167.     }
  168.     public virtual void Update(Vector3 currentPos)
  169.     {
  170.     }
  171. }
  172.  
  173. public class ForceDirectionAndFixedStrength: BaseForceDirectionAndStrength
  174. {
  175.     public ForceDirectionAndFixedStrength(Vector3 direction,float strength)
  176.     {
  177.         Direction = direction;
  178.         Strength = strength;
  179.     }
  180.  
  181.     public void SetDirection(Vector3 direction)
  182.     {
  183.         Direction = direction;
  184.     }
  185.  
  186.     public override void SetStrength(float strength)
  187.     {
  188.         Strength = strength;
  189.     }
  190. }
  191.  
  192. public class ForcePosDirectionAndFixedStrength : BaseForceDirectionAndStrength
  193. {
  194.     private Vector3 _goal;
  195.  
  196.     public Vector3 Goal
  197.     {
  198.         set
  199.         {
  200.             _goal = value;
  201.             Direction = Vector3.zero;
  202.         }
  203.         get { return _goal; }
  204.     }
  205.     public ForcePosDirectionAndFixedStrength(Vector3 goal,float strength)
  206.     {
  207.         Goal = goal;
  208.         Direction = Vector3.zero;
  209.         Strength = strength;
  210.     }
  211.  
  212.     public override void Update(Vector3 currentPos)
  213.     {
  214.         Direction = Goal - currentPos;
  215.     }
  216.  
  217.     public override void SetStrength(float strength)
  218.     {
  219.         Strength = strength;
  220.     }
  221. }
  222.  
  223. public class ForcePosDirectionAndLinearStrength : BaseForceDirectionAndStrength
  224. {
  225.     private Vector3 _goal;
  226.  
  227.     public Vector3 Goal
  228.     {
  229.         set
  230.         {
  231.             _goal = value;
  232.             Direction = Vector3.zero;
  233.         }
  234.         get { return _goal; }
  235.     }
  236.  
  237.     private float _baseStrength;
  238.     public float DistanceStrengthMultiplier;
  239.  
  240.     public ForcePosDirectionAndLinearStrength(Vector3 goal, float strength,float distanceStrengthMultiplier)
  241.     {
  242.         Goal = goal;
  243.         _baseStrength = strength;
  244.         DistanceStrengthMultiplier = distanceStrengthMultiplier;
  245.         Strength =0;
  246.     }
  247.  
  248.     public override void Update(Vector3 currentPos)
  249.     {
  250.         Direction = Goal - currentPos;
  251.         var distance = Direction.magnitude;
  252.         Strength = _baseStrength*DistanceStrengthMultiplier*distance;
  253.     }
  254.  
  255.  
  256.     public override void SetStrength(float strength)
  257.     {
  258.         _baseStrength = strength;
  259.     }
  260.  
  261. }
  262.  
  263. public class ForcePosDirectionAndExponentialStrength : BaseForceDirectionAndStrength
  264. {
  265.     private Vector3 _goal;
  266.     public Vector3 Goal
  267.     {
  268.         set
  269.         {
  270.             _goal = value;
  271.             Direction = Vector3.zero;
  272.         }
  273.         get { return _goal; }
  274.     }
  275.  
  276.     private float _baseStrength;
  277.     public float DistanceStrengthMultiplier;
  278.  
  279.     public ForcePosDirectionAndExponentialStrength(Vector3 goal, float strength, float distanceStrengthMultiplier)
  280.     {
  281.         Goal = _goal;
  282.         _baseStrength = strength;
  283.         DistanceStrengthMultiplier = distanceStrengthMultiplier;
  284.         Direction = Vector3.zero;
  285.         Strength = 0;
  286.     }
  287.  
  288.     public override void Update(Vector3 currentPos)
  289.     {
  290.         Direction = Goal - currentPos;
  291.         var distance = Direction.magnitude;
  292.         Strength = _baseStrength * (DistanceStrengthMultiplier*distance) * (DistanceStrengthMultiplier * distance);
  293.     }
  294.  
  295.     public override void SetStrength(float strength)
  296.     {
  297.         _baseStrength = strength;
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement