node pushing

main
cosmonaut 2021-09-24 15:17:35 -07:00
parent 0be631e12d
commit eee31fa5e9
2 changed files with 43 additions and 16 deletions

View File

@ -352,7 +352,7 @@ void Silkworm_Init()
context->gravity = 200; context->gravity = 200;
context->xBound = 1000; context->xBound = 1000;
context->yBound = 1000; context->yBound = 1000;
context->clothDensity = 2; context->clothDensity = 4;
} }
static inline Silkworm_Node* LookupNode(uint64_t nodeId) static inline Silkworm_Node* LookupNode(uint64_t nodeId)
@ -582,7 +582,7 @@ void Silkworm_Update(double deltaTime)
float translateX = diffX * 0.5f * difference; float translateX = diffX * 0.5f * difference;
float translateY = diffY * 0.5f * difference; float translateY = diffY * 0.5f * difference;
float distanceMoved = (float)sqrt(translateX * translateX + translateY * translateY); float distanceMoved = (float)sqrt(translateX * translateX + translateY * translateY);
if (distanceMoved > link->tearThreshold) 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]; id = context->linkIndexStack[context->linkIndexStackCount - 1];
context->linkIndexStackCount -= 1; context->linkIndexStackCount -= 1;
} }
else else
{ {
id = context->linkCount; id = context->linkCount;
@ -762,15 +762,15 @@ double Silkworm_CreateLink(double aId, double bId, double distance, double tearT
return (double)link->id; 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; int32_t i, j, k, m;
Silkworm_Cloth* cloth = malloc(sizeof(Silkworm_Cloth)); Silkworm_Cloth* cloth = malloc(sizeof(Silkworm_Cloth));
cloth->windFactor = (float)windFactor; cloth->windFactor = (float)windFactor;
cloth->horizontalNodeCount = (uint32_t) horizontalNodeCount; cloth->horizontalNodeCount = ((uint32_t)width / context->clothDensity) + 1;
cloth->verticalNodeCount = (uint32_t) verticalNodeCount; cloth->verticalNodeCount = ((uint32_t)height / context->clothDensity) + 1;
cloth->nodeIndices = malloc(sizeof(uint64_t*) * cloth->horizontalNodeCount); cloth->nodeIndices = malloc(sizeof(uint64_t*) * cloth->horizontalNodeCount);
uint64_t id; uint64_t id;
@ -805,11 +805,11 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta
cloth->linkHash.buckets[i].count = 0; 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); 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); 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); cloth->triangles = malloc(sizeof(Silkworm_Triangle*) * cloth->horizontalNodeCount * cloth->verticalNodeCount * 2);
uint32_t triangleIndex = 0; 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)); cloth->triangles[triangleIndex] = malloc(sizeof(Silkworm_Triangle));
@ -885,9 +885,9 @@ double Silkworm_CreateCloth(double xPosition, double yPosition, double horizonta
cloth->triangleCount = triangleIndex; 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) if (i - 1 >= 0)
{ {
@ -960,8 +960,8 @@ void Silkworm_ApplyWind(double xSpeed, double ySpeed)
if (node != NULL && !node->pinned) if (node != NULL && !node->pinned)
{ {
node->position.x += (float)xSpeed * 0.05f * cloth->windFactor; node->position.x += (float)xSpeed * 0.025f * cloth->windFactor;
node->position.y += (float)ySpeed * 0.05f * 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; 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) void Silkworm_DestroyNodesInRadius(double x, double y, double radius)
{ {
/* TODO: spatial hash implementation */ /* TODO: spatial hash implementation */
@ -1118,7 +1144,7 @@ void Silkworm_DestroyNodesInRadius(double x, double y, double radius)
void Silkworm_ClearAll() void Silkworm_ClearAll()
{ {
uint32_t i; uint32_t i;
for (i = 0; i < context->clothCount; i += 1) for (i = 0; i < context->clothCount; i += 1)
{ {
if (context->cloths[i] != NULL) if (context->cloths[i] != NULL)

View File

@ -77,6 +77,7 @@ SILKWORMAPI double Silkworm_ClothFillTriangleBuffer(double clothId, double leftU
SILKWORMAPI void Silkworm_DestroyNode(double nodeId); 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_DestroyNodesInRadius(double x, double y, double radius);
SILKWORMAPI void Silkworm_ClearAll(); SILKWORMAPI void Silkworm_ClearAll();