From eee31fa5e9e55fbbe6967dffac935f1c3c003550 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 24 Sep 2021 15:17:35 -0700 Subject: [PATCH] node pushing --- src/Silkworm.c | 58 ++++++++++++++++++++++++++++++++++++-------------- src/Silkworm.h | 1 + 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/Silkworm.c b/src/Silkworm.c index 0fe2135..9d6927a 100644 --- a/src/Silkworm.c +++ b/src/Silkworm.c @@ -352,7 +352,7 @@ void Silkworm_Init() context->gravity = 200; context->xBound = 1000; context->yBound = 1000; - context->clothDensity = 2; + context->clothDensity = 4; } static inline Silkworm_Node* LookupNode(uint64_t nodeId) @@ -582,7 +582,7 @@ void Silkworm_Update(double deltaTime) float translateX = diffX * 0.5f * difference; float translateY = diffY * 0.5f * difference; - + float distanceMoved = (float)sqrt(translateX * translateX + translateY * translateY); if (distanceMoved > link->tearThreshold) @@ -734,7 +734,7 @@ double Silkworm_CreateLink(double aId, double bId, double distance, double tearT id = context->linkIndexStack[context->linkIndexStackCount - 1]; context->linkIndexStackCount -= 1; } - else + else { id = context->linkCount; @@ -762,15 +762,15 @@ double Silkworm_CreateLink(double aId, double bId, double distance, double tearT return (double)link->id; } -double Silkworm_CreateCloth(double xPosition, double yPosition, double horizontalNodeCount, double verticalNodeCount, double mass, double friction, double windFactor, double tearThreshold) +double Silkworm_CreateCloth(double xPosition, double yPosition, double width, double height, double mass, double friction, double windFactor, double tearThreshold) { int32_t i, j, k, m; Silkworm_Cloth* cloth = malloc(sizeof(Silkworm_Cloth)); cloth->windFactor = (float)windFactor; - cloth->horizontalNodeCount = (uint32_t) horizontalNodeCount; - cloth->verticalNodeCount = (uint32_t) verticalNodeCount; + cloth->horizontalNodeCount = ((uint32_t)width / context->clothDensity) + 1; + cloth->verticalNodeCount = ((uint32_t)height / context->clothDensity) + 1; cloth->nodeIndices = malloc(sizeof(uint64_t*) * cloth->horizontalNodeCount); uint64_t id; @@ -805,11 +805,11 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta cloth->linkHash.buckets[i].count = 0; } - for (i = 0; i < horizontalNodeCount; i += 1) + for (i = 0; i < cloth->horizontalNodeCount; i += 1) { cloth->nodeIndices[i] = malloc(sizeof(uint64_t) * cloth->verticalNodeCount); - for (j = 0; j < verticalNodeCount; j += 1) + for (j = 0; j < cloth->verticalNodeCount; j += 1) { uint64_t nodeId = (uint64_t) Silkworm_CreateNode(xPosition + i * context->clothDensity, yPosition + j * context->clothDensity, mass, friction, 1, 0.5); @@ -827,11 +827,11 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta cloth->triangles = malloc(sizeof(Silkworm_Triangle*) * cloth->horizontalNodeCount * cloth->verticalNodeCount * 2); uint32_t triangleIndex = 0; - for (i = 0; i < horizontalNodeCount; i += 1) + for (i = 0; i < cloth->horizontalNodeCount; i += 1) { - for (j = 0; j < verticalNodeCount; j += 1) + for (j = 0; j < cloth->verticalNodeCount; j += 1) { - if (i + 1 < horizontalNodeCount && j + 1 < verticalNodeCount) + if (i + 1 < cloth->horizontalNodeCount && j + 1 < cloth->verticalNodeCount) { cloth->triangles[triangleIndex] = malloc(sizeof(Silkworm_Triangle)); @@ -885,9 +885,9 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta cloth->triangleCount = triangleIndex; - for (i = 0; i < horizontalNodeCount; i += 1) + for (i = 0; i < cloth->horizontalNodeCount; i += 1) { - for (j = 0; j < verticalNodeCount; j += 1) + for (j = 0; j < cloth->verticalNodeCount; j += 1) { if (i - 1 >= 0) { @@ -960,8 +960,8 @@ void Silkworm_ApplyWind(double xSpeed, double ySpeed) if (node != NULL && !node->pinned) { - node->position.x += (float)xSpeed * 0.05f * cloth->windFactor; - node->position.y += (float)ySpeed * 0.05f * cloth->windFactor; + node->position.x += (float)xSpeed * 0.025f * cloth->windFactor; + node->position.y += (float)ySpeed * 0.025f * cloth->windFactor; } } } @@ -1090,6 +1090,32 @@ double Silkworm_ClothFillTriangleBuffer(double clothId, double leftUV, double wi return (double)triangleCount; } +void Silkworm_PushNodesInRadius(double x, double y, double radius, double xDirection, double yDirection) +{ + /* TODO: spatial hash implementation */ + + uint32_t i; + + for (i = 0; i < context->nodeCount; i += 1) + { + Silkworm_Node* node = context->nodes[i]; + + if (node != NULL) + { + float xDistance = (float)fabs((float)x - node->position.x); + float yDistance = (float)fabs((float)y - node->position.y); + + float squareDistance = xDistance * xDistance + yDistance * yDistance; + + if (squareDistance <= radius * radius) + { + node->position.x += (float)xDirection; + node->position.y += (float)yDirection; + } + } + } +} + void Silkworm_DestroyNodesInRadius(double x, double y, double radius) { /* TODO: spatial hash implementation */ @@ -1118,7 +1144,7 @@ void Silkworm_DestroyNodesInRadius(double x, double y, double radius) void Silkworm_ClearAll() { uint32_t i; - + for (i = 0; i < context->clothCount; i += 1) { if (context->cloths[i] != NULL) diff --git a/src/Silkworm.h b/src/Silkworm.h index 9ba3b8e..74dc77d 100644 --- a/src/Silkworm.h +++ b/src/Silkworm.h @@ -77,6 +77,7 @@ SILKWORMAPI double Silkworm_ClothFillTriangleBuffer(double clothId, double leftU SILKWORMAPI void Silkworm_DestroyNode(double nodeId); +SILKWORMAPI void Silkworm_PushNodesInRadius(double x, double y, double radius, double xDirection, double yDirection); SILKWORMAPI void Silkworm_DestroyNodesInRadius(double x, double y, double radius); SILKWORMAPI void Silkworm_ClearAll();