public void ChangePivot(JelloBody t) { t.polyCollider.points = JelloShapeTools.RemoveDuplicatePoints(t.polyCollider.points); t.Shape.changeVertices(t.polyCollider.points, t.Shape.InternalVertices); Vector2 diff = pivot; diff = new Vector2(diff.x / t.Scale.x, diff.y / t.Scale.y); diff = JelloVectorTools.rotateVector(diff, -t.Angle); if (t.meshLink != null) { MonoBehaviour monoBehavior; if (t.meshLink.UpdatePivotPoint(diff, out monoBehavior)) { EditorUtility.SetDirty(monoBehavior); } } for (int i = 0; i < t.Shape.VertexCount; i++) { t.Shape.setVertex(i, t.Shape.getVertex(i) - diff); } t.polyCollider.points = t.Shape.EdgeVertices; t.transform.position += (Vector3)JelloVectorTools.rotateVector(new Vector2(diff.x * t.Scale.x, diff.y * t.Scale.y), t.Angle); if (t.transform.childCount > 0) { for (int i = 0; i < t.transform.childCount; i++) { t.transform.GetChild(i).position -= (Vector3)diff; } } if (t.JointCount > 0) { for (int i = 0; i < t.JointCount; i++) { t.GetJoint(i).localAnchorA -= diff; } } if (t.AttachPointCount > 0) { for (int i = 0; i < t.AttachPointCount; i++) { t.GetAttachPoint(i).point -= diff; } } t.updateGlobalShape(true); EditorUtility.SetDirty(t); pivot = Vector2.zero; }
public void CenterPivot(JelloBody t) { Vector2 center = new Vector2(); t.polyCollider.points = JelloShapeTools.RemoveDuplicatePoints(t.polyCollider.points); t.Shape.changeVertices(t.polyCollider.points, t.Shape.InternalVertices); center = JelloShapeTools.FindCenter(t.Shape.EdgeVertices); //using vertices instead of collider.points because need of assigning entire array at once if (t.meshLink != null) { MonoBehaviour monoBehavior; if (t.meshLink.UpdatePivotPoint(center, out monoBehavior)) { EditorUtility.SetDirty(monoBehavior); } } for (int i = 0; i < t.Shape.VertexCount; i++) { t.Shape.setVertex(i, t.Shape.getVertex(i) - center); } t.polyCollider.points = t.Shape.EdgeVertices; t.transform.position += (Vector3)JelloVectorTools.rotateVector(new Vector2(center.x * t.Scale.x, center.y * t.Scale.y), t.Angle); if (t.transform.childCount > 0) { for (int i = 0; i < t.transform.childCount; i++) { t.transform.GetChild(i).position -= (Vector3)center; } } if (t.JointCount > 0) { for (int i = 0; i < t.JointCount; i++) { t.GetJoint(i).localAnchorA -= center; } } if (t.AttachPointCount > 0) { for (int i = 0; i < t.AttachPointCount; i++) { t.GetAttachPoint(i).point -= center; } } t.updateGlobalShape(true); EditorUtility.SetDirty(t); pivot = Vector2.zero; }
/// <summary> /// Finish adding vertices to this JeloClosedShape, and choose whether to convert into local space. /// JelloClosedShape.winding and JelloClosedShape.Triangles will be set. /// Make sure there are no duplicate points before calling this. use JelloShapeTools.RemoveDuplicatePoints(). /// </summary> /// <param name="recenter">whether to convert the positions of the JelloClosedShape into local space.</param> /// /// <dl class="example"><dt>Example</dt></dl> /// ~~~{.c} /// //Create a closed shape square /// JelloClosedShape shape = new JelloClosedShape(); /// /// shape.begin(); /// /// shape.addPoint(new Vector2(-1,1)); //top left /// shape.addPoint(new Vector2(-1,1)); //top right /// shape.addPoint(new Vector2(-1,1)); //bottom right /// shape.addPoint(new Vector2(-1,1)); //bottom left /// /// shape.finish(); /// ~~~ public void finish(bool recenter = true) { if (mInternalVertices != null && mInternalVertices.Length > 0) { //dont allow duplicate points mInternalVertices = JelloShapeTools.RemoveDuplicatePoints(mInternalVertices); //dont allow points outside of the perimiter for (int i = 0; i < mInternalVertices.Length; i++) { if (!JelloShapeTools.Contains(mEdgeVertices, mInternalVertices[i])) { mInternalVertices[i] = Vector2.one * Mathf.Infinity; } } //dont allow points on the perimiter. (this will also remove any null points) mInternalVertices = JelloShapeTools.RemovePointsOnPerimeter(mEdgeVertices, mInternalVertices); } mCenter = JelloShapeTools.FindCenter(mEdgeVertices); if (recenter) { // now subtract this from each element, to get proper "local" coordinates. for (int i = 0; i < mEdgeVertices.Length; i++) { mEdgeVertices[i] -= mCenter; } if (mInternalVertices != null) { for (int i = 0; i < mInternalVertices.Length; i++) { mInternalVertices[i] -= mCenter; } } } if (JelloShapeTools.HasClockwiseWinding(mEdgeVertices)) { winding = Winding.Clockwise; } else { winding = Winding.CounterClockwise; } Triangulate(); }
//todo add onenable and ondisable events to body. public virtual void DrawEditorGUI() { //check polycollider vs pointmasscount if(!Application.isPlaying) //TODO have this be handled by a class that extends the polycollider and recognises changes? { for(int b = 0; b < targets.Length; b++) { JelloBody body = (JelloBody)targets[b]; body.setComponentReferences(); body.polyCollider.points = JelloShapeTools.RemoveDuplicatePoints(body.polyCollider.points); JelloClosedShape shape = new JelloClosedShape(body.polyCollider.points, null, false); if(body.Shape != null) { for(int i = 0; i < body.Shape.InternalVertexCount; i++) shape.addInternalVertex(body.Shape.InternalVertices[i]); shape.finish(false); } if(shape.EdgeVertexCount != body.Shape.EdgeVertexCount || shape.InternalVertexCount != body.Shape.InternalVertexCount) body.smartSetShape(shape, JelloBody.ShapeSettingOptions.MovePointMasses, smartShapeSettingOptions); else body.setShape(shape, JelloBody.ShapeSettingOptions.MovePointMasses); //will i need to do this for constraints as well? for(int i = 0; i < body.AttachPointCount; i++) { body.GetAttachPoint(i).UpdateEditorMode(); } } } serializedObject.Update(); EditorGUI.showMixedValue = eMass.hasMultipleDifferentValues; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(eMass, massContent); if(EditorGUI.EndChangeCheck()) { for(int i = 0; i < serializedObject.targetObjects.Length; i++) { JelloBody b = (JelloBody)serializedObject.targetObjects[i]; b.Mass = eMass.floatValue; } serializedObject.UpdateIfDirtyOrScript(); } EditorGUI.showMixedValue = false; if(!tar.IsStatic) { EditorGUILayout.BeginHorizontal(); EditorGUI.showMixedValue = eAffectedByGravity.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eAffectedByGravity, useGravContent); EditorGUI.showMixedValue = false; EditorGUILayout.EndHorizontal(); if(eAffectedByGravity.boolValue) { EditorGUI.indentLevel++; EditorGUI.showMixedValue = eOverrideGravity.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eOverrideGravity, overrideGravContent); EditorGUI.showMixedValue = false; if(eOverrideGravity.boolValue) { EditorGUI.indentLevel++; EditorGUI.showMixedValue = eGravity.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eGravity, customGravContent); EditorGUI.showMixedValue = false; EditorGUI.indentLevel--; } EditorGUI.indentLevel--; } EditorGUI.showMixedValue = eKinematic.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eKinematic, kinematicContent); EditorGUI.showMixedValue = false; } EditorGUI.showMixedValue = eTrigger.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eTrigger, triggerContent); EditorGUI.showMixedValue = false; EditorGUI.showMixedValue = eAwake.hasMultipleDifferentValues; EditorGUILayout.PropertyField(eAwake, awakeContent); EditorGUI.showMixedValue = false; if(tar.meshLink == null || tar.meshLink.canModifyPivotPoint) { EditorGUI.indentLevel++; EditorGUILayout.BeginHorizontal(); SerializedProperty ePivotOffset = serializedObject.FindProperty("pivotOffset"); GUIStyle pivotStyle = new GUIStyle(EditorStyles.foldout); if(ePivotOffset.prefabOverride) pivotStyle.fontStyle = FontStyle.Bold; showPivot = EditorGUILayout.Foldout(showPivot, pivotFoldoutContent, pivotStyle); if(GUILayout.Button(centerPivotContent, EditorStyles.miniButton)) { Undo.RecordObjects(serializedObject.targetObjects, "Center Pivot"); for(int i = 0; i < serializedObject.targetObjects.Length; i++) { JelloBody jb = (JelloBody)serializedObject.targetObjects[i]; Undo.RecordObject(jb.gameObject.transform, "Center Pivot transform"); if(jb.meshLink != null) { Undo.RecordObject(jb.meshLink, "Center Pivot mesh"); Undo.RecordObject(jb.polyCollider, "Center Pivot collider"); Undo.RecordObject(jb.GetComponent<Renderer>(), "Center Pivot renderer"); Undo.RecordObject(jb.meshLink.LinkedMeshFilter, "Center Pivot filter"); } } for(int i = 0; i < serializedObject.targetObjects.Length; i++) { JelloBody bod = (JelloBody)serializedObject.targetObjects[i]; CenterPivot(bod); EditorUtility.SetDirty(bod.gameObject); EditorUtility.SetDirty(bod.meshLink); //serializedObject.UpdateIfDirtyOrScript(); } SceneView.RepaintAll(); } EditorGUILayout.EndHorizontal(); if(showPivot) { if(!serializedObject.isEditingMultipleObjects) { pivot = EditorGUILayout.Vector2Field("Position", pivot); if(pivot != Vector2.zero) { EditorGUILayout.BeginHorizontal(); if(GUILayout.Button(applyPivotContent, EditorStyles.miniButton)) { JelloBody jb = (JelloBody)serializedObject.targetObject; Undo.RecordObject(jb, "Change Pivot"); Undo.RecordObject(jb.gameObject.transform, "Change Pivot transform"); if(jb.meshLink != null) { Undo.RecordObject(jb.meshLink, "Change Pivot mesh"); Undo.RecordObject(jb.polyCollider, "Change Pivot collider"); Undo.RecordObject(jb.GetComponent<Renderer>(), "Change Pivot renderer"); Undo.RecordObject(jb.meshLink.LinkedMeshFilter, "Change Pivot filter"); } ChangePivot(tar); EditorUtility.SetDirty(tar.gameObject); EditorUtility.SetDirty(tar.meshLink); // serializedObject.UpdateIfDirtyOrScript(); } if(GUILayout.Button(cancelPivotContent, EditorStyles.miniButton)) pivot = Vector2.zero; SceneView.RepaintAll(); EditorGUILayout.EndHorizontal(); } } else { EditorGUILayout.HelpBox("Pivot Points may only be centered when multiple Game Objects are selected", MessageType.Info); } } EditorGUI.indentLevel--; } serializedObject.ApplyModifiedProperties(); }