예제 #1
0
 /// <summary>
 /// 动态添加鼠标的MehcanicalObject组件至root节点,生成其信息。添加StiffSpringForceField组件,连接目标物体和鼠标MechanicalObject
 /// </summary>
 private unsafe void MouseDragAdd()
 {
     if (!SofaPhysics.unityMechanicalObjectInfoDic.ContainsKey(mechanicalObjectName) && !SofaPhysics.sofaMechanicalObjectInfoDic.ContainsKey(mechanicalObjectName))
     {
         //加入mechanicalObject组件及相关info
         double[] dou = new double[3] {
             draggingMousePosition.x, draggingMousePosition.y, draggingMousePosition.z
         };
         SofaPhysicsAPI.AddMechanicalObjectArray(SofaPhysics.ToChar("root"), SofaPhysics.ToChar(mechanicalObjectName), dou, 1);
         for (int i = 0; i < SofaPhysics.nodes.Length; i++)
         {
             if (SofaPhysics.nodes[i].gameObject.name == "root")
             {
                 UnityMechanicalObjectInfo unityMechanicalObjectInfo = new UnityMechanicalObjectInfo(SofaPhysics.nodes[i], mechanicalObjectName);
                 SofaPhysics.unityMechanicalObjectInfoDic.Add(unityMechanicalObjectInfo.componentName, unityMechanicalObjectInfo);
             }
         }
         foreach (string componentName in SofaPhysics.unityMechanicalObjectInfoDic.Keys)
         {
             if (mechanicalObjectName == componentName)
             {
                 SofaMechanicalObjectInfo sofaMechanicalObjectInfo = new SofaMechanicalObjectInfo(componentName);
                 SofaPhysics.sofaMechanicalObjectInfoDic.Add(sofaMechanicalObjectInfo.componentName, sofaMechanicalObjectInfo);
             }
         }
         //加入stiffSpringForceField组件及其Spring
         SofaPhysicsAPI.AddStiffSpringForceField(SofaPhysics.ToChar("root"), SofaPhysics.ToChar(stiffSpringForceFieldName), SofaPhysics.ToChar("@./"), SofaPhysics.ToChar(GetSrcPathToRoot()), dragSpringKs, 0.1, 1);
         SofaPhysicsAPI.SpringAddToStiffSpringForceField(SofaPhysics.ToChar(stiffSpringForceFieldName), 0, draggingMechanicalObjectPointIndex, dragSpringKs, dragSpringKd, 0);
     }
 }