Skip to content

Commit 4f32cf7

Browse files
authored
Merge pull request #189 from ZehMatt/fix-pusher-movedone
Fix pushers
2 parents a698649 + 9649926 commit 4f32cf7

7 files changed

Lines changed: 312 additions & 4 deletions

File tree

‎Game.Server/BaseEntity.cs‎

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,11 +1407,251 @@ public virtual void VPhysicsUpdate(IPhysicsObject physics) {
14071407
PhysicsRelinkChildren(gpGlobals.FrameTime);
14081408
break;
14091409
}
1410+
1411+
case Source.MoveType.Push:
1412+
VPhysicsUpdatePusher(physics);
1413+
break;
1414+
14101415
default:
14111416
break;
14121417
}
14131418
}
14141419

1420+
static BaseEntity? FindPhysicsBlocker(IPhysicsObject physics, PhysicsPushList list, in Vector3 pushVel) {
1421+
IPhysicsFrictionSnapshot snapshot = physics.CreateFrictionSnapshot();
1422+
BaseEntity? blocker = null;
1423+
float maxForce = 0;
1424+
while (snapshot.IsValid()) {
1425+
IPhysicsObject other = snapshot.GetObject(1)!;
1426+
BaseEntity? otherEntity = other.GetGameData() as BaseEntity;
1427+
bool inList = false;
1428+
for (int i = 0; i < list.PushedCount; i++) {
1429+
if (otherEntity == list.PushedEnts[i].Get()) {
1430+
inList = true;
1431+
break;
1432+
}
1433+
}
1434+
1435+
snapshot.GetSurfaceNormal(out Vector3 normal);
1436+
float dot = Vector3.Dot(pushVel, snapshot.GetNormalForce() * normal);
1437+
if (blocker == null || (!inList && dot > maxForce)) {
1438+
blocker = otherEntity;
1439+
if (!inList)
1440+
maxForce = dot;
1441+
}
1442+
1443+
snapshot.NextFrictionData();
1444+
}
1445+
physics.DestroyFrictionSnapshot(snapshot);
1446+
1447+
return blocker;
1448+
}
1449+
1450+
struct PushBlock
1451+
{
1452+
public PhysicsPushList? List;
1453+
public BaseEntity RootParent;
1454+
public BaseEntity? BlockedEntity;
1455+
public float MoveBackFraction;
1456+
public float MoveTime;
1457+
}
1458+
1459+
static void ComputePushStartMatrix(out Matrix3x4 start, BaseEntity entity, in PushBlock pushBlock) {
1460+
Vector3 localOrigin;
1461+
QAngle localAngles;
1462+
if (pushBlock.List != null) {
1463+
localOrigin = pushBlock.List.LocalOrigin;
1464+
localAngles = pushBlock.List.LocalAngles;
1465+
}
1466+
else {
1467+
localOrigin = pushBlock.RootParent.GetAbsOrigin() - pushBlock.RootParent.GetAbsVelocity() * pushBlock.MoveTime;
1468+
localAngles = pushBlock.RootParent.GetAbsAngles() - pushBlock.RootParent.GetLocalAngularVelocity() * pushBlock.MoveTime;
1469+
}
1470+
MathLib.AngleMatrix(localAngles, localOrigin, out Matrix3x4 xform);
1471+
1472+
MathLib.MatrixInvert(pushBlock.RootParent.EntityToWorldTransform(), out Matrix3x4 srcInv);
1473+
MathLib.ConcatTransforms(xform, srcInv, out Matrix3x4 delta);
1474+
MathLib.ConcatTransforms(delta, entity.EntityToWorldTransform(), out start);
1475+
}
1476+
1477+
static void CheckPushedEntity(BaseEntity entity, ref PushBlock pushBlock) {
1478+
IPhysicsObject? physics = entity.VPhysicsGetObject();
1479+
if (physics == null)
1480+
return;
1481+
1482+
Assert(physics.IsMoveable());
1483+
IPhysicsShadowController? shadow = physics.GetShadowController();
1484+
if (!physics.IsMoveable() || shadow == null)
1485+
return;
1486+
1487+
bool checkrot = true;
1488+
bool checkmove = true;
1489+
physics.GetShadowPosition(out Vector3 origin, out QAngle angles);
1490+
float fraction = -1.0f;
1491+
1492+
if (entity == pushBlock.RootParent) {
1493+
if (entity.GetLocalAngularVelocity() == vec3_angle)
1494+
checkrot = false;
1495+
if (entity.GetLocalVelocity() == vec3_origin)
1496+
checkmove = false;
1497+
}
1498+
1499+
if (checkmove) {
1500+
Vector3 dir = entity.GetAbsVelocity();
1501+
float speed = MathLib.VectorNormalize(ref dir);
1502+
shadow.GetTargetPosition(out Vector3 targetPos, out _);
1503+
float targetAmount = Vector3.Dot(targetPos, dir);
1504+
float currentAmount = Vector3.Dot(origin, dir);
1505+
float entityAmount = Vector3.Dot(entity.GetAbsOrigin(), dir);
1506+
1507+
if ((targetAmount - entityAmount) > 1)
1508+
entity.UpdatePhysicsShadowToCurrentPosition(0);
1509+
else {
1510+
float dist = targetAmount - currentAmount;
1511+
if (dist > 1) {
1512+
float movementAmount = targetAmount - (speed * pushBlock.MoveTime);
1513+
if (entity == pushBlock.RootParent) {
1514+
if (pushBlock.List != null) {
1515+
Vector3 localVel = entity.GetLocalVelocity();
1516+
MathLib.VectorNormalize(ref localVel);
1517+
float localTargetAmt = Vector3.Dot(entity.GetLocalOrigin(), localVel);
1518+
movementAmount = targetAmount + Vector3.Dot(pushBlock.List.LocalOrigin, localVel) - localTargetAmt;
1519+
}
1520+
}
1521+
else {
1522+
ComputePushStartMatrix(out Matrix3x4 start, entity, pushBlock);
1523+
MathLib.MatrixPosition(start, out Vector3 startPos);
1524+
movementAmount = Vector3.Dot(startPos, dir);
1525+
}
1526+
float expectedDist = targetAmount - movementAmount;
1527+
if (expectedDist <= 0)
1528+
fraction = 1;
1529+
else {
1530+
fraction = dist / expectedDist;
1531+
fraction = Math.Clamp(fraction, 0.0f, 1.0f);
1532+
}
1533+
}
1534+
}
1535+
}
1536+
1537+
if (checkrot) {
1538+
MathLib.RotationDeltaAxisAngle(angles, entity.GetAbsAngles(), out Vector3 axis, out float deltaAngle);
1539+
if (MathF.Abs(deltaAngle) > 0.5f) {
1540+
shadow.GetTargetPosition(out _, out QAngle targetRot);
1541+
MathLib.RotationDeltaAxisAngle(angles, targetRot, out _, out float deltaTargetAngle);
1542+
if (MathF.Abs(deltaTargetAngle) > 0.01f) {
1543+
float expectedDist = deltaAngle;
1544+
if (entity == pushBlock.RootParent)
1545+
expectedDist = entity.GetLocalAngularVelocity().Length() * pushBlock.MoveTime;
1546+
else {
1547+
ComputePushStartMatrix(out Matrix3x4 start, entity, pushBlock);
1548+
MathLib.MatrixAngles(start, out QAngle startAngles, out _);
1549+
MathLib.RotationDeltaAxisAngle(startAngles, entity.GetAbsAngles(), out Vector3 startAxis, out float startAngle);
1550+
expectedDist = startAngle * Vector3.Dot(startAxis, axis);
1551+
}
1552+
1553+
float t = expectedDist != 0.0f ? MathF.Abs(deltaAngle / expectedDist) : 1.0f;
1554+
t = Math.Clamp(t, 0.0f, 1.0f);
1555+
fraction = Math.Max(fraction, t);
1556+
}
1557+
else
1558+
entity.UpdatePhysicsShadowToCurrentPosition(0);
1559+
}
1560+
}
1561+
1562+
if (fraction >= pushBlock.MoveBackFraction) {
1563+
pushBlock.MoveBackFraction = fraction;
1564+
pushBlock.BlockedEntity = entity;
1565+
}
1566+
}
1567+
1568+
public void VPhysicsUpdatePusher(IPhysicsObject physics) {
1569+
float movetime = (float)(LocalTime - VPhysicsUpdateLocalTime);
1570+
if (movetime <= 0)
1571+
return;
1572+
1573+
if (!PhysIsFinalTick())
1574+
return;
1575+
1576+
if (GetSolid() != SolidType.BSP && physics.GetShadowPosition(out _, out _) != 0) {
1577+
List<BaseEntity> list = [];
1578+
Hierarchy.GetAllInHierarchy(this, list);
1579+
1580+
PhysicsPushList? pushList = null;
1581+
if (HasDataObjectType(DataObjectType.PhysicsPushList)) {
1582+
pushList = GetDataObject<PhysicsPushList>(DataObjectType.PhysicsPushList);
1583+
Assert(pushList != null);
1584+
}
1585+
bool checkrot = GetLocalAngularVelocity() != vec3_angle;
1586+
bool checkmove = GetLocalVelocity() != vec3_origin;
1587+
1588+
PushBlock pushBlock = new() {
1589+
RootParent = this,
1590+
List = pushList,
1591+
BlockedEntity = null,
1592+
MoveBackFraction = 0.0f,
1593+
MoveTime = movetime
1594+
};
1595+
for (int i = 0; i < list.Count; i++) {
1596+
if (list[i].IsSolid())
1597+
CheckPushedEntity(list[i], ref pushBlock);
1598+
}
1599+
1600+
TimeUnit_t physLocalTime = LocalTime;
1601+
if (pushBlock.BlockedEntity != null) {
1602+
float moveback = movetime * pushBlock.MoveBackFraction;
1603+
if (moveback > 0) {
1604+
physLocalTime = LocalTime - moveback;
1605+
if (physLocalTime <= (VPhysicsUpdateLocalTime + movetime * 0.99f)) {
1606+
BaseEntity? blocked = null;
1607+
1608+
if (pushList != null) {
1609+
SetLocalOrigin(pushList.LocalOrigin);
1610+
SetLocalAngles(pushList.LocalAngles);
1611+
physLocalTime = pushList.LocalMoveTime;
1612+
for (int i = 0; i < pushList.PushedCount; i++) {
1613+
BaseEntity? pushedEntity = pushList.PushedEnts[i].Get();
1614+
if (pushedEntity == null)
1615+
continue;
1616+
1617+
pushedEntity.SetAbsOrigin(pushedEntity.GetAbsOrigin() - pushList.PushVec[i]);
1618+
}
1619+
BaseEntity? physicsBlocker = FindPhysicsBlocker(VPhysicsGetObject()!, pushList, pushList.PushVec[0]);
1620+
if (physicsBlocker != null)
1621+
blocked = physicsBlocker;
1622+
}
1623+
else {
1624+
Vector3 origin = GetLocalOrigin();
1625+
QAngle angles = GetLocalAngles();
1626+
1627+
if (checkmove)
1628+
origin -= GetLocalVelocity() * moveback;
1629+
if (checkrot)
1630+
angles -= GetLocalAngularVelocity() * moveback;
1631+
1632+
SetLocalOrigin(origin);
1633+
SetLocalAngles(angles);
1634+
}
1635+
1636+
if (blocked != null)
1637+
Blocked(blocked);
1638+
1639+
LocalTime = physLocalTime;
1640+
}
1641+
}
1642+
}
1643+
}
1644+
1645+
if (HasDataObjectType(DataObjectType.PhysicsPushList))
1646+
DestroyDataObject(DataObjectType.PhysicsPushList);
1647+
1648+
VPhysicsUpdateLocalTime = LocalTime;
1649+
if (MoveDoneTime <= LocalTime && MoveDoneTime > 0) {
1650+
SetMoveDoneTime(-1);
1651+
MoveDone();
1652+
}
1653+
}
1654+
14151655
public void PhysicsRelinkChildren(TimeUnit_t dt) {
14161656
BaseEntity? child;
14171657

‎Game.Server/Physics.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ public static bool PhysIsInCallback(){
258258

259259
public static void PhysAddShadow(BaseEntity entity) => g_ShadowEntities!.AddEntity(entity);
260260
public static void PhysRemoveShadow(BaseEntity entity) => g_ShadowEntities!.DeleteEntity(entity);
261+
public static bool PhysIsFinalTick() => g_PhysicsHook.IsFinalTick;
261262
public static void PhysGetListOfPenetratingEntities(BaseEntity search, List<BaseEntity> list) => g_Collisions.GetListOfPenetratingEntities(search, list);
262263

263264
public static void PhysOnCleanupDeleteList() {
@@ -472,8 +473,18 @@ public override void LevelShutdownPostEntity() {
472473
}
473474
public override void FrameUpdatePostEntityThink() {
474475
base.FrameUpdatePostEntityThink();
475-
PhysFrame(gpGlobals.FrameTime);
476+
477+
TimeUnit_t interval = gpGlobals.FrameTime > 0.0 ? TICK_INTERVAL : 0.0;
478+
479+
if (BaseEntity.IsSimulatingOnAlternateTicks()) {
480+
IsFinalTick = false;
481+
PhysFrame(interval);
482+
}
483+
IsFinalTick = true;
484+
PhysFrame(interval);
476485
}
486+
487+
public bool IsFinalTick = true;
477488
public override void PreClientUpdate() {
478489
ImpactSoundTime += gpGlobals.FrameTime;
479490
if (ImpactSoundTime > 0.05) {

‎Game.Server/PhysicsMain.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,12 @@ void PerformPush(TimeUnit_t movetime) {
565565
}
566566

567567
if (VPhysicsGetObject() != null) {
568-
if (movetime > 0 && Blocker.Get() == null && GetSolid() == SolidType.VPhysics && Physics.PushedEntities.CountMovedEntities() > 0)
569-
throw new NotImplementedException();
568+
if (movetime > 0 && Blocker.Get() == null && GetSolid() == SolidType.VPhysics && Physics.PushedEntities.CountMovedEntities() > 0) {
569+
Assert(!HasDataObjectType(DataObjectType.PhysicsPushList));
570+
PhysicsPushList list = CreateDataObject<PhysicsPushList>(DataObjectType.PhysicsPushList);
571+
if (list != null)
572+
Physics.PushedEntities.StoreMovedEntities(list);
573+
}
570574
}
571575
else {
572576
if (MoveDoneTime <= LocalTime && MoveDoneTime > 0) {

‎Game.Server/PushEntity.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ protected struct PhysicsPushedInfo
4747

4848
public int CountMovedEntities() => Moved.Count;
4949

50+
public void StoreMovedEntities(PhysicsPushList list) {
51+
list.LocalMoveTime = RootPusherStartLocaltime;
52+
list.LocalOrigin = RootPusherStartLocalOrigin;
53+
list.LocalAngles = RootPusherStartLocalAngles;
54+
list.PushedCount = CountMovedEntities();
55+
Assert(list.PushedCount < PhysicsPushList.MAX_PUSHED_ENTITIES);
56+
if (list.PushedCount > PhysicsPushList.MAX_PUSHED_ENTITIES)
57+
list.PushedCount = PhysicsPushList.MAX_PUSHED_ENTITIES;
58+
59+
for (int i = 0; i < list.PushedCount; i++) {
60+
EHANDLE handle = new();
61+
handle.Set(Moved[i].Entity);
62+
list.PushedEnts[i] = handle;
63+
list.PushVec[i] = Moved[i].Entity.GetAbsOrigin() - Moved[i].StartAbsOrigin;
64+
}
65+
}
66+
5067
public void BeginPush(BaseEntity root) {
5168
Moved.Clear();
5269
Pusher.Clear();

‎Game.Shared/PhysicsMainShared.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ public override void Shutdown() {
828828
//PositionWatcher
829829
new EntityDataInstantiator<WatcherList>(),
830830
//PhysicsPushList
831-
null!,
831+
new EntityDataInstantiator<PhysicsPushList>(),
832832
//VPhysicsUpdateAI
833833
new EntityDataInstantiator<VPhysicsUpdateAI>(),
834834
//VPhysicsWatcher

‎Game.Shared/PhysicsPushList.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Source.Common.Mathematics;
2+
3+
using System.Numerics;
4+
5+
namespace Game.Shared;
6+
7+
public class PhysicsPushList
8+
{
9+
public const int MAX_PUSHED_ENTITIES = 32;
10+
11+
public TimeUnit_t LocalMoveTime;
12+
public Vector3 LocalOrigin;
13+
public QAngle LocalAngles;
14+
public int PushedCount;
15+
public readonly EHANDLE[] PushedEnts = new EHANDLE[MAX_PUSHED_ENTITIES];
16+
public readonly Vector3[] PushVec = new Vector3[MAX_PUSHED_ENTITIES];
17+
}

‎Source.Common/Mathematics/MathLib.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,6 +3132,25 @@ public static void VectorAngles(in Vector3 forward, in Vector3 pseudoup, out QAn
31323132
}
31333133
}
31343134

3135+
public static void QuaternionAxisAngle(in Quaternion q, out Vector3 axis, out float angle) {
3136+
angle = RAD2DEG(2 * MathF.Acos(q.W));
3137+
if (angle > 180)
3138+
angle -= 360;
3139+
3140+
axis = new(q.X, q.Y, q.Z);
3141+
VectorNormalize(ref axis);
3142+
}
3143+
3144+
public static void RotationDeltaAxisAngle(in QAngle srcAngles, in QAngle destAngles, out Vector3 deltaAxis, out float deltaAngle) {
3145+
AngleQuaternion(srcAngles, out Quaternion srcQuat);
3146+
AngleQuaternion(destAngles, out Quaternion destQuat);
3147+
QuaternionScale(srcQuat, -1, out Quaternion srcQuatInv);
3148+
QuaternionMult(destQuat, srcQuatInv, out Quaternion result);
3149+
3150+
QuaternionNormalize(ref result);
3151+
QuaternionAxisAngle(result, out deltaAxis, out deltaAngle);
3152+
}
3153+
31353154
public static void AxisAngleQuaternion(in Vector3 axis, float angle, out Quaternion q) {
31363155
SinCos(DEG2RAD(angle) * 0.5f, out float sa, out float ca);
31373156
q.X = axis.X * sa;

0 commit comments

Comments
 (0)