From df792f0560a37eea8cd0f1923f7dfe8b6fdb2055 Mon Sep 17 00:00:00 2001 From: Tungdv Date: Tue, 16 Dec 2025 17:32:16 +0700 Subject: [PATCH] Fix: update check brush. --- Assets/PerfectWorld/Scripts/Move/EC_CDR.cs | 159 ++++++++------------- 1 file changed, 63 insertions(+), 96 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Move/EC_CDR.cs b/Assets/PerfectWorld/Scripts/Move/EC_CDR.cs index c32d5ba7c1..e0d7b0c6ba 100644 --- a/Assets/PerfectWorld/Scripts/Move/EC_CDR.cs +++ b/Assets/PerfectWorld/Scripts/Move/EC_CDR.cs @@ -101,34 +101,44 @@ namespace BrewMonster pEnvTrc.fFraction = 100.0f; pEnvTrc.bStartSolid = false; pEnvTrc.dwClsFlag = 0; + Vector3 vStart = EC_Utility.ToVector3(pEnvTrc.vStart); + Vector3 vExt = EC_Utility.ToVector3(pEnvTrc.vExt); + Vector3 vDelta = EC_Utility.ToVector3(pEnvTrc.vDelta); + Vector3 vTerStart = EC_Utility.ToVector3(pEnvTrc.vTerStart); + Vector3 dir = Vector3.zero; + if ((pEnvTrc.dwCheckFlag & CDR_EVN.CDR_BRUSH) == CDR_EVN.CDR_BRUSH) { - BrushTraceInfo bruInfo = new BrushTraceInfo(); - bruInfo.Init(pEnvTrc.vStart, pEnvTrc.vDelta, pEnvTrc.vExt); - if (AABBCollideWithBrush(ref bruInfo)) + dir = vStart + vDelta; + if (Physics.BoxCast(vStart, vExt, dir, out RaycastHit hit, Quaternion.identity, vDelta.magnitude, 1 << 7)) { - pEnvTrc.fFraction = bruInfo.fFraction; - pEnvTrc.vHitNormal = bruInfo.ClipPlane.GetNormal(); - pEnvTrc.bStartSolid = bruInfo.bStartSolid; + pEnvTrc.fFraction = (hit.distance - vExt.x) / vDelta.magnitude; + pEnvTrc.vHitNormal = EC_Utility.ToA3DVECTOR3(hit.normal); pEnvTrc.dwClsFlag = CDR_EVN.CDR_BRUSH; } + else + { + pEnvTrc.fFraction = 1f; + } } if ((pEnvTrc.dwCheckFlag & CDR_EVN.CDR_TERRAIN) == CDR_EVN.CDR_TERRAIN) { - float fFraction = 0f; - A3DVECTOR3 vTerNormal = new A3DVECTOR3(); - bool bStart = false; - if (CollideWithTerrain(pEnvTrc.vTerStart, pEnvTrc.vDelta, ref fFraction, ref vTerNormal, ref bStart) - && (fFraction < pEnvTrc.fFraction)) + float fFractionTerrain = 0f; + dir = vDelta; + if (Physics.Raycast(vTerStart, dir.normalized, out RaycastHit hit, vDelta.magnitude, 1<<6)) { - //assert(fFraction >= 0.0f); - //pEnvTrc.fFraction = a_Max(0.0f, fFraction - 1E-4f); - pEnvTrc.fFraction = fFraction; - pEnvTrc.vHitNormal = vTerNormal; - pEnvTrc.bStartSolid = bStart; + fFractionTerrain = (hit.distance) / vDelta.magnitude; + pEnvTrc.vHitNormal = EC_Utility.ToA3DVECTOR3(hit.normal); pEnvTrc.dwClsFlag = CDR_EVN.CDR_TERRAIN; } - + else + { + fFractionTerrain = 1f; + } + if(fFractionTerrain < pEnvTrc.fFraction) + { + pEnvTrc.fFraction = fFractionTerrain; + } } if ((pEnvTrc.dwCheckFlag & CDR_EVN.CDR_WATER) == CDR_EVN.CDR_WATER) @@ -156,46 +166,6 @@ namespace BrewMonster return (pEnvTrc.fFraction < 1.0f + 1E-4f); } - // == Thay CollideWithEnv (C++) bằng BoxCast == - //static bool CollideWithEnv_BoxCast(Vector3 vStart, Vector3 vDelta, Vector3 vExt, - // LayerMask mask, - // out RaycastHit hit, out float fFraction, out Vector3 vHitNormal, out bool bStartSolid, - // float skin = 0.01f) - //{ - // hit = default; - // vHitNormal = Vector3.up; - // bStartSolid = false; - // fFraction = 0.0f; - - // float dist = vDelta.magnitude; - // if (dist <= 1e-6f) return false; - - // var hasHit = Physics.Raycast(vStart, vDelta,out hit, dist, mask); - // if (hasHit) - // { - // bStartSolid = true; - // fFraction = (t - 5E-4f) / vDelta.Normalize(); - // return true; - // } - // // start-in-solid - // var overlapped = Physics.OverlapBox(vStart, vExt - Vector3.one * skin, Quaternion.identity, mask, QueryTriggerInteraction.Ignore); - // if (overlapped != null && overlapped.Length > 0) - // { - // bStartSolid = true; - // return true; - // } - - // // sweep AABB - // Vector3 dir = vDelta / Mathf.Max(dist, 1e-6f); - // if (Physics.BoxCast(vStart, vExt - Vector3.one * skin, dir, out hit, Quaternion.identity, dist, mask, QueryTriggerInteraction.Ignore)) - // { - // fFraction = Mathf.Clamp01(hit.distance / Mathf.Max(dist, 1e-6f)); - // vHitNormal = hit.normal; - // return true; - // } - // return false; - //} - // == Thay RetrieveSupportPlane (C++) bằng Raycast xuống == static bool DoGroundProbe(Vector3 vStart, Vector3 vExt, float fDeltaY, LayerMask mask, out Vector3 vEnd, out Vector3 vHitNormal, out bool bSupport, @@ -280,6 +250,10 @@ namespace BrewMonster int nTry = 0; LayerMask mask = UsedMask_Ground(); + env_trace_t trcInfo = new env_trace_t(); + trcInfo.dwCheckFlag = CDR_EVN.CDR_TERRAIN | CDR_EVN.CDR_BRUSH; + trcInfo.vExt = CDRInfo.vExtent; + while (nTry < 1) { vDelta = vVelocity * fTime; @@ -290,22 +264,20 @@ namespace BrewMonster //bool hasHit = CollideWithEnv_BoxCast(vStart, vDelta, vExt, mask, // out RaycastHit hit, out float fFraction, out Vector3 hitNormal, out bool bStartSolid); Vector3 posFoot = vStart - Vector3.up * vExt.y; - //if (Physics.BoxCast(vStart, vExt, (vStart + Vector3.down).normalized, out RaycastHit hit1, Quaternion.identity, vExt.y, mask)) - //{ - // if (hit1.point.y > posFoot.y) - // { - // Debug.LogError("hit.point.y > posFoot.y"); - // posFoot.y = hit1.point.y; - // } - //} - if (Physics.Raycast(vStart, (vStart + Vector3.down).normalized, out RaycastHit hit1, vExt.y, mask)) + + if (Physics.Raycast(vStart, (vStart + Vector3.down).normalized, out RaycastHit hit, vExt.y, mask)) { - if (hit1.point.y > posFoot.y) + if (hit.point.y > posFoot.y) { - posFoot.y = hit1.point.y; + posFoot.y = hit.point.y; } } - bool bClear = !Physics.Raycast(posFoot, (posFoot + vDelta).normalized, out RaycastHit hit, fDeltaDist, mask); + //bool bClear = !Physics.Raycast(posFoot, (posFoot + vDelta).normalized, out hit, fDeltaDist, mask); + trcInfo.vStart = CDRInfo.vCenter; + trcInfo.vDelta = EC_Utility.ToA3DVECTOR3(vDelta); + trcInfo.vTerStart = EC_Utility.ToA3DVECTOR3(posFoot); + //trcInfo.vTerStart.y -= vExt.y; //foot + bool bClear = !CollideWithEnv(ref trcInfo); nTry++; if (bClear) { @@ -314,17 +286,10 @@ namespace BrewMonster CDRInfo.fMoveDist += fDeltaDist; break; } - //if (trcInfo.bStartSolid) - //{ - // CDRInfo.fMoveDist = 0f; - // if (CDRInfo.vTPNormal.y < CDRInfo.fSlopeThresh) CDRInfo.vTPNormal = Vector3.up; - // return; - //} - vStart = hit.point + Vector3.up * vExt.y; + //vStart = hit.point + Vector3.up * vExt.y; + vStart = vDelta * trcInfo.fFraction + Vector3.up * vExt.y; + vFinalPos = vStart; - //CDRInfo.fMoveDist += (fDeltaDist * fFraction); - //fTime -= fTime * fFraction; - //vNormal = hitNormal; // Step-up (giữ tinh thần bản gốc) if (!bFreeFall && !bTryPull && !bJump) @@ -332,36 +297,38 @@ namespace BrewMonster //float skin = 0.01f; posFoot = vStart - Vector3.up * vExt.y + Vector3.up * CDRInfo.fStepHeight; Vector3 vStartUp = new Vector3(0f, CDRInfo.fStepHeight, 0f); - //if (Physics.Raycast(vStart, (vStart + Vector3.down).normalized, out hit1, vExt.y, mask)) - //{ - // if (hit1.point.y > posFoot.y) - // { - // Debug.LogError("hit.point.y > posFoot.y"); - // posFoot.y = hit1.point.y + CDRInfo.fStepHeight; - // } - //} - bPull = !Physics.Raycast(posFoot, (Vector3.up).normalized, out hit, CDRInfo.fStepHeight, mask); + //bPull = !Physics.Raycast(posFoot, (Vector3.up).normalized, out hit, CDRInfo.fStepHeight, mask); + + env_trace_t tmpInfo = new env_trace_t(); + tmpInfo.vStart = EC_Utility.ToA3DVECTOR3(vStart); + tmpInfo.vDelta = new A3DVECTOR3(0.0f, CDRInfo.fStepHeight, 0.0f); + tmpInfo.vExt = CDRInfo.vExtent; + //@note : need check terrain?? By Kuiwu[8/10/2005] + tmpInfo.dwCheckFlag = CDR_EVN.CDR_BRUSH | CDR_EVN.CDR_TERRAIN; + tmpInfo.vTerStart = EC_Utility.ToA3DVECTOR3(posFoot); + + bPull = !CollideWithEnv(ref tmpInfo); if (bPull) { vStart += Vector3.up * CDRInfo.fStepHeight; posFoot = vStart - Vector3.up * vExt.y; - //Vector3 vDelta2 = vVelocity; - bool bMove = !Physics.Raycast(posFoot, (posFoot + vVelocity).normalized, out hit, fDeltaDist, mask); + //bool bMove = !Physics.Raycast(posFoot, (posFoot + vVelocity).normalized, out hit, fDeltaDist, mask); + + tmpInfo.vStart = EC_Utility.ToA3DVECTOR3(vStart); + tmpInfo.vDelta = EC_Utility.ToA3DVECTOR3(vDelta); + tmpInfo.vTerStart = EC_Utility.ToA3DVECTOR3(posFoot); + bool bMove = !CollideWithEnv(ref tmpInfo); + if (!bMove) { //vDelta2 *= frac2; - vFinalPos = hit.point + Vector3.up * vExt.y; + vFinalPos = vDelta * tmpInfo.fFraction + Vector3.up * vExt.y; } else { Debug.DrawLine(vFinalPos, vFinalPos + vDelta, Color.red, 10f); vFinalPos += vDelta; } - //if (vDelta2.sqrMagnitude < (vExt.x * vExt.x * 4f)) - //{ - // vStart -= Vector3.up * CDRInfo.fStepHeight; - // bPull = false; - //} } bTryPull = true; }