Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带立方体的凸壳子弹物理_C#_Bulletphysics - Fatal编程技术网

C# 带立方体的凸壳子弹物理

C# 带立方体的凸壳子弹物理,c#,bulletphysics,C#,Bulletphysics,我正在用c#开发一个游戏引擎,并使用BulletSharp进行物理测试。除了立方体外,它运行良好: (轴对齐边界框为透明红色,模型为白色) 休息时,站在他们的边缘。因为我是从Collada模型加载的,所以我创建了一个ConvexHullShape(),并将数据添加为矢量云。虽然使用BoxShape()会更有效(并且工作正常),但我不能这样做,因为不能保证所有模型都是立方体。我无法理解为什么它们位于顶点而不是平面边上。我的ConvexHullShape实现是否错误,或者我是否需要使用不同类型的形

我正在用c#开发一个游戏引擎,并使用BulletSharp进行物理测试。除了立方体外,它运行良好:

(轴对齐边界框为透明红色,模型为白色)

休息时,站在他们的边缘。因为我是从Collada模型加载的,所以我创建了一个
ConvexHullShape()
,并将数据添加为矢量云。虽然使用
BoxShape()
会更有效(并且工作正常),但我不能这样做,因为不能保证所有模型都是立方体。我无法理解为什么它们位于顶点而不是平面边上。我的
ConvexHullShape
实现是否错误,或者我是否需要使用不同类型的形状(物理才能正常工作)

公共刚体添加动态几何(拼贴几何、Matrix4变换)
{
列表点=新列表();
foreach(几何中的三角形三角形。三角形)
{
添加点(三个顶点[0]);
添加点(三个顶点[1]);
添加点(三个顶点[2]);
}
碰撞形状=新的凸面形状(点);
shape.UserObject=几何体;
添加(形状);
刚体体=创建刚体(geometry.triangles.Count*10,变换,形状);
返回体;
}
公共刚体创建刚体(浮动质量、矩阵4开始变换、碰撞形状)
{
布尔isDynamic=(质量!=0.0f);
Vector3局部神经=Vector3.0;
if(isDynamic)
形状。计算校准(质量、外校准);
DefaultMotionState myMotionState=新的DefaultMotionState(startTransform);
RigidBodyConstructionInfo rbInfo=新的RigidBodyConstructionInfo(质量、运动状态、形状、局部能量);
刚体=新刚体(rbInfo);
物理世界.附加刚体(体);
返回体;
}

ConvexHullShape希望重心(COM)为(0,0,0),但立方体偏离中心,使其向拐角倾斜

您可以使用ConvexTriangleMeshShape.CalculatePrincipalAsistTransform找到正确的COM。然后可以从每个顶点减去COM,使COM返回到0。但是,创建具有立方体局部中心的复合形状更容易

// Create a ConvexTriangleMeshShape from the points
const int indexStride = 3 * sizeof(int);
const int vertexStride = 12;
int vertexCount = points.Count;
int indexCount = vertexCount / 3;

TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray();
IndexedMesh mesh = new IndexedMesh();
mesh.Allocate(vertexCount, vertexStride, indexCount, indexStride);
Vector3Array vdata = mesh.Vertices;
IntArray idata = mesh.TriangleIndices;
for (int i = 0; i < vertexCount; i++)
{
    vdata[i] = points[i];
    idata[i] = i;
}
vertexArray.AddIndexedMesh(mesh);
ConvexTriangleMeshShape shape = new ConvexTriangleMeshShape(vertexArray, true);

// Calculate center of mass
Matrix center = Matrix.Identity;
Vector3 inertia;
float volume;
shape.CalculatePrincipalAxisTransform(ref center, out inertia, out volume);

// Create a CompoundShape with COM offset
CompoundShape compound = new CompoundShape();
compound.AddChildShape(Matrix.Invert(center), shape);
//从点创建凸面三角形网格形状
常数int indexStride=3*sizeof(int);
const int vertexStride=12;
int vertexCount=点数。计数;
int indexCount=vertexCount/3;
三角形索引vertexArray vertexArray=新三角形索引vertexArray();
IndexedMesh网格=新IndexedMesh();
网格分配(顶点计数、顶点步长、索引计数、索引三角形);
Vector3Array vdata=网格顶点;
IntArray idata=网格三角形边界;
对于(int i=0;i
注:ConvexTriangleMeshShape.CalculatePrincipalAsistTransform适用于SVN主干,但不适用于BulletSharp 2.82。很快就会有一个错误修复版本发布

// Create a ConvexTriangleMeshShape from the points
const int indexStride = 3 * sizeof(int);
const int vertexStride = 12;
int vertexCount = points.Count;
int indexCount = vertexCount / 3;

TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray();
IndexedMesh mesh = new IndexedMesh();
mesh.Allocate(vertexCount, vertexStride, indexCount, indexStride);
Vector3Array vdata = mesh.Vertices;
IntArray idata = mesh.TriangleIndices;
for (int i = 0; i < vertexCount; i++)
{
    vdata[i] = points[i];
    idata[i] = i;
}
vertexArray.AddIndexedMesh(mesh);
ConvexTriangleMeshShape shape = new ConvexTriangleMeshShape(vertexArray, true);

// Calculate center of mass
Matrix center = Matrix.Identity;
Vector3 inertia;
float volume;
shape.CalculatePrincipalAxisTransform(ref center, out inertia, out volume);

// Create a CompoundShape with COM offset
CompoundShape compound = new CompoundShape();
compound.AddChildShape(Matrix.Invert(center), shape);