public bool CheckCollision(Actor actor) { if (this.cls != null && !this.cls.IsInstanceOfType(actor)) { return false; } else { float actorX = actor.GetX(); float actorY = actor.GetY(); if (actorX == this.x && actorY == this.y) { return false; } else { float ax = actor.GetX(); float ay = actor.GetY(); float dx; float dy; if (!this.diag) { dx = MathUtils.Abs(ax - this.x); dy = MathUtils.Abs(ay - this.y); return dx + dy <= this.distance; } else { dx = this.x - this.distance; dy = this.y - this.distance; float x2 = this.x + this.distance; float y2 = this.y + this.distance; return ax >= dx && ay >= dy && ax <= x2 && ay <= y2; } } } }
public bool Add(Actor actor) { if (this.Contains(actor)) { return false; } else { ++this.numActors; ActorSet.ListNode newNode = new ActorSet.ListNode(actor, this.listHeadTail.prev); int seq = actor.GetSequenceNumber(); if (this.numActors >= 2 * this.hashMap.Length) { this.Resize(); } else { int hash = seq % this.hashMap.Length; ActorSet.ListNode hashHead = this.hashMap[hash]; this.hashMap[hash] = newNode; newNode.SetHashListHead(hashHead); } this.code += seq; return true; } }
public bool CheckCollision(Actor actor) { obj0 = actor.GetRectBox(); dx = MathUtils.Abs(obj0.GetCenterX() - x); dy = MathUtils.Abs(obj0.GetCenterY() - y); dist = MathUtils.Sqrt(dx * dx + dy * dy); return dist <= this.r; }
public void AddObject(Actor actor) { Type cls = actor.GetType(); if (CollectionUtils.Contains(cls,this.collisionClasses)) { this.collisionChecker.AddObject(actor); } else { LinkedList classSet = (LinkedList) this.freeObjects.Get(cls); if (classSet == null) { classSet = new LinkedList(); this.freeObjects.Put(cls, classSet); } CollectionUtils.Add(classSet,actor); } }
public Actor[] ToArray() { Actor[] r = new Actor[numActors]; IIterator it = Iterator(); for (int i = 0; i < r.Length; i++) { if (!it.HasNext()) { return (Actor[])CollectionUtils.CopyOf(r, i); } r[i] = (Actor)it.Next(); } return it.HasNext() ? FinishToArray(r, it) : r; }
public ActorNode(Actor actor, BSPCollisionNode node) { this.actor = actor; this.node = node; ActorNode first = BSPCollisionChecker.GetNodeForActor(actor); this.next = first; BSPCollisionChecker.SetNodeForActor(actor, this); if (this.next != null) { this.next.prev = this; } this.mark = true; }
/// <summary> /// ��ӽ�ɫ��Layer(��Layer����ӵĽ�ɫ���Զ�������ײ���) /// </summary> /// /// <param name="object"></param> /// <param name="x"></param> /// <param name="y"></param> public void AddObject(Actor o, float x, float y) { if (isClose) { return; } lock (objects) { if (this.objects.Add(o)) { o.AddLayer(x, y, this); this.collisionChecker.AddObject(o); o.AddLayer(this); } } }
private static Actor[] FinishToArray(Actor[] r, IIterator it) { int i = r.Length; while (it.HasNext()) { int cap = r.Length; if (i == cap) { int newCap = ((cap / 2) + 1) * 3; if (newCap <= cap) { if (cap == int.MaxValue) newCap = int.MaxValue; } r = (Actor[])CollectionUtils.CopyOf(r, newCap); } r[i++] = (Actor)it.Next(); } return (i == r.Length) ? r : (Actor[])CollectionUtils.CopyOf(r, i); }
public bool CheckCollision(Actor other) { return (this.cls != null && !this.cls.IsInstanceOfType(other)) ? false : ((this.compareObject == null) ? true : other .Intersects(this.compareObject)); }
public bool CheckOnlyCollision(Actor other) { return ((this.compareObject == null) ? true : other .Intersects(this.compareObject)); }
public void Init(Type cls, Actor actor) { this.cls = cls; this.compareObject = actor; }
public Actor GetOnlyIntersectingObject(Actor actor, Type cls) { RectBox rect = this.GetActorBounds(actor); lock (this.actorQuery) { this.actorQuery.Init(cls, actor); ActorNode node = GetNodeForActor(actor); if (node == null) { return null; } do { BSPCollisionNode bspNode = node.GetBSPNode(); Actor result = this.GetOnlyObjectDownTree(actor, rect, this.actorQuery, bspNode); if (result != null) { return result; } result = this.GetOnlyIntersectingUp(rect, this.actorQuery, actor, bspNode.GetParent()); if (result != null) { return result; } node = node.GetNext(); } while (node != null); return this.GetOnlyIntersectingDown(rect, this.actorQuery, actor); } }
private void UpdateObject(Actor o) { ActorNode node = GetNodeForActor(o); if (node != null) { RectBox newBounds = this.GetActorBounds(o); BSPCollisionNode bspNode; if (!this.bspTree.GetArea().Contains(newBounds)) { for (; node != null;) { bspNode = node.GetBSPNode(); node.Remove(); this.CheckRemoveNode(bspNode); node = node.GetNext(); } this.AddObject(o); } else { RectBox bspArea; RectBox result1 = new RectBox(); RectBox result2 = new RectBox(); while (node != null) { bspNode = node.GetBSPNode(); bspArea = bspNode.GetArea(); if (bspArea.Contains(newBounds)) { for (ActorNode rNode2 = GetNodeForActor(o); rNode2 != null; rNode2 = rNode2 .GetNext()) { if (rNode2 != node) { BSPCollisionNode rNode1 = rNode2.GetBSPNode(); rNode2.Remove(); this.CheckRemoveNode(rNode1); } } return; } if (!bspArea.Intersects(newBounds)) { BSPCollisionNode rNode = node.GetBSPNode(); node.Remove(); this.CheckRemoveNode(rNode); } node.ClearMark(); node = node.GetNext(); } node = GetNodeForActor(o); if (node != null) { for (bspNode = node.GetBSPNode(); bspNode != null && !bspNode.GetArea().Contains(newBounds); bspNode = bspNode .GetParent()) { } if (bspNode == null) { while (node != null) { bspNode = node.GetBSPNode(); node.Remove(); this.CheckRemoveNode(bspNode); node = node.GetNext(); } this.AddObject(o); return; } } else { bspNode = this.bspTree; } bspArea = bspNode.GetArea(); this.InsertObject(o, newBounds, newBounds, bspArea, bspNode, result1, result2); for (node = GetNodeForActor(o); node != null; node = node .GetNext()) { if (!node.CheckMark()) { bspNode = node.GetBSPNode(); node.Remove(); this.CheckRemoveNode(bspNode); } } } } }
public ListNode(Actor actor, ActorSet.ListNode listTail) { this.actor = actor; this.next = listTail.next; this.prev = listTail; listTail.next = this; this.next.prev = this; }
private ActorSet.ListNode GetActorNode(Actor actor) { if (this.hashMap.Length == 0) { return null; } else { int seq = actor.GetSequenceNumber(); int hash = seq % this.hashMap.Length; ActorSet.ListNode hashHead = this.hashMap[hash]; if (hashHead == null) { return null; } else if (hashHead.actor == actor) { return hashHead; } else { for (ActorSet.ListNode curNode = hashHead.nextHash; curNode != hashHead; curNode = curNode.nextHash) { if (curNode.actor == actor) { return curNode; } } return null; } } }
public Actor GetOnlyIntersectingUp(RectBox r, CollisionQuery query, Actor actor, BSPCollisionNode start) { for (; start != null && !start.GetArea().Contains(r); ) { Actor res = this.CheckForOnlyCollision(actor, start, query); if (res != null) { return res; } start = start.GetParent(); } return null; }
private Actor GetOnlyIntersectingDown(RectBox r, CollisionQuery query, Actor actor) { if (this.bspTree == null) { return null; } else { lock (cacheNodeStack) { cacheNodeStack.Clear(); CollectionUtils.Add(cacheNodeStack, this.bspTree); int idx = 0; for (; !(cacheNodeStack.Count == 0) && idx < MAX_SIZE; ) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); if (node.GetArea().Contains(r)) { Actor res = this.CheckForOnlyCollision(actor, node, query); if (res != null) { return res; } BSPCollisionNode left = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left != null) { CollectionUtils.Add(cacheNodeStack, left); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } } } return null; } }
private Actor GetOnlyObjectDownTree(Actor ignore, RectBox r, CollisionQuery query, BSPCollisionNode startNode) { if (startNode == null) { return null; } else { lock (cacheNodeStack) { cacheNodeStack.Clear(); if (startNode != null) { CollectionUtils.Add(cacheNodeStack, startNode); } while (!(cacheNodeStack.Count == 0)) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); if (node.GetArea().Intersects(r)) { Actor res = this.CheckForOnlyCollision(ignore, node, query); if (res != null) { return res; } BSPCollisionNode left = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left != null) { CollectionUtils.Add(cacheNodeStack, left); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } } } return null; } }
private Actor CheckForOnlyCollision(Actor ignore, BSPCollisionNode node, CollisionQuery query) { if (node == null) { return null; } IIterator i = node.GetActorsIterator(); Actor candidate; do { if (!i.HasNext()) { return null; } candidate = (Actor)i.Next(); } while (ignore == candidate || !query.CheckCollision(candidate)); return candidate; }
public void UpdateObjectSize(Actor o) { this.UpdateObject(o); }
public void UpdateObjectLocation(Actor o, float oldX, float oldY) { this.UpdateObject(o); }
public void AddAll(Actor[] o) { int size = o.Length; this.numActors = size; this.Resize(); for (int i = 0; i < size; i++) { Actor actor = o[i]; ActorSet.ListNode newNode = new ActorSet.ListNode(actor, this.listHeadTail.prev); int seq = actor.GetSequenceNumber(); int hash = seq % this.hashMap.Length; ActorSet.ListNode hashHead = this.hashMap[hash]; this.hashMap[hash] = newNode; newNode.SetHashListHead(hashHead); this.code += seq; } }
public bool Contains(Actor actor) { return this.GetActorNode(actor) != null; }
public RectBox GetActorBounds(Actor actor) { return actor.GetBoundingRect(); }
public bool Remove(Actor actor) { ActorSet.ListNode actorNode = this.GetActorNode(actor); if (actorNode != null) { this.Remove(actorNode); this.code -= actor.GetSequenceNumber(); return true; } else { return false; } }
public Actor GetOnlyObjectAt(Actor o, float dx, float dy, Type cls) { lock (this.pointQuery) { float px = dx * this.cellSize + this.cellSize / 2; float py = dy * this.cellSize + this.cellSize / 2; this.pointQuery.Init(px, py, cls); object query = this.pointQuery; if (cls != null) { query = new CollisionClassQuery(cls, this.pointQuery); } return this.GetOnlyIntersectingDown(new RectBox(px, py, 1, 1), (CollisionQuery) query, o); } }
public static void SetNodeForActor(Actor o, ActorNode node) { o.data = node; }
public void RemoveObject(Actor o) { for (ActorNode node = GetNodeForActor(o); node != null; node = GetNodeForActor(o)) { BSPCollisionNode bspNode = node.GetBSPNode(); node.Remove(); this.CheckRemoveNode(bspNode); } }
public IList GetIntersectingObjects(Actor actor, Type cls) { RectBox r = this.GetActorBounds(actor); lock (this.actorQuery) { this.actorQuery.Init(cls, actor); return this.GetIntersectingObjects(r.ToFloat(), this.actorQuery); } }
public IList GetNeighbours(Actor actor, float distance, bool diag, Type cls) { float x = actor.GetX(); float y = actor.GetY(); float xPixel = x * this.cellSize; float yPixel = y * this.cellSize; float dPixel = distance * this.cellSize; float[] r = { xPixel - dPixel, yPixel - dPixel, dPixel * 2 + 1, dPixel * 2 + 1 }; lock (this.neighbourQuery) { this.neighbourQuery.Init(x, y, distance, diag, cls); IList res = this.GetIntersectingObjects(r, this.neighbourQuery); return res; } }