• 12
• 12
• 9
• 10
• 13

# Simple Havok Question

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I put this on the Havok forum, didn't get a response. It's a pretty simple question.

I can't figure out how to get the positions and rotations from havok and position/rotate my directx objects with them. I tried this:

for (int i=0;i<ObjectNum;i++)

{

if (HavokBody)

{

hkVector4 TempPos=HavokBody->getPosition();

Objects->SetPosition(D3DXVECTOR3(TempPos(0),TempPos(1),TempPos(2)));

}

}
But that doesn't work. I didn't even try setting the rotation because I don't understand quaternions (I'm using Euler angles). I don't really understand hkVector4s. From what I understand, you can't just get the x, y, z (and w if I ever need it) values like this:

float xcoord = examplehkVector4.x;

How would I position and rotate my direct x objects to match the ones in the simulation? How would I convert a quaternion to Euler angles (or a matrix)? Thanks for any help.

##### Share on other sites
You're right, it is very easy:
 float p[16]; hkTransform transform; m_chassisRigidBody->approxCurrentTransform(transform); transform.get4x4ColumnMajor(p); 

This returns a column major 4x4 matrix; the last column contains your 1x4 vector. You might want to use a row major matrix instead (which is what I think d3d defaults to) but the principle is the same.

##### Share on other sites
Thanks for the response, I get an error though (I think its something wrong with the way I make the rigid body)
I get my error at the approxCurrentTransform function, the next line of code to be run is this:

HK_ASSERT2(0x3dd87047, m_world, "This function requires the body to be in an hkpWorld, in order to retrieve the current time.");

I assume this is my problem, but I'm pretty sure I added the rigid body, here's my code, for initializing the havok world, and adding rigid bodies.

void DXClass::InitHavok(D3DXVECTOR3 Gravity)
{
// Initialize the base system including our memory system
m_pMemoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, hkMemorySystem::FrameInfo(500000));
hkBaseSystem::init( m_pMemoryRouter, errorReport );

// Get the number of physical threads available on the system
hkHardwareInfo hardwareInfo;
hkGetHardwareInfo( hardwareInfo );

// We use one less than this for our thread pool, because we must also use this thread for our simulation
// This line enables timers collection, by allocating 200 Kb per thread. If you leave this at its default (0),
// timer collection will not be enabled.
// We also need to create a Job queue. This job queue will be used by all Havok modules to run multithreaded work.
hkJobQueueCinfo jobQueueInfo;
jobQueue = new hkJobQueue( jobQueueInfo );

// Enable monitors for this thread.
hkMonitorStream::getInstance().resize(200000);

hkpWorldCinfo info;
info.m_gravity.set(Gravity.x, Gravity.y, Gravity.z);
info.m_collisionTolerance=0.1f;
info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM);
HavokWorld=new hkpWorld(info);
HavokWorld->markForWrite();
hkpAgentRegisterUtil::registerAllAgents( HavokWorld->getCollisionDispatcher() );HavokWorld->registerWithJobQueue( jobQueue );
HavokWorld->unmarkForWrite();
}

void DXClass::MakeHavokBox(int ID, float x, float y, float z, float sizex, float sizey, float sizez, float mass, float friction, bool dynamic)
{
hkpMassProperties massProperties;
hkVector4 boxsize(sizex, sizey, sizez);
hkVector4 position(x, y, z);
HavokWorld->markForWrite();
hkpConvexShape* shape = new hkpBoxShape(boxsize);
hkpRigidBodyCinfo info;
if (dynamic)
{
hkpInertiaTensorComputer::computeBoxVolumeMassProperties(boxsize, mass, massProperties);
info.m_mass=mass;
info.m_friction=friction;
info.m_centerOfMass=massProperties.m_centerOfMass;
info.m_inertiaTensor=massProperties.m_inertiaTensor;
info.m_solverDeactivation=info.SOLVER_DEACTIVATION_MEDIUM;
info.m_shape=shape;
info.m_motionType=hkpMotion::MOTION_BOX_INERTIA;
info.m_qualityType=HK_COLLIDABLE_QUALITY_MOVING;
info.m_position=position;
}
else
{
info.m_shape=shape;
info.m_mass=0.0f;
info.m_motionType=hkpMotion::MOTION_FIXED;
info.m_position=position;
info.m_qualityType=HK_COLLIDABLE_QUALITY_FIXED;
}
HavokBody[ID]=new hkpRigidBody(info);
HavokWorld->lock();
HavokWorld->unmarkForWrite();
shape->removeReference();
HavokBody[ID]->removeReference();
HavokWorld->unlock();
}

Sorry about the long code, I don't understand the code tags on this forum. I tried just doing this (I know the word "codetag" isn't the actual codetag, its just there to show where I would put it).

codetag
code here
/codetag

But it doesn't work. Anyway.

What's in the matrix? Does it have the same stuff as a D3DXMATRIX just in column major form? Does that mean that p[0] would be the first row, first column, and that p[1] would be second row, first column, and so on? Thanks for any help.