/// <summary> /// 判断两个圆形是否发生了碰撞 /// </summary> /// /// <param name="rect1"></param> /// <param name="rect2"></param> /// <returns></returns> public static bool IsCircToCirc(RectBox rect1, RectBox rect2) { Point middle1 = GetMiddlePoint(rect1); Point middle2 = GetMiddlePoint(rect2); float distance = middle1.DistanceTo(middle2); float radius1 = rect1.GetWidth() / 2; float radius2 = rect2.GetWidth() / 2; return (distance - radius2) < radius1; }
/// <summary> /// 检查矩形与圆形是否发生了碰撞 /// </summary> /// /// <param name="rect1"></param> /// <param name="rect2"></param> /// <returns></returns> public static bool IsRectToCirc(RectBox rect1, RectBox rect2) { float radius = rect2.GetWidth() / 2; Point middle = GetMiddlePoint(rect2); Point upperLeft = new Point(rect1.GetMinX(), rect1.GetMinY()); Point upperRight = new Point(rect1.GetMaxX(), rect1.GetMinY()); Point downLeft = new Point(rect1.GetMinX(), rect1.GetMaxY()); Point downRight = new Point(rect1.GetMaxX(), rect1.GetMaxY()); bool collided = true; if (!IsPointToLine(upperLeft, upperRight, middle, radius)) { if (!IsPointToLine(upperRight, downRight, middle, radius)) { if (!IsPointToLine(upperLeft, downLeft, middle, radius)) { if (!IsPointToLine(downLeft, downRight, middle, radius)) { collided = false; } } } } return collided; }
private bool Intersects(RectBox other) { RectBox box = other; Circle circle = this; if (box.Contains(x + radius, y + radius)) { return(true); } float x1 = box.GetX(); float y1 = box.GetY(); float x2 = box.GetX() + box.GetWidth(); float y2 = box.GetY() + box.GetHeight(); Line[] lines = new Line[4]; lines[0] = new Line(x1, y1, x2, y1); lines[1] = new Line(x2, y1, x2, y2); lines[2] = new Line(x2, y2, x1, y2); lines[3] = new Line(x1, y2, x1, y1); float r2 = circle.GetRadius() * circle.GetRadius(); Vector2f pos = new Vector2f(circle.GetCenterX(), circle.GetCenterY()); for (int i = 0; i < 4; i++) { float dis = lines[i].DistanceSquared(pos); if (dis < r2) { return(true); } } return(false); }
private void Load(Stream ins0, string tileSetsLocation) { screenRect = LSystem.screenRect; tilesLocation = tileSetsLocation; try { XMLDocument doc = XMLParser.Parse(ins0); XMLElement docElement = doc.GetRoot(); string orient = docElement.GetAttribute("orientation", ""); if (!"orthogonal".Equals(orient)) { throw new Exception( "Only orthogonal maps supported, found " + orient); } width = docElement.GetIntAttribute("width", 0); height = docElement.GetIntAttribute("height", 0); tileWidth = docElement.GetIntAttribute("tilewidth", 0); tileHeight = docElement.GetIntAttribute("tileheight", 0); XMLElement propsElement = (XMLElement)docElement .GetChildrenByName("properties"); if (propsElement != null) { props = new TMXProperty(); List<XMLElement> property = propsElement.List("property"); for (int i = 0; i < property.Count; i++) { XMLElement propElement = property[i]; string name = propElement.GetAttribute("name", null); string value_ren = propElement.GetAttribute("value", null); props.SetProperty(name, value_ren); } } if (loadTileSets) { TMXTileSet tileSet = null; TMXTileSet lastSet = null; List<XMLElement> setNodes = docElement.List("tileset"); for (int i_0 = 0; i_0 < setNodes.Count; i_0++) { XMLElement current = setNodes[i_0]; tileSet = new TMXTileSet(this, current, true); tileSet.index = i_0; if (lastSet != null) { lastSet.SetLimit(tileSet.firstGID - 1); } lastSet = tileSet; CollectionUtils.Add(tileSets, tileSet); } } List<XMLElement> layerNodes = docElement.List("layer"); for (int i_1 = 0; i_1 < layerNodes.Count; i_1++) { XMLElement current_2 = layerNodes[i_1]; TMXLayer layer = new TMXLayer(this, current_2); layer.index = i_1; CollectionUtils.Add(layers, layer); } List<XMLElement> objectGroupNodes = docElement .List("objectgroup"); for (int i_3 = 0; i_3 < objectGroupNodes.Count; i_3++) { XMLElement current_4 = objectGroupNodes[i_3]; TMXTileGroup objectGroup = new TMXTileGroup(current_4); objectGroup.index = i_3; CollectionUtils.Add(objectGroups, objectGroup); } defWidth = (int)(screenRect.GetWidth() / tileWidth); defHeight = (int)(screenRect.GetHeight() / tileHeight); } catch (Exception ex) { Console.Error.WriteLine(ex.StackTrace); throw new Exception("Failed to parse map", ex); } }
protected internal void DrawChar(SpriteBatch batch, float x, float y, RectBox glyph, RectBox cropping, LColor color) { batch.Draw(texture, x + cropping.x, y + cropping.y, glyph.GetWidth(), glyph.GetHeight(), glyph.GetX(), glyph.GetY(), glyph.GetWidth(), glyph.GetHeight(), color); }