当前位置:网站首页>分离轴定理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步骤也一样,只不过分离轴凸多边形边的法向量凸多面体的面的法向量
边栏推荐
- SphereEx苗立尧:云原生架构下的Database Mesh研发实践
- zabbix auto-discovery and auto-registration
- Py's mlxtend: a detailed guide to the introduction, installation, and usage of the mlxtend library
- ASP.NET Core Web API 幂等性
- 【暑期每日一题】洛谷 P1551 亲戚
- 【21天学习挑战赛】顺序查找
- 有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
- Launch Space on-premises deployment (local) Beta!
- 提交代码流程
- postgres 多个变量填充字符串,字串格式化
猜你喜欢
随机推荐
The nacos source code can not find the istio package
Xgboost报错ValueError:无效的形状:标签(1650 2)
Leetcode周赛304
HCIP 第四天
See the picture to understand | How to choose sales indicators to measure the health of business growth
MySQL Advanced Statements (1)
pointer arithmetic in c language
Xgboost报错 ValueError: Invalid shape: (1650, 2) for label
zabbix auto-discovery and auto-registration
HCIP day 3 experiment
MySQL union query (multi-table query)
数据库概论之MySQL表的增删改查2
Nodejs installation tutorial
zabbix email alarm and WeChat alarm
Ant three sides: MQ message loss, duplication, backlog problem, what are the solutions?
(笔记整理未完成)【图论】图的遍历
Node installation and environment variable configuration
MySQL高级SQL语句(二)
request.getSession(), the story
暑期总结(三)



![(Notes are not completed) [Graph Theory] Traversal of graphs](/img/1d/d2909dcfa0ab5c207005971a7b4a2d.gif)





