Moderators: Sascha Willems, walaber
// test function to see if NewtonTreeCollisionSetUserRayCastCallback is still working
static dFloat UserCollisionCallcack (dFloat interception, dFloat* normal, int faceId, void* usedData)
{
// this does get called with all correct parameters.
// do what ever you want here.
return 1.0f;
}
NewtonCollision* CreateMeshCollision (NewtonWorld* world, Entity* ent, int* shapeIdArray)
{
NewtonCollision* collision;
// now create and empty collision tree
collision = NewtonCreateTreeCollision (world, 0);
// start adding faces to the collision tree
NewtonTreeCollisionBeginBuild (collision);
// step over the collision geometry and add all faces to the collision tree
for (int i = 0; i < ent->m_subMeshCount; i ++) {
// add each sub mesh as a face id, will will sue this later for a multi material sound effect in and advanced tutorial
for (int j = 0; j < ent->m_subMeshes[i].m_indexCount; j += 3 ) {
int index;
dVector face[3];
index = ent->m_subMeshes[i].m_indexArray[j + 0] * 3;
face[0] = dVector (ent->m_vertex[index + 0], ent->m_vertex[index + 1], ent->m_vertex[index + 2]);
index = ent->m_subMeshes[i].m_indexArray[j + 1] * 3;
face[1] = dVector (ent->m_vertex[index + 0], ent->m_vertex[index + 1], ent->m_vertex[index + 2]);
index = ent->m_subMeshes[i].m_indexArray[j + 2] * 3;
face[2] = dVector (ent->m_vertex[index + 0], ent->m_vertex[index + 1], ent->m_vertex[index + 2]);
if (shapeIdArray) {
NewtonTreeCollisionAddFace(collision, 3, &face[0].m_x, sizeof (dVector), shapeIdArray[i]);
} else {
NewtonTreeCollisionAddFace(collision, 3, &face[0].m_x, sizeof (dVector), i + 1);
}
}
}
// end adding faces to the collision tree, also optimize the mesh for best performance
NewtonTreeCollisionEndBuild (collision, 1);
// look here, this is how you add the user callback to the collision tree mesh,
// not to be confused with the filter callback wihch is called on each collision shape that the ray hit.
// this function is called on each face of this collision tree that ray hit.
NewtonTreeCollisionSetUserRayCastCallback(collision, UserCollisionCallcack);
return collision;
}
Chooka wrote:cool thankyou. also could you consider adding some methods for getting/setting attributes at x,y positions and maybe include the attribute info into the callback?
Chooka wrote:can NewtonHeightFieldCollisionCallback and NewtonHeightFieldCollisionSetUserRayCastCallback be added to the next version. or do i just use NewtonTreeCollisionCallback and NewtonTreeCollisionSetUserRayCastCallback ?
static dFloat UserHeightFieldCollisionCallback (const NewtonBody* const body, const NewtonCollision* const heightField, dFloat interception, int row, int col, dFloat* normal, int faceId, void* usedData)
{
return 1.0f;
}
// Create a height field collision form a elevation file
NewtonCollision* CreateHeightFieldCollision (NewtonWorld* world, char* fileName, int* shapeIdArray)
{
int width;
int height;
char* attibutes;
unsigned short* elevations;
FILE* file;
NewtonCollision* collision;
char fullPathName[2048];
#define CELL_SIZE 12.0f
#define ELEVATION_SCALE 256.0f
#define TEXTURE_SCALE (1.0f / 16.0f)
#define ELEVATION_SCALE_INV (1.0f / ELEVATION_SCALE)
//load from raw data
GetWorkingFileName (fileName, fullPathName);
file = fopen (fullPathName, "rb");
_ASSERTE (file);
width = 256;
height = 256;
// load the data;
elevations = (unsigned short*) malloc (width * height * sizeof (unsigned short));
attibutes = (char*) malloc (width * width * sizeof (char));
fread (elevations, sizeof (unsigned short), width * height, file);
memset (attibutes, 1, width * height * sizeof (char));
if (shapeIdArray) {
for (int i = 0; i < width * height; i ++) {
attibutes[i] = char(shapeIdArray[0]);
}
}
collision = NewtonCreateHeightFieldCollision (world, width, height, 0, elevations, attibutes, CELL_SIZE, ELEVATION_SCALE_INV, 0);
free (elevations);
free (attibutes);
fclose (file);
// look here, this is how you add the user callback to the collision tree mesh,
// not to be confused with the filter callback which is called on each collision shape that the ray hit.
// this function is called on each face of this collision tree that ray hit.
NewtonHeightFieldSetUserRayCastCallback(collision, UserHeightFieldCollisionCallback);
return collision;
}
Users browsing this forum: No registered users and 1 guest