Merge pull request #415 from ZappoMan/voxel_animation

make dragonfly move in circle instead of line
This commit is contained in:
Philip Rosedale 2013-05-24 10:24:47 -07:00
commit 75f8ef736a

View file

@ -63,10 +63,32 @@ static void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) {
} }
} }
const float BUG_VOXEL_SIZE = 0.125f / TREE_SCALE; glm::vec3 rotatePoint(glm::vec3 point, float angle)
glm::vec3 bugPosition = glm::vec3(BUG_VOXEL_SIZE * 10.0, BUG_VOXEL_SIZE * 30.0, BUG_VOXEL_SIZE * 10.0); {
// 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); glm::vec3 bugDirection = glm::vec3(0, 0, 1);
const int VOXELS_PER_BUG = 18; 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 { class BugPart {
public: public:
@ -122,9 +144,14 @@ static void renderMovingBug() {
// Generate voxels for where bug used to be // Generate voxels for where bug used to be
for (int i = 0; i < VOXELS_PER_BUG; i++) { for (int i = 0; i < VOXELS_PER_BUG; i++) {
details[i].s = BUG_VOXEL_SIZE; 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)); glm::vec3 partAt = bugParts[i].partLocation * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1.0f : 1.0f);
details[i].z = bugPosition.z + (bugParts[i].partLocation.z * BUG_VOXEL_SIZE * (bugDirection.z < 0 ? -1 : 1)); 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].red = bugParts[i].partColor[0];
details[i].green = bugParts[i].partColor[1]; details[i].green = bugParts[i].partColor[1];
@ -146,17 +173,40 @@ static void renderMovingBug() {
} }
// Move the bug... // Move the bug...
bugPosition.x += (bugDirection.x * BUG_VOXEL_SIZE); if (moveBugInLine) {
bugPosition.y += (bugDirection.y * BUG_VOXEL_SIZE); bugPosition.x += (bugDirection.x * BUG_VOXEL_SIZE);
bugPosition.z += (bugDirection.z * BUG_VOXEL_SIZE); bugPosition.y += (bugDirection.y * BUG_VOXEL_SIZE);
bugPosition.z += (bugDirection.z * BUG_VOXEL_SIZE);
// Check boundaries // Check boundaries
if (bugPosition.z > 1.0) { if (bugPosition.z > 1.0) {
bugDirection.z = -1; bugDirection.z = -1;
} }
if (bugPosition.z < BUG_VOXEL_SIZE) { if (bugPosition.z < BUG_VOXEL_SIZE) {
bugDirection.z = 1; 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("bugPosition=(%f,%f,%f)\n", bugPosition.x, bugPosition.y, bugPosition.z);
//printf("bugDirection=(%f,%f,%f)\n", bugDirection.x, bugDirection.y, bugDirection.z); //printf("bugDirection=(%f,%f,%f)\n", bugDirection.x, bugDirection.y, bugDirection.z);
// would be nice to add some randomness here... // would be nice to add some randomness here...
@ -164,9 +214,14 @@ static void renderMovingBug() {
// Generate voxels for where bug is going to // Generate voxels for where bug is going to
for (int i = 0; i < VOXELS_PER_BUG; i++) { for (int i = 0; i < VOXELS_PER_BUG; i++) {
details[i].s = BUG_VOXEL_SIZE; 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)); glm::vec3 partAt = bugParts[i].partLocation * BUG_VOXEL_SIZE * (bugDirection.x < 0 ? -1.0f : 1.0f);
details[i].z = bugPosition.z + (bugParts[i].partLocation.z * BUG_VOXEL_SIZE * (bugDirection.z < 0 ? -1 : 1)); 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].red = bugParts[i].partColor[0];
details[i].green = bugParts[i].partColor[1]; details[i].green = bugParts[i].partColor[1];