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 SquareSquareCheck(Entity square1, Entity square2) { //square1 ComponentCollisionBox collisionComponent1 = (ComponentCollisionBox)square1.FindComponent(ComponentTypes.COMPONENT_COLLISION_BOX); bool shouldScale = collisionComponent1.ShouldScale; Matrix4 transform1 = GetTransform(square1, shouldScale); Vector3 collision1_1 = collisionComponent1.Point1; Vector3 collision1_2 = collisionComponent1.Point2; //square2 ComponentCollisionBox collisionComponent2 = (ComponentCollisionBox)square2.FindComponent(ComponentTypes.COMPONENT_COLLISION_BOX); shouldScale = collisionComponent2.ShouldScale; Matrix4 transform2 = GetTransform(square2, shouldScale); Vector3 collision2_1 = collisionComponent2.Point1; Vector3 collision2_2 = collisionComponent2.Point2; Vector4 p1_1 = new Vector4(collision1_1, 1) * transform1; Vector4 p1_2 = new Vector4(collision1_2, 1) * transform1; Vector4 p2_1 = new Vector4(collision2_1, 1) * transform2; Vector4 p2_2 = new Vector4(collision2_2, 1) * transform2; // Collision x-axis? bool collisionX = p1_2.X >= p2_1.X && p1_1.X <= p2_2.X; // Collision z-axis? bool collisionZ = p1_2.Z >= p2_1.Z && p1_1.Z <= p2_2.Z; // Collision only if on both axes return(collisionX && collisionZ); }
private static XmlNode SaveComponentCollisionBox(XmlDocument file, ComponentCollisionBox component) { XmlNode collisionBoxNode = file.CreateElement("CollisionBox"); //point1 Vector3 point1 = component.Point1; XmlNode point1Node = file.CreateElement("point1"); collisionBoxNode.AppendChild(point1Node); XmlNode xNode = file.CreateElement("x"); xNode.InnerText = point1.X.ToString(); point1Node.AppendChild(xNode); XmlNode yNode = file.CreateElement("y"); yNode.InnerText = point1.Y.ToString(); point1Node.AppendChild(yNode); XmlNode zNode = file.CreateElement("z"); zNode.InnerText = point1.Z.ToString(); point1Node.AppendChild(zNode); //point2 Vector3 point2 = component.Point2; XmlNode point2Node = file.CreateElement("point1"); collisionBoxNode.AppendChild(point2Node); xNode = file.CreateElement("x"); xNode.InnerText = point2.X.ToString(); point2Node.AppendChild(xNode); yNode = file.CreateElement("y"); yNode.InnerText = point2.Y.ToString(); point2Node.AppendChild(yNode); zNode = file.CreateElement("z"); zNode.InnerText = point2.Z.ToString(); point2Node.AppendChild(zNode); return(collisionBoxNode); }