static bool TestAngleBetween() { /// :CodeSample: Vec2.AngleBetween Vec2 directionA = new Vec2(1, 1); Vec2 directionB = new Vec2(-1, 1); float angle90 = Vec2.AngleBetween(directionA, directionB); directionA = new Vec2(1, 1); directionB = new Vec2(0, -2); float angleNegative135 = Vec2.AngleBetween(directionA, directionB); /// :End: directionA = new Vec2(1, 1); directionB = new Vec2(0, 1); float angle45 = Vec2.AngleBetween(directionA, directionB); Log.Info($"AngleBetween 45 == {angle45}, 90 == {angle90}, -135 == {angleNegative135}"); return(angle45 == 45 && angle90 == 90 && angleNegative135 == -135); }
private Vec2 GetDirectionAux(Formation f, Agent targetAgent) { if (f.IsMounted() && targetAgent != null && (double)targetAgent.Velocity.LengthSquared > (double)targetAgent.RunSpeedCached * (double)targetAgent.RunSpeedCached * 0.0900000035762787) { return(targetAgent.Velocity.AsVec2.Normalized()); } if (this.OrderEnum == FacingOrder.FacingOrderEnum.LookAtDirection) { return(this.lookAtDirection); } Vec2 currentPosition = f.CurrentPosition; Vec2 averageOfEnemies = Mission.Current.GetWeightedAverageOfEnemies(f.Team, currentPosition, true); if (!averageOfEnemies.IsValid) { return(f.Direction); } Vec2 vec2_1 = averageOfEnemies - currentPosition; Vec2 vec2_2 = vec2_1.Normalized(); vec2_1 = averageOfEnemies - currentPosition; float length = vec2_1.Length; int enemyUnitCount = f.QuerySystem.Team.EnemyUnitCount; int countOfUnits = f.CountOfUnits; Vec2 vector2 = f.Direction; bool flag = (double)length >= (double)countOfUnits * 0.200000002980232; if (enemyUnitCount == 0 || countOfUnits == 0) { flag = false; } float num = !flag ? 1f : MBMath.ClampFloat((float)countOfUnits * 1f / (float)enemyUnitCount, 0.3333333f, 3f) * MBMath.ClampFloat(length / (float)countOfUnits, 0.3333333f, 3f); if (flag && (double)Math.Abs(vec2_2.AngleBetween(vector2)) > 0.174532920122147 * (double)num) { vector2 = vec2_2; } return(vector2); }
bool TestVector2Angles() { float angle43 = Vec2.AngleBetween(Vec2.FromAngle(0), Vec2.FromAngle(43)); float angle16 = Vec2.AngleBetween(Vec2.FromAngle(27), Vec2.FromAngle(43)); float angle74 = Vec2.AngleBetween(Vec2.FromAngle(117), Vec2.FromAngle(43)); Log.Info($"Vec2.AngleBetween(Vec2.FromAngle(0 ), Vec2.FromAngle(43)): {angle43} - expected 43"); Log.Info($"Vec2.AngleBetween(Vec2.FromAngle(27 ), Vec2.FromAngle(43)): {angle16} - expected 16"); Log.Info($"Vec2.AngleBetween(Vec2.FromAngle(117), Vec2.FromAngle(43)): {angle74} - expected -74"); if (MathF.Abs(angle43 - 43) > tolerance) { return(false); } if (MathF.Abs(angle16 - 16) > tolerance) { return(false); } if (MathF.Abs(angle74 + 74) > tolerance) { return(false); } return(true); }
protected override object ProcessValue(object curValue) { Vec2 pos; float rot; if (curValue is ICharacterLikeMovement clm) { pos = clm.SmoothPosition; rot = clm.SmoothRotation; if (EnvironmentAPI.Input.MouseDirFromCameraCenter.Length > 0.01) { _visual.VisualPrefab.transform.rotation = Quaternion.Euler(0, Vec2.AngleBetween(EnvironmentAPI.Input.MouseDirFromCameraCenter, Vec2.New(0, 1)), 0); } } else if (curValue is IPositionedEntity pe) { pos = pe.Position; rot = pe.Rotation; } else { return(curValue); } if (_lastPos == default) { _lastPos = pos; _lastRot = rot; _visual._animator.SetFloat("dirX", 0); _visual._animator.SetFloat("dirY", 0); _visual.VisualPrefab.transform.rotation = Quaternion.identity; _visual._animator.SetBool("IsRun", false); } else { var velocity = (pos - _lastPos) / Time.deltaTime; //if (!(curValue is ICharacterLikeMovement) && ((pos - _lastPos).Length > 0.001)) // _visual.VisualPrefab.transform.rotation = Quaternion.Euler(0, Vec2.AngleBetween(pos - _lastPos, Vec2.New(0, 1)), 0); _lastPos = pos; if (curValue is ICharacterLikeMovement charMove) { lock (charMove.PhysicsBody.World) { var lv = charMove.PhysicsBody.LinearVelocity; velocity = new Vec2(lv.x, lv.y); } } //var mouseDir = EnvironmentAPI.Input.MouseDirFromCameraCenter; SFML.Graphics.Transform t = SFML.Graphics.Transform.Identity; float rotation = rot; _hasRunLerp = Mathf.Clamp(Mathf.Lerp(_hasRunLerp, velocity.Length > 0 ? 1 : 0, 0.3f), 0, 1); bool hasRun = _hasRunLerp > 0.5f; t.Rotate(360 - rotation); var tv = t.TransformPoint(-velocity.X, velocity.Y); var currentDir = new Vec2(tv.X, tv.Y); if (_lastDir == default) { _lastDir = currentDir; } else {//leeerp var newVec = System.Numerics.Vector2.Lerp( new System.Numerics.Vector2(_lastDir.X, _lastDir.Y), new System.Numerics.Vector2(currentDir.X, currentDir.Y), 0.1f); _lastDir = new Vec2(newVec.X, newVec.Y); currentDir = _lastDir; } if (curValue is ICharacterLikeMovement) { EnvironmentAPI.Draw.Text(new TextHandle() { Position = new Vec2(200, 400), Text = $"{currentDir} {rotation} {velocity}" }); } _visual._animator.SetFloat("dirX", -currentDir.X / _assumedMaxVelocity); _visual._animator.SetFloat("dirY", currentDir.Y / _assumedMaxVelocity); _visual._animator.SetBool("IsRun", hasRun); } return(curValue); }