检测球体和三角形之间的碰撞

如何解决检测球体和三角形之间的碰撞

我正在尝试检测C ++和OpenGL中的球体和三角形之间的碰撞,但是遇到了麻烦。我从《实时碰撞检测》一书中获得了这种方法,但是这会引起很多错误标记,并且某些方法无法正常工作。如何准确检测球体和三角形之间的碰撞?

这是我在代码中感到疲倦的地方,该函数采用球体的位置,半径和3个三角形顶点:

bool CollisionHelper::isSphereIntersectingTriangle(glm::vec3 sphere,float radius,glm::vec3 tri1,glm::vec3 tri2,glm::vec3 tri3)
{
    float dist1 = glm::sqrt((sphere.x - tri1.x) * (sphere.x - tri1.x) + (sphere.y - tri1.y) * (sphere.y - tri1.y) + (sphere.z - tri1.z) * (sphere.z - tri1.z));
    float dist2 = glm::sqrt((sphere.x - tri2.x) * (sphere.x - tri2.x) + (sphere.y - tri2.y) * (sphere.y - tri2.y) + (sphere.z - tri2.z) * (sphere.z - tri2.z));
    float dist3 = glm::sqrt((sphere.x - tri3.x) * (sphere.x - tri3.x) + (sphere.y - tri3.y) * (sphere.y - tri3.y) + (sphere.z - tri3.z) * (sphere.z - tri3.z));
    float closestDist = glm::min(glm::min(dist1,dist2),dist3);
    glm::vec3 v;
    if (closestDist == dist1)
        v = tri1 - sphere;
    else if (closestDist == dist2)
        v = tri2 - sphere;
    else (closestDist == dist3)
        v = tri3 - sphere;
    return glm::dot(v,v) <= radius * radius;
}

解决方法

这是我要执行的测试。 首先,我将坐标转换为球体的中心为(0,0)。 最容易检查的是其中一个角是否在内部。 接下来是检查边缘之一是否在切割。 最后,我将测试球体是否通过平面而不切割边缘。 为此,我将计算由三角形给出的平面到原点的正交距离。这也给出了平面法线开始的点。如果它比半径短并且在三角形周长之内,请完成。

修改

这里有一些python代码可以澄清

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection 

fig = plt.figure()
ax = fig.add_subplot( 1,1,projection='3d' )

# Make data
u = np.linspace( 0,.5 * np.pi,15 )
v = np.linspace( 0,15 )

### radius is 1 but problem can be scaled
R = 1
x = R * np.outer(np.cos(u),np.sin(v) )
y = R * np.outer(np.sin(u),np.sin(v) )
z = R * np.outer(np.ones(np.size(u) ),np.cos(v) )

"""
Random Test
"""
# ~scale = 3
# ~A = np.array( (
    # ~scale * np.random.random(),# ~scale * np.random.random(),# ~scale * np.random.random() ) 
# ~)
# ~B = np.array( (
    # ~scale * np.random.random(),# ~scale * np.random.random() ) 
# ~)
# ~C = np.array( (
    # ~scale * np.random.random(),# ~scale * np.random.random() ) 
# ~)

"""
TestCases
"""
if 0: #definitive outside
    A = np.array( [ 1.3,0.4,0.6 ] )
    B = np.array( [ 1.3,1.4,0.6 ] )
    C = np.array( [ 1.3,1.6 ] )
if 0: # outside but plane normal inside
    A = np.array( [ 1.3,0.6 ] )
    B = np.array( [ 0.7,0.6 ] )
    C = np.array( [ 1.8,1.0 ] )
if 0: # cutting edge
    A = np.array( [ 1.1,0.0,0.1 ] )
    B = np.array( [ 0.1,-0.2 ] )
    C = np.array( [ 1.8,1.0 ] )
if 1: # cutting plane
    A = np.array( [ 1.4,-0.2 ] )
    C = np.array( [ -0.03,0.1,2.0 ] )

"""
Most simple check:
is one of the vertices indside
"""

print np.linalg.norm( A ),np.linalg.norm( A ) < R 
print np.linalg.norm( B ),np.linalg.norm( B ) < R 
print np.linalg.norm( C ),np.linalg.norm( C ) < R 

"""
checking if one edge cuts the sphere
this uses simple derivatives of the distance function
"""
for F,G in [ ( B,A ),(C,B),(A,C)]:
    a =  F - G
    s = -np.dot( a,G )/ np.dot( a,a )
    print "s: ",s,s > 0 and s < 1
    d  = np.linalg.norm( G + s * a )
    print "d: ",d,d < R
    ### if both are true,it is cutting
    print "---------"

"""
checking if the sphere cuts the area
e.g in the extreme case of (but not restricted to) a sphere
passing through
"""
a = B - A
c = C - A
aa = np.dot( a,a)
cc = np.dot( c,c)
ac = np.dot( a,c)
aA = np.dot( a,A)
cA = np.dot( c,A)

MI = np.array( [
    np.array([ cc,-ac ] ),np.array([ -ac,aa ] )
])
MI /= ( aa * cc - ac**2 ) ### div by det

st = np.dot( MI,[ -aA,-cA ] )
s=st[0]
t=st[1]
P = A + s * a + t * c

"""
If this is larger than R we can stop here
if otherwise we detect if P inside triangle by repeating the stuff 
above with respect to B
"""
a2 = A - B
c2 = C - B
aa2 = np.dot( a2,a2 )
cc2 = np.dot( c2,c2 )
ac2 = np.dot( a2,c2 )
aB2 = np.dot( a2,B )
cB2 = np.dot( c2,B )

MI2 = np.array( [
    np.array([ +cc2,-ac2 ] ),np.array([ -ac2,+aa2 ] )
])
MI2 /= ( aa2 * cc2 - ac2**2 ) 
uv = np.dot( MI2,[ -aB2,-cB2 ] )
u = uv[0]
v = uv[1]
P2 = B + u * a2 + v * c2

print "must be identical"
print P,np.linalg.norm( P ) < R
print P2,np.linalg.norm( P2 ) < R

print "is inside if all 4 are positive"
print s,t,u,v



### finally some plotting
verts = [ [ A,B,C  ] ]
srf = Poly3DCollection( verts,alpha=.9,facecolor='#800000' )

ax.plot_wireframe( x,y,z,color='b' )
ax.plot( [ 0,P[0] ],[ 0,P[1] ],P[2] ] )
ax.add_collection3d(srf)
ax.set_xlim( [-0.5,2 ] )
ax.set_ylim( [-0.5,2 ] )
ax.set_zlim( [-0.5,2 ] )
plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-