Added ai generated animation

This commit is contained in:
Vladislav Khorev committed 2026-04-01 19:07:21 +03:00
1 parent 7840bfdb4e
commit a96d0b207a
5 files changed
+464792 -10

No files matched your search

+2 -2
View File
@@ -33,8 +33,8 @@ Eigen::Vector2f Environment::tapDownCurrentPos = { 0, 0 };
ClientState Environment::shipState;
const float Environment::CONST_Z_NEAR = 5.f;
const float Environment::CONST_Z_FAR = 5000.f;
const float Environment::CONST_Z_NEAR = 0.1f;
const float Environment::CONST_Z_FAR = 100.f;
float Environment::projectionWidth = 1280.0f;
float Environment::projectionHeight = 720.0f;
+113 -8
View File
@@ -173,6 +173,8 @@ namespace ZL
violaIdleModel.LoadFromFile("resources/idleviola_uv010.txt", CONST_ZIP_FILE);
violaWalkModel.LoadFromFile("resources/walkviola_uv010.txt", CONST_ZIP_FILE);
zombieModel.LoadFromFile("resources/w/zombie002.txt", CONST_ZIP_FILE);
violaTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/viola.png", CONST_ZIP_FILE));
@@ -211,7 +213,9 @@ namespace ZL
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
//renderer.TranslateMatrix({ 0, -6.f, 0 });
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI * 30 / 180.f, Eigen::Vector3f::UnitX())).toRotationMatrix());
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraInclination, Eigen::Vector3f::UnitX())).toRotationMatrix());
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraAzimuth, Eigen::Vector3f::UnitY())).toRotationMatrix());
renderer.TranslateMatrix({ -violaPosition.x(), -violaPosition.y(), -violaPosition.z() });
glBindTexture(GL_TEXTURE_2D, roomTexture->getTexID());
renderer.DrawVertexRenderStruct(roomMesh);
@@ -224,10 +228,20 @@ namespace ZL
renderer.DrawVertexRenderStruct(benchMesh);
renderer.PushMatrix();
renderer.TranslateMatrix({ 0,0,-15 });
renderer.ScaleMatrix(0.01);
zombieModelMutable.AssignFrom(zombieModel.mesh);
zombieModelMutable.RefreshVBO();
glBindTexture(GL_TEXTURE_2D, violaTexture->getTexID());
renderer.DrawVertexRenderStruct(zombieModelMutable);
renderer.PopMatrix();
renderer.PushMatrix();
renderer.ScaleMatrix(0.2);
renderer.TranslateMatrix({ violaPosition.x(), violaPosition.y(), violaPosition.z() });
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(-violaFacingAngle, Eigen::Vector3f::UnitY())).toRotationMatrix());
renderer.ScaleMatrix(0.12);
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(-M_PI * 0.5f, Eigen::Vector3f::UnitX())).toRotationMatrix());
renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitZ())).toRotationMatrix());
@@ -327,6 +341,36 @@ namespace ZL
lastTickCount = newTickCount;
// Move Viola toward target
Eigen::Vector3f toTarget = violaTarget - violaPosition;
toTarget.y() = 0.f;
float dist = toTarget.norm();
if (dist > VIOLA_WALK_THRESHOLD) {
Eigen::Vector3f dir = toTarget / dist;
float moveAmount = VIOLA_WALK_SPEED * delta / 1000.f;
if (moveAmount >= dist) {
violaPosition = violaTarget;
violaPosition.y() = 0.f;
} else {
violaPosition += dir * moveAmount;
}
violaTargetFacingAngle = atan2(dir.x(), -dir.z());
violaCurrentAnimation = 1;
} else {
violaCurrentAnimation = 0;
}
// Rotate Viola toward target facing angle at constant angular speed
float angleDiff = violaTargetFacingAngle - violaFacingAngle;
while (angleDiff > M_PI) angleDiff -= 2.f * static_cast<float>(M_PI);
while (angleDiff < -M_PI) angleDiff += 2.f * static_cast<float>(M_PI);
float rotStep = VIOLA_ROTATION_SPEED * delta / 1000.f;
if (std::fabs(angleDiff) <= rotStep) {
violaFacingAngle = violaTargetFacingAngle;
} else {
violaFacingAngle += (angleDiff > 0.f ? rotStep : -rotStep);
}
if (violaCurrentAnimation == 0) {
violaCurrentIdleFrame += delta / 24.f;
@@ -354,6 +398,17 @@ namespace ZL
}
}
zombieCurrentIdleFrame += delta / 24.f;
while (zombieCurrentIdleFrame >= 90) {
zombieCurrentIdleFrame -= 90;
}
if (int(zombieCurrentIdleFrame) != zombieLastIdleFrame) {
zombieModel.Interpolate(int(zombieCurrentIdleFrame));
zombieLastIdleFrame = int(zombieCurrentIdleFrame);
}
}
@@ -435,18 +490,68 @@ namespace ZL
#endif
if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
// Преобразуем экранные пиксели в проекционные единицы
int mx = static_cast<int>((float)event.button.x / Environment::width * Environment::projectionWidth);
int my = static_cast<int>((float)event.button.y / Environment::height * Environment::projectionHeight);
if (event.button.button == SDL_BUTTON_LEFT) {
// Преобразуем экранные пиксели в проекционные единицы
int mx = static_cast<int>((float)event.button.x / Environment::width * Environment::projectionWidth);
int my = static_cast<int>((float)event.button.y / Environment::height * Environment::projectionHeight);
if (event.type == SDL_MOUSEBUTTONDOWN) {
handleDown(ZL::UiManager::MOUSE_FINGER_ID, mx, my);
if (event.type == SDL_MOUSEBUTTONDOWN) handleDown(ZL::UiManager::MOUSE_FINGER_ID, mx, my);
else handleUp(ZL::UiManager::MOUSE_FINGER_ID, mx, my);
//std::cout << "Mouse button " << (event.type == SDL_MOUSEBUTTONDOWN ? "down" : "up") << ": x=" << mx << " y=" << my << std::endl;
// Unproject click to ground plane (y=0) for Viola's walk target
float ndcX = 2.0f * event.button.x / Environment::width - 1.0f;
float ndcY = 1.0f - 2.0f * event.button.y / Environment::height;
float aspect = (float)Environment::width / (float)Environment::height;
float tanHalfFov = tan(CAMERA_FOV_Y * 0.5f);
float cosAzim = cos(cameraAzimuth), sinAzim = sin(cameraAzimuth);
float cosIncl = cos(cameraInclination), sinIncl = sin(cameraInclination);
Eigen::Vector3f camRight(cosAzim, 0.f, sinAzim);
Eigen::Vector3f camForward(sinAzim * cosIncl, -sinIncl, -cosAzim * cosIncl);
Eigen::Vector3f camUp(sinAzim * sinIncl, cosIncl, -cosAzim * sinIncl);
Eigen::Vector3f camPos = violaPosition + Eigen::Vector3f(-sinAzim * cosIncl, sinIncl, cosAzim * cosIncl) * Environment::zoom;
Eigen::Vector3f rayDir = (camForward + camRight * (ndcX * aspect * tanHalfFov) + camUp * (ndcY * tanHalfFov)).normalized();
if (rayDir.y() < -0.001f) {
float t = -camPos.y() / rayDir.y();
Eigen::Vector3f hit = camPos + rayDir * t;
violaTarget = Eigen::Vector3f(hit.x(), 0.f, hit.z());
}
} else {
handleUp(ZL::UiManager::MOUSE_FINGER_ID, mx, my);
}
}
else if (event.button.button == SDL_BUTTON_RIGHT) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
rightMouseDown = true;
lastMouseX = event.button.x;
lastMouseY = event.button.y;
}
else {
rightMouseDown = false;
}
}
}
else if (event.type == SDL_MOUSEMOTION) {
int mx = static_cast<int>((float)event.motion.x / Environment::width * Environment::projectionWidth);
int my = static_cast<int>((float)event.motion.y / Environment::height * Environment::projectionHeight);
handleMotion(ZL::UiManager::MOUSE_FINGER_ID, mx, my);
if (rightMouseDown) {
int dx = event.motion.x - lastMouseX;
int dy = event.motion.y - lastMouseY;
lastMouseX = event.motion.x;
lastMouseY = event.motion.y;
const float sensitivity = 0.005f;
cameraAzimuth += dx * sensitivity;
cameraInclination += dy * sensitivity;
const float minInclination = M_PI * 30.f / 180.f;
const float maxInclination = M_PI * 0.5f;
cameraInclination = max(minInclination, min(maxInclination, cameraInclination));
}
}
if (event.type == SDL_MOUSEWHEEL) {
+24
View File
@@ -63,8 +63,14 @@ namespace ZL {
BoneSystem violaWalkModel;
VertexRenderStruct violaWalkModelMutable;
BoneSystem zombieModel;
VertexRenderStruct zombieModelMutable;
std::shared_ptr<Texture> violaTexture;
float zombieCurrentIdleFrame = 0;
int zombieLastIdleFrame = 0;
float violaCurrentIdleFrame = 0;
float violaCurrentWalkFrame = 0;
@@ -72,7 +78,25 @@ namespace ZL {
int violaLastWalkFrame = 0;
int violaCurrentAnimation = 0;
float cameraAzimuth = 0.0f;
float cameraInclination = M_PI * 30.f / 180.f;
Eigen::Vector3f violaPosition = Eigen::Vector3f(0.f, 0.f, 0.f);
Eigen::Vector3f violaTarget = Eigen::Vector3f(0.f, 0.f, 0.f);
float violaFacingAngle = 0.0f;
private:
bool rightMouseDown = false;
int lastMouseX = 0;
int lastMouseY = 0;
static constexpr float CAMERA_FOV_Y = 1.0f / 1.5f;
static constexpr float VIOLA_WALK_SPEED = 3.0f;
static constexpr float VIOLA_WALK_THRESHOLD = 0.05f;
static constexpr float VIOLA_ROTATION_SPEED = 8.0f; // radians per second
float violaTargetFacingAngle = 0.0f;
int64_t getSyncTimeMs();
void processTickCount();
void drawScene();