diff --git a/src/Silkworm.c b/src/Silkworm.c index 6af7833..280bacb 100644 --- a/src/Silkworm.c +++ b/src/Silkworm.c @@ -283,14 +283,16 @@ void Silkworm_NodeSetDestroyable(double nodeId) LookupNode(nodeId)->destroyable = true; } -void Silkworm_NodePin(double nodeId) +void Silkworm_ClothNodePin(double clothId, double i, double j) { - LookupNode(nodeId)->pinned = true; + Silkworm_Cloth* cloth = LookupCloth(clothId); + context->nodes[cloth->nodeIndices[(uint64_t)i][(uint64_t)j]]->pinned = true; } -void Silkworm_NodeUnpin(double nodeId) +void Silkworm_ClothNodeUnpin(double clothId, double i, double j) { - LookupNode(nodeId)->pinned = false; + Silkworm_Cloth* cloth = LookupCloth(clothId); + context->nodes[cloth->nodeIndices[(uint64_t)i][(uint64_t)j]]->pinned = false; } double Silkworm_CreateLink(double aId, double bId, double distance, double tearThreshold) @@ -339,22 +341,27 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta cloth->verticalNodeCount = (uint32_t) verticalNodeCount; cloth->nodeIndices = malloc(sizeof(uint64_t*) * cloth->horizontalNodeCount); + cloth->id = context->clothCount; + context->cloths = realloc(context->cloths, sizeof(Silkworm_Cloth*) * (context->clothCount + 1)); + context->cloths[context->clothCount] = cloth; + context->clothCount += 1; + for (i = 0; i < horizontalNodeCount; i += 1) { cloth->nodeIndices[i] = malloc(sizeof(uint64_t) * cloth->verticalNodeCount); for (j = 0; j < verticalNodeCount; j += 1) { - uint64_t id = (uint64_t) Silkworm_CreateNode(xPosition + i * context->clothDensity, yPosition + j * context->clothDensity, mass, friction, 1, 0.5); + uint64_t nodeId = (uint64_t) Silkworm_CreateNode(xPosition + i * context->clothDensity, yPosition + j * context->clothDensity, mass, friction, 1, 0.5); + + cloth->nodeIndices[i][j] = nodeId; if (j == 0) { - Silkworm_NodePin((double)id); + Silkworm_ClothNodePin((double)cloth->id, (double)i, (double)j); } - Silkworm_NodeSetDestroyable((double)id); - - cloth->nodeIndices[i][j] = id; + Silkworm_NodeSetDestroyable((double)nodeId); } } @@ -427,14 +434,6 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta } } - uint64_t id; - id = context->clothCount; - - cloth->id = id; - context->cloths = realloc(context->cloths, sizeof(Silkworm_Cloth*) * (context->clothCount + 1)); - context->cloths[context->clothCount] = cloth; - context->clothCount += 1; - return (double)cloth->id; } diff --git a/src/Silkworm.h b/src/Silkworm.h index 92590bb..391502b 100644 --- a/src/Silkworm.h +++ b/src/Silkworm.h @@ -137,8 +137,8 @@ SILKWORMAPI void Silkworm_Update(double delta); SILKWORMAPI double Silkworm_CreateNode(double xPosition, double yPosition, double mass, double friction, double radius, double pushFactor); SILKWORMAPI void Silkworm_NodeSetDestroyable(double nodeId); -SILKWORMAPI void Silkworm_NodePin(double nodeId); -SILKWORMAPI void Silkworm_NodeUnpin(double nodeId); +SILKWORMAPI void Silkworm_ClothNodePin(double clothId, double i, double j); +SILKWORMAPI void Silkworm_ClothNodeUnpin(double clothId, double i, double j); SILKWORMAPI double Silkworm_CreateLink(double aId, double bId, double distance, double tearThreshold);