AndresTraks / BulletSharp

.NET wrapper for the Bullet physics library
http://andrestraks.github.io/BulletSharp
MIT License
273 stars 59 forks source link

btTransform wrapper #59

Closed Zelyutin closed 5 years ago

Zelyutin commented 5 years ago

Hi!

I'm trying to port some C++ code using BulletPhysics to C# using BulletSharp.

colMapObject = new btCompoundShape();
colMapObject->addChildShape(btTransform(btQuaternion(0, 0, 0, 1), btVector3(x, y, z), sphere);

For btCompoundShape I use CompoundShape class, similarly for Quaterion and Vector3. But what's need to be used for btTransform?

AndresTraks commented 5 years ago

It depends on the version of BulletSharp, but it's usually a class called Matrix.

Your IDE should suggest the right type when you type in:

colMapObject = new CompoundShape()
colMapObject.AddChildShape(
Zelyutin commented 5 years ago

Yeah, I know that the Matrix should be passed as an argument. I use bullet sharp 3 and VS2017. But I didn't found a method in Matrix that will create it from Quaternion and Vector3 as in original btTransform. That's why I asked it.

AndresTraks commented 5 years ago

I mean it depends on whether it's the generic version or a version that uses types from a graphics library like OpenTK/SharpDX/etc.

The generic version has no special Matrix constructors, but you can always multiply a rotation matrix and a translation matrix.

Matrix rotation = Matrix.RotationQuaternion(new Quaternion(0, 0, 0, 1));
Matrix translation = Matrix.Translation(x, y, z);
Matrix transform = rotation * translation;
colMapObject.AddChildShape(transform, sphere);

See here about multiplying matrices: https://docs.microsoft.com/en-us/windows/desktop/direct3d9/transforms#concatenating-matrices With a Matrix class from some other framework, you can do the same, except if the matrix layout is different (like in OpenTK), you may have to reverse the multiplication order to translation * rotation.

In this particular case, since (0, 0, 0, 1) is an identity quaternion that specifies no rotation, you could just use the translation matrix: colMapObject.AddChildShape(Matrix.Translation(x, y, z), sphere);

The generic version of BulletSharp has math classes based on SlimDX: https://slimdx.org/docs/#T_SlimDX_Matrix However, the Matrix.Transformation constructor that takes a rotation quaternion and a translation vector has been removed since it depends on DirectX.

Zelyutin commented 5 years ago

Thank you very much for you rapid and detailed answer! It helped!