@@ -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
0 commit comments