mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 10:46:17 +00:00
提交代码
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using BepuPhysics;
|
||||
using BepuPhysics.Collidables;
|
||||
|
||||
namespace Plugins.JNGame.BepuPhysics.Components
|
||||
{
|
||||
public abstract class BPhysicsBase : UnityEngine.MonoBehaviour
|
||||
{
|
||||
|
||||
//世界索引
|
||||
public int worldIndex = 0;
|
||||
|
||||
protected BodyHandle BodyId;
|
||||
protected StaticHandle StaticBodyId;
|
||||
|
||||
//是否静态
|
||||
public Boolean isStatic = false;
|
||||
|
||||
public JNBepuPhysics Physics
|
||||
{
|
||||
get
|
||||
{
|
||||
JNBepuPhysics.Physics.TryGetValue(worldIndex, out var value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
protected void AddDynamic<TShape>(TShape shape,BodyInertia shapeInertia) where TShape : unmanaged, IShape
|
||||
{
|
||||
|
||||
var sphereIndex = this.Physics.Simulation.Shapes.Add(shape);
|
||||
var transformPosition = transform.position;
|
||||
var transformRotation = transform.rotation;
|
||||
var spherePose = new RigidPose(new Vector3(transformPosition.x,transformPosition.y,transformPosition.z), new Quaternion(transformRotation.x,transformRotation.y,transformRotation.z,transformRotation.w));
|
||||
BodyId = this.Physics.Simulation.Bodies.Add(BodyDescription.CreateDynamic(spherePose,
|
||||
shapeInertia,
|
||||
new CollidableDescription(sphereIndex, 0.01f),
|
||||
new BodyActivityDescription(0.01f)));
|
||||
|
||||
}
|
||||
|
||||
protected void AddStatic<TShape>(TShape shape) where TShape : unmanaged, IShape
|
||||
{
|
||||
|
||||
var transformPosition = transform.position;
|
||||
var transformRotation = transform.rotation;
|
||||
StaticBodyId = this.Physics.Simulation.Statics.Add(
|
||||
new StaticDescription(new Vector3(transformPosition.x, transformPosition.y, transformPosition.z),
|
||||
new Quaternion(transformRotation.x,transformRotation.y,transformRotation.z,transformRotation.w),
|
||||
new CollidableDescription(this.Physics.Simulation.Shapes.Add(shape), 0.01f)));
|
||||
|
||||
}
|
||||
|
||||
public void OnPhysicsUpdate(float dt)
|
||||
{
|
||||
|
||||
if (this.Physics == null) return;
|
||||
if (this.isStatic) return;
|
||||
|
||||
this.Physics.Simulation.Bodies.GetDescription(this.BodyId,out var info);
|
||||
|
||||
//同步位置
|
||||
var transform1 = transform;
|
||||
transform1.position =
|
||||
new UnityEngine.Vector3(info.Pose.Position.X, info.Pose.Position.Y, info.Pose.Position.Z);
|
||||
//同步旋转
|
||||
transform1.rotation =
|
||||
new UnityEngine.Quaternion(info.Pose.Orientation.X, info.Pose.Orientation.Y, info.Pose.Position.Z,info.Pose.Orientation.W);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e204f22f96e497ab13682ad13267883
|
||||
timeCreated: 1706687376
|
@@ -0,0 +1,37 @@
|
||||
using BepuPhysics;
|
||||
using BepuPhysics.Collidables;
|
||||
using UnityEngine;
|
||||
using NotImplementedException = System.NotImplementedException;
|
||||
using Quaternion = System.Numerics.Quaternion;
|
||||
using Vector3 = System.Numerics.Vector3;
|
||||
|
||||
namespace Plugins.JNGame.BepuPhysics.Components
|
||||
{
|
||||
public class BPhysicsBox : BPhysicsBase
|
||||
{
|
||||
|
||||
private BodyHandle bodyId;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
if (this.Physics == null) return;
|
||||
Debug.Log("BPhysicsSphere");
|
||||
|
||||
this.Physics.Shapes.Add(this);
|
||||
var localScale = transform.localScale;
|
||||
var sphereBox = new Box(localScale.x,localScale.y,localScale.z);
|
||||
if (this.isStatic)
|
||||
{
|
||||
this.AddStatic(sphereBox);
|
||||
}
|
||||
else
|
||||
{
|
||||
sphereBox.ComputeInertia(0.5f, out var inertia);
|
||||
this.AddDynamic(sphereBox,inertia);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f56304910dd4846981c5f978f90ebf6
|
||||
timeCreated: 1706690344
|
@@ -0,0 +1,24 @@
|
||||
using BepuPhysics.Collidables;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Plugins.JNGame.BepuPhysics.Components
|
||||
{
|
||||
public class BPhysicsSphere : BPhysicsBase
|
||||
{
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
if (this.Physics == null) return;
|
||||
Debug.Log("BPhysicsSphere");
|
||||
|
||||
this.Physics.Shapes.Add(this);
|
||||
var sphereShape = new Sphere(transform.localScale.x / 2);
|
||||
sphereShape.ComputeInertia(0.5f, out var sphereInertia);
|
||||
this.AddDynamic(sphereShape,sphereInertia);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98c11bfd0d424f98a6634403fca61c34
|
||||
timeCreated: 1706686910
|
@@ -0,0 +1,8 @@
|
||||
namespace Plugins.JNGame.BepuPhysics.Components
|
||||
{
|
||||
public enum BShape
|
||||
{
|
||||
/// <summary> Sphere shape type </summary>
|
||||
Sphere = 1,
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 499b044e9991435ab5054521c2833c00
|
||||
timeCreated: 1706686981
|
Reference in New Issue
Block a user