I recalled an example that I was pointed to for another question recently but couldn't get working looked like it had the physics in it so I went digging and sure enough they work. Here is the code for iMiniB3D, if there's interest and anyone gets confused by the C syntax I'll translate it over to blitz.
core is the collision entity (I use a pivot and attach the actual mesh to that so I can rotate them independently). mx/my/mz are floats with the momentum of the ball along the various axis. kBounceyness is a float with how bouncy the collision is (1 would be perfect bouncyness, 0 would be no bounce at all. normally this would be a factor of how bouncy the ball is and how bouncy the surface is, but this is just a basic example an there are no materials. appropriate bouncyness will be affected by your scale and relative movement speeds.)
The collisions you should set should be Collisions(balltype, worldtype, 2, 2);
Hope this helps someone as I found it a bit tricky to track down.
for(int collision = 1; collision <= core->CountCollisions(); collision++) {
// Get normal of surface which we collided with
float Nx = core->CollisionNX(collision);
float Ny = core->CollisionNY(collision);
float Nz = core->CollisionNZ(collision);
// compute the dot product of the momentum and the normal
float VdotN = mx*Nx + my*Ny + mz*Nz;
// calculate normal forces
float NFx = -2.0 * Nx * VdotN;
float NFy = -2.0 * Ny * VdotN;
float NFz = -2.0 * Nz * VdotN;
// add normal forces to momentums
mx += NFx * kBounceyness;
my += NFy * kBounceyness;
mz += NFz * kBounceyness;
}
}
P.S. I barely understand how collision normals work and that's the key to this so I'm afraid I can't really explain it all that well.
|