1 [
ComponentEditorProps(
category:
"GameScripted/Physics", description:
"Component that automatically handles switching of between static and dynamic physics of the object")]
11 class SCR_HybridPhysicsInfo
21 [
Attribute(
"50", UIWidgets.EditBox,
"How large a contact impulse must be to switch the object into dynamic physics")]
22 float m_fDynamicContactImpulse;
24 private ref SCR_HybridPhysicsInfo m_HybridPhysicsInfo =
null;
28 private void ClearPhysicsInfo()
30 if (!m_HybridPhysicsInfo)
33 delete m_HybridPhysicsInfo;
34 m_HybridPhysicsInfo =
null;
40 private void StorePhysicsInfo(IEntity owner)
42 auto physics = owner.GetPhysics();
48 m_HybridPhysicsInfo =
new SCR_HybridPhysicsInfo;
49 m_HybridPhysicsInfo.m_fMass = physics.GetMass();
51 int numGeoms = physics.GetNumGeoms();
52 for (
int i = 0; i < numGeoms; i++)
54 m_HybridPhysicsInfo.m_aLayerMasks.Insert(physics.GetGeomInteractionLayer(i));
58 physics = Physics.CreateStatic(owner, -1);
60 int numStoredGeoms = m_HybridPhysicsInfo.m_aLayerMasks.Count();
61 numGeoms = physics.GetNumGeoms();
62 for (
int i = 0; i < numStoredGeoms; i++)
67 physics.SetGeomInteractionLayer(i, m_HybridPhysicsInfo.m_aLayerMasks.Get(i));
74 private void ApplyPhysicsInfo(IEntity owner)
76 auto physics = owner.GetPhysics();
84 physics = Physics.CreateDynamic(owner, m_HybridPhysicsInfo.m_fMass, -1);
85 int numStoredGeoms = m_HybridPhysicsInfo.m_aLayerMasks.Count();
86 int numGeoms = physics.GetNumGeoms();
87 for (
int i = 0; i < numStoredGeoms; i++)
92 physics.SetGeomInteractionLayer(i, m_HybridPhysicsInfo.m_aLayerMasks.Get(i));
99 override void EOnContact(IEntity owner, IEntity other, Contact contact)
102 if (contact.Impulse < m_fDynamicContactImpulse)
105 auto physics = owner.GetPhysics();
106 if (physics.IsDynamic())
110 ClearEventMask(owner, EntityEvent.CONTACT);
113 SetEventMask(owner, EntityEvent.FRAME);
117 override void EOnFrame(IEntity owner,
float timeSlice)
120 auto physics = owner.GetPhysics();
121 if (physics.IsDynamic() && !physics.IsActive())
123 StorePhysicsInfo(owner);
124 SetEventMask(owner, EntityEvent.CONTACT);
125 ClearEventMask(owner, EntityEvent.FRAME);
129 if (m_HybridPhysicsInfo)
130 ApplyPhysicsInfo(owner);
134 override void OnPostInit(IEntity owner)
136 super.OnPostInit(owner);
137 SetEventMask(owner, EntityEvent.INIT);
138 owner.SetFlags(EntityFlags.ACTIVE);
142 override void EOnInit(IEntity owner)
144 if (m_fDynamicContactImpulse <= 0)
147 auto physics = owner.GetPhysics();
151 if (!physics.IsDynamic())
154 SetEventMask(owner, EntityEvent.CONTACT);
156 StorePhysicsInfo(owner);