destroy nodes with rectangle

main
cosmonaut 2022-06-24 12:04:57 -07:00
parent 18b84ca0c4
commit 65f84abdae
2 changed files with 55 additions and 14 deletions

View File

@ -445,7 +445,7 @@ void Silkworm_DestroyNode(void* nodePtr)
} }
} }
void Silkworm_Internal_DestroyLink(Silkworm_Link* link) void Silkworm_DestroyLink(Silkworm_Link* link)
{ {
link->markedForDestroy = true; link->markedForDestroy = true;
@ -462,11 +462,6 @@ void Silkworm_Internal_DestroyLink(Silkworm_Link* link)
context->linkDestructionDataCount += 1; context->linkDestructionDataCount += 1;
} }
void Silkworm_DestroyLink(double linkId)
{
Silkworm_Internal_DestroyLink(LookupLink((uint64_t)linkId));
}
void Silkworm_ClothDestroy(void* clothPtr) void Silkworm_ClothDestroy(void* clothPtr)
{ {
uint32_t i, j; uint32_t i, j;
@ -662,8 +657,10 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY)
{ {
if (!node->pinned) if (!node->pinned)
{ {
node->position.x += (0.5f * sinf(context->timeElapsed * 4 + node->position.y / 2) + 0.5f) * windSpeedX * delta * 0.05f * node->windFactor; float windFactorX = 0.5f * sinf(context->timeElapsed * 4 + node->position.y / 2) + 0.5f;
node->position.y += (0.5f * sinf(context->timeElapsed * 4 + node->position.x / 3) + 0.5f) * windSpeedY * delta * 0.05f * node->windFactor; float windFactorY = 0.5f * sinf(context->timeElapsed * 4 + node->position.x / 3) + 0.5f;
node->position.x += windFactorX * windSpeedX * delta * 0.05f * node->windFactor;
node->position.y += windFactorY * windSpeedY * delta * 0.05f * node->windFactor;
} }
} }
} }
@ -690,7 +687,7 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY)
if (distanceMovedSquared > link->tearThreshold * link->tearThreshold) if (distanceMovedSquared > link->tearThreshold * link->tearThreshold)
{ {
Silkworm_DestroyLink((double)link->id); Silkworm_DestroyLink(link);
} }
else else
{ {
@ -918,7 +915,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo)
nodeCreateInfo.friction = clothCreateInfo->friction; nodeCreateInfo.friction = clothCreateInfo->friction;
nodeCreateInfo.mass = clothCreateInfo->mass; nodeCreateInfo.mass = clothCreateInfo->mass;
nodeCreateInfo.pinned = false; nodeCreateInfo.pinned = false;
nodeCreateInfo.pushFactor = 0; nodeCreateInfo.pushFactor = 1;
nodeCreateInfo.radius = 1; nodeCreateInfo.radius = 1;
nodeCreateInfo.windFactor = clothCreateInfo->windFactor; nodeCreateInfo.windFactor = clothCreateInfo->windFactor;
@ -1259,6 +1256,40 @@ void Silkworm_DestroyNodesInRadius(float x, float y, float radius)
Silkworm_PerformDestroys(); Silkworm_PerformDestroys();
} }
void Silkworm_DestroyNodesInRectangle(Silkworm_Rectangle* rectangle)
{
/* TODO: spatial hash implementation */
uint32_t i;
for (i = 0; i < context->nodeCount; i += 1)
{
Silkworm_Node* node = context->nodes[i];
if (node != NULL && node->destroyable)
{
if (
node->position.x >= rectangle->x &&
node->position.x <= rectangle->x + rectangle->w &&
node->position.y >= rectangle->y &&
node->position.y <= rectangle->y + rectangle->h
) {
Silkworm_DestroyNode(node);
if (context->nodeDestructionDataCount >= context->nodeDestructionDataCapacity)
{
context->nodeDestructionDataCapacity *= 2;
context->nodeDestructionData = realloc(context->nodeDestructionData, sizeof(Silkworm_Vector2) * context->nodeDestructionDataCapacity);
}
context->nodeDestructionData[context->nodeDestructionDataCount] = node->position;
context->nodeDestructionDataCount += 1;
}
}
}
}
void* Silkworm_FindClothInRadius(float x, float y, float radius) void* Silkworm_FindClothInRadius(float x, float y, float radius)
{ {
/* TODO: spatial hash implementation */ /* TODO: spatial hash implementation */

View File

@ -86,6 +86,14 @@ typedef struct Silkworm_ClothCreateInfo
float bottomUV; float bottomUV;
} Silkworm_ClothCreateInfo; } Silkworm_ClothCreateInfo;
typedef struct Silkworm_Rectangle
{
float x;
float y;
float w;
float h;
} Silkworm_Rectangle;
typedef struct Silkworm_Vertex typedef struct Silkworm_Vertex
{ {
float x; float x;
@ -107,14 +115,16 @@ SILKWORMAPI void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo
SILKWORMAPI void Silkworm_ClothNodePin(void* clothPtr, uint32_t i, uint32_t j); SILKWORMAPI void Silkworm_ClothNodePin(void* clothPtr, uint32_t i, uint32_t j);
SILKWORMAPI void Silkworm_ClothNodeUnpin(void* clothPtr, uint32_t i, uint32_t j); SILKWORMAPI void Silkworm_ClothNodeUnpin(void* clothPtr, uint32_t i, uint32_t j);
SILKWORMAPI void Silkworm_ClothNodeDestroy(void* clothPtr, uint32_t i, uint32_t j); SILKWORMAPI void Silkworm_ClothNodeDestroy(void* clothPtr, uint32_t i, uint32_t j);
SILKWORMAPI uint32_t Silkworm_ClothRender(void **pVertexBuffer, uint32_t *pVertexBufferLengthInBytes);
SILKWORMAPI void Silkworm_ClothDestroy(void* clothPtr); SILKWORMAPI void Silkworm_ClothDestroy(void* clothPtr);
SILKWORMAPI uint32_t Silkworm_ClothRender(void** pVertexBuffer, uint32_t* pVertexBufferLengthInBytes);
SILKWORMAPI void Silkworm_PinNodesInRadius(float x, float y, float radius); SILKWORMAPI void Silkworm_PinNodesInRadius(float x, float y, float radius);
SILKWORMAPI void Silkworm_UnpinNodesInRadius(float x, float y, float radius); SILKWORMAPI void Silkworm_UnpinNodesInRadius(float x, float y, float radius);
SILKWORMAPI void Silkworm_PushNodesInRadius(float x, float y, float radius, float xDirection, float yDirection); SILKWORMAPI void Silkworm_PushNodesInRadius(float x, float y, float radius, float xDirection, float yDirection);
SILKWORMAPI void Silkworm_DestroyNodesInRadius(float x, float y, float radius);
SILKWORMAPI void* Silkworm_FindClothInRadius(float x, float y, float radius); SILKWORMAPI void* Silkworm_FindClothInRadius(float x, float y, float radius);
SILKWORMAPI void Silkworm_DestroyNodesInRadius(float x, float y, float radius);
SILKWORMAPI void Silkworm_DestroyNodesInRectangle(Silkworm_Rectangle* rectangle);
SILKWORMAPI void Silkworm_PerformDestroys(); SILKWORMAPI void Silkworm_PerformDestroys();
SILKWORMAPI void Silkworm_ClearAll(); SILKWORMAPI void Silkworm_ClearAll();