resolveCollision 26%

This commit is contained in:
intns 2024-05-12 18:49:03 +01:00
parent b406d4ce6f
commit 07cc12a63e
4 changed files with 50 additions and 30 deletions

View File

@ -13,6 +13,12 @@ namespace Game {
struct RigidConfig {
RigidConfig() { }
inline void setMomentum(const Vector3f& momentum)
{
mMomentum = mMomentum + momentum;
mRotatedMomentum = mRotatedTransform.mtxMult(momentum);
}
Vector3f mPosition; // _00, for mConfigs: (_034, _0BC)
Vector3f mVelocity; // _0C, for mConfigs: (_040, _0C8)
Vector3f mForce; // _18, for mConfigs: (_04C, _0D4)
@ -20,7 +26,7 @@ struct RigidConfig {
Vector3f mMomentum; // _30, for mConfigs: (_064, _0EC)
Vector3f mTorque; // _3C, for mConfigs: (_070, _0F8)
Quat mPrimaryRotation; // _48, for mConfigs: (_07C, _104)
Matrixf _58; // _58, for mConfigs: (_08C, _114)
Matrixf mRotatedTransform; // _58, for mConfigs: (_08C, _114)
};
struct Rigid {

View File

@ -369,7 +369,7 @@ struct Matrixf {
* @param other The vector to be multiplied.
* @return The resulting vector after multiplication.
*/
inline Vector3f mtxMult(Vector3f& other)
inline Vector3f mtxMult(const Vector3f& other)
{
Vector3f outVec;
PSMTXMultVec(this->mMatrix.mtxView, (Vec*)&other, (Vec*)&outVec);

View File

@ -204,6 +204,9 @@ struct Vector3 {
this->z /= other.z;
}
// Negation operator
inline Vector3 operator-() const { return Vector3(-x, -y, -z); }
inline void addXZ(const Vector3& other)
{
this->x += other.x;

View File

@ -57,8 +57,8 @@ void Game::Rigid::initPosition(Vector3f& posVec, Vector3f& quatVec)
{
initPositionIndex(posVec, 0, quatVec);
initPositionIndex(posVec, 1, quatVec);
PSMTXIdentity(mConfigs[0]._58.mMatrix.mtxView);
PSMTXIdentity(mConfigs[1]._58.mMatrix.mtxView);
PSMTXIdentity(mConfigs[0].mRotatedTransform.mMatrix.mtxView);
PSMTXIdentity(mConfigs[1].mRotatedTransform.mMatrix.mtxView);
PSMTXIdentity(mTransformationMtx.mMatrix.mtxView);
mTimeStep = 1.0f;
updateMatrix(0);
@ -302,13 +302,13 @@ void Game::Rigid::integrate(f32 timeStep, int configIdx)
rotationMtx.makeQ(thisConfig->mPrimaryRotation);
PSMTXTranspose(rotationMtx.mMatrix.mtxView, transposeMtx.mMatrix.mtxView);
PSMTXConcat(rotationMtx.mMatrix.mtxView, mTransformationMtx.mMatrix.mtxView, concatMtx.mMatrix.mtxView);
PSMTXConcat(concatMtx.mMatrix.mtxView, transposeMtx.mMatrix.mtxView, thisConfig->_58.mMatrix.mtxView);
PSMTXConcat(concatMtx.mMatrix.mtxView, transposeMtx.mMatrix.mtxView, thisConfig->mRotatedTransform.mMatrix.mtxView);
thisConfig->mPosition = thisConfig->mPosition + thisConfig->mVelocity * timeStep;
thisConfig->mMomentum = thisConfig->mMomentum + thisConfig->mTorque * timeStep;
thisConfig->mVelocity = thisConfig->mVelocity + thisConfig->mForce * (timeStep * mTimeStep);
thisConfig->mRotatedMomentum = thisConfig->_58.mtxMult(thisConfig->mMomentum);
thisConfig->mRotatedMomentum = thisConfig->mRotatedTransform.mtxMult(thisConfig->mMomentum);
Quat q1; // 0x160
Quat q2(0.0f, thisConfig->mRotatedMomentum); // 0x150
@ -329,7 +329,7 @@ void Game::Rigid::integrate(f32 timeStep, int configIdx)
if (yDeg4 < yDeg48) {
Vector3f yAxis(0.0f, 1.0f, 0.0);
thisConfig->mMomentum = thisConfig->mMomentum + vec1.cross(yAxis) * 1000.0f;
thisConfig->mRotatedMomentum = thisConfig->_58.mtxMult(thisConfig->mMomentum);
thisConfig->mRotatedMomentum = thisConfig->mRotatedTransform.mtxMult(thisConfig->mMomentum);
if (!(yDeg4 < f29)) {
thisConfig->mPrimaryRotation = q4;
}
@ -780,36 +780,47 @@ lbl_8013AB2C:
bool Game::Rigid::resolveCollision(int configIndex, Vector3f& collisionPoint, Vector3f& collisionNormal, f32 restitutionCoefficient)
{
if (DynamicsParms::mInstance->mMicroCollision.mValue == 1120.0f) {
;
#if _DEBUG
// Stripped from release build
OSReport("rassclaaat");
#endif
}
RigidConfig& config = mConfigs[configIndex];
Vector3f relativePosition = collisionPoint - config.mPosition;
Vector3f relativeVelocity = (config.mVelocity + relativePosition.cross(config.mRotatedMomentum));
relativeVelocity.negate();
f32 velocityDotNormal = relativeVelocity.dot(collisionNormal);
if (velocityDotNormal < -0.0f) {
RigidConfig* config = &this->mConfigs[configIndex];
Vector3f positionDelta = collisionPoint - config->mPosition;
Vector3f angularMomentum(config->mRotatedMomentum.z * positionDelta.y - config->mRotatedMomentum.y * positionDelta.z,
config->mRotatedMomentum.x * positionDelta.z - config->mRotatedMomentum.z * positionDelta.x,
config->mRotatedMomentum.y * positionDelta.x - config->mRotatedMomentum.x * positionDelta.y);
config->mRotatedMomentum = angularMomentum;
Vector3f impulse = angularMomentum + config->mVelocity;
impulse.negate2();
float impulseMagnitude = impulse.dot(collisionNormal);
// If there's a collision
if (impulseMagnitude < 0.0f * -0.0f) {
return false;
}
if (absF(velocityDotNormal) <= -0.0f) {
// If it's tiny, just set it to 0
if (fabs(impulseMagnitude) <= 0.0f) {
restitutionCoefficient = 1.0f;
velocityDotNormal = -0.0f;
impulseMagnitude = 0.0f;
}
f32 timeStep = mTimeStep;
f32 impulseMagnitude = -(1.0f + restitutionCoefficient) * velocityDotNormal;
Vector3f vec = relativePosition;
vec = vec.cross(collisionNormal);
vec = config._58.mtxMult(vec);
vec = vec.cross(collisionNormal);
Vector3f rotatedVelocity = config->mRotatedTransform.mtxMult(positionDelta.cross(collisionNormal));
timeStep += collisionNormal.dot(vec);
Vector3f impulse = collisionNormal;
impulse *= -(impulseMagnitude / timeStep);
config.mVelocity = config.mVelocity + impulse * mTimeStep;
vec = relativePosition;
vec = vec.cross(impulse);
config.mMomentum = config.mMomentum + vec;
config.mRotatedMomentum = config._58.mtxMult(config.mMomentum);
f32 dynamicCoefficient = collisionNormal.dot(rotatedVelocity);
f32 scalar = -(1.0f + restitutionCoefficient) * impulseMagnitude / dynamicCoefficient;
Vector3f collisionImpulse = positionDelta * (collisionNormal * scalar);
config->mVelocity = config->mVelocity + collisionImpulse;
config->setMomentum(positionDelta.cross(collisionImpulse));
return true;
/*
stwu r1, -0x90(r1)