neighbor normal for heightfield
This commit is contained in:
parent
3b4878b7f2
commit
5d591a90db
4 changed files with 216 additions and 0 deletions
|
|
@ -585,6 +585,54 @@ int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangle
|
|||
return b3GetHeightFieldMaterialIndices( heightField )[cellIndex];
|
||||
}
|
||||
|
||||
bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int triangleIndex, int edgeIndex, b3Vec3* normal )
|
||||
{
|
||||
B3_ASSERT( 0 <= triangleIndex && triangleIndex < b3GetHeightFieldTriangleCount( heightField ) );
|
||||
B3_ASSERT( 0 <= edgeIndex && edgeIndex < 3 );
|
||||
|
||||
if ( heightField->clockwise )
|
||||
{
|
||||
// Reflected
|
||||
edgeIndex = 2 - edgeIndex;
|
||||
}
|
||||
|
||||
int cellColumnCount = heightField->columnCount - 1;
|
||||
int cellRowCount = heightField->rowCount - 1;
|
||||
int cellIndex = triangleIndex >> 1;
|
||||
int row = cellIndex / cellColumnCount;
|
||||
int column = cellIndex - row * cellColumnCount;
|
||||
bool odd = ( triangleIndex & 1 ) == 1;
|
||||
|
||||
// Get the row and column for the neighbor cell.
|
||||
if ( edgeIndex == 0 )
|
||||
{
|
||||
column += odd ? 1 : -1;
|
||||
}
|
||||
else if ( edgeIndex == 2 )
|
||||
{
|
||||
row += odd ? 1 : -1;
|
||||
}
|
||||
|
||||
if ( row < 0 || row >= cellRowCount || column < 0 || column >= cellColumnCount )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int neighborCell = row * cellColumnCount + column;
|
||||
|
||||
const uint8_t* materialIndices = b3GetHeightFieldMaterialIndices( heightField );
|
||||
if ( materialIndices[neighborCell] == B3_HEIGHT_FIELD_HOLE )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
b3Triangle neighbor = b3GetHeightFieldTriangle( heightField, 2 * neighborCell + ( odd ? 0 : 1 ) );
|
||||
b3Vec3 e1 = b3Sub( neighbor.vertices[1], neighbor.vertices[0] );
|
||||
b3Vec3 e2 = b3Sub( neighbor.vertices[2], neighbor.vertices[0] );
|
||||
*normal = b3Normalize( b3Cross( e1, e2 ) );
|
||||
return true;
|
||||
}
|
||||
|
||||
b3AABB b3ComputeHeightFieldAABB( const b3HeightFieldData* shape, b3Transform transform )
|
||||
{
|
||||
return b3AABB_Transform( transform, shape->aabb );
|
||||
|
|
|
|||
12
src/mesh.c
12
src/mesh.c
|
|
@ -1092,6 +1092,18 @@ bool b3GetMeshNeighborNormal( const b3Mesh* mesh, int triangleIndex, int edgeInd
|
|||
b3CosSin cs = b3ComputeCosSin( (float)quantized * ( B3_PI / B3_EDGE_ANGLE_STEPS ) );
|
||||
b3Vec3 unscaled = b3Add( b3MulSV( cs.cosine, n ), b3MulSV( cs.sine, outward ) );
|
||||
|
||||
b3Vec3 absScale = b3Abs( scale );
|
||||
if ( absScale.y == absScale.x && absScale.z == absScale.x )
|
||||
{
|
||||
// Shortcut for uniform scale. Common case.
|
||||
*normal = (b3Vec3){
|
||||
scale.x < 0.0f ? -unscaled.x : unscaled.x,
|
||||
scale.y < 0.0f ? -unscaled.y : unscaled.y,
|
||||
scale.z < 0.0f ? -unscaled.z : unscaled.z,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handle non-uniform scale.
|
||||
b3Vec3 invScale = { 1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z };
|
||||
*normal = b3Normalize( b3Mul( invScale, unscaled ) );
|
||||
|
|
|
|||
13
src/shape.h
13
src/shape.h
|
|
@ -129,6 +129,8 @@ float b3ComputeHullProjectedArea( const b3HullData* hull, b3Vec3 direction );
|
|||
b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int triangleIndex );
|
||||
int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangleIndex );
|
||||
|
||||
bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int triangleIndex, int edgeIndex, b3Vec3* normal );
|
||||
|
||||
static inline int b3GetHeightFieldTriangleCount( const b3HeightFieldData* heightField )
|
||||
{
|
||||
int cellCount = ( heightField->rowCount - 1 ) * ( heightField->columnCount - 1 );
|
||||
|
|
@ -185,6 +187,17 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde
|
|||
|
||||
bool b3GetMeshNeighborNormal( const b3Mesh* mesh, int triangleIndex, int edgeIndex, b3Vec3* normal );
|
||||
|
||||
static inline bool b3GetNeighborNormal( const b3Shape* shape, int triangleIndex, int edgeIndex, b3Vec3* normal )
|
||||
{
|
||||
if ( shape->type == b3_meshShape )
|
||||
{
|
||||
return b3GetMeshNeighborNormal( &shape->mesh, triangleIndex, edgeIndex, normal );
|
||||
}
|
||||
|
||||
B3_ASSERT( shape->type == b3_heightShape );
|
||||
return b3GetHeightFieldNeighborNormal( shape->heightField, triangleIndex, edgeIndex, normal );
|
||||
}
|
||||
|
||||
bool b3IsValidMesh( const b3MeshData* meshData );
|
||||
|
||||
static inline bool b3ShouldShapesCollide( b3Filter filterA, b3Filter filterB )
|
||||
|
|
|
|||
Loading…
Reference in a new issue