fix: update logic jump HP on ground.

This commit is contained in:
Tungdv
2025-12-25 18:13:21 +07:00
parent f03425d5d8
commit b766229425
3 changed files with 42 additions and 59 deletions
@@ -320,7 +320,6 @@ namespace BrewMonster
m_CDRInfo.vAbsVelocity.y = fVertSpeed;
m_CDRInfo.fYVel = 0.0f;
m_CDRInfo.vTPNormal = EC_Utility.ToA3DVECTOR3(Vector3.zero);
if (m_iJumpCount == 1)
{
m_MoveCtrl.SendMoveCmd(GetPos(), 2, GPDataTypeHelper.g_vAxisY, (m_CDRInfo.vAbsVelocity), m_iMoveMode, true);
+39 -57
View File
@@ -17,8 +17,16 @@ namespace BrewMonster
public static LayerMask TerrainMask { get; set; } = 1 << 6;
public static LayerMask WaterMask { get; set; } = 1 << 8;
public static RaycastHit[] hits = new RaycastHit[5];
const float LOCAL_EPSILON = 1e-5f;
const float FLY_MAX_HEIGHT = 800.0f; // ·ÉÐеÄ×î´ó¸ß¶È£¡
const float FLY_MAX_HEIGHT = 800.0f; // ·ÉÐеÄ×î´ó¸ß¶È£¡
const float VEL_EPSILON = 1e-4f;
const float DIST_EPSILON = 1e-4f;
const float NORMAL_EPSILON = 1e-2f;
const float VEL_MAX_SPEED = 200f;
const float VEL_REFLECT = 0.3f;
// change this array when some new submap can go!
static uint[,] available_maps =
@@ -177,30 +185,24 @@ namespace BrewMonster
float dist = fDeltaY + vExt.y;//Mathf.Max(fDeltaY, 0f) + vExt.y;
Vector3 origin = vStart /*+ Vector3.down * vExt.y*/;
if (Physics.Raycast(origin, Vector3.down, out RaycastHit hit, dist, mask))
int countHits = Physics.RaycastNonAlloc(origin, Vector3.down, hits, dist, mask);
if (countHits > 0)
{
vHitNormal = hit.normal;
//Debug.DrawLine(origin, vHitNormal, Color.black, 10f);
vEnd = new Vector3(vStart.x, hit.point.y + vExt.y, vStart.z);
vHitNormal = hits[0].normal;
vEnd = new Vector3(vStart.x, hits[0].point.y + vExt.y, vStart.z);
bSupport = (vHitNormal.y >= 0f);
return true;
}
return true; // không thấy ground → bSupport=false
return true;
}
// ======= STATIC OnGroundMove GIỮ NGUYÊN VAI TRÒ TOÀN CỤC (C API) =======
public static void OnGroundMove(ref CDR_INFO CDRInfo)
{
const float VEL_EPSILON = 1e-4f;
const float DIST_EPSILON = 1e-4f;
const float NORMAL_EPSILON = 1e-2f;
const float VEL_MAX_SPEED = 200f;
const float VEL_REFLECT = 0.3f;
CDRInfo.fMoveDist = 0.0f;
bool bFreeFall = (CDRInfo.vTPNormal.y < CDRInfo.fSlopeThresh);
Debug.LogError("CDRInfo.vTPNormal.y = " + CDRInfo.vTPNormal.y);
if (CDRInfo.fYVel < VEL_EPSILON && CDRInfo.fYVel > -VEL_EPSILON && CDRInfo.fSpeed < VEL_EPSILON && CDRInfo.fSpeed > -VEL_EPSILON && !bFreeFall)
return;
@@ -208,31 +210,26 @@ namespace BrewMonster
bool bJump = (fYVel > 0.5f);
Vector3 vVelocity = CDRInfo.fSpeed * EC_Utility.ToVector3(CDRInfo.vXOZVelDir) + fYVel * Vector3.up;
Debug.LogError("1 vVelocity = " + vVelocity);
if (bFreeFall)
{
vVelocity += -CDRInfo.fGravityAccel * CDRInfo.t * Vector3.up;
Debug.LogError("2 vVelocity = " + vVelocity);
fYVel += -CDRInfo.fGravityAccel * CDRInfo.t;
}
A3DVECTOR3 vVelDir = EC_Utility.ToA3DVECTOR3(vVelocity);
float fVelSpeed = vVelDir.Normalize();
if (!bFreeFall)
{
//fVelSpeed = Mathf.Max(fVelSpeed, VEL_MAX_SPEED);
if (fVelSpeed > VEL_MAX_SPEED)
{
fVelSpeed = VEL_MAX_SPEED;
}
}
vVelocity = EC_Utility.ToVector3(vVelDir) * fVelSpeed;
Debug.LogError("3 vVelocity = " + vVelocity);
float dtp = DotProduct(vVelDir, CDRInfo.vTPNormal);
if (dtp < 0f || !bJump)
{
vVelocity = EC_Utility.ToVector3((vVelDir -(CDRInfo.vTPNormal) * dtp - (CDRInfo.vTPNormal) * dtp * 0.01f) * fVelSpeed);
Debug.LogError("4 vVelocity = " + vVelocity);
}
CDRInfo.vAbsVelocity = EC_Utility.ToA3DVECTOR3(vVelocity);
@@ -252,42 +249,39 @@ namespace BrewMonster
trcInfo.dwCheckFlag = CDR_EVN.CDR_TERRAIN | CDR_EVN.CDR_BRUSH;
trcInfo.vExt = CDRInfo.vExtent;
int countHits = 0;
while (nTry < 1)
{
vDelta = vVelocity * (fTime);
float fDeltaDist = vDelta.magnitude;
Vector3 posFoot = vStart - Vector3.up * vExt.y;
Debug.LogError("fDeltaDist = " + fDeltaDist + " vVelocity = " + vVelocity + " fTime = " + fTime + " speed = " + (fDeltaDist / fTime) + " posFoot + vDelta = " + (posFoot + vDelta) + " posFoot = " + posFoot);
vFinalPos = vStart;
if (fDeltaDist < DIST_EPSILON) break;
if (Physics.Raycast(vStart, (Vector3.down).normalized, out RaycastHit hit, vExt.y, mask))
countHits = Physics.RaycastNonAlloc(vStart, (Vector3.down * vExt.y).normalized, hits, vExt.y, mask);
if (countHits > 0)
{
if (hit.point.y > posFoot.y)
if (hits[0].point.y >= posFoot.y)
{
posFoot.y = hit.point.y;
posFoot.y = hits[0].point.y;
}
}
bool bClear = !Physics.Raycast(posFoot, (posFoot + vDelta).normalized, out hit, fDeltaDist, mask);
Debug.DrawLine(posFoot, posFoot + vDelta, Color.yellow, 10f);
//Debug.LogError("fDeltaDist = " + fDeltaDist + " vVelocity = " + vVelocity + " fTime = " + fTime + " speed = " + (fDeltaDist / fTime) + " posFoot + vDelta = " + (posFoot + vDelta) + " posFoot = " + posFoot);
countHits = Physics.RaycastNonAlloc(posFoot, (posFoot + vDelta).normalized, hits, fDeltaDist, mask);
bool bClear = !(countHits > 0);
nTry++;
if (bClear)
if (bClear || (countHits > 0 && Vector3.Distance(hits[0].point, posFoot) < 0.0009f)) // Is 0.0009f the tolerance used to check if two points are the same?
{
//Debug.LogError("bClear = true");
//Debug.DrawLine(posFoot, posFoot + vDelta, Color.yellow, 10f);
vFinalPos = vStart + vDelta;
CDRInfo.fMoveDist += fDeltaDist;
break;
}
//Debug.LogError("bClear = false");
vStart = hit.point + Vector3.up * vExt.y;
//vStart += vDelta * trcInfo.fFraction + Vector3.up * vExt.y;
vStart = hits[0].point + Vector3.up * vExt.y;
vFinalPos = vStart;
if (Physics.Raycast(vStart, (Vector3.down).normalized, out hit, vExt.y, mask))
countHits = Physics.RaycastNonAlloc(vStart, (Vector3.down).normalized, hits, vExt.y, mask);
if (countHits > 0)
{
vNormal = hit.normal;
vNormal = hits[0].normal;
}
else
{
@@ -297,45 +291,36 @@ namespace BrewMonster
// Step-up (giữ tinh thần bản gốc)
if (!bFreeFall && !bTryPull && !bJump)
{
//float skin = 0.01f;
posFoot = vStart - Vector3.up * vExt.y /*+ Vector3.up * CDRInfo.fStepHeight*/;
if (Physics.Raycast(vStart, (vStart + Vector3.down).normalized, out hit, vExt.y, mask))
posFoot = vStart - Vector3.up * vExt.y;
countHits = Physics.RaycastNonAlloc(vStart, (vStart + Vector3.down).normalized, hits, vExt.y, mask);
if (countHits > 0)
{
if (hit.point.y > posFoot.y)
if (hits[0].point.y > posFoot.y)
{
posFoot.y = hit.point.y;
posFoot.y = hits[0].point.y;
}
}
//Vector3 vStartUp = new Vector3(0f, CDRInfo.fStepHeight, 0f);
bPull = !Physics.Raycast(posFoot, (Vector3.up).normalized, out hit, CDRInfo.fStepHeight, mask);
//if(bPull == false)
//{
// Debug.LogError("bPull = false hit = " + hit.collider.name);
//}
countHits = Physics.RaycastNonAlloc(posFoot, (Vector3.up).normalized, hits, CDRInfo.fStepHeight, mask);
bPull = !(countHits > 0);
if (bPull)
{
vStart += Vector3.up * CDRInfo.fStepHeight;
posFoot = vStart - Vector3.up * vExt.y;
fDeltaDist = (vVelocity.normalized).magnitude;
bool bMove = !Physics.Raycast(posFoot, (posFoot + vVelocity).normalized, out hit, fDeltaDist, mask);
//Vector3 posDelta = vDelta;
countHits = Physics.RaycastNonAlloc(posFoot, (posFoot + vVelocity).normalized, hits, fDeltaDist, mask);
bool bMove = !(countHits > 0);
if (!bMove)
{
//posDelta = vFinalPos - (hit.point + Vector3.up * vExt.y);
fDeltaDist *= Vector3.Distance(vFinalPos, (hit.point + Vector3.up * vExt.y)) / fDeltaDist;
vFinalPos = hit.point + Vector3.up * vExt.y;
//vFinalPos = vStart + vDelta * tmpInfo.fFraction + Vector3.up * vExt.y;
fDeltaDist *= Vector3.Distance(vFinalPos, (hits[0].point + Vector3.up * vExt.y)) / fDeltaDist;
vFinalPos = hits[0].point + Vector3.up * vExt.y;
}
else
{
//Debug.DrawLine(vFinalPos, vFinalPos + vDelta, Color.red, 10f);
vFinalPos += vDelta;
}
//Debug.LogError("vExt.x * vExt.x * 4 = " + vExt.x * vExt.x * 4 + " fDeltaDist = " + fDeltaDist);
if (fDeltaDist < (vExt.x * vExt.x * 4))
{
//Debug.LogError("set bPull = false");
vFinalPos.y -= CDRInfo.fStepHeight;
bPull = false;
}
@@ -363,7 +348,6 @@ namespace BrewMonster
{
if (vNormal.y >= CDRInfo.fSlopeThresh || vNormal.y < -NORMAL_EPSILON)
{
//Debug.LogError("1 fYVel = 0f");
fYVel = 0f;
}
}
@@ -371,7 +355,6 @@ namespace BrewMonster
{
if (vNormal.y >= CDRInfo.fSlopeThresh)
{
//Debug.LogError("2 fYVel = 0f");
fYVel = 0f;
}
}
@@ -381,7 +364,7 @@ namespace BrewMonster
// “vertical ground trace” thay RetrieveSupportPlane
Vector3 vTPNormal = Vector3.zero;
Vector3 vFinal = vFinalPos;
mask = BrushMask;
//mask = BrushMask;
float downDist = 0.3f;
if (bPull) downDist = CDRInfo.fStepHeight + 0.1f;
@@ -408,7 +391,6 @@ namespace BrewMonster
if ((vTPNormal.y >= CDRInfo.fSlopeThresh && fYVel < 0.0f) || (!bJump && fYVel > 0.0f))
{
//Debug.LogError("3 fYVel = 0f");
fYVel = 0.0f;
}
+3 -1
View File
@@ -2253,7 +2253,9 @@ namespace BrewMonster
m_MoveCtrl.SetLastSevPos(EC_Utility.ToA3DVECTOR3(pos));
//m_CDRInfo.vTPNormal = GroundCheck(out RaycastHit hit) ? hit.normal : Vector3.zero;
m_CDRInfo.vExtent = m_aabbServer.Extents;
if (Physics.Raycast(pos, Vector3.down, out RaycastHit hit, 0.1f, 1<<6))
Vector3 pStart = pos;
pos.y += m_CDRInfo.vExtent.y;
if (Physics.Raycast(pos, Vector3.down, out RaycastHit hit, m_CDRInfo.vExtent.y, 1<<6))
{
m_CDRInfo.vTPNormal = EC_Utility.ToA3DVECTOR3(hit.normal);
}