unity3d中的一些数学计算方法
来源:互联网 发布:python中exec 编辑:程序博客网 时间:2024/05/21 07:04
以下所说的点都是unity中的坐标点(x, y, z)形式的
using UnityEngine;using System.Collections;public class MathfHelper : MonoBehaviour { public static float Multiply(float p1x, float p1y, float p2x, float p2y, float p0x, float p0y) { return ((p1x - p0x) * (p2y - p0y) - (p2x - p0x) * (p1y - p0y)); } /// <summary> /// 是否在矩形内 /// </summary> public static bool InTheRect(Vector2 point, Vector2 leftEnd, Vector2 rightEnd, Vector2 right, Vector2 left) { float x = point.x; float y = point.y; float leftEndX = leftEnd.x; float leftEndY = leftEnd.y; float rightEndX = rightEnd.x; float rightEndY = rightEnd.y; float rightX = right.x; float rightY = right.y; float leftX = left.x; float lefrY = left.y; if (Multiply(x, y, leftEndX, leftEndY, rightEndX, rightEndY) * Multiply(x, y, leftX, lefrY, rightX, rightY) <= 0 && Multiply(x, y, leftX, lefrY, leftEndX, leftEndY) * Multiply(x, y, rightX, rightY, rightEndX, rightEndY) <= 0) { return true; } else { return false; } } /// <summary> /// 圆与矩形是否相交 /// </summary> /// <returns></returns> public static bool CircleAndRectIsIntersect(Vector3 cPos, int radius, Vector3 rLeftPos, Vector3 rRightEndPos) { float dist = 0; if (rLeftPos.x > rRightEndPos.x) { dist = rRightEndPos.x; rRightEndPos.x = rLeftPos.x; rLeftPos.x = dist; } if (rLeftPos.y > rRightEndPos.y) { dist = rRightEndPos.y; rRightEndPos.y = rLeftPos.y; rLeftPos.y = dist; } dist = radius / (float)1.4142136; float xSquare1 = cPos.x - dist; float xSquare2 = cPos.x + dist; float ySquare1 = cPos.y - dist; float ySquare2 = cPos.y + dist; if (xSquare1 <= rRightEndPos.x && xSquare2 >= rLeftPos.x && ySquare1 <= rLeftPos.y && ySquare2 >= rRightEndPos.y) { return true; } float xrectangle = (rLeftPos.x + rRightEndPos.x) / 2; float yrectangle = (rLeftPos.y + rRightEndPos.y) / 2; if (xrectangle <= cPos.x && yrectangle <= cPos.y && Mathf.Sqrt((rRightEndPos.x - cPos.x) * (rRightEndPos.x - cPos.x) + (rRightEndPos.y - cPos.y) * (rRightEndPos.y - cPos.y)) <= radius) return true; else if (xrectangle > cPos.x && yrectangle <= cPos.y && Mathf.Sqrt((rLeftPos.x - cPos.x) * (rLeftPos.x - cPos.x) + (rRightEndPos.y - cPos.y) * (rRightEndPos.y - cPos.y)) <= radius) return true; else if (xrectangle <= cPos.x && yrectangle > cPos.y && Mathf.Sqrt((rRightEndPos.x - cPos.x) * (rRightEndPos.x - cPos.x) + (rLeftPos.y - cPos.y) * (rLeftPos.y - cPos.y)) <= radius) return true; else if (Mathf.Sqrt((rLeftPos.x - cPos.x) * (rLeftPos.x - cPos.x) + (rLeftPos.y - cPos.y) * (rLeftPos.y - cPos.y)) <= radius) return true; if ((cPos.x - radius >= rLeftPos.x && cPos.x - radius <= rRightEndPos.x || cPos.x + radius >= rLeftPos.x && cPos.x + radius <= rRightEndPos.x) && cPos.y >= rLeftPos.y && cPos.y <= rRightEndPos.y || (cPos.y - radius >= rLeftPos.y && cPos.y - radius >= rRightEndPos.y || cPos.y + radius >= rLeftPos.y && cPos.y + radius <= rRightEndPos.y) && cPos.x >= rLeftPos.x && cPos.x <= rRightEndPos.x) return true; return false; } /// <summary> /// 获得点与线之间的最小距离 /// </summary> public static float GetPointAndLineMinDistance(Vector3 pos, Vector3 line1, Vector3 line2) { var d1 = Vector3.Distance(pos, line1); var d2 = Vector3.Distance(pos, line2); var d3 = Vector3.Distance(line1, line2); var x = (Mathf.Sqrt(d1) + Mathf.Sqrt(d3) - Mathf.Sqrt(d2)) / (2 * d3); return (float)Mathf.Abs(Mathf.Pow(Mathf.Sqrt(d1) - Mathf.Sqrt(x), 0.5f)); } /// <summary> /// 两点之间的角度 /// </summary> public static float PointAndPointAngle(Vector3 centerPos, Vector3 pos) { var x = pos.x - centerPos.x; var y = pos.y - centerPos.y; var radina = Mathf.Atan(Mathf.Abs(pos.y - centerPos.y) / Mathf.Abs(pos.x - centerPos.x)); float angle = radina * Mathf.Rad2Deg; if (x > 0 && y < 0) { angle = 360 - angle; } else if (x < 0 && y > 0) { angle = 180 - angle; } else if (x < 0 && y < 0) { angle = 180 + angle; } else if (x == 0 && y > 0) { angle = 90f; } else if (x == 0 && y < 0) { angle = 270; } else if (x > 0 && y == 0) { angle = 0; } else if (x < 0 && y == 0) { angle = 180; } return angle; } /// <summary> /// 通过点,获得椭圆角 /// </summary> public static Vector3 GetPointByEllipseAngle(Vector3 center, float angle, float a, float b) { var f = Mathf.Tan(angle * Mathf.Deg2Rad); var aa = Mathf.Pow(a, 2); var bb = Mathf.Pow(b, 2); var ff = Mathf.Pow(f, 2); var num = aa * bb / (bb + aa * ff); var x = 0f; if (num != 0) { x = Mathf.Sqrt(num); } var xx = Mathf.Pow(x, 2); num = (1 - xx / aa) * bb; var y = 0f; if (num != 0) { y = Mathf.Sqrt(num); } Vector3 pos = new Vector3(x, y, 0); if (angle >= 0 && angle <= 90) { } else if (angle > 90 && angle <= 180) { pos.x = -pos.x; } else if (angle > 180 && angle <= 270) { pos.x = -pos.x; pos.y = -pos.y; } else if (angle > 270 && angle < 360) { pos.y = -pos.y; } return pos + center; } /// <summary> /// 判断点是否在扇形内 /// </summary> public static bool InTheCircularSector(Vector3 cPoint, Vector3 uPoint, float radius, float angle, Vector3 point) { float distance = Vector3.Distance(cPoint, point); if (distance > radius) { return false; } float centerAngle = PointAndPointAngle(cPoint, uPoint); float cAngle = PointAndPointAngle(cPoint, point); float leftAngle = 0f; float rightAngle = 0f; if (centerAngle >= angle && centerAngle + angle <= 360) { leftAngle = centerAngle + angle; rightAngle = centerAngle - angle; if (cAngle >= rightAngle && cAngle <= leftAngle) { return true; } } else if (centerAngle >= angle && centerAngle + angle > 360) { leftAngle = centerAngle + angle - 360; rightAngle = centerAngle - angle; if ((cAngle >= rightAngle && cAngle <= 360) || (cAngle >= 0 && cAngle <= leftAngle)) { return true; } } else if (centerAngle < angle && centerAngle + angle <= 360) { leftAngle = centerAngle + angle; rightAngle = 360 - (angle - centerAngle); if ((cAngle >= rightAngle && cAngle <= 360) || (cAngle >= 0 && cAngle <= leftAngle)) { return true; } } return false; }}
0 0
- unity3d中的一些数学计算方法
- Unity3D中的一些Atrribute
- unity3d 中的一些Attribute
- 数学模板 - 计算方法
- 【Unity3d开发基础】Mathf中的数学运算
- unity3d学习中的一些问题
- c++中的一些数学库函数
- Java中的一些数学运算
- 基于距离的数学计算方法
- H.264中的一些数学函数
- 关于机器学习中的一些数学方法
- cocos2d-x 中的一些数学函数
- Ccocos2d-x中的一些常用数学函数
- 数学表述中的一些绕弯现象
- css中的百分比计算方法
- AS3的一些优化计算方法
- Direct3D中常用的数学计算方法总结
- Direct3D中常用的数学计算方法
- h.264并行解码算法分析
- 提高项目执行力的六板斧
- 1063. 计算谱半径(20)
- 今天开始写博客
- 16. 3Sum Closest
- unity3d中的一些数学计算方法
- 第十五周oj题目字符串替换
- 提取所有用户的第二个订单信息
- 1064. 朋友数(20)
- Eclipse老是闪退,什么问题?及解决办法
- opencv计算机视觉学习笔记一
- 提高GPGPU-Sim运行benchmark的速度-小白版
- GNS 3 unable to create generic ethernet NOI 解决方法
- PHP内存管理 垃圾回收