metaforce/Runtime/Character/CGroundMovement.cpp

782 lines
31 KiB
C++
Raw Permalink Normal View History

#include "Runtime/Character/CGroundMovement.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/Collision/CAABoxFilter.hpp"
#include "Runtime/Collision/CCollisionInfoList.hpp"
#include "Runtime/Collision/CGameCollision.hpp"
#include "Runtime/Collision/CollisionUtil.hpp"
#include "Runtime/World/CPhysicsActor.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "Runtime/World/CScriptPlatform.hpp"
#include "Runtime/World/CWorld.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
#include "math.h"
2017-06-19 07:00:50 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-12-08 05:30:43 +00:00
void CGroundMovement::CheckFalling(CPhysicsActor& actor, CStateManager& mgr, float /*dt*/) {
2018-12-08 05:30:43 +00:00
bool oob = true;
zeus::CAABox plBox = *actor.GetTouchBounds();
2018-12-08 05:30:43 +00:00
for (const CGameArea& area : *mgr.GetWorld()) {
if (area.GetAABB().intersects(plBox)) {
2018-12-08 05:30:43 +00:00
oob = false;
break;
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
}
if (oob) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::OnFloor);
actor.SetAngularVelocityWR(actor.GetAngularVelocityWR() * 0.98f);
zeus::CVector3f vel = actor.GetTransform().transposeRotate(actor.GetVelocity());
vel.z() = 0.f;
actor.SetVelocityOR(vel);
actor.SetMomentumWR(zeus::skZero3f);
} else {
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::Falling);
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
}
void CGroundMovement::MoveGroundCollider(CStateManager& mgr, CPhysicsActor& actor, float dt,
2018-12-08 05:30:43 +00:00
const rstl::reserved_vector<TUniqueId, 1024>* nearList) {
CMotionState oldState = actor.GetMotionState();
CMotionState newState = actor.PredictMotion_Internal(dt);
float deltaMag = newState.x0_translation.magnitude();
TUniqueId idDetect = kInvalidUniqueId;
CCollisionInfoList collisionList;
zeus::CAABox motionVol = actor.GetMotionVolume(dt);
rstl::reserved_vector<TUniqueId, 1024> useColliderList;
if (nearList != nullptr) {
2018-12-08 05:30:43 +00:00
useColliderList = *nearList;
}
2018-12-08 05:30:43 +00:00
mgr.BuildColliderList(useColliderList, actor, motionVol);
CAreaCollisionCache cache(motionVol);
float collideDt = dt;
2018-12-15 06:29:41 +00:00
if (actor.GetCollisionPrimitive()->GetPrimType() != FOURCC('OBTG')) {
2018-12-08 05:30:43 +00:00
CGameCollision::BuildAreaCollisionCache(mgr, cache);
if (deltaMag > 0.5f * CGameCollision::GetMinExtentForCollisionPrimitive(*actor.GetCollisionPrimitive())) {
zeus::CVector3f point = actor.GetCollisionPrimitive()->CalculateAABox(actor.GetPrimitiveTransform()).center();
TUniqueId intersectId = kInvalidUniqueId;
CMaterialFilter filter = CMaterialFilter::MakeInclude({EMaterialTypes::Solid});
CRayCastResult result = mgr.RayWorldIntersection(intersectId, point, newState.x0_translation.normalized(),
deltaMag, filter, useColliderList);
if (result.IsValid()) {
collideDt = dt * (result.GetT() / deltaMag);
newState = actor.PredictMotion_Internal(collideDt);
}
2018-12-15 06:29:41 +00:00
}
}
actor.MoveCollisionPrimitive(newState.x0_translation);
if (CGameCollision::DetectCollision_Cached(mgr, cache, *actor.GetCollisionPrimitive(), actor.GetPrimitiveTransform(),
actor.GetMaterialFilter(), useColliderList, idDetect, collisionList)) {
2018-12-15 06:29:41 +00:00
actor.AddMotionState(newState);
float resolved = 0.f;
if (ResolveUpDown(cache, mgr, actor, actor.GetMaterialFilter(), useColliderList, actor.GetStepUpHeight(), 0.f,
resolved, collisionList)) {
actor.SetMotionState(oldState);
MoveGroundColliderXY(cache, mgr, actor, actor.GetMaterialFilter(), useColliderList, collideDt);
}
2017-06-19 07:00:50 +00:00
2018-12-15 06:29:41 +00:00
} else {
actor.AddMotionState(newState);
}
2018-12-08 05:30:43 +00:00
2018-12-15 06:29:41 +00:00
float stepDown = actor.GetStepDownHeight();
float resolved = 0.f;
collisionList.Clear();
TUniqueId stepZId = kInvalidUniqueId;
if (stepDown >= 0.f && MoveGroundColliderZ(cache, mgr, actor, actor.GetMaterialFilter(), useColliderList, -stepDown,
resolved, collisionList, stepZId)) {
if (collisionList.GetCount() > 0) {
CCollisionInfoList filteredList;
CollisionUtil::FilterByClosestNormal(zeus::CVector3f{0.f, 0.f, 1.f}, collisionList, filteredList);
if (filteredList.GetCount() > 0) {
if (CGameCollision::IsFloor(filteredList.Front().GetMaterialLeft(), filteredList.Front().GetNormalLeft())) {
if (TCastToPtr<CScriptPlatform> plat = mgr.ObjectById(stepZId)) {
mgr.SendScriptMsg(plat.GetPtr(), actor.GetUniqueId(), EScriptObjectMessage::AddPlatformRider);
2018-12-08 05:30:43 +00:00
}
CGameCollision::SendMaterialMessage(mgr, filteredList.Front().GetMaterialLeft(), actor);
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::OnFloor);
} else {
CheckFalling(actor, mgr, dt);
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
}
2018-12-15 06:29:41 +00:00
} else {
CheckFalling(actor, mgr, dt);
}
actor.ClearForcesAndTorques();
actor.MoveCollisionPrimitive(zeus::skZero3f);
2018-12-15 06:29:41 +00:00
if (actor.GetMaterialList().HasMaterial(EMaterialTypes::Player)) {
CGameCollision::CollisionFailsafe(mgr, cache, actor, *actor.GetCollisionPrimitive(), useColliderList, 0.f, 1);
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
}
bool CGroundMovement::ResolveUpDown(CAreaCollisionCache& cache, CStateManager& mgr, CPhysicsActor& actor,
const CMaterialFilter& filter, rstl::reserved_vector<TUniqueId, 1024>& nearList,
2018-12-08 05:30:43 +00:00
float stepUp, float stepDown, float& fOut, CCollisionInfoList& list) {
float zextent = stepDown;
if (list.GetCount() <= 0) {
2018-12-08 05:30:43 +00:00
return true;
}
2017-06-19 07:00:50 +00:00
zeus::CAABox aabb = zeus::CAABox();
zeus::CVector3f normAccum = zeus::skZero3f;
2018-12-08 05:30:43 +00:00
for (CCollisionInfo& info : list) {
if (CGameCollision::IsFloor(info.GetMaterialLeft(), info.GetNormalLeft())) {
aabb.accumulateBounds(info.GetPoint());
aabb.accumulateBounds(info.GetExtreme());
normAccum += info.GetNormalLeft();
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
if (normAccum.canBeNormalized()) {
2018-12-08 05:30:43 +00:00
normAccum.normalize();
} else {
2018-12-08 05:30:43 +00:00
return true;
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
zeus::CAABox actorAABB = actor.GetBoundingBox();
if (normAccum.z() >= 0.f) {
zextent = aabb.max.z() - actorAABB.min.z() + 0.02f;
if (zextent > stepUp) {
2018-12-08 05:30:43 +00:00
return true;
}
2018-12-08 05:30:43 +00:00
} else {
zextent = aabb.min.z() - actorAABB.max.z() - 0.02f;
if (zextent < -stepDown) {
2018-12-08 05:30:43 +00:00
return true;
}
2018-12-08 05:30:43 +00:00
}
actor.MoveCollisionPrimitive({0.f, 0.f, zextent});
if (!CGameCollision::DetectCollisionBoolean_Cached(mgr, cache, *actor.GetCollisionPrimitive(),
actor.GetPrimitiveTransform(), filter, nearList)) {
fOut = zextent;
actor.SetTranslation(actor.GetTranslation() + zeus::CVector3f(0.f, 0.f, zextent));
actor.MoveCollisionPrimitive(zeus::skZero3f);
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
bool floor = false;
for (CCollisionInfo& info : list) {
if (CGameCollision::IsFloor(info.GetMaterialLeft(), info.GetNormalLeft())) {
floor = true;
break;
}
2017-06-19 07:00:50 +00:00
}
if (!floor) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::LandOnNotFloor);
}
2018-12-08 05:30:43 +00:00
return false;
}
return true;
2017-06-19 07:00:50 +00:00
}
bool CGroundMovement::MoveGroundColliderZ(CAreaCollisionCache& cache, CStateManager& mgr, CPhysicsActor& actor,
2018-12-08 05:30:43 +00:00
const CMaterialFilter& filter,
rstl::reserved_vector<TUniqueId, 1024>& nearList, float amt, float& resolved,
2018-12-08 05:30:43 +00:00
CCollisionInfoList& list, TUniqueId& idOut) {
actor.MoveCollisionPrimitive({0.f, 0.f, amt});
zeus::CAABox aabb = zeus::CAABox();
2018-12-08 05:30:43 +00:00
if (CGameCollision::DetectCollision_Cached(mgr, cache, *actor.GetCollisionPrimitive(), actor.GetPrimitiveTransform(),
filter, nearList, idOut, list)) {
for (CCollisionInfo& info : list) {
aabb.accumulateBounds(info.GetPoint());
aabb.accumulateBounds(info.GetExtreme());
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
zeus::CAABox actorAABB = actor.GetBoundingBox();
float zextent = 0.f;
if (amt > 0.f) {
2018-12-08 05:30:43 +00:00
zextent = aabb.min.z() - actorAABB.max.z() - 0.02f + amt;
} else {
2018-12-08 05:30:43 +00:00
zextent = aabb.max.z() - actorAABB.min.z() + 0.02f + amt;
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
actor.MoveCollisionPrimitive({0.f, 0.f, zextent});
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
if (!CGameCollision::DetectCollisionBoolean_Cached(mgr, cache, *actor.GetCollisionPrimitive(),
actor.GetPrimitiveTransform(), filter, nearList)) {
resolved = zextent;
2018-12-08 05:30:43 +00:00
actor.SetTranslation(actor.GetTranslation() + zeus::CVector3f(0.f, 0.f, zextent));
actor.MoveCollisionPrimitive(zeus::skZero3f);
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
bool floor = false;
for (CCollisionInfo& info : list) {
if (CGameCollision::IsFloor(info.GetMaterialLeft(), info.GetNormalLeft())) {
floor = true;
break;
}
}
2017-06-19 07:00:50 +00:00
if (!floor) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::LandOnNotFloor);
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
CCollisionInfoList filteredList;
if (amt > 0.f) {
2018-12-08 05:30:43 +00:00
CollisionUtil::FilterByClosestNormal({0.f, 0.f, -1.f}, list, filteredList);
} else {
2018-12-08 05:30:43 +00:00
CollisionUtil::FilterByClosestNormal({0.f, 0.f, 1.f}, list, filteredList);
}
2017-06-19 07:00:50 +00:00
if (filteredList.GetCount() > 0) {
2018-12-08 05:30:43 +00:00
CGameCollision::MakeCollisionCallbacks(mgr, actor, idOut, filteredList);
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
return true;
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
return false;
2017-06-19 07:00:50 +00:00
}
void CGroundMovement::MoveGroundColliderXY(CAreaCollisionCache& cache, CStateManager& mgr, CPhysicsActor& actor,
2018-12-08 05:30:43 +00:00
const CMaterialFilter& filter,
rstl::reserved_vector<TUniqueId, 1024>& nearList, float dt) {
bool didCollide = false;
bool isPlayer = actor.GetMaterialList().HasMaterial(EMaterialTypes::Player);
float remDt = dt;
float originalDt = dt;
TCastToPtr<CPhysicsActor> otherActor;
CCollisionInfoList collisionList;
CMotionState newMState = actor.PredictMotion_Internal(dt);
float transMag = newMState.x0_translation.magnitude();
float divMag = NAN;
if (isPlayer) {
2018-12-08 05:30:43 +00:00
divMag = std::max(transMag / 5.f, 0.005f);
} else {
2018-12-08 05:30:43 +00:00
divMag = std::max(transMag / 3.f, 0.02f);
}
2018-12-08 05:30:43 +00:00
float minExtent = 0.5f * CGameCollision::GetMinExtentForCollisionPrimitive(*actor.GetCollisionPrimitive());
if (transMag > minExtent) {
dt = minExtent * (dt / transMag);
originalDt = dt;
newMState = actor.PredictMotion_Internal(dt);
divMag = std::min(divMag, minExtent);
}
float nonCollideDt = dt;
do {
actor.MoveCollisionPrimitive(newMState.x0_translation);
collisionList.Clear();
TUniqueId otherId = kInvalidUniqueId;
bool collided =
CGameCollision::DetectCollision_Cached(mgr, cache, *actor.GetCollisionPrimitive(),
actor.GetPrimitiveTransform(), filter, nearList, otherId, collisionList);
if (collided) {
2018-12-08 05:30:43 +00:00
otherActor = mgr.ObjectById(otherId);
}
actor.MoveCollisionPrimitive(zeus::skZero3f);
2018-12-08 05:30:43 +00:00
if (collided) {
didCollide = true;
if (newMState.x0_translation.magnitude() < divMag) {
CCollisionInfoList backfaceFilteredList;
CCollisionInfoList floorFilteredList;
2018-12-08 05:30:43 +00:00
zeus::CVector3f deltaVel = actor.GetVelocity();
if (otherActor) {
2018-12-08 05:30:43 +00:00
deltaVel -= otherActor->GetVelocity();
}
2018-12-08 05:30:43 +00:00
CollisionUtil::FilterOutBackfaces(deltaVel, collisionList, backfaceFilteredList);
CAABoxFilter::FilterBoxFloorCollisions(backfaceFilteredList, floorFilteredList);
CGameCollision::MakeCollisionCallbacks(mgr, actor, otherId, floorFilteredList);
if (floorFilteredList.GetCount() == 0 && isPlayer) {
CMotionState lastNonCollideState = actor.GetLastNonCollidingState();
lastNonCollideState.x1c_velocity *= zeus::CVector3f(0.5f);
lastNonCollideState.x28_angularMomentum *= zeus::CVector3f(0.5f);
actor.SetMotionState(lastNonCollideState);
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
for (const CCollisionInfo& info : floorFilteredList) {
CCollisionInfo infoCopy = info;
float restitution =
CGameCollision::GetCoefficientOfRestitution(infoCopy) + actor.GetCoefficientOfRestitutionModifier();
if (otherActor) {
2018-12-08 05:30:43 +00:00
CGameCollision::CollideWithDynamicBodyNoRot(actor, *otherActor, infoCopy, restitution, true);
} else {
2018-12-08 05:30:43 +00:00
CGameCollision::CollideWithStaticBodyNoRot(actor, infoCopy.GetMaterialLeft(), infoCopy.GetMaterialRight(),
infoCopy.GetNormalLeft(), restitution, true);
}
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
remDt -= dt;
nonCollideDt = std::min(originalDt, remDt);
dt = nonCollideDt;
} else {
nonCollideDt *= 0.5f;
dt *= 0.5f;
}
} else {
actor.AddMotionState(newMState);
remDt -= dt;
dt = nonCollideDt;
actor.MoveCollisionPrimitive(zeus::skZero3f);
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
newMState = actor.PredictMotion_Internal(dt);
} while (remDt > 0.f);
2017-06-19 07:00:50 +00:00
if (!didCollide && !actor.GetMaterialList().HasMaterial(EMaterialTypes::GroundCollider)) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::Falling);
}
2017-06-19 07:00:50 +00:00
actor.MoveCollisionPrimitive(zeus::skZero3f);
2017-06-19 07:00:50 +00:00
}
2017-06-21 07:24:26 +00:00
zeus::CVector3f CGroundMovement::CollisionDamping(const zeus::CVector3f& vel, const zeus::CVector3f& dir,
2018-12-08 05:30:43 +00:00
const zeus::CVector3f& cNorm, float normCoeff, float deltaCoeff) {
zeus::CVector3f dampedDir = (cNorm * -2.f * cNorm.dot(dir) + dir).normalized();
zeus::CVector3f dampedNorm = cNorm * cNorm.dot(dampedDir);
return (dampedDir - dampedNorm) * vel.magnitude() * deltaCoeff + normCoeff * vel.magnitude() * dampedNorm;
2017-06-19 07:00:50 +00:00
}
void CGroundMovement::MoveGroundCollider_New(CStateManager& mgr, CPhysicsActor& actor, float dt,
2018-12-08 05:30:43 +00:00
const rstl::reserved_vector<TUniqueId, 1024>* nearList) {
zeus::CAABox motionVol = actor.GetMotionVolume(dt);
rstl::reserved_vector<TUniqueId, 1024> useNearList;
if (nearList != nullptr) {
2018-12-08 05:30:43 +00:00
useNearList = *nearList;
} else {
2018-12-08 05:30:43 +00:00
mgr.BuildColliderList(useNearList, actor, motionVol);
}
2018-12-08 05:30:43 +00:00
CAreaCollisionCache cache(motionVol);
CGameCollision::BuildAreaCollisionCache(mgr, cache);
auto& player = static_cast<CPlayer&>(actor);
2018-12-08 05:30:43 +00:00
player.x9c5_28_slidingOnWall = false;
bool applyJump = player.x258_movementState == CPlayer::EPlayerMovementState::ApplyJump;
bool dampUnderwater = false;
if (player.x9c4_31_inWaterMovement) {
if (!mgr.GetPlayerState()->HasPowerUp(CPlayerState::EItemType::GravitySuit)) {
2018-12-08 05:30:43 +00:00
dampUnderwater = true;
}
}
2018-12-08 05:30:43 +00:00
bool noJump = (player.x258_movementState != CPlayer::EPlayerMovementState::ApplyJump &&
player.x258_movementState != CPlayer::EPlayerMovementState::Jump);
float stepDown = player.GetStepDownHeight();
float stepUp = player.GetStepUpHeight();
bool doStepDown = true;
CMaterialList material(EMaterialTypes::NoStepLogic);
SMoveObjectResult result;
if (!applyJump) {
const SMovementOptions opts{
.x0_setWaterLandingForce = false,
.x4_waterLandingForceCoefficient = 0.f,
.x8_minimumWaterLandingForce = 0.f,
.xc_anyZThreshold = 0.37f,
.x10_downwardZThreshold = 0.25f,
.x14_waterLandingVelocityReduction = 0.f,
.x18_dampForceAndMomentum = true,
.x19_alwaysClip = false,
.x1a_disableClipForFloorOnly = noJump,
.x1c_maxCollisionCycles = 4,
.x20_minimumTranslationDelta = 0.002f,
.x24_dampedNormalCoefficient = 0.f,
.x28_dampedDeltaCoefficient = 1.f,
.x2c_floorElasticForce = 0.f,
.x30_wallElasticConstant = 0.02f,
.x34_wallElasticLinear = 0.2f,
.x38_maxPositiveVerticalVelocity = player.GetMaximumPlayerPositiveVerticalVelocity(mgr),
.x3c_floorPlaneNormal = player.GetLastFloorPlaneNormal(),
};
2018-12-08 05:30:43 +00:00
if (noJump) {
zeus::CVector3f vel = player.GetVelocity();
vel.z() = 0.f;
actor.SetVelocityWR(vel);
actor.x15c_force.z() = 0.f;
actor.x150_momentum.z() = 0.f;
actor.x168_impulse.z() = 0.f;
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
CPhysicsState physStatePre = actor.GetPhysicsState();
CMaterialList material2 = MoveObjectAnalytical(mgr, actor, dt, useNearList, cache, opts, result);
CPhysicsState physStatePost = actor.GetPhysicsState();
/* NoStepLogic must be the only set material bit to bypass step logic */
if (material2.XOR({EMaterialTypes::NoStepLogic}) != 0u) {
2018-12-08 05:30:43 +00:00
SMovementOptions optsCopy = opts;
optsCopy.x19_alwaysClip = noJump;
optsCopy.x30_wallElasticConstant = 0.03f;
const zeus::CVector3f postToPre = physStatePre.GetTranslation() - physStatePost.GetTranslation();
const float postToPreMag = postToPre.magSquared();
const float quarterStepUp = 0.25f * stepUp;
2018-12-08 05:30:43 +00:00
rstl::reserved_vector<CPhysicsState, 2> physStateList;
rstl::reserved_vector<float, 2> stepDeltaList;
rstl::reserved_vector<CCollisionInfo, 2> collisionInfoList;
rstl::reserved_vector<TUniqueId, 2> uniqueIdList;
rstl::reserved_vector<CMaterialList, 2> materialListList;
bool done = false;
for (int i = 0; i < 2 && !done; ++i) {
double useStepUp = (i == 0) ? quarterStepUp : stepUp;
actor.SetPhysicsState(physStatePre);
2017-06-21 07:24:26 +00:00
CCollisionInfo collisionInfo;
TUniqueId id = kInvalidUniqueId;
2018-12-08 05:30:43 +00:00
CGameCollision::DetectCollision_Cached_Moving(mgr, cache, *actor.GetCollisionPrimitive(), actor.GetTransform(),
actor.GetMaterialFilter(), useNearList, {0.f, 0.f, 1.f}, id,
collisionInfo, useStepUp);
if (collisionInfo.IsValid()) {
useStepUp = std::max(0.0, useStepUp - optsCopy.x20_minimumTranslationDelta);
done = true;
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
if (useStepUp > 0.0005) {
actor.SetTranslation(actor.GetTranslation() + zeus::CVector3f(0.f, 0.f, static_cast<float>(useStepUp)));
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
SMoveObjectResult result2;
CMaterialList material3 = MoveObjectAnalytical(mgr, actor, dt, useNearList, cache, optsCopy, result2);
CCollisionInfo collisionInfo2;
double useStepDown2 = useStepUp + stepDown;
TUniqueId id2 = kInvalidUniqueId;
if (useStepDown2 > 0.0) {
CGameCollision::DetectCollision_Cached_Moving(mgr, cache, *actor.GetCollisionPrimitive(),
actor.GetTransform(), actor.GetMaterialFilter(), useNearList,
{0.f, 0.f, -1.f}, id2, collisionInfo2, useStepDown2);
} else {
useStepDown2 = 0.0;
}
float minStep = std::min(useStepUp, useStepDown2);
zeus::CVector3f offsetStep = actor.GetTranslation() - zeus::CVector3f(0.f, 0.f, minStep);
bool floor = (collisionInfo2.IsValid() &&
CGameCollision::CanBlock(collisionInfo2.GetMaterialLeft(), collisionInfo2.GetNormalLeft()));
zeus::CVector3f postToPre2 = physStatePre.GetTranslation() - offsetStep;
float stepDelta = postToPre2.magSquared();
if (floor && postToPreMag < stepDelta) {
useStepDown2 = std::max(0.0, useStepDown2 - 0.0005);
actor.SetTranslation(actor.GetTranslation() - zeus::CVector3f(0.f, 0.f, static_cast<float>(useStepDown2)));
2018-12-08 05:30:43 +00:00
physStateList.push_back(actor.GetPhysicsState());
stepDeltaList.push_back(stepDelta);
collisionInfoList.push_back(collisionInfo2);
uniqueIdList.push_back(id2);
materialListList.push_back(material3);
}
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
}
if (physStateList.empty()) {
2018-12-08 05:30:43 +00:00
actor.SetPhysicsState(physStatePost);
material = material2;
} else {
float maxFloat = -1.0e10f;
int maxIdx = -1;
for (size_t i = 0; i < physStateList.size(); ++i) {
2018-12-08 05:30:43 +00:00
if (maxFloat < stepDeltaList[i]) {
maxFloat = stepDeltaList[i];
maxIdx = i;
}
2017-06-21 07:24:26 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
actor.SetPhysicsState(physStateList[maxIdx]);
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::OnFloor);
if (CEntity* ent = mgr.ObjectById(uniqueIdList[maxIdx])) {
result.x0_id.emplace(uniqueIdList[maxIdx]);
result.x8_collision.emplace(collisionInfoList[maxIdx]);
if (TCastToPtr<CScriptPlatform>(ent)) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(ent, actor.GetUniqueId(), EScriptObjectMessage::AddPlatformRider);
}
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
CCollisionInfo& cInfo = collisionInfoList[maxIdx];
CGameCollision::SendMaterialMessage(mgr, cInfo.GetMaterialLeft(), actor);
doStepDown = false;
actor.SetLastFloorPlaneNormal({cInfo.GetNormalLeft()});
}
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
} else {
const SMovementOptions opts{
.x0_setWaterLandingForce = true,
.x4_waterLandingForceCoefficient = dampUnderwater ? 35.f : 1.f,
.x8_minimumWaterLandingForce = dampUnderwater ? 5.f : 0.f,
.xc_anyZThreshold = dampUnderwater ? 0.05f : 0.37f,
.x10_downwardZThreshold = dampUnderwater ? 0.01f : 0.25f,
.x14_waterLandingVelocityReduction = dampUnderwater ? 0.2f : 0.f,
.x18_dampForceAndMomentum = false,
.x19_alwaysClip = false,
.x1a_disableClipForFloorOnly = false,
.x1c_maxCollisionCycles = 4,
.x20_minimumTranslationDelta = 0.002f,
.x24_dampedNormalCoefficient = 0.f,
.x28_dampedDeltaCoefficient = 1.f,
.x2c_floorElasticForce = 0.1f,
.x30_wallElasticConstant = 0.2f,
.x38_maxPositiveVerticalVelocity = player.GetMaximumPlayerPositiveVerticalVelocity(mgr),
.x3c_floorPlaneNormal = player.GetLastFloorPlaneNormal(),
};
2018-12-08 05:30:43 +00:00
material = MoveObjectAnalytical(mgr, actor, dt, useNearList, cache, opts, result);
}
if (doStepDown) {
CCollisionInfo collisionInfo;
double stepDown2 = actor.GetStepDownHeight();
float zOffset = 0.f;
TUniqueId id = kInvalidUniqueId;
if (stepDown2 > FLT_EPSILON) {
zeus::CTransform xf = actor.GetTransform();
xf.origin += zeus::CVector3f(0.f, 0.f, 0.0005f);
if (!CGameCollision::DetectCollisionBoolean_Cached(mgr, cache, *actor.GetCollisionPrimitive(), xf,
actor.GetMaterialFilter(), useNearList)) {
actor.SetTranslation(xf.origin);
zOffset = 0.0005f;
stepDown2 += 0.0005;
}
CGameCollision::DetectCollision_Cached_Moving(mgr, cache, *actor.GetCollisionPrimitive(), actor.GetTransform(),
actor.GetMaterialFilter(), useNearList, {0.f, 0.f, -1.f}, id,
collisionInfo, stepDown2);
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
if (id != kInvalidUniqueId) {
result.x0_id.emplace(id);
result.x8_collision.emplace(collisionInfo);
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
if (!collisionInfo.IsValid() ||
!CGameCollision::CanBlock(collisionInfo.GetMaterialLeft(), collisionInfo.GetNormalLeft())) {
if (zOffset > 0.f) {
zeus::CTransform xf = actor.GetTransform();
xf.origin -= zeus::CVector3f(0.f, 0.f, zOffset);
}
if (collisionInfo.IsValid()) {
2018-12-08 05:30:43 +00:00
player.x9c5_28_slidingOnWall = true;
}
2018-12-08 05:30:43 +00:00
CheckFalling(actor, mgr, dt);
player.SetLastFloorPlaneNormal({});
} else {
mgr.SendScriptMsg(&actor, kInvalidUniqueId, EScriptObjectMessage::OnFloor);
stepDown2 = std::max(0.0, stepDown2 - 0.0005);
actor.SetTranslation(actor.GetTranslation() - zeus::CVector3f(0.f, 0.f, static_cast<float>(stepDown2)));
if (TCastToPtr<CScriptPlatform> plat = mgr.ObjectById(id)) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(plat.GetPtr(), actor.GetUniqueId(), EScriptObjectMessage::AddPlatformRider);
}
2018-12-08 05:30:43 +00:00
CGameCollision::SendMaterialMessage(mgr, collisionInfo.GetMaterialLeft(), actor);
actor.SetLastFloorPlaneNormal({collisionInfo.GetNormalLeft()});
}
}
actor.ClearForcesAndTorques();
if (material.HasMaterial(EMaterialTypes::Wall)) {
2018-12-08 05:30:43 +00:00
player.SetPlayerHitWallDuringMove();
}
2018-12-08 05:30:43 +00:00
if (result.x0_id) {
CCollisionInfoList list;
list.Add(*result.x8_collision, false);
CGameCollision::MakeCollisionCallbacks(mgr, actor, *result.x0_id, list);
}
CMotionState mState = actor.GetMotionState();
mState.x0_translation = actor.GetLastNonCollidingState().x0_translation;
mState.x1c_velocity = actor.GetLastNonCollidingState().x1c_velocity;
actor.SetLastNonCollidingState(mState);
const CCollisionPrimitive* usePrim = actor.GetCollisionPrimitive();
std::unique_ptr<CCollisionPrimitive> prim;
if (usePrim->GetPrimType() == FOURCC('AABX')) {
const auto& existingAABB = static_cast<const CCollidableAABox&>(*usePrim);
prim = std::make_unique<CCollidableAABox>(
zeus::CAABox(existingAABB.GetBox().min + 0.0001f, existingAABB.GetBox().max - 0.0001f), usePrim->GetMaterial());
2018-12-08 05:30:43 +00:00
usePrim = prim.get();
} else if (usePrim->GetPrimType() == FOURCC('SPHR')) {
const auto& existingSphere = static_cast<const CCollidableSphere&>(*usePrim);
prim = std::make_unique<CCollidableSphere>(
2018-12-08 05:30:43 +00:00
zeus::CSphere(existingSphere.GetSphere().position, existingSphere.GetSphere().radius - 0.0001f),
usePrim->GetMaterial());
2018-12-08 05:30:43 +00:00
usePrim = prim.get();
}
CGameCollision::CollisionFailsafe(mgr, cache, actor, *usePrim, useNearList, 0.f, 1);
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
bool CGroundMovement::RemoveNormalComponent(const zeus::CVector3f& a, const zeus::CVector3f& b, zeus::CVector3f& c,
float& d) {
float dot = a.dot(c);
if (std::fabs(dot) > 0.99f) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
float dot2 = b.dot(c);
float dot3 = b.dot((c - a * dot).normalized());
if (dot2 > 0.f && dot3 < 0.f) {
2018-12-08 05:30:43 +00:00
return false;
}
if (std::fabs(dot2) > 0.01f && std::fabs(dot3 / dot2) > 4.f) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
c -= dot * a;
d = dot;
return true;
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
bool CGroundMovement::RemoveNormalComponent(const zeus::CVector3f& a, zeus::CVector3f& b) {
float dot = a.dot(b);
if (std::fabs(dot) > 0.99f) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
b -= a * dot;
return true;
2017-06-21 07:24:26 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
static bool RemovePositiveZComponentFromNormal(zeus::CVector3f& vec) {
if (vec.z() > 0.f && vec.z() < 0.99f) {
vec.z() = 0.f;
vec.normalize();
return true;
}
return false;
2017-06-19 07:00:50 +00:00
}
2017-06-21 07:24:26 +00:00
CMaterialList CGroundMovement::MoveObjectAnalytical(CStateManager& mgr, CPhysicsActor& actor, float dt,
2017-06-19 07:00:50 +00:00
rstl::reserved_vector<TUniqueId, 1024>& nearList,
CAreaCollisionCache& cache, const SMovementOptions& opts,
2018-12-08 05:30:43 +00:00
SMoveObjectResult& result) {
result.x6c_processedCollisions = 0;
CMaterialList ret;
zeus::CVector3f floorPlaneNormal = opts.x3c_floorPlaneNormal ? *opts.x3c_floorPlaneNormal : zeus::skZero3f;
bool floorCollision = opts.x3c_floorPlaneNormal.has_value();
2018-12-08 05:30:43 +00:00
float remDt = dt;
for (size_t i = 0; remDt > 0.f; ++i) {
2018-12-08 05:30:43 +00:00
float collideDt = remDt;
CMotionState mState = actor.PredictMotion_Internal(remDt);
double mag = mState.x0_translation.magnitude();
zeus::CVector3f normTrans = (1.f / ((float(mag) > FLT_EPSILON) ? float(mag) : 1.f)) * mState.x0_translation;
TUniqueId id = kInvalidUniqueId;
CCollisionInfo collisionInfo;
if (mag > opts.x20_minimumTranslationDelta) {
double oldMag = mag;
CGameCollision::DetectCollision_Cached_Moving(mgr, cache, *actor.GetCollisionPrimitive(),
actor.GetPrimitiveTransform(), actor.GetMaterialFilter(), nearList,
normTrans, id, collisionInfo, mag);
if (id != kInvalidUniqueId) {
result.x0_id.emplace(id);
result.x8_collision.emplace(collisionInfo);
}
collideDt = static_cast<float>(mag / oldMag * remDt);
2018-12-08 05:30:43 +00:00
}
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
mag = std::max(0.f, float(mag) - opts.x20_minimumTranslationDelta);
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
zeus::CVector3f collisionNorm = collisionInfo.GetNormalLeft();
bool floor = CGameCollision::CanBlock(collisionInfo.GetMaterialLeft(), collisionNorm);
bool clipCollision = true;
if (!opts.x19_alwaysClip) {
if (!opts.x1a_disableClipForFloorOnly || floor) {
2018-12-08 05:30:43 +00:00
clipCollision = false;
}
2018-12-08 05:30:43 +00:00
}
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
float collisionFloorDot = 0.f;
if (collisionInfo.IsValid()) {
result.x6c_processedCollisions += 1;
if (floor) {
ret.Add(EMaterialTypes::Floor);
floorPlaneNormal = collisionInfo.GetNormalLeft();
floorCollision = true;
} else {
ret.Add(EMaterialTypes::Wall);
}
if (clipCollision) {
if (floorCollision) {
if (!CGroundMovement::RemoveNormalComponent(floorPlaneNormal, normTrans, collisionNorm, collisionFloorDot)) {
2018-12-08 05:30:43 +00:00
RemovePositiveZComponentFromNormal(collisionNorm);
} else {
2018-12-08 05:30:43 +00:00
collisionNorm.normalize();
}
2018-12-08 05:30:43 +00:00
} else {
RemovePositiveZComponentFromNormal(collisionNorm);
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
mState = actor.PredictMotion_Internal(collideDt);
}
2017-06-21 07:24:26 +00:00
mState.x0_translation = normTrans * static_cast<float>(mag);
2018-12-08 05:30:43 +00:00
actor.AddMotionState(mState);
if (collisionInfo.IsValid()) {
zeus::CVector3f vel =
actor.GetVelocity().canBeNormalized()
? CGroundMovement::CollisionDamping(actor.GetVelocity(), actor.GetVelocity().normalized(), collisionNorm,
opts.x24_dampedNormalCoefficient, opts.x28_dampedDeltaCoefficient)
: zeus::skZero3f;
2018-12-08 05:30:43 +00:00
float elasticForce = floor ? opts.x2c_floorElasticForce
: opts.x34_wallElasticLinear * collisionFloorDot + opts.x30_wallElasticConstant;
float dot = collisionNorm.dot(vel);
if (dot < elasticForce) {
2018-12-08 05:30:43 +00:00
vel += (elasticForce - dot) * collisionNorm;
}
if (clipCollision && floorCollision) {
if (!CGroundMovement::RemoveNormalComponent(floorPlaneNormal, vel)) {
2018-12-08 05:30:43 +00:00
vel.z() = 0.f;
}
}
if (vel.z() > opts.x38_maxPositiveVerticalVelocity) {
2018-12-08 05:30:43 +00:00
vel *= zeus::CVector3f(opts.x38_maxPositiveVerticalVelocity / vel.z());
}
2018-12-08 05:30:43 +00:00
if (opts.x18_dampForceAndMomentum) {
if (actor.x15c_force.canBeNormalized()) {
// zeus::CVector3f prevForce = actor.x15c_force;
actor.x15c_force = CGroundMovement::CollisionDamping(actor.x15c_force, actor.x15c_force.normalized(),
collisionNorm, 0.f, 1.f);
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
if (actor.x150_momentum.canBeNormalized()) {
actor.x150_momentum = CGroundMovement::CollisionDamping(actor.x150_momentum, actor.x150_momentum.normalized(),
collisionNorm, 0.f, 1.f);
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
}
if (opts.x0_setWaterLandingForce && !floor) {
if (collisionInfo.GetNormalLeft().z() < -0.1f && vel.z() > 0.f) {
2018-12-08 05:30:43 +00:00
vel.z() *= 0.5f;
}
2018-12-08 05:30:43 +00:00
float zNormAbs = std::fabs(collisionInfo.GetNormalLeft().z());
if ((zNormAbs > opts.x10_downwardZThreshold && vel.z() < 0.f) || zNormAbs > opts.xc_anyZThreshold) {
actor.x15c_force = zeus::CVector3f(
0.f, 0.f,
-(1.f + std::max(opts.x4_waterLandingForceCoefficient * zNormAbs, opts.x8_minimumWaterLandingForce)) *
actor.GetWeight());
vel *= zeus::CVector3f(1.f - opts.x14_waterLandingVelocityReduction);
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
actor.SetVelocityWR(vel);
} else {
zeus::CVector3f vel = actor.x138_velocity;
if (actor.x138_velocity.z() > opts.x38_maxPositiveVerticalVelocity) {
2018-12-08 05:30:43 +00:00
vel *= zeus::CVector3f(opts.x38_maxPositiveVerticalVelocity / vel.z());
}
2017-06-21 07:24:26 +00:00
2018-12-08 05:30:43 +00:00
actor.SetVelocityWR(vel);
2017-06-21 07:24:26 +00:00
}
2018-12-08 05:30:43 +00:00
actor.ClearImpulses();
remDt -= collideDt;
if (i >= opts.x1c_maxCollisionCycles) {
2018-12-08 05:30:43 +00:00
break;
}
2018-12-08 05:30:43 +00:00
}
2017-06-19 07:00:50 +00:00
2018-12-08 05:30:43 +00:00
result.x70_processedDt = dt - remDt;
return ret;
2017-06-19 07:00:50 +00:00
}
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
} // namespace metaforce