Skip to content

Commit

Permalink
39
Browse files Browse the repository at this point in the history
  • Loading branch information
imengyu committed Nov 18, 2021
1 parent 2ad3991 commit 139d487
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 92 deletions.
Binary file modified Assets/Plugins/BallancePhysics/x64/BallancePhysicsEditor.dll
Binary file not shown.
6 changes: 3 additions & 3 deletions Assets/Scenes/EmptyScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &820011693
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -579,7 +579,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &1084468640
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -969,7 +969,7 @@ MonoBehaviour:
Gravity: {x: 0, y: -20, z: 0}
SimulationRate: 66
DeleteAllSurfacesWhenDestroy: 1
Simulate: 0
Simulate: 1
--- !u!1 &1859497821
GameObject:
m_ObjectHideFlags: 0
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/BallancePhysics/Api/ApiDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace BallancePhysics.Api
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void PhantomEventCallback(int enter, IntPtr self, IntPtr other);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CollisionEventCallback(IntPtr self, IntPtr other, IntPtr contact_point_ws, IntPtr speed, IntPtr surf_normal);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FrictionEventCallback(int create, IntPtr self, IntPtr other, IntPtr friction_handle, IntPtr contact_point_ws, IntPtr speed, IntPtr surf_normal);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
Expand Down Expand Up @@ -155,4 +158,12 @@ namespace BallancePhysics.Api
public delegate int fn_surface_exist_by_name(IntPtr name);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void fn_motion_controller_set_target_pos(IntPtr controller, IntPtr pos_ws);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate float fn_get_quat_z(IntPtr qt);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate float fn_get_quat_x(IntPtr qt);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate float fn_get_quat_y(IntPtr qt);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate float fn_get_quat_w(IntPtr qt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace BallancePhysics.Editor
[InitializeOnLoad]
public class PhysicsSystemInitEditor
{
static bool firstPlay = false;

static PhysicsSystemInitEditor()
{
EditorApplication.pauseStateChanged += PauseStateChanged;
Expand All @@ -18,9 +16,6 @@ static PhysicsSystemInitEditor()

static void PauseStateChanged(PauseState state)
{
if (firstPlay)
return;
firstPlay = true;
PhysicsSystemInit.DoInit();
}
static void Quitting()
Expand Down
101 changes: 67 additions & 34 deletions Assets/Scripts/BallancePhysics/PhysicsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,16 @@ public static class PhysicsApi
public const int sWarning = 1;
public const int sInfo = 2;

public delegate int ErrorReportCallback(int level, IntPtr msg);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int ErrorReportCallback(int level, int len, IntPtr msg);

public delegate void InitFinishCallback();

[DllImport(DLL_NNAME, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ballance_physics_entry(int init, IntPtr options);

public static InitFinishCallback InitFinish;

private static int ErrorReport(int level, IntPtr _msg)
{
string msg = Marshal.PtrToStringAnsi(_msg);
if (level == sInfo)
Debug.Log(msg);
else if (level == sWarning)
Debug.LogWarning(msg);
else if (level == sError)
Debug.LogError(msg);
else
Debug.Log(msg);
return 1;
}

public const int sTrue = 1;
public const int sFalse = 0;

Expand All @@ -61,28 +49,73 @@ public static int boolToSbool(bool b) {

public static void PhysicsApiInit()
{
ErrorReportCallback callback = ErrorReport;

//拷贝初始配置结构
sInitStruct initStruct = new sInitStruct();
initStruct.smallPoolSize = PhysicsOptions.Instance.SmallPoolSize;
initStruct.showConsole = boolToSbool(PhysicsOptions.Instance.ShowConsole);
initStruct.eventCallback = Marshal.GetFunctionPointerForDelegate(callback);

IntPtr initStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf<sInitStruct>());
Marshal.StructureToPtr(initStruct, initStructPtr, false);

//调用初始化
IntPtr apiStructPtr = ballance_physics_entry(sTrue, initStructPtr);

//获取所有函数指针
API.initAll(apiStructPtr, 256);

//释放
Marshal.FreeHGlobal(initStructPtr);
Debug.Log("PhysicsApiInit");

IntPtr apiStructPtr = IntPtr.Zero;

//检查是否已经初始化
if(ballance_physics_entry(4, IntPtr.Zero).ToInt64() == 1) {

//BUG. 不这样放,mono 生成的指针似乎不正确,c++那边回调会出现问题
ErrorReportCallback callback = (int level, int len, IntPtr _msg) =>
{
string msg = Marshal.PtrToStringAnsi(_msg, len);
if (level == sInfo)
Debug.Log(msg);
else if (level == sWarning)
Debug.LogWarning(msg);
else if (level == sError)
Debug.LogError(msg);
else
Debug.Log(msg);
return 1;
};

//已经初始化过,则只需要更新回调函数
apiStructPtr = ballance_physics_entry(5, Marshal.GetFunctionPointerForDelegate(callback));

//获取所有函数指针
API.initAll(apiStructPtr, 256);
} else {

//BUG. 不这样放,mono 生成的指针似乎不正确,c++那边回调会出现问题
ErrorReportCallback callback = (int level, int len, IntPtr _msg) =>
{
string msg = Marshal.PtrToStringAnsi(_msg, len);
if (level == sInfo)
Debug.Log(msg);
else if (level == sWarning)
Debug.LogWarning(msg);
else if (level == sError)
Debug.LogError(msg);
else
Debug.Log(msg);
return 1;
};

//拷贝初始配置结构
sInitStruct initStruct = new sInitStruct();
initStruct.smallPoolSize = PhysicsOptions.Instance.SmallPoolSize;
initStruct.showConsole = boolToSbool(PhysicsOptions.Instance.ShowConsole);
initStruct.eventCallback = Marshal.GetFunctionPointerForDelegate(callback);

IntPtr initStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf<sInitStruct>());
Marshal.StructureToPtr(initStruct, initStructPtr, false);

//调用初始化
apiStructPtr = ballance_physics_entry(sTrue, initStructPtr);

//获取所有函数指针
API.initAll(apiStructPtr, 256);

//释放
Marshal.FreeHGlobal(initStructPtr);
}
}
public static void PhysicsApiDestroy()
{
Debug.Log("PhysicsApiDestroy");

ballance_physics_entry(sFalse, IntPtr.Zero);
}

Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/BallancePhysics/PhysicsApiSideInit.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
* ✅ LUA 安全性
* ✅ LUA 按包鉴别
* ✅ 模块包安全系研究
* ✅ 修复物理坐标问题
* ✅ 修复物理约束碰撞问题
* ✅ 物理弹簧
* ✅ 物理滑动约束
*【弃用】修复物理坐标问题
*【弃用】修复物理约束碰撞问题
*【弃用】物理弹簧
*【弃用】物理滑动约束
* ✅ LevelEnd
* ✅ LevelBuilder
* ✅ 机关逻辑
Expand All @@ -98,6 +98,10 @@
* ✅ 背景音乐相关逻辑
* ✅ 分数相关逻辑
* ✅ 自动归组
* ✅ 【new】ivp 物理引擎的C#包装与编译
* ✅ 【new】ivp 物理引擎初步调试成功
* 🅾 将基础球的物理从hovok迁移至ivp物理引擎
* 🅾 将机关的物理从hovok迁移至ivp物理引擎
* ✅ 复杂机关 01
* 🅾 复杂机关 03
* 🅾 复杂机关 08
Expand Down
Loading

0 comments on commit 139d487

Please sign in to comment.