我只是在处理一些东西,因为我知道它比任何其他语言都好,并且偶然发现在这个网站上Custom 2d physics engine。到现在为止还挺好。我在我有2个矩形碰撞的地步,我需要解决碰撞。根据论文我应该使用的代码:试图在java中使用C++创建一个物理引擎纪录片
void ResolveCollision(Object A, Object B)
{
// Calculate relative velocity
Vec2 rv = B.velocity - A.velocity
// Calculate relative velocity in terms of the normal direction
float velAlongNormal = DotProduct(rv, normal)
// Do not resolve if velocities are separating
if(velAlongNormal > 0)
return;
// Calculate restitution
float e = min(A.restitution, B.restitution)
// Calculate impulse scalar
float j = -(1 + e) * velAlongNormal
j /= 1/A.mass + 1/B.mass
// Apply impulse
Vec2 impulse = j * normal
A.velocity -= 1/A.mass * impulse
B.velocity += 1/B.mass * impulse
}
这是用C++编写的,所以我需要将它移植到java。在这里,我陷入了两件事。 1:作者用“正常”来表示什么?我如何获得“正常”?事情2是这3行代码:
Vec2 impulse = j * normal
A.velocity -= 1/A.mass * impulse
B.velocity += 1/B.mass * impulse
他创建一个向量只有一个数字? j *正常吗? 我真的没有清楚地了解究竟发生了什么事情,这对我没有任何好处。
如果你不知道什么是正常的矢量,这不是一个真正的编程问题:你应该对正在建模的数学做一些背景阅读。 http://mathworld.wolfram.com/NormalVector.html –
正常在其他地方定义。搜索对象或任何'全局' – JFPicard
“自定义3D物理引擎” - >您链接到一个名为“如何创建自定义2D物理引擎”的页面。 –