public static float Distance(Vec2D a, Vec2D b) { float x = b[0] - a[0]; float y = b[1] - a[1]; return((float)Math.Sqrt(x * x + y * y)); }
public static float Length(Vec2D a) { float x = a[0]; float y = a[1]; return((float)Math.Sqrt(x * x + y * y)); }
public static Vec2D Lerp(Vec2D o, Vec2D a, Vec2D b, float f) { float ax = a[0]; float ay = a[1]; o[0] = ax + f * (b[0] - ax); o[1] = ay + f * (b[1] - ay); return(o); }
public static Vec2D TransformMat2(Vec2D o, Vec2D a, Mat2D m) { float x = a[0]; float y = a[1]; o[0] = m[0] * x + m[2] * y; o[1] = m[1] * x + m[3] * y; return(o); }
static public void Scale(Mat2D o, Mat2D a, Vec2D v) { float a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], v0 = v[0], v1 = v[1]; o[0] = a0 * v0; o[1] = a1 * v0; o[2] = a2 * v1; o[3] = a3 * v1; o[4] = a4; o[5] = a5; }
static public void GetScale(Mat2D m, Vec2D s) { float x = m[0]; float y = m[1]; s[0] = Math.Sign(x) * (float)Math.Sqrt(x * x + y * y); x = m[2]; y = m[3]; s[1] = Math.Sign(y) * (float)Math.Sqrt(x * x + y * y); }
public static void Normalize(Vec2D result, Vec2D a) { float x = a[0]; float y = a[1]; float len = x * x + y * y; if (len > 0.0f) { len = (float)(1.0 / Math.Sqrt(len)); result[0] = a[0] * len; result[1] = a[1] * len; } }
static public float Decompose(Mat2D a, Vec2D s) { float a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; float det = a0 * a3 - a2 * a1; float sx = (float)Math.Sqrt(a0 * a0 + a1 * a1); float sy = (float)Math.Sqrt(a2 * a2 + a3 * a3); if (det < 0) { sx = -sx; } s[0] = sx; s[1] = sy; return((float)Math.Atan2(a1 / sx, a0 / sx)); }
public static Vec2D Scale(Vec2D o, Vec2D a, float scale) { o[0] = a[0] * scale; o[1] = a[1] * scale; return(o); }
public static Vec2D Add(Vec2D o, Vec2D a, Vec2D b) { o[0] = a[0] + b[0]; o[1] = a[1] + b[1]; return(o); }
public static Vec2D Subtract(Vec2D o, Vec2D a, Vec2D b) { o[0] = a[0] - b[0]; o[1] = a[1] - b[1]; return(o); }
static public void Copy(Vec2D o, Vec2D a) { o[0] = a[0]; o[1] = a[1]; }
public Vec2D(Vec2D copy) : this() { m_Buffer[0] = copy.m_Buffer[0]; m_Buffer[1] = copy.m_Buffer[1]; }
public static float Dot(Vec2D a, Vec2D b) { return(a[0] * b[0] + a[1] * b[1]); }
public static void Negate(Vec2D a, Vec2D b) { a[0] = -b[0]; a[1] = -b[1]; }
public void Update() { if (m_Actor == null) { return; } if (m_IsRunningProperty != null) { // Just an example of how to detect if the character is running from the animated properties (not really necessary here but shows how you can use custom properties). // Debug.Log("RUNNING " + m_IsRunningProperty.Value); } float elapsedSeconds = Time.deltaTime; Nima.Actor actorInstance = m_Actor.ActorInstance; float scaleX = 1.0f; //ActorAsset.NimaToUnityScale; // Find cursor position in world space. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 actorLocalCursor = m_Actor.gameObject.transform.InverseTransformPoint(ray.origin); if (actorLocalCursor[0] < 0.0f) { scaleX *= -1.0f; actorLocalCursor[0] *= -1.0f; } actorInstance.Root.ScaleX = scaleX; // Advance idle animation first. if (m_Idle != null) { m_IdleTime = (m_IdleTime + elapsedSeconds) % m_Idle.Duration; m_Idle.Apply(m_IdleTime, m_Actor.ActorInstance, 1.0f); } // Update input. if (m_HorizontalSpeed != -1.0f && (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))) { m_HorizontalSpeed = -1.0f; } else if (m_HorizontalSpeed == -1.0f && (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))) { m_HorizontalSpeed = 0.0f; } if (m_HorizontalSpeed != 1.0f && (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))) { m_HorizontalSpeed = 1.0f; } else if (m_HorizontalSpeed == 1.0f && (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))) { m_HorizontalSpeed = 0.0f; } if (!m_IsRunning && (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))) { m_IsRunning = true; } else if (m_IsRunning && (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))) { m_IsRunning = false; } if (m_HorizontalSpeed != 0.0f) { if (m_IsRunning) { if (m_WalkMix > 0.0f) { m_WalkMix = Math.Max(0.0f, m_WalkMix - elapsedSeconds * MixSpeed); } if (m_RunMix < 1.0f) { m_RunMix = Math.Min(1.0f, m_RunMix + elapsedSeconds * MixSpeed); } } else { if (m_WalkMix < 1.0f) { m_WalkMix = Math.Min(1.0f, m_WalkMix + elapsedSeconds * MixSpeed); } if (m_RunMix > 0.0f) { m_RunMix = Math.Max(0.0f, m_RunMix - elapsedSeconds * MixSpeed); } } m_WalkToIdleTime = 0.0f; } else { if (m_WalkMix > 0.0f) { m_WalkMix = Math.Max(0.0f, m_WalkMix - elapsedSeconds * MixSpeed); } if (m_RunMix > 0.0f) { m_RunMix = Math.Max(0.0f, m_RunMix - elapsedSeconds * MixSpeed); } } float moveSpeed = m_IsRunning ? 11.0f : 6.0f; // m_Actor.gameObject.transform. // actorInstance.Root.X += m_HorizontalSpeed * elapsedSeconds * moveSpeed; float speedModifier = 1.0f; if (m_GroundSpeedProperty != null) { speedModifier = (m_IsRunning ? 1.0f - m_GroundSpeedProperty.Value : m_GroundSpeedProperty.Value) * 0.5f + 0.5f; } m_Actor.gameObject.transform.Translate(new Vector3(1.0f, 0.0f, 0.0f) * m_HorizontalSpeed * speedModifier * elapsedSeconds * moveSpeed); if (m_Walk != null && m_Run != null) { if (m_HorizontalSpeed == 0.0f && m_WalkMix == 0.0f && m_RunMix == 0.0f) { m_Walk.Time = 0.0f; //m_WalkTime = 0.0f; m_RunTime = 0.0f; } else { //m_WalkTime = m_WalkTime + elapsedSeconds * 0.9f * (m_HorizontalSpeed > 0 ? 1.0f : -1.0f) * (scaleX < 0.0 ? -1.0f : 1.0f); m_Walk.Advance(elapsedSeconds * 0.9f * (m_HorizontalSpeed > 0 ? 1.0f : -1.0f) * (scaleX < 0.0 ? -1.0f : 1.0f)); // Sync up the run and walk times. //m_WalkTime %= m_Walk.Duration; /*if(m_WalkTime < 0.0f) * { * m_WalkTime += m_Walk.Duration; * }*/ m_RunTime = (m_Walk.Time - m_Walk.MinTime) / m_Walk.MaxTime * m_Run.Duration; } if (m_WalkMix != 0.0f) { m_Walk.Apply(m_WalkMix); } if (m_RunMix != 0.0f) { m_Run.Apply(m_RunTime, actorInstance, m_RunMix); } if (m_WalkToIdle != null && m_HorizontalSpeed == 0.0f && m_WalkToIdleTime < m_WalkToIdle.Duration) { m_WalkToIdleTime += elapsedSeconds; m_WalkToIdle.Apply(m_WalkToIdleTime, actorInstance, Math.Min(1.0f, m_WalkToIdleTime / m_WalkToIdle.Duration)); //m_RunMix = m_WalkMix = 0.0; } } if (m_Aim != null) { // Figure out best aim position. Nima.Math2D.Vec2D actorTarget = new Nima.Math2D.Vec2D(actorLocalCursor[0], actorLocalCursor[1]); // Now actorTarget is in Nima root space. float maxDot = -1.0f; int bestIndex = 0; AimSlice[] lookup = m_HorizontalSpeed == 0.0f ? m_AimLookup : m_AimWalkingLookup; Nima.Math2D.Vec2D targetDir = new Nima.Math2D.Vec2D(); for (int i = 0; i < AimSliceCount; i++) { AimSlice aim = lookup[i]; Nima.Math2D.Vec2D.Subtract(targetDir, actorTarget, aim.point); Nima.Math2D.Vec2D.Normalize(targetDir, targetDir); float d = Nima.Math2D.Vec2D.Dot(targetDir, aim.dir); if (d > maxDot) { maxDot = d; bestIndex = i; } } float targetAimTime = bestIndex / (float)(AimSliceCount - 1) * m_Aim.Duration; m_AimAnimationTime += (targetAimTime - m_AimAnimationTime) * Math.Min(1.0f, elapsedSeconds * 10.0f); m_Aim.Apply(m_AimAnimationTime, m_Actor.ActorInstance, 1.0f); } }