Sự va chạm phần là dễ dàng. Kiểm tra xem khoảng cách giữa các quả cầu có nhỏ hơn tổng bán kính của chúng hay không.
Đối với thư bị trả lại, bạn cần trao đổi số tiền vận tốc góp phần vào tổng vận tốc vuông góc với va chạm của các hình cầu. (Giả sử tất cả các lĩnh vực của bạn có khối lượng bằng nhau, nó sẽ là khác nhau cho một sự kết hợp của quần chúng khác nhau)
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;
}
EDIT: Nếu bạn cần độ chính xác hơn, sau đó khi một vụ va chạm, bạn nên tính toán bao xa để di chuyển cả các lĩnh vực va chạm ngược lại để chúng chỉ chạm vào nhau, sau đó kích hoạt chức năng thực hiện va chạm. Điều đó sẽ đảm bảo các góc sẽ chính xác hơn.
Phần khó nhất là spin ... –
Tôi không quan tâm đến spin, tôi chỉ cần colissions và "phản ứng phẳng". – Artemix
Hướng chỉ đơn giản dọc theo đường nối các trung tâm. – Mau