diff --git a/animation-server/src/main.cpp b/animation-server/src/main.cpp index 78386934e4..e04502e77a 100644 --- a/animation-server/src/main.cpp +++ b/animation-server/src/main.cpp @@ -63,10 +63,32 @@ static void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) { } } -const float BUG_VOXEL_SIZE = 0.125f / TREE_SCALE; -glm::vec3 bugPosition = glm::vec3(BUG_VOXEL_SIZE * 10.0, BUG_VOXEL_SIZE * 30.0, BUG_VOXEL_SIZE * 10.0); +glm::vec3 rotatePoint(glm::vec3 point, float angle) +{ + // First, create the quaternion based on this angle of rotation + glm::quat q(glm::vec3(0, -angle, 0)); + + // Next, create a rotation matrix from that quaternion + glm::mat4 rotation = glm::mat4_cast(q); + + // Transform the original vectors by the rotation matrix to get the new vectors + glm::vec4 quatPoint(point.x, point.y, point.z, 0); + glm::vec4 newPoint = quatPoint * rotation; + + return glm::vec3(newPoint.x, newPoint.y, newPoint.z); +} + + +const float BUG_VOXEL_SIZE = 0.0625f / TREE_SCALE; +glm::vec3 bugPosition = glm::vec3(BUG_VOXEL_SIZE * 20.0, BUG_VOXEL_SIZE * 30.0, BUG_VOXEL_SIZE * 20.0); glm::vec3 bugDirection = glm::vec3(0, 0, 1); const int VOXELS_PER_BUG = 18; +glm::vec3 bugPathCenter = glm::vec3(BUG_VOXEL_SIZE * 150.0, BUG_VOXEL_SIZE * 30.0, BUG_VOXEL_SIZE * 150.0); +float bugPathRadius = BUG_VOXEL_SIZE * 140.0; +float bugPathTheta = 0.0 * PI_OVER_180; +float bugRotation = 0.0 * PI_OVER_180; +float bugAngleDelta = 0.2 * PI_OVER_180; +bool moveBugInLine = false; class BugPart { public: @@ -122,10 +144,15 @@ static void renderMovingBug() { // Generate voxels for where bug used to be for (int i = 0; i < VOXELS_PER_BUG; i++) { details[i].s = BUG_VOXEL_SIZE; - details[i].x = bugPosition.x + (bugParts[i].partLocation.x * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1 : 1)); - details[i].y = bugPosition.y + (bugParts[i].partLocation.y * BUG_VOXEL_SIZE * (bugDirection.y < 0 ? -1 : 1)); - details[i].z = bugPosition.z + (bugParts[i].partLocation.z * BUG_VOXEL_SIZE * (bugDirection.z < 0 ? -1 : 1)); + + glm::vec3 partAt = bugParts[i].partLocation * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1.0f : 1.0f); + glm::vec3 rotatedPartAt = rotatePoint(partAt, bugRotation); + glm::vec3 offsetPartAt = rotatedPartAt + bugPosition; + details[i].x = offsetPartAt.x; + details[i].y = offsetPartAt.y; + details[i].z = offsetPartAt.z; + details[i].red = bugParts[i].partColor[0]; details[i].green = bugParts[i].partColor[1]; details[i].blue = bugParts[i].partColor[2]; @@ -146,17 +173,40 @@ static void renderMovingBug() { } // Move the bug... - bugPosition.x += (bugDirection.x * BUG_VOXEL_SIZE); - bugPosition.y += (bugDirection.y * BUG_VOXEL_SIZE); - bugPosition.z += (bugDirection.z * BUG_VOXEL_SIZE); + if (moveBugInLine) { + bugPosition.x += (bugDirection.x * BUG_VOXEL_SIZE); + bugPosition.y += (bugDirection.y * BUG_VOXEL_SIZE); + bugPosition.z += (bugDirection.z * BUG_VOXEL_SIZE); - // Check boundaries - if (bugPosition.z > 1.0) { - bugDirection.z = -1; - } - if (bugPosition.z < BUG_VOXEL_SIZE) { - bugDirection.z = 1; + // Check boundaries + if (bugPosition.z > 1.0) { + bugDirection.z = -1; + } + if (bugPosition.z < BUG_VOXEL_SIZE) { + bugDirection.z = 1; + } + } else { + + //printf("bugPathCenter=(%f,%f,%f)\n", bugPathCenter.x, bugPathCenter.y, bugPathCenter.z); + + bugPathTheta += bugAngleDelta; // move slightly + bugRotation -= bugAngleDelta; // rotate slightly + + // If we loop past end of circle, just reset back into normal range + if (bugPathTheta > (360.0f * PI_OVER_180)) { + bugPathTheta = 0; + bugRotation = 0; + } + + float x = bugPathCenter.x + bugPathRadius * cos(bugPathTheta); + float z = bugPathCenter.z + bugPathRadius * sin(bugPathTheta); + float y = bugPathCenter.y; + + bugPosition = glm::vec3(x, y, z); + //printf("bugPathTheta=%f\n", bugPathTheta); + //printf("bugRotation=%f\n", bugRotation); } + //printf("bugPosition=(%f,%f,%f)\n", bugPosition.x, bugPosition.y, bugPosition.z); //printf("bugDirection=(%f,%f,%f)\n", bugDirection.x, bugDirection.y, bugDirection.z); // would be nice to add some randomness here... @@ -164,10 +214,15 @@ static void renderMovingBug() { // Generate voxels for where bug is going to for (int i = 0; i < VOXELS_PER_BUG; i++) { details[i].s = BUG_VOXEL_SIZE; - details[i].x = bugPosition.x + (bugParts[i].partLocation.x * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1 : 1)); - details[i].y = bugPosition.y + (bugParts[i].partLocation.y * BUG_VOXEL_SIZE * (bugDirection.y < 0 ? -1 : 1)); - details[i].z = bugPosition.z + (bugParts[i].partLocation.z * BUG_VOXEL_SIZE * (bugDirection.z < 0 ? -1 : 1)); + + glm::vec3 partAt = bugParts[i].partLocation * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1.0f : 1.0f); + glm::vec3 rotatedPartAt = rotatePoint(partAt, bugRotation); + glm::vec3 offsetPartAt = rotatedPartAt + bugPosition; + details[i].x = offsetPartAt.x; + details[i].y = offsetPartAt.y; + details[i].z = offsetPartAt.z; + details[i].red = bugParts[i].partColor[0]; details[i].green = bugParts[i].partColor[1]; details[i].blue = bugParts[i].partColor[2];