Moderators: Sascha Willems, Thomas
void PhysX::createRigidBody(NewtonWorld* world, SceneNode* sourceNode,
NewtonCollision* m_col, dFloat mass, const String& materialId,
Terrain* terrain)
{
NewtonBody* body = NewtonCreateBody(mWorld, m_col);
float matrix[16];
if (sourceNode != NULL)
{
// here make a 90 quaternion rotation
OgreQuat rot (OgreVectror (0, 1, 0), 3.1416* 0.5f);
rot = rot * sourceNode->getOrientation();
PhysXTools::QuatPosToMatrix(rot, sourceNode->getPosition(), &matrix[0]); _matManager->assignMaterial(body, materialId);
}
else
{
SceneNode* sn = SceneHandler::getSingleton().getSceneManager("Octree1")->getRootSceneNode();
// here make a 90 quaternion rotation
OgreQuat rot (OgreVectror (0, 1, 0), 3.1416* 0.5f);
rot = rot * sn->getOrientation();
PhysXTools::QuatPosToMatrix(rot, sn->getPosition(), &matrix[0]);
_matManager->assignMaterial(body, "GroundMaterial");
}
// here set the origin offset like you did to align the mesh and the collision
x = (width / 2) * OgreTerrainGetCellSize();
z = (height / 2) * OgreTerrainGetCellSize();
matrix[3 * 4 + 0] -= x;
matrix[3 * 4 + 2] -= z;
NewtonBodySetMatrix(body, &matrix[0]);
NewtonBodySetForceAndTorqueCallback(body, globalForceCallback);
NewtonBodySetTransformCallback(body, globalTransformCallback);
NewtonBodySetUserData(body, sourceNode);
DataLog.LogMessage(LogDataManager::MT_OBJ_CREATED,
"NewtonBody created at: %p", body);
}
void PhysX::ObjectParser::createHieghtFieldCollision(String& filename,
Terrain* terrain, int* shapeIdArray)
{
int width, height;
width = height = terrain->getSize() - 1;
int size = width * height;
float* array32 = terrain->getHeightData();
char* attributes;
// find the min am max in the map
float min = terrain->getMinHeight();
float max = terrain->getMaxHeight();
// crate the new map
unsigned short* elevation = new unsigned short[size];
attributes = (char*) malloc(width * width * sizeof(char));
float scale = max - min;
Ogre::Real horizontalScale = terrain->getWorldSize()
/ (terrain->getSize() - 2);
float verticalScale = (1.0f / (65535 * scale));
for (int i = 0; i < size; i++)
{
elevation[i] = (unsigned short) (65535.0f * (array32[i] - min)
* scale);
cout << elevation[i] << "\t" << array32[i] << endl;
}
memset(attributes, 1, width * height * sizeof(char));
if (shapeIdArray)
{
for (int i = 0; i < width * height; i++)
{
attributes[i] = char(shapeIdArray[0]);
}
}
collision = NewtonCreateHeightFieldCollision(WORLD, width, height, 1,
elevation, attributes, horizontalScale, verticalScale, 0);
delete[] elevation;
free(attributes);
PhysX::getSingleton().createRigidBody(PhysX::getSingleton().getWorld(),
collision, terrain);
NewtonReleaseCollision(PhysX::getSingleton().getWorld(), collision);
}
void PhysX::ObjectParser::createHieghtFieldCollision(String& filename, Terrain* terrain, int* shapeIdArray)
{
int width, height;
width = height = terrain->getSize() - 1;
int size = width * height;
float* array32 = terrain->getHeightData();
char* attributes;
// find the min am max in the map
float min = terrain->getMinHeight();
float max = terrain->getMaxHeight();
// crate the new map
unsigned short* elevation = new unsigned short[size];
attributes = (char*) malloc(width * width * sizeof(char));
// this is wrong
// float scale = max - min;
float scale = (fabsf (max - min) > 1.0e-1f) ? 1.0 : 66535.0f / (max - min);
Ogre::Real horizontalScale = terrain->getWorldSize() / (terrain->getSize() - 2);
for (int i = 0; i < size; i++)
{
// this is wrong
// elevation[i] = (unsigned short) (65535.0f * (array32[i] - min) * scale);
elevation[i] = (unsigned short) ((array32[i] - min) * scale);
cout << elevation[i] << "\t" << array32[i] << endl;
}
memset(attributes, 1, width * height * sizeof(char));
if (shapeIdArray)
{
for (int i = 0; i < width * height; i++)
{
attributes[i] = char(shapeIdArray[0]);
}
}
float verticalScale = 1.0f / scale;
collision = NewtonCreateHeightFieldCollision(WORLD, width, height, 1,
elevation, attributes, horizontalScale, verticalScale, 0);
delete[] elevation;
free(attributes);
PhysX::getSingleton().createRigidBody(PhysX::getSingleton().getWorld(),
collision, terrain);
NewtonReleaseCollision(PhysX::getSingleton().getWorld(), collision);
}
Users browsing this forum: No registered users and 8 guests