private bool CircleSquareCheck(Entity circle, Entity square) { //square ComponentTransform squareTransformComponent = (ComponentTransform)square.FindComponent(ComponentTypes.COMPONENT_TRANSFORM); ComponentCollisionBox collisionBoxComponent = (ComponentCollisionBox)square.FindComponent(ComponentTypes.COMPONENT_COLLISION_BOX); Matrix4 squareTransform = squareTransformComponent.Transform; Vector4 point1 = squareTransform * new Vector4(collisionBoxComponent.Point1, 1); Vector4 point2 = squareTransform * new Vector4(collisionBoxComponent.Point2.X, point1.Y, point1.Z, 1); Vector4 point3 = squareTransform * new Vector4(point1.X, point1.Y, collisionBoxComponent.Point2.Z, 1); Vector4 point4 = squareTransform * new Vector4(collisionBoxComponent.Point2, 1); Vector3[] points = { point1.Xyz, point2.Xyz, point3.Xyz, point4.Xyz }; //circle ComponentTransform circleTransformComponent = (ComponentTransform)circle.FindComponent(ComponentTypes.COMPONENT_TRANSFORM); ComponentCollisionCircle collisionCircleComponent = (ComponentCollisionCircle)circle.FindComponent(ComponentTypes.COMPONENT_COLLISION_CIRCLE); Vector3 circlePosition = circleTransformComponent.Position; float radius = collisionCircleComponent.Radius * circleTransformComponent.Scale.X; //WARNING: radius scales off of X, not the largest scalar return(IsPointInPolygon(points, circlePosition) || IsIntersectingCircle(circlePosition, radius, point1.Xyz, point2.Xyz) || IsIntersectingCircle(circlePosition, radius, point2.Xyz, point3.Xyz) || IsIntersectingCircle(circlePosition, radius, point3.Xyz, point4.Xyz) || IsIntersectingCircle(circlePosition, radius, point4.Xyz, point1.Xyz)); }
private bool CircleCircleCheck(Entity circle1, Entity circle2) { //entity 1 Vector3 position1 = GetPosition(circle1); ComponentCollisionCircle collisionCircleComponent1 = (ComponentCollisionCircle)circle1.FindComponent(ComponentTypes.COMPONENT_COLLISION_CIRCLE); float radius1 = collisionCircleComponent1.Radius; //entity 2 Vector3 position2 = GetPosition(circle2); ComponentCollisionCircle collisionCircleComponent2 = (ComponentCollisionCircle)circle2.FindComponent(ComponentTypes.COMPONENT_COLLISION_CIRCLE); float radius2 = collisionCircleComponent2.Radius; //check for collision Vector3 vectorDistanceApart = position2 - position1; float scalarDistanceApart = vectorDistanceApart.Length; float radiiSum = radius1 + radius2; if (radiiSum < scalarDistanceApart) { return(true); } else { return(false); } }
public void CollideCircleCircle(int entityOne, Vector3 positionOne, ComponentCollisionCircle circleOne, int entityTwo, Vector3 positionTwo, ComponentCollisionCircle circleTwo) { float diffX = positionOne.X - positionTwo.X; float diffY = positionOne.Z - positionTwo.Z; float minDistance = circleOne.Radius() + circleTwo.Radius(); float distance = (float)Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2)); if (distance < minDistance) { collisionManager.CollisionDetected(entities[entityOne], entities[entityTwo]); } }
public void CollideCircleSquare(int entityOne, Vector3 positionOne, ComponentCollisionCircle circle, int entityTwo, Vector3 positionTwo, ComponentCollisionSquare square) { float circleX = positionOne.X; float circleY = positionOne.Z; float rectMinX = positionTwo.X - (square.Width() / 2); float rectMaxX = positionTwo.X + (square.Width() / 2); float rectMinY = positionTwo.Z - (square.Height() / 2); float rectMaxY = positionTwo.Z + (square.Height() / 2); float nearestX = Math.Min(rectMaxX, Math.Max(circleX, rectMinX)); float nearestY = Math.Min(rectMaxY, Math.Max(circleY, rectMinY)); //float nearestX = Math.Max(positionTwo.X + (square.Width() / 2), Math.Min(positionOne.X, positionTwo.X - (square.Width() / 2))); //float nearestY = Math.Max(positionTwo.Z + (square.Height() / 2), Math.Min(positionOne.Z, positionTwo.Z - (square.Height() / 2))); float diffX = circleX - nearestX; float diffY = circleY - nearestY; float distance = (float)Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2)); if (distance < circle.Radius()) { collisionManager.CollisionDetected(entities[entityOne], entities[entityTwo]); } }