- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在尝试找到一种工作算法来检测射线(代表枪中的子弹)和敌人周围的球体之间的交点……我尝试了一些在网上找到的算法,但似乎没有一个能正常工作,也许我做错了什么......
这是我目前使用的:
//// Ray-sphere intersection.
// p=(ray origin position - sphere position),
// d=ray direction,
// r=sphere radius,
// Output:
// i1=first intersection distance,
// i2=second intersection distance
// i1<=i2
// i1>=0
// returns true if intersection found,false otherwise.//
bool Player::RaySphereIntersect(const Vector3 &p, const Vector3 &d, double r, double &i1, double &i2){
double det,b;
b = -Vector3::dot(p,d);
det = b*b - Vector3::dot(p,p) + r*r;
if (det<0){
return false;
}
det= sqrt(det);
i1= b - det;
i2= b + det;
// intersecting with ray?
if(i2<0)
return false;
if(i1<0)
i1=0;
return true;
}
我使用敌人的位置作为球体位置,大致将玩家枪支的位置作为射线原点,投影鼠标坐标作为射线方向...这是我用来将鼠标坐标投影到的 OpenGL 代码远平面:
Vector3 projectedMouse(float mx, float my){
GLdouble model_view[16];
GLint viewport[4];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble dx, dy, dz, bx, by, bz;
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
winX = (float)mx;
winY = (float)viewport[3] - (float)my;
glReadPixels ((int)mx, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, 1, model_view, projection, viewport, &dx, &dy, &dz);
projectedAim = Vector3(dx, dy, dz);
return projectedAim;
}
这似乎是正确的,因为我正在用它绘制一条 GL 线,它看起来不错......所以也许它是交集代码,但似乎没有任何效果......我尝试了另一个应该返回交点距离的代码,但对于任何给定的敌人位置,它仍然会给我非常随机的结果:
double intersectRaySphere(Vector3 rO, Vector3 rV, Vector3 sO, double sR)
Vector3 Q = sO-rO;
double c = Q.magnitude();
double v = Vector3::dot(Q,rV);
double d = sR*sR - (c*c - v*v);
// If there was no intersection, return -1
if (d < 0.0) return (-1.0f);
// Return the distance to the [first] intersecting point
return (v - sqrt(d));
它们都经过了轻微修改,以匹配我正在使用的库中的 Math 函数......任何人都可以发现它们有什么问题,或者建议另一个吗?这让我发疯....
谢谢!
最佳答案
看起来您正在使用 Cramer 规则来解决交叉问题。考虑替代。多项式的根会告诉您交点。
从 2D 情况开始,我们想要查看位于圆 C 中心的点 P 的正交(因此是最小)距离是否小于圆 C 的半径 R。
基本上。我们找到圆心和您的射线/线之间的最小距离。我们如何做到这一点?有几种方法。
我们知道最短距离是一条直线,它从圆的中间开始与我们的射线具有正交斜率(在 R2 负 recip 中)。然后我们找到两条线之间的交点。如果我们不得不走的长度超过 R,我们就在外面,我们不在乎它有多远。
http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
解决两条线的交点并看到它的交点比 R 更远可能不是最有效的方法,但幸运的是,wolfram 有更好的方法来使用更高级别的数学来完成基本相同的事情。
现在考虑 R3 中的一条射线与球体相交,它基本上是同一件事,但是“正交”比 R2 中更难取悦,所以我们使用双交叉产品。并求解参数方程。
http://www.cs.umbc.edu/~olano/435f02/ray-sphere.html
这是一个聪明的方法来查看我们的光线是否满足作为约束的球体方程。
float a = Vector3::dot(d,d);
float b = Vector3::dot(d * 2, p);
float c = Vector3::dot(p,p) - r*r
// if the discriminant of the quadratic formula is positive
// we have at least one intersection
return (b*b - 4 * a * c) >= 0
简而言之。当我的列是函数及其导数时,我才发现 Cramer 规则对微分方程有用。通常在找到 Wronskian 时。
关于c++ - 鼠标到射线 - 球体碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8000363/
我一直在尝试根据内部“ Shiny 球体”图案在球体上产生发光效果,但一直坚持定位“球体”的某些方面。 就目前而言,我的 CSS 看起来像这样: .sphere { height: 200px;
我正在尝试使用下面给出的旋转矩阵来旋转“轨道”: [cos(angle) -sin(angle) 0; sin(angle) cos (angle) 0; 0 0
我有一组由地理 (WGS84) 坐标指定的多边形:它们位于一个球体上。 我有一个由经纬度对指定的点。 我想(有效地)找到点和多边形之间的最小大圆距离。 我当前的堆栈包括 fiona、shapely、g
将 boost::geometry::line_interpolate 与 boost::geometry::srs::spheroid 结合使用,我正在计算沿最短距离的大圆导航点2个地理点。下面的代
如何在 JavaFX 中填充具有线性渐变(如 2d 圆)的 3D 球体?我使用 JavaFX Scene Builder。 最佳答案 正如@mohsenmadi 所指出的,漫反射颜色不允许您使用一种颜
我想使用 Python PyOpenGL 生成三个球体的场景。两个在有颜色的一侧(红色和绿色)。中间一个上面有任何纹理(砖 block 纹理实际上是与代码位于同一目录中的方形 jpg 文件)。 到目前
我从不同网格变量中的 .x 文件加载多个网格。现在我想计算我加载的所有网格(以及正在显示的网格)的包围球请指导我如何实现这一目标。可以将 VertexBuffers 附加到一个变量中并使用它计算 bo
我正在尝试用“粒子”(由 3D XYZ 向量表示)以最佳方式填充 3D 球形体积,这些粒子需要彼此保持特定距离,同时尽量减少它们之间存在的自由空间量. 但有一个问题 - 粒子本身可能会落在球形体积的边
我想创建一个球体,实际上是一个地球仪。但我似乎找不到任何有关如何处理球体的顶点和索引以及如何设置它们的有用信息。你们中的任何人都可以引导我走上正确的轨道,也许给我一些示例代码或指向教程的链接吗? 最佳
我终于手动画了一个球体:) 我希望我的球体是红色的,但轮廓是绿色的: 为了实现这一点,我做了以下事情。我用红色画了一个实心球体,然后我画了同一个球体,但线框和绿色。当我打开 DEPTH_TEST 时,
我想在 HTML 5.0 Canvas 中绘制 3D 球或球体。我想了解有关如何绘制 3D 球体的算法。谁可以与我分享这个? 最佳答案 您将需要为一个球体建模,并让它具有不同的颜色,以便在它旋转时您可
我正在尝试使用 webGL 构建 3D 太阳系。 我让所有恒星按照应有的方式绕太阳旋转,并且我希望它们也绕自己的 Y 轴旋转。 我怎样才能添加它?我尝试过: mat4.rotate(mvMatrix,
我有二维彩色图像。所有彩色点都位于此矩形图像中心的圆形区域内,圆圈外的所有点都是黑色的(我从鱼眼相机获得这些矩形图像)。我知道这个圆心的坐标和它的半径。 我需要将所有彩色点从 2D 图像上的圆形区域移
我正在用球体上的粒子进行 Metropolis Monte Carlo 模拟,并有一个关于给定时间步长内随机运动的问题。 我知道要在球体上获得均匀分布的随机点开始,使用最简单的方法是不够的(使用恒定
我想让球体向前移动一定的厘米,但到目前为止我还没有设法让任何东西正常工作这是我现在的代码: EditText distanceText = (EditText) findViewById(R.id.d
我想仅使用 Canvas 制作一个旋转对象(球体、盒子等)。但我找不到教程。如果您看到某个地方或解释如何做,请提供帮助。 像这样example ,仅无任何效果 最佳答案 希望你喜欢数学。如果您愿意编写
使用 GL_TRIANGLES 在 OpenGL ES 2.0 中绘制纹理球体的最简单方法是什么? 我特别想知道如何计算顶点。 最佳答案 有多种方法可以对球体进行三角测量。受欢迎的,不太受欢迎的,好的
我有一个关于在 HTML5/Canvas/Javascript 中伪造 3d 的问题... 基本上,我在 上绘制了一个二维图像使用 drawImage() . 我想做的是绘制图像,然后置换球体上的纹
我在 OpenGL ES 中绘制行星,遇到了一些有趣的性能问题。普遍的问题是:如何最好地在球体上渲染“非常详细”的纹理? (球体是有保证的;我对球体特定的优化很感兴趣) 基本案例: 窗口大约是。 20
对于我的论文项目,我使用 DataArts WebGL Globe 来开发一个网页,该网页将在触摸显示器上的展览中使用。作为监视器触摸,我需要使地球可点击以选择单个国家并打开弹出窗口并突出显示选定的国
我是一名优秀的程序员,十分优秀!