1 [
EntityEditorProps(
category:
"GameBase/Joints", description:
"Physics joint base", sizeMin:
"-0.05 -0.05 -0.05", sizeMax:
"0.05 0.05 0.05", color:
"0 0 255 255")]
13 [
Attribute(
"-1", UIWidgets.ComboBox,
"Snap joint to bone in parent (one-time)",
"")]
14 int SNAP_TO_PARENT_BONE;
17 [
Attribute(
"1", UIWidgets.CheckBox,
"Whether to create collision blocker between parent and child" )]
18 bool m_CollisionBlocker;
20 IEntity m_JointParent =
null;
21 IEntity m_JointChild =
null;
24 PhysicsJoint m_Joint =
null;
31 void MoveJointToParentBone()
36 if (SNAP_TO_PARENT_BONE <= 0)
41 m_JointParent.GetAnimation().GetBoneMatrix(SNAP_TO_PARENT_BONE, boneMat);
43 vector angles = Math3D.MatrixToAngles(boneMat);
44 vector pos = boneMat[3];
48 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
49 worldEditorAPI.BeginEntityAction(
"Move to parent bone",
"");
50 worldEditorAPI.ClearEntitySelection();
52 IEntitySource src = _WB_GetEditorAPI().EntityToSource(
this);
53 worldEditorAPI.SetVariableValue(src,
null,
"coords", pos.ToString(
false));
54 worldEditorAPI.SetVariableValue(src,
null,
"angleX", angles[1].ToString());
55 worldEditorAPI.SetVariableValue(src,
null,
"angleY", angles[0].ToString());
56 worldEditorAPI.SetVariableValue(src,
null,
"angleZ", angles[2].ToString());
57 worldEditorAPI.SetVariableValue(src,
null,
"SNAP_TO_PARENT_BONE",
"-1");
58 worldEditorAPI.EndEntityAction(
"Move to parent bone");
114 override array<ref ParamEnum> _WB_GetUserEnums(
string varName, IEntitySource src)
116 if (varName ==
"SNAP_TO_PARENT_BONE" && m_JointParent)
117 return SCR_Global.GetBonesAsParamEnums(m_JointParent);
129 override void _WB_AfterWorldUpdate(
float timeSlice)
132 Shape.CreateArrow(m_JointParent.GetOrigin(),
GetOrigin(), 0.05, ARGB(255, 128, 128, 0), ShapeFlags.ONCE|ShapeFlags.NOZBUFFER);
134 Shape.CreateArrow(
GetOrigin(), m_JointChild.GetOrigin(), 0.05, ARGB(255, 128, 128, 0), ShapeFlags.ONCE|ShapeFlags.NOZBUFFER);
145 if (m_JointParent || m_JointChild)
156 void TryCreateJoint(
bool showErrors)
160 if (!m_JointParent && !m_JointChild)
164 Print(
"JointHingeEntity::CreateJoint: ERROR - cannot create joint, no parent and no child!");
165 CreateFailedJointDebug(128, 255, 0, 0);
170 Physics parentPhysics =
null;
171 Physics childPhysics =
null;
173 parentPhysics = m_JointParent.GetPhysics();
175 childPhysics = m_JointChild.GetPhysics();
176 if (!parentPhysics && !childPhysics)
180 Print(
"JointHingeEntity::CreateJoint: ERROR - cannot create joint, neither parent nor child have physics!");
181 CreateFailedJointDebug(128, 255, 0, 0);
186 if (parentPhysics && childPhysics && !parentPhysics.IsDynamic() && !childPhysics.IsDynamic())
190 Print(
"JointHingeEntity::CreateJoint: ERROR - cannot create joint, neither parent nor child are dynamic!");
191 CreateFailedJointDebug(128, 255, 0, 0);
196 IEntity parent = m_JointParent;
197 IEntity child = m_JointChild;
200 if (parent && parentPhysics && !parentPhysics.IsDynamic())
202 if (child && childPhysics && !childPhysics.IsDynamic())
205 if (showErrors && !parent && !child)
207 Print(
"JointHingeEntity::CreateJoint: ERROR - cannot create joint, no connected dynamic entities!");
208 CreateFailedJointDebug(128, 255, 128, 0);
211 if (!parent || !child)
215 vector jointDummyMat[4];
216 GetTransform(jointDummyMat);
217 m_JointDummy.SetDummyTransform(jointDummyMat);
221 mass = parentPhysics.GetMass();
223 mass = childPhysics.GetMass();
225 m_JointDummy.GetPhysics().SetMass(mass * 1000);
230 child = m_JointDummy;
232 parent = m_JointDummy;
236 if (!parentPhysics || !parentPhysics.IsDynamic())
238 IEntity tmp = parent;
244 vector parentMat[4], childMat[4], jointMat[4];
245 parent.GetTransform(parentMat);
246 child.GetTransform(childMat);
247 GetTransform(jointMat);
250 RotateJoint(jointMat);
253 vector jointMatParentLocal[4], jointMatChildLocal[4];
254 Math3D.MatrixInvMultiply4(parentMat, jointMat, jointMatParentLocal);
255 Math3D.MatrixInvMultiply4(childMat, jointMat, jointMatChildLocal);
259 jointMatParentLocal[3] = jointMatParentLocal[3] - parentPhysics.GetCenterOfMass();
261 jointMatParentLocal[3] = jointMatParentLocal[3];
263 jointMatChildLocal[3] = jointMatChildLocal[3] - childPhysics.GetCenterOfMass();
265 jointMatChildLocal[3] = jointMatChildLocal[3];
268 CreateJoint(parent, child, jointMatParentLocal, jointMatChildLocal);
272 void RotateJoint(inout vector jointMat[4])
277 void CreateJoint(IEntity parent, IEntity child, vector jointMat1[4], vector jointMat2[4])
282 void CreateFailedJointDebug(
int a,
int r,
int g,
int b)
284 int color = ARGB(a, r, g, b);
286 Shape.Create(ShapeType.BBOX, color, ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER|ShapeFlags.TRANSP,
"-0.1 0.2 -0.1" + myPos,
"0.1 1.2 0.1" + myPos);
287 Shape.Create(ShapeType.BBOX, color, ShapeFlags.NOOUTLINE|ShapeFlags.NOZBUFFER|ShapeFlags.TRANSP,
"-0.1 -0.1 -0.1" + myPos,
"0.1 0.1 0.1" + myPos);
296 override event void EOnInit(IEntity owner)
302 MoveJointToParentBone();
309 m_JointParent = parent;
311 SetEventMask(EntityEvent.INIT);