Game Development Community

implemented Bullet HeightFieldFluid, need help setting z-up axis

by deepscratch · in Torque 3D Professional · 05/01/2012 (3:28 am) · 7 replies

hi all,

this one has me stumped.
I've successfully implemented Bullet HeightFieldFluid into 1.2, but I'm having trouble getting it to the Torque z-up.

any ideas welcome.

here is a screenie, the mesh is the HeightFieldFluid,
I need to get the mesh aligned with the water surface,
and I'm sure its simple to do,
but I dont see it.

deep.deta.in/store/freestuff/HFFluid_rotated.jpg


#1
05/01/2012 (3:47 pm)
@deepscratch, you should take a look at physicsbulletbtCasts.h. It has casts for MatrixF -> btTransform and btTransform -> MatrixF. I think they do the Y-up to Z-up conversion (not tested that so I'm not sure). They're used like this (check BtCollision::_addShape):

MatrixF aTorqueXfm;
btTransform aBulletXfm = btCast<btTransform>( aTorqueXfm );

Hope this helps!
#2
05/02/2012 (2:14 am)
thanks Ross, I am using those casts already,
but they fail.

here is the code I'm using(straight from the bullet demo)

PhysFluidBullet::PhysFluidBullet(Physics* phys, const PhysFluidInfo &physDescr) : PhysFluid(physDescr)
{
	PhysicsBullet* physics = static_cast<PhysicsBullet*>(phys);
	m_world = static_cast<btHfFluidRigidDynamicsWorld*>(physics->getWorld());
	
	m_fluid = new btHfFluid (physDescr.gridCellWidth, physDescr.numNodesWidth, physDescr.numNodesLength);

    // original. fail
//	btTransform xform;
//	xform.setIdentity ();
//	xform.getOrigin() = btCast<btVector3>(physDescr.position);
//	m_fluid->setWorldTransform (xform);

	// also fail
	MatrixF aTorqueXfm;
	btTransform aBulletXfm = btCast<btTransform>(aTorqueXfm);
	aBulletXfm.setIdentity ();
	aBulletXfm.getOrigin() = btCast<btVector3>(physDescr.position);
	m_fluid->setWorldTransform (aBulletXfm);

	m_fluid->setVolumeDisplacementScale (btScalar(physDescr.displacementScale));
	m_world->addHfFluid(m_fluid);
	
	for (int i = 0; i < m_fluid->getNumNodesLength()*m_fluid->getNumNodesWidth(); i++)
	{
		m_fluid->setFluidHeight(i, btScalar(physDescr.fluidHeight));
	}

	m_fluid->prep ();
}

#3
05/02/2012 (10:50 am)
Try replacing this:

//
    MatrixF aTorqueXfm;  
    btTransform aBulletXfm = btCast<btTransform>(aTorqueXfm);  
    aBulletXfm.setIdentity ();  
    aBulletXfm.getOrigin() = btCast<btVector3>(physDescr.position);  
    m_fluid->setWorldTransform (aBulletXfm);

with this:

//
    MatrixF aTorqueXfm;  
    aTorqueXfm.setColumn(3, physDescr.position);
    m_fluid->setWorldTransform (btCast<btTransform>(aTorqueXfm));

That's assuming that physDescr.position is a Point3F or VectorF (Torque3D type) which it appears to be from your code.
#4
05/02/2012 (12:13 pm)
yes, physDescr.position is a VectorF,

no, the above code also fails Ross :(

I'm pretty much at wits end on this one.
#5
05/02/2012 (12:48 pm)
What do you mean by fails? Does it compile? If so, do you mean that it fails to fix the rotation of the fluid heightfield?
#6
05/02/2012 (1:17 pm)
So, looking at the terrain code for Bullet compatibility, btHeightfieldTerrainShape has a parameter to specify the up-axis. In BtCollision::addHeightfield, it passes a 2 to the up-axis parameter (2 = z-up).

I looked through the btHfFluid code and it doesn't appear to have the same facility.

You could try a 90-degree rotation around the X axis and see if that works.

//
    MatrixF aTorqueXfm( true );  
    QuatF rotAroundX( Point3F( 1.0f, 0, 0 ), 90.0f );
    rotAroundX.setMatrix( &aTorqueXfm );
    aTorqueXfm.setColumn( 3, physDescr.position );
    m_fluid->setWorldTransform( btCast<btTransform>( aTorqueXfm ) );

I'm not sure if QuatF takes degrees or radians anymore, but if this rotates it strangely or too much around the x-axis, try doing mRadToDeg( 90.0f ) in place of the 90.0f in the QuatF constructor.

Edit: Looking at the heightfield code and the fluid code, it appears both use collision shapes derived from btConcaveShape. The btHeightfieldTerrainShape has code for handling most of the methods while taking into account the up-axis. You may be able to port across the axis related code from btHeightfieldTerrainShape to btHfFluidCollisionShape, though you might run into places where the fluid code doesn't take it into account.
#7
06/01/2012 (2:36 pm)
Hey deepscratch, have you had any luck with this, I'm interested in getting some better buoyancy in Torque.

Do you mind sharing what you have so far so we can work together on it, I just started looking into using the btHfFluid class?