From 65f84abdae2eade80015b7bd1217ce836e1850e2 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 24 Jun 2022 12:04:57 -0700 Subject: [PATCH] destroy nodes with rectangle --- src/Silkworm2.c | 55 ++++++++++++++++++++++++++++++++++++++----------- src/Silkworm2.h | 14 +++++++++++-- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/Silkworm2.c b/src/Silkworm2.c index b7b3a2c..d7d84cc 100644 --- a/src/Silkworm2.c +++ b/src/Silkworm2.c @@ -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; @@ -462,11 +462,6 @@ void Silkworm_Internal_DestroyLink(Silkworm_Link* link) context->linkDestructionDataCount += 1; } -void Silkworm_DestroyLink(double linkId) -{ - Silkworm_Internal_DestroyLink(LookupLink((uint64_t)linkId)); -} - void Silkworm_ClothDestroy(void* clothPtr) { uint32_t i, j; @@ -627,7 +622,7 @@ void Silkworm_PerformDestroys() cloth->nodes[node->clothReference.horizontalIndex][node->clothReference.verticalIndex] = NULL; } - + free(context->nodes[i]->links); free(context->nodes[i]); context->nodes[i] = NULL; @@ -662,8 +657,10 @@ void Silkworm_Update(float delta, float windSpeedX, float windSpeedY) { if (!node->pinned) { - node->position.x += (0.5f * sinf(context->timeElapsed * 4 + node->position.y / 2) + 0.5f) * windSpeedX * delta * 0.05f * node->windFactor; - node->position.y += (0.5f * sinf(context->timeElapsed * 4 + node->position.x / 3) + 0.5f) * windSpeedY * delta * 0.05f * node->windFactor; + float windFactorX = 0.5f * sinf(context->timeElapsed * 4 + node->position.y / 2) + 0.5f; + 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) { - Silkworm_DestroyLink((double)link->id); + Silkworm_DestroyLink(link); } else { @@ -918,7 +915,7 @@ void* Silkworm_ClothCreate(Silkworm_ClothCreateInfo* clothCreateInfo) nodeCreateInfo.friction = clothCreateInfo->friction; nodeCreateInfo.mass = clothCreateInfo->mass; nodeCreateInfo.pinned = false; - nodeCreateInfo.pushFactor = 0; + nodeCreateInfo.pushFactor = 1; nodeCreateInfo.radius = 1; nodeCreateInfo.windFactor = clothCreateInfo->windFactor; @@ -1142,7 +1139,7 @@ uint32_t Silkworm_ClothRender(void **pVertexBuffer, uint32_t *pVertexBufferLengt *pVertexBuffer = context->vertexBuffer; *pVertexBufferLengthInBytes = sizeof(Silkworm_Vertex) * vertexCount; } - + return vertexCount; } @@ -1259,6 +1256,40 @@ void Silkworm_DestroyNodesInRadius(float x, float y, float radius) 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) { /* TODO: spatial hash implementation */ diff --git a/src/Silkworm2.h b/src/Silkworm2.h index 66348ef..7cb7587 100644 --- a/src/Silkworm2.h +++ b/src/Silkworm2.h @@ -86,6 +86,14 @@ typedef struct Silkworm_ClothCreateInfo float bottomUV; } Silkworm_ClothCreateInfo; +typedef struct Silkworm_Rectangle +{ + float x; + float y; + float w; + float h; +} Silkworm_Rectangle; + typedef struct Silkworm_Vertex { 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_ClothNodeUnpin(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 uint32_t Silkworm_ClothRender(void** pVertexBuffer, uint32_t* pVertexBufferLengthInBytes); + SILKWORMAPI void Silkworm_PinNodesInRadius(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_DestroyNodesInRadius(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_ClearAll();