NSEntity: BBox will now account for rotation whenever SetModel() is used.

This commit is contained in:
Marco Cawthorne 2022-01-20 12:18:24 -08:00
parent d41c770393
commit 9e2b89e386
Signed by: eukara
GPG Key ID: C196CD8BA993248A
4 changed files with 59 additions and 6 deletions

View File

@ -82,4 +82,5 @@ class NSEntity:NSTrigger
virtual void(vector, vector) SetSize;
virtual void(string, string) SpawnKey;
nonvirtual void(void) Destroy;
virtual void(void) UpdateBounds;
};

View File

@ -283,6 +283,12 @@ NSEntity::SetSolid(float newSolid)
SetSendFlags(BASEFL_CHANGED_SOLID);
}
void
NSEntity::UpdateBounds(void)
{
SetSize(m_vecMins, m_vecMaxs);
}
void
NSEntity::SetAngles(vector newAngles)
{
@ -291,20 +297,54 @@ NSEntity::SetAngles(vector newAngles)
angles = newAngles;
SetSendFlags(BASEFL_CHANGED_ANGLES);
UpdateBounds();
}
void
NSEntity::SetSize(vector newMins, vector newMaxs)
{
float sz = 1.0f;
float flScale = 1.0f;
m_vecMins = newMins;
m_vecMaxs = newMaxs;
if (scale != 0.0)
sz = scale;
/* avoid useless computation */
if (angles != [0,0,0]) {
/* adjust bbox according to rotation */
vector vecCorner[8];
newMins = newMaxs = [0,0,0];
for (int i = 0; i < 8; i++) {
vecCorner[i][0] = (i & 1) ? m_vecMins[0] : m_vecMaxs[0];
vecCorner[i][1] = (i & 2) ? m_vecMins[1] : m_vecMaxs[1];
vecCorner[i][2] = (i & 4) ? m_vecMins[2] : m_vecMaxs[2];
vecCorner[i] += origin;
vecCorner[i] = Math_RotateAroundPivot(vecCorner[i], origin, angles[1]);
vecCorner[i] -= origin;
setsize(this, m_vecMins * sz, m_vecMaxs * sz);
if (!(vecCorner[i][0] <= newMaxs[0]))
newMaxs[0] = vecCorner[i][0];
if (!(vecCorner[i][1] <= newMaxs[1]))
newMaxs[1] = vecCorner[i][1];
if (!(vecCorner[i][2] <= newMaxs[2]))
newMaxs[2] = vecCorner[i][2];
if (!(vecCorner[i][0] >= newMins[0]))
newMins[0] = vecCorner[i][0];
if (!(vecCorner[i][1] >= newMins[1]))
newMins[1] = vecCorner[i][1];
if (!(vecCorner[i][2] >= newMins[2]))
newMins[2] = vecCorner[i][2];
}
}
/* 0.0 is never valid, if you want it to disappear do something else */
if (scale != 0.0)
flScale = scale;
setsize(this, newMins * flScale, newMaxs * flScale);
SetSendFlags(BASEFL_CHANGED_SIZE);
}
void
NSEntity::SetOrigin(vector newOrigin)
{
@ -315,13 +355,15 @@ NSEntity::SetOrigin(vector newOrigin)
SetSendFlags(BASEFL_CHANGED_ORIGIN);
}
void
NSEntity::SetModel(string newModel)
{
model = newModel;
setmodel(this, newModel);
/* mins/maxs have been updated by setmodel */
SetSize(mins, maxs);
SetSendFlags(BASEFL_CHANGED_MODELINDEX);
}
void

View File

@ -20,4 +20,5 @@ float Math_LerpAngle(float fStart, float fEnd, float fAmount);
float Math_Lerp(float fA, float fB, float fPercent);
float Math_FixDelta(float fDelta);
vector Math_Reflect(vector v1, vector v2);
vector Math_RandomVector(float flyup);
vector Math_RandomVector(float flyup);
vector Math_RotateAroundPivot(vector pos, vector pivot, float degr);

View File

@ -58,4 +58,13 @@ Math_RandomVector(float fFlyUp)
}
return tmp * 2.0f;
}
vector
Math_RotateAroundPivot(vector pos, vector pivot, float degr)
{
vector new = pos;
new[0] = pivot[0] + (pos[0] - pivot[0]) * cos(degr) - (pos[1] - pivot[1]) * sin(degr);
new[1] = pivot[1] + (pos[0] - pivot[0]) * sin(degr) + (pos[1] - pivot[1]) * cos(degr);
return new;
}