2 つの球体が衝突したときと、衝突後すぐにどの方向に進むかを検出するアルゴリズムを作成する必要があります。
たとえば、ビリヤードの試合でテーブルを開いたときに、すべてのボールが「ランダムに」互いに衝突していると想像してください。
そのため、自分でコードを書き始める前に、これが既に実装されているかどうかを考えていました。
事前にthx!
Cyas.-
2 つの球体が衝突したときと、衝突後すぐにどの方向に進むかを検出するアルゴリズムを作成する必要があります。
たとえば、ビリヤードの試合でテーブルを開いたときに、すべてのボールが「ランダムに」互いに衝突していると想像してください。
そのため、自分でコードを書き始める前に、これが既に実装されているかどうかを考えていました。
事前にthx!
Cyas.-
衝突部分は簡単です。球の中心間の距離が半径の合計より小さいかどうかを確認します。
跳ね返りに関しては、球の衝突に垂直な合計速度に寄与する速度量を交換する必要があります。(すべての球体の質量が等しいと仮定すると、異なる質量の組み合わせでは異なります)
struct Vec3 {
double x, y, z;
}
Vec3 minus(const Vec3& v1, const Vec3& v2) {
Vec3 r;
r.x = v1.x - v2.x;
r.y = v1.y - v2.y;
r.z = v1.z - v2.z;
return r;
}
double dotProduct(const Vec3& v1, const Vec3& v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vec3 scale(const Vec3& v, double a) {
Vec3 r;
r.x = v.x * a;
r.y = v.y * a;
r.z = v.z * a;
return r;
}
Vec3 projectUonV(const Vec3& u, const Vec3& v) {
Vec3 r;
r = scale(v, dotProduct(u, v) / dotProduct(v, v));
return r;
}
int distanceSquared(const Vec3& v1, const Vec3& v2) {
Vec3 delta = minus(v2, v1);
return dotProduct(delta, delta);
}
struct Sphere {
Vec3 position;
Vec3 velocity;
int radius;
}
bool doesItCollide(const Sphere& s1, const Sphere& s2) {
int rSquared = s1.radius + s2.radius;
rSquared *= rSquared;
return distanceSquared(s1.position, s2.position) < rSquared;
}
void performCollision(Sphere& s1, Sphere& s2) {
Vec3 nv1; // new velocity for sphere 1
Vec3 nv2; // new velocity for sphere 2
// this can probably be optimised a bit, but it basically swaps the velocity amounts
// that are perpendicular to the surface of the collistion.
// If the spheres had different masses, then u would need to scale the amounts of
// velocities exchanged inversely proportional to their masses.
nv1 = s1.velocity;
nv1 += projectUonV(s2.velocity, minus(s2.position, s1.position));
nv1 -= projectUonV(s1.velocity, minus(s1.position, s2.position));
nv2 = s2.velocity;
nv2 += projectUonV(s1.velocity, minus(s2.position, s1.position));
nv2 -= projectUonV(s2.velocity, minus(s1.position, s2.position));
s1.velocity = nv1;
s2.velocity = nv2;
}
編集:さらに精度が必要な場合は、衝突時に、衝突する両方の球体を後方に移動して互いに接触する距離を計算してから、衝突の実行関数をトリガーする必要があります。これにより、角度がより正確になります。