/// <summary> /// Checks whether a given box is fully encapsulated by this box. /// </summary> public bool IsInsideOrOn(hwmBox2D box) { return(box.Min.x >= Min.x && box.Min.y >= Min.y && box.Max.x <= Max.x && box.Max.y <= Max.y); }
internal AABBEnumerator(Node rootNode, hwmBox2D aabb) { m_RootNode = rootNode; m_AABB = aabb; m_ChilderIndex = 0; Current = null; }
public void DoUpdateCollide() { hwmBox2D collideAABB = hwmBox2D.BuildAABB(m_Head.GetPosition(), new Vector2(m_Head.Radius, m_Head.Radius)); hwmSphere2D headSphere = new hwmSphere2D(m_Head.GetPosition(), m_Properties.HeadColliderRadius); if ((slConstants.DAMAGETYPE_WALL & m_EnableDamageType) != 0 && !slWorld.GetInstance().GetMap().GetMapBox().IsInsideOrOn(m_Head.AABB)) { m_AliveState = AliveState.DeadHitWall; return; } hwmQuadtree <QuadtreeElement> .AABBEnumerator enumerator = new hwmQuadtree <QuadtreeElement> .AABBEnumerator(m_Head.OwnerQuadtree.GetRootNode() , collideAABB); while (enumerator.MoveNext()) { hwmQuadtree <QuadtreeElement> .Node iterNode = enumerator.Current; hwmBetterList <QuadtreeElement> elements = iterNode.GetElements(); for (int iElement = elements.Count - 1; iElement >= 0; iElement--) { QuadtreeElement iterElement = elements[iElement]; if ((slConstants.DAMAGETYPE_SNAKE & m_EnableDamageType) != 0 && iterElement.Owner != m_Guid && iterElement.NodeType != slConstants.NodeType.Predict && (iterElement.GetPosition() - m_Head.GetPosition()).sqrMagnitude <= ((m_Head.Radius + iterElement.Radius) * (m_Head.Radius + iterElement.Radius))) { m_AliveState = AliveState.DeadHitSnake; } } } }
/// <summary> /// Checks whether a given box is fully encapsulated by this sphere. /// </summary> /// <param name="box"></param> /// <param name="tolerance">Tolerance Error Tolerance.</param> public bool IsInside(hwmBox2D box, float tolerance = hwmConstants.KINDA_SMALL_NUMBER) { float radiusSquared = Radius * Radius + tolerance * tolerance; return((box.Min - Center).sqrMagnitude <= radiusSquared && (box.Max - Center).sqrMagnitude <= radiusSquared); }
/// <summary> /// Checks whether the given bounding box intersects this bounding box. /// </summary> /// <param name="box">Other The bounding box to intersect with.</param> /// <returns>true if the boxes intersect, false otherwise.</returns> public bool Intersect(hwmBox2D box) { return(Min.x <= box.Max.x && Max.x >= box.Min.x && Min.y <= box.Max.y && Max.y >= box.Min.y); }
public bool TryFindContains(ref Node foundNode, hwmBox2D box, Node rootNode = null) { if (rootNode == null) { rootNode = m_Root; } return(rootNode.TryFindContains(ref foundNode, box)); }
public override bool Equals(object other) { if (!(other is hwmBox2D)) { return(false); } hwmBox2D box = (hwmBox2D)other; return(Max.Equals(box.Max) && Min.Equals(box.Min)); }
public void Initialize(Vector2 mapSize) { m_MapBox = hwmBox2D.BuildAABB(Vector2.zero, mapSize * 0.5f); // Presentation if (hwmWorld.GetInstance().NeedPresentation()) { m_Presentation = (Instantiate(hwmSystem.GetInstance().GetAssetLoader().LoadAsset(hwmAssetLoader.AssetType.Game, "MapPresentation")) as GameObject).GetComponent <slMapPresentation>(); m_Presentation.transform.SetParent(transform, false); m_Presentation.Initialize(this); } }
private void OnDrawGizmos() { if (Performance == null) { Performance = new hwmPerformanceStatistics(); Performance.Initialize(); } box = hwmBox2D.BuildAABB(transform.position, new Vector2(6400f, 5120f)); Quaternion rotation = transform.localRotation; Vector2 center = box.GetCenter(); { Gizmos.color = Color.black; Gizmos.DrawSphere(sphere, radius); Vector2 leftUp = new Vector2(box.Min.x, box.Max.y); Vector2 leftDown = box.Min; Vector2 rightUp = box.Max; Vector2 rightDown = new Vector2(box.Max.x, box.Min.y); Vector2 offset = center - hwmMath.QuaternionMultiplyVector(rotation, center); leftUp = hwmMath.QuaternionMultiplyVector(rotation, leftUp) + offset; leftDown = hwmMath.QuaternionMultiplyVector(rotation, leftDown) + offset; rightUp = hwmMath.QuaternionMultiplyVector(rotation, rightUp) + offset; rightDown = hwmMath.QuaternionMultiplyVector(rotation, rightDown) + offset; Gizmos.DrawLine(leftDown, leftUp); Gizmos.DrawLine(leftUp, rightUp); Gizmos.DrawLine(rightUp, rightDown); Gizmos.DrawLine(rightDown, leftDown); Gizmos.color = Color.blue; hwmUtility.GizmosDrawBox2D(box); } { Vector2 sp = sphere; //Vector2 offset = center - hwmMath.QuaternionMultiplyVector(Quaternion.Inverse(rotation), center); // 红色矩形绕原点旋转后,中心坐标的偏移 //sp = hwmMath.QuaternionMultiplyVector(Quaternion.Inverse(rotation), sp) // 黑色圆绕原点旋转后的坐标 // + offset; sp = center + hwmMath.QuaternionMultiplyVector(Quaternion.Inverse(rotation), (sp - center)); Gizmos.color = box.IntersectSphere(sp, radius * radius) ? Color.red : Color.green; Gizmos.DrawSphere(sp, radius); } if (Count++ == 100) { Count = 0; Performance.LogAndRecord(); } }
public void GetAllIntersectNode(ref List <Node> allNode, hwmBox2D aabb) { if (m_LooseBox.Intersect(aabb)) { allNode.Add(this); if (!m_IsLeaf) { m_Childers[0].GetAllIntersectNode(ref allNode, aabb); m_Childers[1].GetAllIntersectNode(ref allNode, aabb); m_Childers[2].GetAllIntersectNode(ref allNode, aabb); m_Childers[3].GetAllIntersectNode(ref allNode, aabb); } } }
public void Initialize(hwmQuadtree <T> owner, Node parent, hwmBox2D box, hwmQuadtreeChilderNodeIndex indexInParent) { bool isRoot = parent == null; m_Owner = owner; m_Parent = parent; m_IndexInParent = indexInParent; m_Box = box; m_LooseBox = new hwmBox2D(m_Box.Min - m_Owner.m_LooseSize, m_Box.Max + m_Owner.m_LooseSize); m_IsLeaf = true; m_Depth = isRoot ? 1 : parent.m_Depth + 1; m_Elements = new hwmBetterList <T>(); }
public static void GizmosDrawBox2D(hwmBox2D box, float z = 0) { #if UNITY_EDITOR Vector3 leftDown = box.Min; leftDown.z = z; Vector3 rightUp = box.Max; rightUp.z = z; Vector3 leftUp = new Vector3(leftDown.x, rightUp.y, z); Vector3 rightDown = new Vector3(rightUp.x, leftDown.y, z); Gizmos.DrawLine(leftDown, leftUp); Gizmos.DrawLine(leftUp, rightUp); Gizmos.DrawLine(rightUp, rightDown); Gizmos.DrawLine(rightDown, leftDown); #endif }
private void CreateLine(Vector2 startPoint, Vector2 direction) { hwmBox2D mapBox = m_Owner.GetMapBox(); Vector2 mapSize = m_Owner.GetMapBox().GetSize(); Vector2 currentPoint = startPoint; while (mapBox.IsInsideOrOn(currentPoint)) { GameObject go = Instantiate(Line.gameObject); go.transform.parent = m_LineRoot.transform; go.transform.localScale = direction.x == 0 ? new Vector3(mapSize.x / LineSpriteSize.x * 100.0f, 1, 1) : new Vector3(1, mapSize.y / LineSpriteSize.y * 100.0f, 1); go.transform.localPosition = currentPoint; currentPoint += direction * CellSize; } }
public void Initialize(slMap map) { m_Owner = map; hwmBox2D mapBox = m_Owner.GetMapBox(); Vector3 mapSize = mapBox.GetSize(); Background.transform.localScale = new Vector3(mapSize.x + 1000.0f, mapSize.y + 1000.0f, 1); Mapground.transform.localScale = new Vector3(mapSize.x, mapSize.y, 1); m_LineRoot = new GameObject("LineRoot"); m_LineRoot.transform.SetParent(transform, false); CreateLine(Vector2.zero, Vector2.up); CreateLine(Vector2.zero, Vector2.down); CreateLine(Vector2.zero, Vector2.left); CreateLine(Vector2.zero, Vector2.right); Line.gameObject.SetActive(false); }
public static void GizmosDrawRotateBox2D(hwmBox2D box, Quaternion rotation, float z = 0) { #if UNITY_EDITOR Vector2 center = box.GetCenter(); Vector2 leftUp = new Vector2(box.Min.x, box.Max.y); Vector2 leftDown = box.Min; Vector2 rightUp = box.Max; Vector2 rightDown = new Vector2(box.Max.x, box.Min.y); Vector2 offset = center - hwmMath.QuaternionMultiplyVector(rotation, center); leftUp = hwmMath.QuaternionMultiplyVector(rotation, leftUp) + offset; leftDown = hwmMath.QuaternionMultiplyVector(rotation, leftDown) + offset; rightUp = hwmMath.QuaternionMultiplyVector(rotation, rightUp) + offset; rightDown = hwmMath.QuaternionMultiplyVector(rotation, rightDown) + offset; Gizmos.DrawLine(leftDown, leftUp); Gizmos.DrawLine(leftUp, rightUp); Gizmos.DrawLine(rightUp, rightDown); Gizmos.DrawLine(rightDown, leftDown); #endif }
public void Initialize(int maxDepth , int maxElementPerNode , int minElementPreParentNode , Vector2 looseSize , hwmBox2D worldBox) { hwmDebug.Assert(maxDepth > 0, "maxDepth > 0"); hwmDebug.Assert(maxElementPerNode > 0, "maxElementPerNode > 0"); hwmDebug.Assert(minElementPreParentNode > 0, "minElementPreParentNode > 0"); hwmDebug.Assert(maxElementPerNode > minElementPreParentNode, "maxElementPerNode > minElementPreParentNode"); hwmDebug.Assert(looseSize.x >= 0 && looseSize.y >= 0, "looseSize.x >= 0 && looseSize.y >= 0"); m_MaxDepth = maxDepth; m_MaxElementPerNode = maxElementPerNode; m_MinElementPreParentNode = minElementPreParentNode; m_LooseSize = looseSize; AutoMergeAndSplitNode = true; m_Root = new Node(); m_Root.Initialize(this, null, worldBox, hwmQuadtreeChilderNodeIndex.Root); }
public bool TryFindContains(ref Node foundNode, hwmBox2D box) { if (m_LooseBox.IsInsideOrOn(box)) { if (!m_IsLeaf) { for (int iChilder = 0; iChilder < CHILDER_COUNT; iChilder++) { if (m_Childers[iChilder].TryFindContains(ref foundNode, box)) { return(true); } } } foundNode = this; return(true); } else { return(false); } }
public void EatFood(float radius) { ms_FoodQuadtreeNodes.Clear(); hwmBox2D eatAABB = hwmBox2D.BuildAABB(m_Head.GetPosition(), new Vector2(radius, radius)); slWorld.GetInstance().GetFoodSystem().GetQuadtree().GetRootNode().GetAllIntersectNode(ref ms_FoodQuadtreeNodes, eatAABB); hwmSphere2D headSphere = new hwmSphere2D(m_Head.GetPosition(), radius); for (int iNode = 0; iNode < ms_FoodQuadtreeNodes.Count; iNode++) { hwmQuadtree <slFood> .Node iterNode = ms_FoodQuadtreeNodes[iNode]; hwmBetterList <slFood> foods = iterNode.GetElements(); bool inHeadSphere = headSphere.IsInside(iterNode.GetLooseBox()); for (int iFood = foods.Count - 1; iFood >= 0; iFood--) { slFood iterFood = foods[iFood]; if (inHeadSphere || (iterFood.GetPosition() - m_Head.GetPosition()).sqrMagnitude <= radius * radius) { EatFood(iterFood); } } } }
public void UpdateQuadtree() { Box = hwmBox2D.BuildAABB(m_Position, Extent); AABB = m_Rotation * Box; OwnerQuadtree.UpdateElement(this); }