问题
I want to write a c++ program that will calculate collision between sphere and plane.
The rule is that the angle of the falling object equals to angle of reflection.
What do I have for sphere:
//sphere coordinates and radius
float x;
float y;
float z;
float r;
//sphere velocity vector projections
float vx;
float vy;
float vz;
Plane is described by plane equation coefficients:
float A;
float B;
float C;
float D;
With sphere-plane collision detection I have no problem. But how to find velocity after collision?
What did I find:
So, ultimately I need to calculate updated values for vx vy vz
.
回答1:
The equation defining the plane is
Ax + By + Cz + D = 0
So the vector normal to the plane is
W = (A, B, C)
Normalize it:
n = W/|W|
Now take the velocity vector:
V = (vx, vy, vz)
Its component normal to the plane is
Vn = (V . n) n
and the rest of it, the part parallel to the plane is
Vp = V - Vn
We want to reverse the normal component and leave the parallel component unchanged:
V' = -Vn + Vp
which works out to
V' = V - 2(V . n)n
回答2:
@Beta’s answer on c++:
float wl = sqrt(plane->A*plane->A+plane->B*plane->B+plane->C+plane->C); // “W” vector length
float nx = plane->A/wl; //Normal components
float ny = plane->B/wl;
float nz = plane->C/wl;
float scope = (sphere->vx*nx + sphere->vy*ny + sphere->vz*nz)*2; // 2(V . n)
nx = nx*scope; // 2(V . n)n
ny = ny*scope;
nz = nz*scope;
sphere->vx -= nx; // V' = V - 2(V . n)n
sphere->vy -= ny;
sphere->vz -= nz;
来源:https://stackoverflow.com/questions/63103184/sphere-plane-collision-resolve