/// <summary> /// Check if the drawer should be shown /// or hidden depending on how much of /// the drawer is visible on the screen. /// </summary> public void CheckVisibility() { Vector2 _showRange = rectTransform.sizeDelta.Abs() * Mathf.Abs(showRangePercent); Vector2 _hideRange = rectTransform.sizeDelta.Abs() * Mathf.Abs(hideRangePercent); Vector2 _currentPosition = rectTransform.anchoredPosition.Abs(); if (!isVisible) { if (drawerSide == UIDrawerSide.LEFT || drawerSide == UIDrawerSide.RIGHT) { if (Mathf.Abs(hidePosition.Abs().x - _currentPosition.x) >= _showRange.x) { Show(); } else { Hide(); } } else { if (Mathf.Abs(hidePosition.Abs().y - _currentPosition.y) >= _showRange.y) { Show(); } else { Hide(); } } return; } if (drawerSide == UIDrawerSide.LEFT || drawerSide == UIDrawerSide.RIGHT) { if (Mathf.Abs(showPosition.Abs().x - _currentPosition.x) >= _hideRange.x) { Hide(); } else { Show(); } } else { if (Mathf.Abs(showPosition.Abs().y - _currentPosition.y) >= _hideRange.y) { Hide(); } else { Show(); } } }
public void Abs_Test() { Vector2 vec1 = new Vector2(-1, -2); Vector2 vec2 = new Vector2(15, -15); Vector2 vec3 = new Vector2(-10, 99999); vec2.Abs(); vec3.Abs(); Assert.That(vec1.Abs() == new Vector2(1, 2)); Assert.That(vec2 == new Vector2(15, -15)); Assert.That(vec3.Abs() == new Vector2(10, 99999)); }
/// <summary> /// Opens the specified editor window (shows it with editor options handling for new windows). /// </summary> /// <param name="window">The window.</param> public void Open(EditorWindow window) { var newLocation = (DockState)Editor.Options.Options.Interface.NewWindowLocation; if (newLocation == DockState.Float) { // Check if there is a floating window that has the same size Vector2 defaultSize = window.DefaultSize; for (var i = 0; i < Editor.UI.MasterPanel.FloatingPanels.Count; i++) { var win = Editor.UI.MasterPanel.FloatingPanels[i]; if (Vector2.Abs(win.Size - defaultSize).LengthSquared < 100) { window.Show(DockState.DockFill, win); window.Focus(); return; } } window.ShowFloating(defaultSize); } else { window.Show(newLocation); } }
private void DragDrawer() { currentInputPosition = Input.mousePosition; if (uIDrawerSide == UIDrawerSide.LEFT || uIDrawerSide == UIDrawerSide.RIGHT) { //Debug.Log("Current Input: " + currentInputPosition + " Last Input " + lastInputPosition); deltaPosition.x = currentInputPosition.x - lastInputPosition.x; } else if (uIDrawerSide == UIDrawerSide.TOP || uIDrawerSide == UIDrawerSide.BOTTOM) { //Debug.Log("Current Input: " + currentInputPosition + " Last Input " + lastInputPosition); deltaPosition.y = currentInputPosition.y - lastInputPosition.y; } //Prevents calling MoveUiDrawer if not dragging. if (lastInputPosition != Vector2.zero) { if (Vector2.Distance(deltaPosition.Abs(), currentInputPosition - lastInputPosition) > 0.1f) { isDragging = true; } currentDrawer.MoveDrawer(deltaPosition); } lastInputPosition = currentInputPosition; }
/// <summary> /// Gets if a position is currently aligned to the grid. /// </summary> /// <param name="pos">The position to check if aligned to the grid.</param> /// <returns>True if aligned to the grid; otherwise false.</returns> public bool IsAligned(Vector2 pos) { pos = pos.Abs(); return ((int)Math.Round(pos.X) % (int)Math.Round(GridSize.X) == 0 && (int)Math.Round(pos.Y) % (int)Math.Round(GridSize.Y) == 0); }
CanvasGeometry GetSelectionGeometry(ICanvasResourceCreator resourceCreator, List <Vector2> points) { Vector2 start = points.First(); Vector2 end = points.Last(); switch (RegionSelectionMode) { case SelectionMode.Rectangle: { Vector2 min = Vector2.Min(start, end); Vector2 size = Vector2.Abs(start - end); return(CanvasGeometry.CreateRectangle(resourceCreator, min.X, min.Y, size.X, size.Y)); } case SelectionMode.Ellipse: { Vector2 center = (start + end) / 2; Vector2 radius = Vector2.Abs(start - end) / 2; return(CanvasGeometry.CreateEllipse(resourceCreator, center, radius.X, radius.Y)); } case SelectionMode.Freehand: { return(CanvasGeometry.CreatePolygon(resourceCreator, points.ToArray())); } default: throw new NotSupportedException(); } }
public static float BoxSDF(Vector2 point, Vector2 center, Vector2 halfSize) { point -= center; Vector2 d = point.Abs() - halfSize; return(Vector2.Max(d, Vector2.zero).magnitude + Mathf.Min(Mathf.Max(d.x, d.y), 0)); }
public void Adjust(Vector2 startPosition, Vector2 currentPosition) { Size = Vector2.Abs(startPosition - currentPosition); if (currentPosition.X <= startPosition.X) { Position = currentPosition.Y <= startPosition.Y ? currentPosition : new Vector2(currentPosition.X, startPosition.Y); } else { Position = currentPosition.Y <= startPosition.Y ? new Vector2(startPosition.X, currentPosition.Y) : startPosition; } _points = new Vector2[] { TopLeft, TopRight, BottomRight, BottomLeft }; }
//// Rect \\\\ public static float SignedDistance(Rect r, Vector2 p) { p = p - r.position; Vector2 q = p.Abs() - r.size * 0.5f; return(q.Max(Vector2.zero).magnitude + Mathf.Min(q.MaxComponent(), 0.0f)); }
/// <summary> /// Opens the specified item in dedicated editor window. /// </summary> /// <param name="item">The content item.</param> /// <param name="disableAutoShow">True if disable automatic window showing. Used during workspace layout loading to deserialize it faster.</param> /// <returns>Opened window or null if cannot open item.</returns> public EditorWindow Open(ContentItem item, bool disableAutoShow = false) { if (item == null) { throw new ArgumentNullException(); } // Check if any window is already editing this item var window = Editor.Windows.FindEditor(item); if (window != null) { window.Focus(); return(window); } // Find proxy object var proxy = Editor.ContentDatabase.GetProxy(item); if (proxy == null) { // Error Editor.Log("Missing content proxy object for " + item); return(null); } // Open try { window = proxy.Open(Editor, item); } catch (Exception ex) { Editor.LogWarning(ex); } if (window != null && !disableAutoShow) { // Check if there is a floating window that has the same size Vector2 defaultSize = window.DefaultSize; for (var i = 0; i < Editor.UI.MasterPanel.FloatingPanels.Count; i++) { var win = Editor.UI.MasterPanel.FloatingPanels[i]; // Check if size is similar if (Vector2.Abs(win.Size - defaultSize).LengthSquared < 100) { // Dock window.Show(DockState.DockFill, win); window.Focus(); return(window); } } // Show floating window.ShowFloating(defaultSize); } return(window); }
public static bool CanAttack(Queen white, Queen black) { var normalizedAbsolute = Vector2.Abs(Vector2.Normalize(new Vector2(Math.Abs(white.Row - black.Row), Math.Abs(white.Column - black.Column)))); return(new List <Vector2> { Vector2.UnitX, Vector2.UnitY, Vector2.Normalize(Vector2.One) }.Contains(normalizedAbsolute)); }
public override void Update(float frameTime) { Vector2 gasVel = IoCManager.Resolve <IMapManager>().GetGasVelocity(IoCManager.Resolve <IMapManager>().GetTileArrayPositionFromWorldPosition(Owner.Position)); if (gasVel.Abs() > weight) // Stop tiny wobbles (hack fix until we add weight) { Translate(Owner.Position.X + gasVel.X, Owner.Position.Y + gasVel.Y); } }
public static float OrientedBoxSDF(Vector2 point, Vector2 center, Vector2 rotation, Vector2 halfSize) { point -= center; //point = new Matrix2x2(rotation.x, -rotation.y, rotation.y, rotation.x).Multiply(point); point = point.InvertRotate(rotation);// new Matrix2x2(rotation.x, rotation.y, -rotation.y, rotation.x).Multiply(point); Vector2 d = point.Abs() - halfSize; return(Vector2.Max(d, Vector2.zero).magnitude + Mathf.Min(Mathf.Max(d.x, d.y), 0)); }
private Vector2 GetInput(float delta) { Vector2 inputVelocity = Vector2.Zero; if (!_sword.Swinging) { inputVelocity.x = Input.GetActionStrength("right") - Input.GetActionStrength("left"); inputVelocity.y = Input.GetActionStrength("down") - Input.GetActionStrength("up"); inputVelocity = inputVelocity.Normalized(); } if (inputVelocity == Vector2.Zero) { _isMoving = false; } else { _isMoving = true; if (inputVelocity.Abs() != Vector2.One) { _playerDirection = inputVelocity.Round(); } } if (_isMoving) { _aTreePlayback.Travel("Running"); _aTree.Set("parameters/AnimationNodeStateMachine/Idle/blend_position", inputVelocity); _aTree.Set("parameters/AnimationNodeStateMachine/Running/blend_position", inputVelocity); _currentSpeed += _acceleration * delta; if (_currentSpeed > _maxSpeed) { _currentSpeed = _maxSpeed; } float animationSpeed = 1 + 2 * (_currentSpeed / _maxSpeed); _aTree.Set("parameters/TimeScale/scale", animationSpeed); return(_velocity.MoveToward(inputVelocity * _currentSpeed, _acceleration * delta)); } _aTreePlayback.Travel("Idle"); _aTree.Set("parameter/playback", "Idle"); _currentSpeed = 0; return(_velocity.MoveToward(Vector2.Zero, _friction * delta)); }
public static Vector2 IntersectionWithRayFromCenter(this Rect rect, Vector2 pointOnRay) { Vector2 pointOnRay_local = pointOnRay - rect.center; Vector2 edgeToRayRatios = (rect.Max() - rect.center).DividedBy(pointOnRay_local.Abs()); return((edgeToRayRatios.x < edgeToRayRatios.y) ? new Vector2(pointOnRay_local.x > 0 ? rect.xMax : rect.xMin, pointOnRay_local.y * edgeToRayRatios.x + rect.center.y) : new Vector2(pointOnRay_local.x * edgeToRayRatios.y + rect.center.x, pointOnRay_local.y > 0 ? rect.yMax : rect.yMin)); }
/// <summary> /// Responsible for moving the game object. Should be called once a frame. /// <param name="frameTime">Time in seconds since the last update.</param> /// </summary> public virtual void Update(float frameTime) { var abs = Vector2.Abs(Center); //bounce off window edges if (abs.X > 1f || abs.Y > 1f) { Velocity = -Velocity; } Center += frameTime * Velocity; }
public void Vector2AbsTest() { Vector2 v1 = new Vector2(-2.5, 2.0); Vector2 v3 = Vector2.Abs(new Vector2(0.0, Double.NegativeInfinity)); Vector2 v = Vector2.Abs(v1); Assert.Equal(2.5, v.X); Assert.Equal(2.0, v.Y); Assert.Equal(0.0, v3.X); Assert.Equal(Double.PositiveInfinity, v3.Y); }
public static Vector2 ClosestInRange(this Vector2 origin, Vector2 dest, Vector2 length) { if (origin.Abs() - dest.Abs() < length.Abs()) { return(origin); } else { return(dest + dest.DirectionTo(origin) * length); } }
void UpdateSelectorGrid() { var min = Vector2.Min(GridPoint1, GridPoint2); var size = Vector2.Abs(GridPoint1 - GridPoint2); SelectorGrid.Width = size.X; SelectorGrid.Height = size.Y; Canvas.SetTop(SelectorGrid, min.Y); Canvas.SetLeft(SelectorGrid, min.X); }
public void Vector2AbsTest() { Vector2 v1 = new Vector2(-2.5f, 2.0f); Vector2 v3 = Vector2.Abs(new Vector2(0.0f, Single.NegativeInfinity)); Vector2 v = Vector2.Abs(v1); Assert.Equal(2.5f, v.X); Assert.Equal(2.0f, v.Y); Assert.Equal(0.0f, v3.X); Assert.Equal(Single.PositiveInfinity, v3.Y); }
/// <summary> /// Method which displace <see cref="Node"/> with new calculated position. /// </summary> /// <param name="node">Node to be displaced.</param> private void DisplaceNode(Node node) { Vector2 distanceVector = Vector2.Divide(node.Displacement, Vector2.Abs(node.Displacement)); float distanceValue = Vector2.Distance(new Vector2(0), node.Displacement); Vector2 ratio = Vector2.Divide(distanceVector, distanceValue); Vector2 displacment = Vector2.Multiply(ratio, distanceValue); node.Position = Vector2.Add(node.Position, displacment); node.Position.X = (float)Math.Min(mainCanvas.ActualWidth - 20, Math.Max(0, node.Position.X)); node.Position.Y = (float)Math.Min(mainCanvas.ActualHeight - 20, Math.Max(0, node.Position.Y)); }
public static Vector2 RayIntersectToCenter(this Rect rect, Vector2 pointOnRay, float extrude = 0f) { Vector2 pointOnRay_local = pointOnRay - rect.center; Vector2 edgeToRayRatios = (rect.Max() - rect.center).DividedBy(pointOnRay_local.Abs()); Vector2 result = (edgeToRayRatios.x < edgeToRayRatios.y) ? new Vector2(pointOnRay_local.x > 0 ? rect.xMax : rect.xMin, pointOnRay_local.y * edgeToRayRatios.x + rect.center.y) : new Vector2(pointOnRay_local.x * edgeToRayRatios.y + rect.center.x, pointOnRay_local.y > 0 ? rect.yMax : rect.yMin); return(result + (result - rect.center).normalized * extrude); }
public override void Start(Unit unit) { Arrived = false; unit.Direction = unit.CurrentPosition.DirectionTo(_target); unit.CurrentSpeed = 1.0; _velocity = Vector2.Subtract(_target, unit.CurrentPosition.ToVector2()); _distance = Vector2.Abs(_velocity); _velocity = Vector2.Normalize(_velocity); base.Start(unit); }
//this feature does not work! don't use it until fixed private bool IsIntersectWithCircleInternal(Vector2 circleCenter, float radiusSqr) { if (Range.Contains(circleCenter.X, circleCenter.Y)) //check if the center inside the range { return(true); } //algorithum reference: https://blog.csdn.net/noahzuo/article/details/52037151 Vector2 a = Vector2.Abs(circleCenter - center); Vector2 e = Vector2.Max(a - (new Vector2(Range.Right, Range.Top) - center), Vector2.Zero); return(Vector2.Dot(e, e) <= radiusSqr);//use dot instead of lengthsquart to prevent boxing }
public void AbsTest() { for (var x = -10; x < 10; x++) { for (var y = -10; y < 10; y++) { var v = new Vector2(x, y); v = v.Abs(); Assert.LessOrEqual(0, v.X); Assert.LessOrEqual(0, v.Y); } } }
/// <summary> /// Updates the <see cref="Entity"/>'s greatest velocity. /// </summary> /// <param name="currentVelocity">Current velocity.</param> void UpdateGreatestVelocity(Vector2 currentVelocity) { currentVelocity = currentVelocity.Abs(); if (currentVelocity.X > _greatestVelocity.X) { _greatestVelocity.X = currentVelocity.X; } if (currentVelocity.Y > _greatestVelocity.Y) { _greatestVelocity.Y = currentVelocity.Y; } }
public static int VectorAbs() { Vector2 A = new Vector2(-1f); Vector2 B = Vector2.Abs(A); if (!(CheckValue <float>(B.X, 1f))) { return(Fail); } if (!(CheckValue <float>(B.Y, 1f))) { return(Fail); } return(Pass); }
/// <summary> /// Initializes a new instance of the <see cref="EngineSettings"/> class. /// </summary> /// <param name="gravity">The world gravity. Only valid if not using a top-down perspective.</param> /// <param name="maxVelocity">The max velocity for an <see cref="Entity"/>.</param> /// <param name="soundListenerDepth">The <see cref="SoundListenerDepth"/>.</param> /// <param name="soundMinDistance">The <see cref="SoundMinDistance"/>.</param> /// <param name="soundAttenuation">The <see cref="SoundAttenuation"/>.</param> /// <param name="maxWallStepUpHeight">The <see cref="MaxWallStepUpHeight"/>.</param> /// <param name="soundUpdateRate">The <see cref="SoundUpdateRate"/>.</param> public EngineSettings(Vector2 gravity, Vector2 maxVelocity, float soundListenerDepth = 300f, float soundMinDistance = 600f, float soundAttenuation = 3f, int maxWallStepUpHeight = 5, uint soundUpdateRate = 1000u) { #if !TOPDOWN _gravity = gravity; _maxWallStepUpHeight = maxWallStepUpHeight; #endif _maxVelocity = maxVelocity.Abs(); _soundAttenuation = soundAttenuation; _soundMinDistance = soundMinDistance; _soundListenerDepth = soundListenerDepth; _soundUpdateRate = soundUpdateRate; }
private void RayCastBoxes(Node node, Ray input, List <Box> boxes) { if (node == null) { return; } // Build a bounding box for the segment. var segmentRect = Rect.FromMinMax( Vector2.Min(input.From, input.To), Vector2.Max(input.From, input.To)); if (!node.Rect.Overlaps(segmentRect)) { return; } var ray = input.To - input.From; Debug.Assert(ray.LengthSquared() > 0.0f); ray.Normalize(); // v is perpendicular to the segment. var v = new Vector2(-ray.Y, ray.X); var absV = Vector2.Abs(v); // Separating axis for segment (Gino, p80). // |dot(v, p1 - c)| > dot(|v|, h) var separation = Math.Abs(Vector2.Dot(v, input.From - node.Rect.Center)) - Vector2.Dot(absV, node.Rect.Extents); if (separation > 0.0f) { return; } if (node.IsLeaf) { boxes.Add(node.Box); } else { RayCastBoxes(node.Child_1, input, boxes); RayCastBoxes(node.Child_2, input, boxes); } }
private void ResizingOverview() { var size = Vector2.Abs(GridPoint1 - GridPoint2); if (size.X < OverviewBorder.MinWidth) { return; } if (size.Y < OverviewBorder.MinHeight) { return; } OverviewBorder.Width = size.X; OverviewBorder.Height = size.Y; SetScaleOffset(OverCanvas, DrawModel.EditData); OverCanvas.Invalidate(); }
/// <summary> /// Gets if a position is currently aligned to the grid. /// </summary> /// <param name="pos">The position to check if aligned to the grid.</param> /// <returns>True if aligned to the grid; otherwise false.</returns> public bool IsAligned(Vector2 pos) { pos = pos.Abs(); return (int)Math.Round(pos.X) % (int)Math.Round(GridSize.X) == 0 && (int)Math.Round(pos.Y) % (int)Math.Round(GridSize.Y) == 0; }
/// <summary> /// Handles collision for when this wall is a wall. /// </summary> /// <param name="map">The map that the <see cref="WallEntityBase"/> is on.</param> /// <param name="wall">The wall/platform that the <paramref name="other"/> entity has collided into.</param> /// <param name="other">The <see cref="Entity"/> that collided into this <see cref="WallEntityBase"/>.</param> /// <param name="displacement">The minimum transitional displacement to move the <paramref name="other"/> /// to make it no longer overlap this wall.</param> /// <param name="moveVector">Vector describing how the entity has moved.</param> static void HandleCollideIntoWall(IMap map, Entity wall, Entity other, Vector2 displacement, Vector2 moveVector) { // NOTE: This seems to be working fine now, but it seems like we should stop calling this for walls once we move the "other" entity. bool displaced = false; #if !TOPDOWN // Allow entities to walk up very small inclines. Makes no sense in top-down, so its used in sidescroller only. // Check if we have a displacement just on the X axis if (displacement.Y == 0) { // Check how far the "other" is from being on top of "this" var distFromThis = other.Max.Y - wall.Position.Y; // If they are not that far away from being on top of "this", then instead of displacing them horizontally away // from us, pop them up on top of us, effectively "stepping" up if (distFromThis > 0 && distFromThis < _maxWallStepUpHeight) { var horizontalOffset = (int)distFromThis + 1; var otherRect = other.ToRectangle(); otherRect.Y -= horizontalOffset; // Make sure that if we move them up on top of us, that they will not be colliding with any walls. If they will // be colliding with any walls, do NOT let them up here if (!map.Spatial.Contains<WallEntityBase>(otherRect, x => !x.IsPlatform)) { other.Move(new Vector2(0, -horizontalOffset)); displaced = true; } } } #endif if (ContainsWallAt(map, other.Position + displacement, other.Size)) { moveVector = -moveVector; // If we can, offset by the move vector, preferring the smaller translation direction bool moved = false; Vector2 absMoveVector = moveVector.Abs(); displacement = Vector2.Zero; // Check X, if x < y if (absMoveVector.X > 0 && (absMoveVector.Y <= float.Epsilon || absMoveVector.X < absMoveVector.Y)) { if (!ContainsWallAt(map, other.Position + new Vector2(moveVector.X, 0), other.Size)) { moved = true; displacement = new Vector2(moveVector.X, 0); } } // Check Y if (!moved && absMoveVector.Y > 0) { if (!ContainsWallAt(map, other.Position + new Vector2(0, moveVector.Y), other.Size)) { moved = true; displacement = new Vector2(0, moveVector.Y); } } // Check X if (!moved && absMoveVector.X > 0) { if (!ContainsWallAt(map, other.Position + new Vector2(moveVector.X, 0), other.Size)) { moved = true; displacement = new Vector2(moveVector.X, 0); } } if (!moved) { // Couldn't correct the movement? Send them back to where they came from! displacement = moveVector; } } // Move the other entity away from the wall using the MTD if (!displaced) { other.Move(displacement); } #if !TOPDOWN // Check for vertical collision if (displacement.Y != 0) { if (other.Velocity.Y >= 0 && displacement.Y < 0) { // Collision from falling (land on feet) other.SetVelocity(new Vector2(other.Velocity.X, 0.0f)); other.StandingOn = wall; } else if (other.Velocity.Y < 0 && displacement.Y > 0) { // Collision from rising (hit head) other.SetVelocity(new Vector2(other.Velocity.X, 0.0f)); } } #endif }
/// <summary> /// Updates the <see cref="Entity"/>'s greatest velocity. /// </summary> /// <param name="currentVelocity">Current velocity.</param> void UpdateGreatestVelocity(Vector2 currentVelocity) { currentVelocity = currentVelocity.Abs(); if (currentVelocity.X > _greatestVelocity.X) _greatestVelocity.X = currentVelocity.X; if (currentVelocity.Y > _greatestVelocity.Y) _greatestVelocity.Y = currentVelocity.Y; }