Cinema 4D and Bullet Physics

Post Reply
Kuroyume
Posts: 6
Joined: Tue Feb 19, 2013 10:14 pm

Cinema 4D and Bullet Physics

Post by Kuroyume » Tue Sep 19, 2017 12:09 am

Since there is quite a bit of confusion about converting Cinema 4D<->Bullet transform matrices (or, generally, how to go convert left-handed<->right-handed coordinate transforms), here is the solution that I use (from source for another project). It is really that simple - use a Similarity Transform.

Code: Select all

// Vector/btVector3
//*---------------------------------------------------------------------------*
btVector3 c4dVector_btVector3(const Vector& c4d_vector)
//*---------------------------------------------------------------------------*
{
	return btVector3(c4d_vector.x, c4d_vector.y, c4d_vector.z);
}
//*---------------------------------------------------------------------------*
Vector btVector3_c4dVector(const btVector3& bt_vector)
//*---------------------------------------------------------------------------*
{
	return Vector(bt_vector.getX(), bt_vector.getY(), bt_vector.getZ());
}
// Matrix/btTransform
//*---------------------------------------------------------------------------*
Matrix btTransform_c4dMatrix(const btTransform& bt_transform)
//*---------------------------------------------------------------------------*
{
	btMatrix3x3 bt_basis =	bt_transform.getBasis();
	Matrix	c4d_mat;
	c4d_mat.v1 =		btVector3_c4dVector(bt_basis.getRow(0));
	c4d_mat.v2 =		btVector3_c4dVector(bt_basis.getRow(1));
	c4d_mat.v3 =		btVector3_c4dVector(bt_basis.getRow(2));
	c4d_mat.off =		btVector3_c4dVector(bt_transform.getOrigin());
	// Apply Similarity Transform from Bullet's R-H/RowMajor matrix (bt_transform)
	// - (z-trans and z row-column are negated except for overlap (v3.z))
	c4d_mat.v1.z *=		-1.0;
	c4d_mat.v2.z *=		-1.0;
	c4d_mat.v3.x *=		-1.0;
	c4d_mat.v3.y *=		-1.0;
	c4d_mat.off.z *=	-1.0;
	return c4d_mat;
}
//*---------------------------------------------------------------------------*
btTransform c4dMatrix_btTransform(const Matrix& c4d_matrix)
//*---------------------------------------------------------------------------*
{
	// Apply Similarity Transform to create Bullet's R-H/RowMajor matrix (bt_trans)
	// - (z-trans and z row-column are negated except for overlap (v3.z))
	/*
	Matrix	c4d_mat =	Matrix(c4d_matrix);
	c4d_mat.v1.z *=		-1.0;
	c4d_mat.v2.z *=		-1.0;
	c4d_mat.v3.x *=		-1.0;
	c4d_mat.v3.y *=		-1.0;
	c4d_mat.off.z *=	-1.0;
	*/
	btMatrix3x3	bt_basis = btMatrix3x3(c4d_matrix.v1.x, c4d_matrix.v1.y, c4d_matrix.v1.z*-1.0, c4d_matrix.v2.x, c4d_matrix.v2.y, c4d_matrix.v2.z*-1.0, c4d_matrix.v3.x*-1.0, c4d_matrix.v3.y*-1.0, c4d_matrix.v3.z);
	btTransform	bt_trans;
	bt_trans.setOrigin(c4dVector_btVector3(Vector(c4d_matrix.off.x, c4d_matrix.off.y, c4d_matrix.off.z*-1.0)));
	bt_trans.setBasis(bt_basis);
	return bt_trans;
}

Post Reply