I'm using the convex cast to place my objects vertically in my world with the code below (It is adapted from the tutorials). It works well with "normal" collision shapes, but when I add a TreeCollision as the ground, the ConvexCast returns some confusing values. When I cast sphere shapes, it *always* returns the correct value, but when I cast other primitives or convex hulls, the result is unpredictable. There are faces of the TreeCollision that always work, and faces that never work. And there are faces (see the picture) that work only in a special range. Again, when I cast spheres, it always works, regardless of the face.
In the black areas of the picture, the objects are placed at the ground level (below the ramp).
- Code: Select all
static unsigned ConvexCastCallback(const NewtonBody* body, const NewtonCollision* collision, void* userData)
{
std::cout << body << std::endl;
NewtonBody* other = (NewtonBody*)userData;
return (other == body) ? 0 : 1;
}
float getConvexCastPlacement(NewtonBody* body)
{
Mat4f matrix;
NewtonBodyGetMatrix(body, matrix[0]);
matrix._42 += 200.0f;
Vec3f p(matrix.getW());
p.y -= 400.0f;
NewtonWorld* world = NewtonBodyGetWorld(body);
NewtonCollision* collision = NewtonBodyGetCollision(body);
float param = 0.0f;
NewtonWorldConvexCastReturnInfo info[16];
NewtonCollisionInfoRecord collisionInfo;
NewtonCollisionGetInfo(collision, &collisionInfo);
if (collisionInfo.m_collisionType == SERIALIZE_ID_COMPOUND) {
float maximum = -200.0f;
// the nodes of my compounds to not have a special offset matrix, so use the normal one
for (int i = 0; i < collisionInfo.m_compoundCollision.m_chidrenCount; ++i) {
NewtonCollision* node = collisionInfo.m_compoundCollision.m_chidren[i];
NewtonWorldConvexCast(world, matrix[0], &p[0], node, ¶m, body, ConvexCastCallback, info, 16, 0);
float current = matrix._42 + (p.y - matrix._42) * param;
if (current > maximum) maximum = current;
}
return maximum;
} else {
NewtonWorldConvexCast(world, matrix[0], &p[0], collision, ¶m, body, ConvexCastCallback, info, 16, 0);
matrix._42 += (p.y - matrix._42) * param;
return matrix._42;
}
}