void InstIntersections(int startId) { int id = startId + 1; for (int i = 0; i < tracks.Length; i++) { for (int j = i + 1; j < tracks.Length; j++) { for (int x = 1; x < tracks[i].lineRenderer.positionCount; x++) { for (int y = 1; y < tracks[j].lineRenderer.positionCount; y++) { if (Math2d.LineSegmentsIntersection(tracks[i].lineRenderer.GetPosition(x - 1), tracks[i].lineRenderer.GetPosition(x), tracks[j].lineRenderer.GetPosition(y - 1), tracks[j].lineRenderer.GetPosition(y), out Vector2 intersection)) { GameObject intersectionObj = Instantiate(intersectionPrefab); intersectionObj.transform.parent = transform; intersectionObj.transform.position = new Vector3(intersection.x, intersection.y, 1); intersectionObj.GetComponent <Intersection>().Init(id, tracks[i].lineRenderer, tracks[j].lineRenderer, i, j, x - 1, y - 1); id++; intersectionsObjs.Add(intersectionObj); } } } } } }
public static bool Intersect(this Rect _r, Vector2 p1, Vector2 p2, out Vector2 point) { bool result = false; point = new Vector2(); if (Math2d.LineSegmentsIntersection(p1, p2, _r.position, _r.position + Vector2.up * _r.height, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position, _r.position + Vector2.right * _r.width, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position + Vector2.up * _r.height, _r.position + _r.size, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position + Vector2.right * _r.width, _r.position + _r.size, out point)) { result = true; p2 = point; } point = p2; return(result); }
/// <summary> /// 通过自定义笔刷编辑地形 /// </summary> /// <param name="center">中心点</param> /// <param name="radius">半径</param> /// <param name="opacity">力度</param> /// <param name="brushIndex">笔刷索引</param> /// <param name="isRise">抬高还是降低</param> private async void InternalChangeHeightWithBrush(Vector3 center, float radius, float opacity, int brushIndex, bool isRise, bool regesterUndo, bool immediate) { if (!TryGetHeightMapCmd(center, radius, out HeightCmdData arg)) { return; } if (regesterUndo) { RegisterUndo(new HeightCmd(new HeightCmdData(arg))); } // 是否反转透明度 if (!isRise) { opacity = -opacity; } //修改高度图 //float[,] deltaMap = await Utility.BilinearInterp(brushDic[brushIndex], 2 * mapRadius, 2 * mapRadius); float[,] deltaMap = await Math2d.ZoomBilinearInterpAsync(brushDic[brushIndex], 2 *arg.mapRadiusX, 2 *arg.mapRadiusX); for (int i = 0; i < 2 * arg.mapRadiusX; i++) { for (int j = 0; j < 2 * arg.mapRadiusX; j++) { arg.heightMap[i, j] += deltaMap[i, j] * deltaHeight * opacity; } } // 重新设置高度图 SetHeightMap(arg.terrain, arg.heightMap, arg.startMapIndex.x, arg.startMapIndex.y, immediate); }
/// <summary> /// 是否和小方块有碰撞 /// </summary> /// <param name="origin">方块左下角</param> /// <param name="offset"></param> /// <param name="camera"></param> /// <returns></returns> private bool HitQuad(Vector3 origin, Vector3 dir0, Vector3 dir1, Camera camera) { Vector3 pos1 = origin + dir0; Vector3 pos2 = origin + dir0 + dir1; Vector3 pos3 = origin + dir1; Vector2 mousePos = Input.mousePosition; Vector2 screenOrigin = camera.WorldToScreenPoint(origin); Vector2 screenPos1 = camera.WorldToScreenPoint(pos1); Vector2 screenPos2 = camera.WorldToScreenPoint(pos2); Vector2 screenPos3 = camera.WorldToScreenPoint(pos3); //PhysicsMath.IsPointInsidePolygon //if (mousePos.x > Mathf.Max(screenOrigin.x, screenOffset.x) || // mousePos.x < Mathf.Min(screenOrigin.x, screenOffset.x) || // mousePos.y > Mathf.Max(screenOrigin.y, screenOffset.y) || // mousePos.y < Mathf.Min(screenOrigin.y, screenOffset.y)) // return false; //else // return true; return(Math2d.IsPointInsidePolygon(mousePos, new Vector2[] { screenOrigin, screenPos1, screenPos2, screenPos3 })); }
public void SendMove(float walkForward, float walkRight, float cameraPitch, float cameraYaw, float leftExtend, float rightExtend, bool jump, bool playDead, bool shooting) { if (host != null && host.isReady) { NetStream netStream = NetGame.BeginMessage(NetMsgId.Move); try { netStream.WriteNetId(netId); netStream.Write(NetFloat.Quantize(walkForward, 1f, 8), 8); netStream.Write(NetFloat.Quantize(walkRight, 1f, 8), 8); netStream.Write(NetFloat.Quantize(Math2d.NormalizeAngleDeg(cameraPitch), 180f, 9), 9); netStream.Write(NetFloat.Quantize(Math2d.NormalizeAngleDeg(cameraYaw), 180f, 9), 9); netStream.Write(NetFloat.Quantize(leftExtend, 1f, 5), 5); netStream.Write(NetFloat.Quantize(rightExtend, 1f, 5), 5); netStream.Write(jump); netStream.Write(playDead); netStream.Write(shooting); NetGame.instance.SendUnreliableToServer(netStream, -1); } finally { if (netStream != null) { netStream = netStream.Release(); } } } }
public override bool CheckInstantSuccess(bool playerInside) { Vector3 eulerAngles = body.rotation.eulerAngles; float z = eulerAngles.z; Vector3 eulerAngles2 = bodyStartRot.eulerAngles; return(Mathf.Abs(Math2d.NormalizeAngleDeg(z - eulerAngles2.z)) > angleDistanceSuccess); }
public void FixedUpdate() { int num = 0; float num2 = 0f; float num3 = 0f; Vector3 v = referenceTransform.TransformDirection(referenceAxis); for (int i = 0; i < Human.all.Count; i++) { Human human = Human.all[i]; Vector3 v2 = Quaternion.Euler(0f, human.controls.targetYawAngle, 0f) * Vector3.forward; float num4 = Math2d.SignedAngle(v.To2D(), v2.To2D()); int num5 = 0; num5 = ((-90f < num4 && num4 < 90f) ? 1 : (-1)); if (human.ragdoll.partLeftHand.sensor.grabObject == grabTarget) { num2 += num4; num3 += (float)num5; num++; } if (human.ragdoll.partRightHand.sensor.grabObject == grabTarget) { num2 += num4; num3 += (float)num5; num++; } } if (steerMode == SteerMode.Steer) { if (num > 0) { num2 /= (float)num; if (num3 >= 0f) { SetValue(num2 / (float)num); } else if (num2 < -90f) { SetValue(-180f - num2); } else { SetValue(180f - num2); } } } else { if (num3 < 2f) { num3 /= 4f; } SetValue(num3); } }
public bool Intersects(Triangle2D otherTriangle) { return (Math2d.CheckLinesIntersect(this.V1, this.V2, otherTriangle.V1, otherTriangle.V2) || Math2d.CheckLinesIntersect(this.V1, this.V2, otherTriangle.V2, otherTriangle.V3) || Math2d.CheckLinesIntersect(this.V1, this.V2, otherTriangle.V3, otherTriangle.V1) || Math2d.CheckLinesIntersect(this.V2, this.V3, otherTriangle.V1, otherTriangle.V2) || Math2d.CheckLinesIntersect(this.V2, this.V3, otherTriangle.V2, otherTriangle.V3) || Math2d.CheckLinesIntersect(this.V2, this.V3, otherTriangle.V3, otherTriangle.V1) || Math2d.CheckLinesIntersect(this.V3, this.V1, otherTriangle.V1, otherTriangle.V2) || Math2d.CheckLinesIntersect(this.V3, this.V1, otherTriangle.V2, otherTriangle.V3) || Math2d.CheckLinesIntersect(this.V3, this.V1, otherTriangle.V3, otherTriangle.V1)); }
// Update is called once per frame void Update() { if (originalSatelite.Count < 1) { return; } if (count2 >= originalSatelite.Count | (((100 - ((pastWaight * -1) / originalSatelite.Count) * 100) < 10) && count2 >= 20)) { StateOfMachine.Instance.SetSate = true; count2 = 0; } if (satelite.Count < 1) { if (wight < pastWaight) { bestObstions = new Vector3[lr.positionCount]; lr.GetPositions(bestObstions); nr_of_points_send += bestObstions.Length; nr_of_paths += 1; FindObjectOfType <GameServer>().SendPoint(bestObstions); pastWaight = wight; Satalite_manager.gameObject.SetActive(true); } pointFound.Clear(); pointFound.Add(originalSatelite[count2]); count = 0; wight = -originalSatelite.Count - 1; satelite = new List <Vector3>(originalSatelite); count2++; } else { Vector3 temp = CheckDistance(satelite, pointFound[pointFound.Count - 1]); pointFound.Add(temp); satelite.Remove(temp); for (int i = 0; i < pointFound.Count - 3; i += 2) { if (Math2d.LineSegmentsIntersection(pointFound[pointFound.Count - 2], pointFound[pointFound.Count - 1], pointFound[i], pointFound[i + 1], out Vector)) { wight += 1f; } } lr.positionCount = count + 1; lr.SetPosition(count, temp); count++; } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "ConnectLine") { ConnectLine otherLine = collision.GetComponent <ConnectLine>(); if (otherLine) { //交点を求める. Vector2 intersection; if (Math2d.LineSegmentsIntersection(this.point1.position, this.point2.position, otherLine.point1.position, otherLine.point2.position, out intersection)) { Instantiate(this.CrossedEffect, intersection, Quaternion.identity); } } } }
public void Update() { Vector2 lineStart = Helper.GetVector2(_lineStartTrans); Vector2 lineEnd = Helper.GetVector2(_lineEndTrans); Vector2 circlePos = Helper.GetVector2(_circlePosTrans); Vector2 r0; Vector2 r1; int rootCount = Math2d.LineCircleCollision(lineStart, lineEnd, circlePos, _radius, out r0, out r1); if (rootCount > 0) { _interTrans0.position = r0; _interTrans1.position = r1; } }
/// <summary> /// 画数据折线 /// </summary> private void DrawLine(VertexHelper vh) { foreach (var data in datas) { Vector2[] pixelPoints = new Vector2[data.Length]; for (int i = 0; i < data.Length; i++) { pixelPoints[i].x = (data[i].x - axleMinValue.x) / axleMaxValue.x * width; pixelPoints[i].y = (data[i].y - axleMinValue.y) / axleMaxValue.y * height; pixelPoints[i] += offset; } UIVertex[] verts = new UIVertex[4]; for (int i = 0; i < verts.Length; i++) { verts[i].color = lineColor; } for (int i = 0; i < pixelPoints.Length - 1; i++) { SetVerts(pixelPoints[i], pixelPoints[i + 1], lineWidth, verts); vh.AddUIVertexQuad(verts); } // 点状数据 //foreach (var item in pixelPoints) //{ // verts[0].position = item; // verts[1].position = new Vector2(5, 0) + item; // verts[2].position = new Vector2(5, 5) + item; // verts[3].position = new Vector2(0, 5) + item; // vh.AddUIVertexQuad(verts); //} void SetVerts(Vector2 _start, Vector2 _end, float _width, UIVertex[] _verts) { Vector2[] tmp = Math2d.GetRect(_start, _end, _width); _verts[0].position = tmp[0]; _verts[1].position = tmp[1]; _verts[2].position = tmp[3]; _verts[3].position = tmp[2]; } } }
/// <summary> /// Tells the camera the current available space for displaying your game. The camera will then build the largest rectangle that maintains a certain aspect ratio. /// Returns true iff the supplied display sizes are different from what was previously stored. /// </summary> public Boolean UpdateDisplayInformation(Int32 displayWidth, Int32 displayHeight) { Boolean displayChanged = false; if (m_currentFullDisplayHeight != displayHeight || m_currentFullDisplayWidth != displayWidth) { displayChanged = true; m_currentFullDisplayWidth = displayWidth; m_currentFullDisplayHeight = displayHeight; Position displayPosition = new Position(displayWidth / 2, displayHeight, displayWidth, displayHeight); Math2d.GetLargestRectangleThatFits(AspectRatio, displayPosition, out m_displayTargetPosition); // First, determine the scale factor from the logical viewport to the display viewport. // So if the display window is actually 8 times the size of the logical window, you'll have to scale everything by 8 m_currentScale = m_displayTargetPosition.Width / m_logicalPosition.Width; } return(displayChanged); }
public override void GetVertexs(ref List <UIVertex> vertexs) { if (m_Poses.Count < 2) { return; } int count = m_Poses.Count; Vector2 dir = Math2d.GetHorizontalDir((m_Poses[1] - m_Poses[0]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[0].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[0].XY() + dir * width, color = color, }); for (int i = 1; i < count - 1; i++) { dir = Math2d.GetHorizontalDir((m_Poses[i + 1] - m_Poses[i - 1]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[i].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[i].XY() + dir * width, color = color }); } dir = Math2d.GetHorizontalDir((m_Poses[count - 1] - m_Poses[count - 2]).XY()); vertexs.Add(new UIVertex() { position = m_Poses[count - 1].XY() - dir * width, color = color, }); vertexs.Add(new UIVertex() { position = m_Poses[count - 1].XY() + dir * width, color = color, }); }
/// <summary> /// 平滑地形 /// </summary> /// <param name="center">中心点</param> /// <param name="radius">半径</param> /// <param name="dev">这是高斯模糊的一个参数,会影响平滑的程度</param> /// <param name="level">构建高斯核的半径</param> private void InternalSmooth(Vector3 center, float radius, float dev, int level, bool regesterUndo, bool immediate) { center.x -= terrainSize.x / (heightMapRes - 1) * level; center.z -= terrainSize.z / (heightMapRes - 1) * level; radius += terrainSize.x / (heightMapRes - 1) * level; if (!TryGetHeightMapCmd(center, radius, out HeightCmdData arg)) { return; } if (regesterUndo) { RegisterUndo(new HeightCmd(new HeightCmdData(arg))); } // 利用高斯模糊做平滑处理 Math2d.GaussianBlur(arg.heightMap, dev, level, false); SetHeightMap(arg.terrain, arg.heightMap, arg.startMapIndex.x, arg.startMapIndex.y, immediate); }
public void Update() { Vector2 circlePos = GetV2(_circlePosTrans); //float rectX = _rect.x; //float rectY = _rect.y; //float rectWidth = _rect.width; //float rectHeight = _rect.height; //float circleX = circlePos.x; //float circleY = circlePos.y; //float nearestX = Mathf.Max(rectX, Mathf.Min(circleX, rectX + rectWidth)); //float nearestY = Mathf.Max(rectY, Mathf.Min(circleY, rectY + rectHeight)); //_nearestPos = new Vector2(nearestX, nearestY); _nearestPos = Math2d.GetNearestRectPoint(circlePos, _rect); _isOverlap = Math2d.IsCircleRectOverlap(circlePos, _radius, _rect); _nearestPosTrans.position = new Vector3(_nearestPos.x, 0, _nearestPos.y); }
public bool Intersect(Vector2 p1, Vector2 p2, out List <Vector2> intersection) { intersection = new List <Vector2>(); Vector2 r; if (Math2d.LineSegmentsIntersection(p1, p2, rect.position, rect.position + Vector2.up * rect.height, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position, rect.position + Vector2.right * rect.width, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position + rect.size, rect.position + Vector2.up * rect.height, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position + rect.size, rect.position + Vector2.right * rect.width, out r)) { intersection.Add(r); } return(intersection.Count != 0); }