public void RemTree(Vector3 v3) { Vector2L tree = new Vector2L(v3.X, v3.Z); bool found = false; int foundId = 0; for (int i = 0; i < Trees.Count; i++) { if (Trees[i] == tree) { foundId = Trees[i].ID; Trees.RemoveAt(i); found = true; break; } } if (!found) { return; } List <string> list = new List <string>(); list.Add("damageTreeCallback"); list.Add(foundId.ToString()); Map.SendAll(list); for (int i = 0; i < 4; i++) { Map.thrownOutItems.ThrowNewObject( 116, "砍掉的树干", "", v3.ADD(new Vector3(0, i + 3, 0))); } }
public Struct12(Point2L start, Point2L end, Vector2L delta) { this.point2L_0 = start; this.point2L_1 = end; this.vector2L_0 = delta; this.long_0 = this.vector2L_0.GetLengthSquared(); }
/// <summary> /// 判断两条线段是否相交 /// 现在有线段AB和线段CB //用线段AB的方向和C,D两点分别做差乘比较。如果C,D在同侧则return跳出 //用线段CD的方向和A,B两点分别做差乘比较。如果A,B在同侧则return跳出 //最终返回相交 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <param name="d"></param> /// <returns></returns> static bool IsTwoSegmentIntersection(Vector2L a2d, Vector2L b2d, Vector2L c2d, Vector2L d2d) { Vector3L a = new Vector3L(a2d.x, 0, a2d.y); Vector3L b = new Vector3L(b2d.x, 0, b2d.y); Vector3L c = new Vector3L(c2d.x, 0, c2d.y); Vector3L d = new Vector3L(d2d.x, 0, d2d.y); var crossA = FixPointMath.Sign(Vector3L.Cross(d - c, a - c).y); var crossB = FixPointMath.Sign(Vector3L.Cross(d - c, b - c).y); if (FixPointMath.Approximately(crossA, crossB)) { return(false); } var crossC = FixPointMath.Sign(Vector3L.Cross(b - a, c - a).y); var crossD = FixPointMath.Sign(Vector3L.Cross(b - a, d - a).y); if (FixPointMath.Approximately(crossC, crossD)) { return(false); } return(true); }
public bool Contains(Point2L point) { if (point.X > System.Math.Max(this.start.X, this.end.X) || point.X < System.Math.Min(this.start.X, this.end.X) || (point.Y > System.Math.Max(this.start.Y, this.end.Y) || point.Y < System.Math.Min(this.start.Y, this.end.Y))) { return(false); } if (this.start == this.end) { return(true); } Vector2L delta = this.GetDelta(); Vector2L u = point - this.start; Vector2L v = new Vector2L(-delta.Y, delta.X); if (Vector2L.DotProduct(u, v) != 0L) { return(false); } long num = Vector2L.DotProduct(u, delta); if (num < 0L) { return(false); } long lengthSquared = delta.GetLengthSquared(); return(num <= lengthSquared); }
/// <summary> /// 算法来自 game physics cookbook /// </summary> /// <param name="line"></param> /// <param name="circle"></param> /// <returns></returns> public static bool Line2dWithCircle2d(Line2d line, Circle2d circle) { Vector2L ab = line.m_point2 - line.m_point1; FloatL t = Vector2L.Dot(circle.m_pos - line.m_point1, ab) / Vector2L.Dot(ab, ab); Vector2L closestPoint = line.m_point1 + ab * t; return((circle.m_pos - closestPoint).sqrMagnitude < circle.m_radius * circle.m_radius); }
public static Vector2L ClampMagnitude(Vector2L vector, FloatL maxLength) { if (vector.sqrMagnitude > maxLength * maxLength) { return(vector.normalized * maxLength); } return(vector); }
static Vector2L GetAxisNormal(List <Vector2L> vertexArray, int pointIndex) { Vector2L pt1 = vertexArray[pointIndex]; Vector2L pt2 = pointIndex >= vertexArray.Count - 1 ? vertexArray[0] : vertexArray[pointIndex + 1]; Vector2L p = new Vector2L(-(pt2.y - pt1.y), pt2.x - pt1.x); p.Normalize(); return(p); }
public bool method_1(Point2L point) { long num = Vector2L.DotProduct(point - this.point2L_0, this.vector2L_0); if (num >= 0L) { return(num <= this.long_0); } return(false); }
public LongRational?GetNormalizedProjection(Point2L point) { if (this.start == this.end) { return(new LongRational?()); } Vector2L delta = this.GetDelta(); return(new LongRational?(new LongRational(Vector2L.DotProduct(point - this.start, delta), delta.GetLengthSquared()))); }
public override bool Equals(object other) { if (!(other is Vector2L)) { return(false); } Vector2L vector = (Vector2L)other; return(this.x.Equals(vector.x) && this.y.Equals(vector.y)); }
public static bool Segment2dWithCircle2d(Segment2d segment, Circle2d circle) { Segment3d segment3d = new Segment3d( new Vector3L(segment.m_point1.x, 0, segment.m_point1.y), new Vector3L(segment.m_point2.x, 0, segment.m_point2.y)); Vector3L closestPoint = Distance3d.ClosestPointOfPoint3dWithSegment3d(new Vector3L(circle.m_pos.x, 0, circle.m_pos.y), segment3d); Vector2L distance = circle.m_pos - new Vector2L(closestPoint.x, closestPoint.z); return(distance.magnitude <= circle.m_radius); }
public static Vector2L MoveTowards(Vector2L current, Vector2L target, FloatL maxDistanceDelta) { Vector2L a = target - current; FloatL magnitude = a.magnitude; if (magnitude <= maxDistanceDelta || magnitude == 0f) { return(target); } return(current + a / magnitude * maxDistanceDelta); }
public static bool Point2dWithLine2d(Vector2L point, Line2d line2d) { // Find the slope FloatL dy = (line2d.m_point2.y - line2d.m_point1.y); FloatL dx = (line2d.m_point2.x - line2d.m_point1.x); FloatL M = dy / dx; // Find the Y-Intercept FloatL B = line2d.m_point1.y - M * line2d.m_point1.x; // Check line equation return(FixPointMath.Approximately(point.y, M * point.x + B)); }
public static bool Point2dWithRectangle2d(Vector2L point2d, OrientedRectangle2d rectangle2d) { Vector3L forward = RotateHelper.GetForward(rectangle2d.m_rotation); Vector3L right = Vector3L.Cross(Vector3L.up, forward); Vector3L pos = new Vector3L(rectangle2d.m_pos.x, 0, rectangle2d.m_pos.y); Vector3L a = pos + forward * rectangle2d.m_length * 0.5f + -right * rectangle2d.m_width * 0.5f; Vector3L b = pos + forward * rectangle2d.m_length * 0.5f + right * rectangle2d.m_width * 0.5f; Vector3L c = pos + -forward * rectangle2d.m_length * 0.5f + right * rectangle2d.m_width * 0.5f; Vector3L d = pos + -forward * rectangle2d.m_length * 0.5f + -right * rectangle2d.m_width * 0.5f; return(IsInRectangle2d(a, b, c, d, new Vector3L(point2d.x, 0, point2d.y))); }
public bool ContainsProjection(Point2L point) { Vector2L delta = this.GetDelta(); long num = Vector2L.DotProduct(point - this.start, delta); if (num < 0L) { return(false); } long lengthSquared = delta.GetLengthSquared(); return(num * num <= lengthSquared); }
public static bool Point2dWithSector2d(Vector2L pos, Sector2d sector2d) { Vector2L distance = pos - sector2d.m_pos; if (distance.magnitude > sector2d.m_radius) { return(false); } Vector3L sectorForward = RotateHelper.GetForward(sector2d.m_rotation); FloatL cosTarget = Vector3L.Dot(sectorForward, distance) / sectorForward.magnitude / distance.magnitude; FloatL cosHalfDegree = FixPointMath.Cos((FixPointMath.Deg2Rad * sector2d.m_theraDegree / 2)); return(cosTarget > cosHalfDegree); }
static bool linesCross(Vector2L v0, Vector2L v1, Vector2L t0, Vector2L t1, ref Vector2L intersectionPoint) { if (v1 == t0 || v0 == t0 || v1 == t1 || v0 == t1) { return(false); } Vector2L vnormal = v1 - v0; vnormal = b2Cross(1.0f, vnormal); FloatL v0d = Vector2L.Dot(vnormal, v0); FloatL t0d = Vector2L.Dot(vnormal, t0); FloatL t1d = Vector2L.Dot(vnormal, t1); if (t0d > v0d && t1d > v0d) { return(false); } if (t0d < v0d && t1d < v0d) { return(false); } Vector2L tnormal = t1 - t0; tnormal = b2Cross(1.0f, tnormal); t0d = Vector2L.Dot(tnormal, t0); v0d = Vector2L.Dot(tnormal, v0); FloatL v1d = Vector2L.Dot(tnormal, v1); if (v0d > t0d && v1d > t0d) { return(false); } if (v0d < t0d && v1d < t0d) { return(false); } intersectionPoint = v0 + ((t0d - v0d) / (v1d - v0d)) * (v1 - v0); return(true); }
public static void Test() { Line2d line1 = new Line2d(); line1.m_point1 = new Vector2L(0, 0); line1.m_point2 = new Vector2L(100, 100); Line2d line2 = new Line2d(); line2.m_point1 = new Vector2L(0, 100); line2.m_point2 = new Vector2L(100, 0); Vector2L intersectionPoint = new Vector2L(); if (Line2dWithLine2d(line1, line2, ref intersectionPoint)) { Debug.Log("双线相交 交点 " + intersectionPoint); } else { Debug.Log("双线没有相交"); } List <Vector2L> pointList = new List <Vector2L> { new Vector2L(-4.5f, -10f), new Vector2L(-4.5f, 10f), new Vector2L(4.5f, 10f), new Vector2L(4.5f, -10f) }; Convex2d convex1 = new Convex2d(new Vector2L(-5, 0), QuaternionL.Euler(Vector3L.zero), pointList); Convex2d convex2 = new Convex2d(new Vector2L(+5, 0), QuaternionL.Euler(Vector3L.zero), pointList); Debug.Log("凸多边形相交测试1 " + (Convex2dWithConvex2d(convex1, convex2, false, true) != null)); Convex2d convex3 = new Convex2d(new Vector2L(+5, 0), QuaternionL.Euler(new Vector3L(0, -90, 0)), pointList); Debug.Log("凸多边形相交测试2 " + (Convex2dWithConvex2d(convex1, convex3, false, true) != null)); Circle2d circle = new Circle2d(new Vector3L(-5, 0), 5); Debug.Log("圆与凸多边形相交测试1 " + (Circle2dWithConvex2d(circle, convex2, false, true) != null)); Debug.Log("圆与凸多边形相交测试2 " + (Circle2dWithConvex2d(circle, convex3, false, true) != null)); }
public static bool Point2dWithPolygon2d(Vector2L pos, Polygon2d polygon2d) { var edgePoint = (polygon2d.m_pointList[1] + polygon2d.m_pointList[0]) * 0.5f; Vector2L outPoint = (edgePoint - pos).normalized * 10000; int count = 0; List <Vector2L> pointList = polygon2d.GetWorldPosList(); for (int i = 0; i < pointList.Count; i++) { var a = pointList[i % pointList.Count]; var b = pointList[(i + 1) % pointList.Count]; var r = IsTwoSegmentIntersection(a, b, pos, outPoint); if (r) { count += 1; } } return(count % 2 == 1); }
public void GenAll() { Trees.Clear(); Logger.WriteLine(LogLevel.Info, "开始生成全地形的树木"); for (int i = 0; i < TreeMap.GetLength(0); i++) { for (int j = 0; j < TreeMap.GetLength(1); j++) { if (TreeMap[i, j]) { Random rd = new Random(GetRandomSeed()); if (rd.Next(0, 20) == 5) { Vector2L vec2 = GetRealLocationForDetails(new Vector2Int(i, j)); Trees.Add(vec2); } } } } WriteTrees(); Logger.WriteLine(LogLevel.Info, "全地形树木生成成功"); }
public static Vector2L Max(Vector2L lhs, Vector2L rhs) { return(new Vector2L(FixPointMath.Max(lhs.x, rhs.x), FixPointMath.Max(lhs.y, rhs.y))); }
public static FloatL SqrMagnitude(Vector2L a) { return(a.x * a.x + a.y * a.y); }
public static FloatL Distance(Vector2L a, Vector2L b) { return((a - b).magnitude); }
public static FloatL Angle(Vector2L from, Vector2L to) { return(FixPointMath.Acos(FixPointMath.Clamp(Vector2L.Dot(from.normalized, to.normalized), -1, 1)) * 57.29578d); }
public static FloatL Dot(Vector2L lhs, Vector2L rhs) { return(lhs.x * rhs.x + lhs.y * rhs.y); }
public void Scale(Vector2L scale) { this.x *= scale.x; this.y *= scale.y; }
public static Vector2L Scale(Vector2L a, Vector2L b) { return(new Vector2L(a.x * b.x, a.y * b.y)); }
public static Vector2L Lerp(Vector2L from, Vector2L to, FloatL t) { t = FixPointMath.Clamp01(t); return(new Vector2L(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t)); }
/// <summary> /// 2d线和2d线相交 /// </summary> /// <param name="line2"></param> /// <returns></returns> // public static bool Line2dWithLine2d(Line2d line1, Line2d line2) // { // return Math.Abs(line1.m_slope - line2.m_slope) > Line2d.epsilon // || Math.Abs(line1.m_y_interept - line2.m_y_interept) < Line2d.epsilon; // } public static bool Line2dWithLine2d(Line2d line1, Line2d line2, ref Vector2L intersectionPoint) { return(linesCross(line1.m_point1, line1.m_point2, line2.m_point1, line2.m_point2, ref intersectionPoint)); }
/// Perform the cross product on a scalar and a vector. In 2D this produces /// a vector. static Vector2L b2Cross(FloatL s, Vector2L a) { return(new Vector2L(-s * a.y, s * a.x)); }