/////////////////////////////////////////////////////////////////////////// public static void SnapTo(this float f1, float f2, float epsilon = 0.001f) { if (f1.AlmostEquals(f2)) { f1 = f2; } }
/// <summary> /// Found it at: /// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect /// Algorithm from Graphics Gems, page 304. /// </summary> public static SegmentRelation RelationInfo( Vector2 firstP, Vector2 firstQ, Vector2 secondP, Vector2 secondQ, out Vector2 intersection, float epsilon = float.Epsilon) { Vector2 r = firstQ - firstP, s = secondQ - secondP; var w = secondP - firstP; float rXs = r.Cross(s); float wXr = w.Cross(r); if (rXs.AlmostEquals(0, epsilon)) { if (wXr.AlmostEquals(0, epsilon)) { float rds = Vector2.Dot(r, s); float sqrr = r.sqrMagnitude; float t0 = Vector2.Dot(w, r) / sqrr; float t1 = t0 + rds / sqrr; if (t0 < 0 && t1 > 1) { intersection = firstP; return(SegmentRelation.CollinearOverlap); } else if (t0 >= 0 && t0 <= 1) { intersection = firstP + t0 * r; return(SegmentRelation.CollinearOverlap); } else if (t1 >= 0 && t1 <= 1) { intersection = firstP + t1 * r; return(SegmentRelation.CollinearOverlap); } else { intersection = firstP + t0 * r; return(SegmentRelation.CollinearDisjoint); } } else { intersection = Vectors.NaN2; return(SegmentRelation.Parallel); } } else { float t = w.Cross(s) / rXs; float u = wXr / rXs; intersection = firstP + t * r; if (t >= 0 && t <= 1 && u >= 0 && u <= 1) { return(SegmentRelation.Intersect); } else { return(SegmentRelation.Disjoint); } } }
/// <summary> /// Tilts the vector <paramref name="direction"/> randomly at an angle of <paramref name="angle"/>. /// Imagine a random plane that is created aligned to <paramref name="direction"/>, then this method /// will rotate <paramref name="direction"/> by <paramref name="angle"/>. /// </summary> public static Float3 RandomTilt(Float3 direction, float angle) { if (angle.AlmostEquals()) { return(direction); } var axis = Quaternion.FromToRotation(Float3.forward, direction) * Float2.right.Rotate(RandomHelper.Range(360f)).U(); return(Quaternion.AngleAxis(angle, axis) * direction); }
/// <summary> /// <paramref name="acceleration"/> is how fast will input speed up/slow down /// 0 means no speed change (linear); negative means slowing down; positive means speeding up /// </summary> public static float Ease(float input, float acceleration) { CheckRange(ref input); if (acceleration.AlmostEquals()) { return(input); } if (acceleration > 0f) { return((float)Math.Pow(input, acceleration + 1f)); } return(1f - (float)Math.Pow(1f - input, -acceleration + 1f)); }
public override void OnUpdate() { Vector3 dir = ((GameObjectParameter)owner.GetParameter(target)).Position - owner.transform.position; dir.y = 0; float angle = FindAngle(owner.transform.forward, dir, owner.transform.up); float angularSpeed = angle / 0.8f; if (!lastAngularSpeed.AlmostEquals(Mathf.Abs(angularSpeed), 0.001f)) { lastAngularSpeed = angularSpeed; ((FloatParameter)owner.GetParameter(store)).Value = angularSpeed; } Finish(); }
IEnumerator StepToHide() { while (true) { float delta = hidenPosY - transform.localPosition.y; if (delta.AlmostEquals(0, 0.1f)) { break; } float stepY = delta / 3; transform.transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + stepY, transform.localPosition.z); yield return(null); } Refresh(); StartCoroutine(StepToShow()); }
void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); m_rigidbody.velocity = movement * speed; m_rigidbody.position = new Vector3 ( Mathf.Clamp(m_rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(m_rigidbody.position.z, boundary.zMin, boundary.zMax) ); //tell the animator if we are moving if (animator != null) { animator.SetBool("IsMoving", !(moveHorizontal.AlmostEquals(0f) && moveVertical.AlmostEquals(0f))); } }
public void Update(GameTime gameTime) { if (_currentCameraLocation != _futureCameraLocation) { rotateAlongRing(); if (_currentPosition.AlmostEquals(_futurePosition, _turnSpeed)) { _currentCameraLocation = _futureCameraLocation; _currentPosition = this.allowableCameraPositions[(int)_currentCameraLocation]; calculateRelativeDirectionOffset(); _isAcceptingCommands = true; } } else if (_currentTarget != _futureTarget) { moveSmoothly(); if (_currentTarget.AlmostEquals(_futureTarget, _moveSpeed)) { _currentTarget = _futureTarget; _currentPosition = this.allowableCameraPositions[(int)_currentCameraLocation]; _isAcceptingCommands = true; } } else if (_cameraBoxSize != _futureCameraBoxSize) { zoomSmoothly(); if (_cameraBoxSize.AlmostEquals(_futureCameraBoxSize, _zoomSpeed)) { _currentPosition = this.allowableCameraPositions[(int)_currentCameraLocation]; _cameraBoxSize = _futureCameraBoxSize; _isAcceptingCommands = true; } } recalculateViewMatrix(); recalculateProjectionMatrix(); }
public void endGame(bool winningTeam) { if (!gameOver) { gameOver = true; gameoverObject.SetActive(true); cam.gameObject.GetComponent <SmoothMouseLook>().endGame(); gc.endGame(); if (elapsedTime.AlmostEquals(-1f, .01f)) { elapsedTime += Time.time - startTime; } else { elapsedTime += Time.time - startTime + 1; } //If victory if (winningTeam == redTeam) { Debug.Log("We won!"); victoryText.gameObject.SetActive(true); defeatText.gameObject.SetActive(false); } //If a loss else { Debug.Log("We lost!"); victoryText.gameObject.SetActive(false); defeatText.gameObject.SetActive(true); } gameOver = true; StartCoroutine(waitThenShowXP(winningTeam == redTeam)); } }
public static void DetermineAspectRatio() { //Standard = .56 or 1.78 // iPhoneX = .46 or 2.1 //iPad = .75 or 1.33 float aspectRatioDec = 0f; #if UNITY_EDITOR || UNITY_ANDROID #if UNITY_EDITOR string[] res = UnityStats.screenRes.Split('x'); Vector2 resolution = new Vector2(float.Parse(res[0]), float.Parse(res[1])); #else Vector2 resolution = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height); #endif aspectRatioDec = resolution.x / resolution.y; if (aspectRatioDec.AlmostEquals(.56f) || aspectRatioDec.AlmostEquals(1.78f)) { aspectRatio = AspectRatio.Standard; } else if (aspectRatioDec.AlmostEquals(.46f) || aspectRatioDec.AlmostEquals(2.1f)) { aspectRatio = AspectRatio.iPhoneX; } else if (aspectRatioDec.AlmostEquals(.75f) || aspectRatioDec.AlmostEquals(1.33f)) { aspectRatio = AspectRatio.Tablet; } #elif UNITY_IOS string deviceID = SystemInfo.deviceModel; if (iPhoneXDeviceIDs.Contains(deviceID)) { aspectRatio = AspectRatio.iPhoneX; } else if (deviceID.Contains("iPad")) { aspectRatio = AspectRatio.Tablet; } else { aspectRatio = AspectRatio.Standard; } #endif }
public static List <Vector2> ApproximateCircularArc(List <Vector2> points) { Vector2 a = points[0]; Vector2 b = points[1]; Vector2 c = points[2]; float aSq = (b - c).LengthSquared(); float bSq = (a - c).LengthSquared(); float cSq = (a - b).LengthSquared(); if (aSq.AlmostEquals(0, 1e-3f) || bSq.AlmostEquals(0, 1e-3f) || cSq.AlmostEquals(0, 1e-3f)) { return(new List <Vector2>()); } float s = aSq * (bSq + cSq - aSq); float t = bSq * (aSq + cSq - bSq); float u = cSq * (aSq + bSq - cSq); float sum = s + t + u; if (sum.AlmostEquals(0, 1e-3f)) { return(new List <Vector2>()); } Vector2 centre = (s * a + t * b + u * c) / sum; Vector2 dA = a - centre; Vector2 dC = c - centre; float r = dA.Length(); double thetaStart = Math.Atan2(dA.Y, dA.X); double thetaEnd = Math.Atan2(dC.Y, dC.X); while (thetaEnd < thetaStart) { thetaEnd += 2 * Math.PI; } double dir = 1; double thetaRange = thetaEnd - thetaStart; Vector2 orthoAtoC = c - a; orthoAtoC = new Vector2(orthoAtoC.Y, -orthoAtoC.X); if (Vector2.Dot(orthoAtoC, b - a) < 0) { dir = -dir; thetaRange = 2 * Math.PI - thetaRange; } int amountPoints = 2 * r <= 0.1f ? 2 : Math.Max(2, (int)Math.Ceiling(thetaRange / (2 * Math.Acos(1 - 0.1f / r)))); List <Vector2> output = new List <Vector2>(amountPoints); for (int i = 0; i < amountPoints; ++i) { double fract = (double)i / (amountPoints - 1); double theta = thetaStart + dir * fract * thetaRange; Vector2 o = new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * r; output.Add(centre + o); } return(output); }