当前位置:网站首页>分离轴定理SAT凸多边形精确碰撞检测
分离轴定理SAT凸多边形精确碰撞检测
2022-08-02 06:13:00 【CairBin】
分离轴定理SAT凸多边形精确碰撞检测
定理
Separating Axis Theorem,缩写SAT,中文为分离轴理论或分离轴定理,通过判断凸多边形在分离轴上的投影是否重叠来判断是否发生碰撞。
所谓的分离轴实际上是一个方向轴,该轴的选取在二维平面情况下一般为凸多边形的边的法线。
局限性:分离轴定理仅适用于凸多边形,不能用于凹多边形。但可以通过转化的方法,即将凹多边形转化为多个凸多边形,然后对多个凸多边形分别使用SAT,达到精确碰撞检测的目的
证明
证明过程可以看SAT(分离轴定理)证明 - 知乎 (zhihu.com)
代码
2D
我们先来探讨二维平面下的情况
步骤大概有如下几步:
- 获取其中一个凸多边形每个边的垂直向量(法向量)
- 点乘获得投影(这里不用除以法向量的模,因为对判断碰撞无影响),其中所有结果中的最小值和最大值就是获得的投影(这里是把两个值当作投影线段两端点在分离轴这个一维数轴的坐标了)
- 判断投影是否相交
以下是代码,每一步还可以进一步优化,这里代码仅为阐述上述步骤
# 获取边的法向量
def getNormal(points: list)->list:
normalVec = {
} # 法向量的集合(Set)
for i in points:
for j in points[points.index(i)::]:
# edge = (i[0]-j[0], i[1]-j[1]) #边对应的向量
vec = (j[1]-i[1], i[0]-j[0]) # 法线对应的向量。可以看出(x,y)垂直的一个向量为(-y,x)
normalVec.add(vec)
return list(normalVec)
# 点乘,获取所有结果中的最大值与最小值
def dotProduct(lst1:list, lst2:list)->list:
res = []
for i in lst1:
for j in lst2:
res.append(i[0]*j[0] + i[1]*j[1])
return (min(res), max(res))
# 碰撞检测
def collided(points1:list, points2:list)->bool:
vec = getNormal(points1)
res1 = dotProduct(vec, points1)
res2 = dotProduct(vec, points2)
# 判断重影是否重叠,重叠就发生碰撞返回True
if (res1[0]<=res2[1] and res1[0]>=res2[0]) or (res1[1]<=res2[1] and res1[1]>=res2[0]):
return True
else:
return False
# ------------ 调用---------------#
# 这里存放点的列表仅做表示,获取点的方法根据自己情况来
# 存放凸多边形1顶点的列表
shape1_points = []
# 存放凸多边形2顶点的列表
shape2_points = []
print(collided(shape1_points, shape2_points))
3D
其实3D步骤也一样,只不过分离轴凸多边形边的法向量凸多面体的面的法向量
边栏推荐
- Leetcode Weekly 304
- APP special test: traffic test
- July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
- 交换网络----三种生成树协议
- 暑期总结(三)
- Two good php debug tutorials
- PMP新考纲通关秘籍,告别抓瞎
- 有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
- 数据库概论-MySQL的数据表的基本操作
- The stock price has repeatedly hit new lows, and the real estate SaaS giant is in trouble. How should Mingyuan Cloud transform and save itself?
猜你喜欢
随机推荐
HCIP 第二天
Pagoda+FastAdmin 404 Not Found
解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
MySQL classic 50 practice questions and the most detailed analysis of the whole network
C# FileInfo class
宝塔+FastAdmin 404 Not Found
request.getSession(), the story
GCC编译器技术解析
【暑期每日一题】洛谷 P1255 数楼梯
2022年8月计划,着重ue4视频教程
Mining game (C language)
Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
Detailed explanation of 9 common reasons for MySQL index failure
Go inside the basic knowledge
PHP Warning: putenv() has been disabled for security reasons in phar
APP special test: traffic test
HCIP 第一天
实验8 VLAN综合实验
交换--STP协议









