public void EnforceNonPenetrationConstraint(DynamicGameEntity centralEntity) { foreach (DynamicGameEntity entity in dynamicEntities) { //make sure we don't check against the individual if (entity == centralEntity) { continue; } // calculate the distance between the positions of the entities Vector2 ToEntity = Vector2.Subtract(centralEntity.Pos, entity.Pos); float distFromEachOther = ToEntity.Length(); //if this distance is smaller than the sum of their radii then this entity must be moved away in the direction parallel to the ToEntity vector float amountOfOverlap = 10 + 10 - distFromEachOther; //move the entity a distance away equivalent to the amount of overlap if (amountOfOverlap >= 0) { centralEntity.Pos += Vector2.Multiply(Vector2.Divide(ToEntity, distFromEachOther), amountOfOverlap); } } }
public void TagNeighbours(DynamicGameEntity centralEntity, double radius) { foreach (DynamicGameEntity entity in dynamicEntities) { // Clear current tag. entity.Tag = false; // Calculate the difference in space Vector2 difference = Vector2.Subtract(entity.Pos, centralEntity.Pos); // When the entity is in range it gets tageed. if (entity != centralEntity && difference.LengthSquared() < radius * radius) { entity.Tag = true; } } }