Skip to content

Commit

Permalink
character: update
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansarihan committed Nov 9, 2023
1 parent e54bd8c commit 0ebf19f
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 65 deletions.
32 changes: 19 additions & 13 deletions src/character/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Character::Character(RenderManager *renderManager, TaskManager *taskManager, Res
void Character::init()
{
// Animation
m_model = m_resourceManager->getModel("assets/gltf/swat5.glb");
m_model = m_resourceManager->getModel("assets/character/mixamo-reaction.glb");
Animation *animation0 = new Animation("idle", m_model);
Animation *animation1 = new Animation("walking-forward", m_model);
Animation *animation2 = new Animation("left", m_model, true);
Expand Down Expand Up @@ -197,19 +197,24 @@ void Character::init()
m_blendTargets[i] = 0.0f;

// Character
m_rigidbody = m_physicsWorld->createCapsule(10.0f, 1.0f, 0.5f, 2.0f, BulletGLM::getBulletVec3(m_position));
m_rigidbody = m_physicsWorld->createCapsule(10.0f, 1.0f, 0.25f, 1.3f, BulletGLM::getBulletVec3(m_position));
m_rigidbody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
m_rigidbody->setDamping(0.9f, 0.9f);
m_rigidbody->setFriction(0.0f);
m_rigidbody->setGravity(btVector3(0, -20.0f, 0));

m_controller = new CharacterController(m_physicsWorld->m_dynamicsWorld, m_rigidbody, m_followCamera);
m_ragdoll = new Ragdoll(m_physicsWorld, animationRagdoll, BulletGLM::getBulletVec3(m_position), 2.0f);
m_ragdoll = new Ragdoll(m_physicsWorld, animationRagdoll, BulletGLM::getBulletVec3(m_position), 1.0f);
m_ragdoll->m_modelOffset = glm::vec3(0.f, -1.0f, 0.f);

eTransform transform;
transform.setScale(glm::vec3(2.f, 2.f, 2.f));
// TODO: blender gltf exporter bug - when root armature is directly parented to skinned objects
eTransform offset;
offset.setScale(glm::vec3(100.f));
offset.setRotation(glm::quat(0.707f, -0.707f, 0.f, 0.f));
m_renderSource = RenderSourceBuilder()
.setTransform(transform)
.setOffset(offset)
.setModel(m_model)
.setAnimator(m_animator)
.build();
Expand Down Expand Up @@ -241,11 +246,6 @@ void Character::update(float deltaTime)
// update animation
m_animator->update(deltaTime);

// update ragdoll blend
AnimPose &ragdolPose = getRagdolPose();
ragdolPose.blendFactor += deltaTime * m_stateChangeSpeed * (m_ragdollActive ? 1.f : -1.f);
ragdolPose.blendFactor = std::max(0.0f, std::min(ragdolPose.blendFactor, 1.0f));

// update pistol-aim blend
m_aimBlend += deltaTime * m_aimStateChangeSpeed * (m_controller->m_aimLocked ? 1.f : -1.f);
m_aimBlend = std::max(0.0f, std::min(m_aimBlend, 1.0f));
Expand Down Expand Up @@ -416,6 +416,13 @@ void Character::update(float deltaTime)
}

updateModelMatrix();

// NOTE: should after updateModelMatrix
// update ragdoll blend
AnimPose &ragdolPose = getRagdolPose();
ragdolPose.blendFactor = (m_ragdollActive ? 1.f : -1.f);
// ragdolPose.blendFactor += deltaTime * m_stateChangeSpeed * (m_ragdollActive ? 1.f : -1.f);
ragdolPose.blendFactor = std::max(0.0f, std::min(ragdolPose.blendFactor, 1.0f));
}

void Character::updateModelMatrix()
Expand Down Expand Up @@ -444,9 +451,8 @@ void Character::updateModelMatrix()

m_modelMatrix = model;

m_renderSource->setTransform(CommonUtil::positionFromModel(model),
glm::quat_cast(glm::scale(model, glm::vec3(1.f / m_scale))),
glm::vec3(m_scale));
m_renderSource->transform.setModelMatrix(model);
m_renderSource->updateModelMatrix();
}

void Character::interpolateBlendTargets()
Expand Down Expand Up @@ -569,7 +575,7 @@ void Character::activateRagdoll()
btVector3 modelPos = BulletGLM::getBulletVec3(m_position);
m_ragdoll->resetTransforms(modelPos, m_rotation.y);
m_ragdoll->unFreezeBodies();
m_ragdoll->changeState(RagdollState::fetal);
m_ragdoll->changeState(RagdollState::loose);
m_ragdollActive = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/character/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Character : public Updatable
glm::mat4 m_modelMatrix;
glm::vec3 m_position = glm::vec3(200.f, 5.f, 250.f);
glm::vec3 m_rotation = glm::vec3(0.0f, 0.0f, 0.0f);
float m_scale = 2.0;
float m_scale = 1.0;

// animation
float m_blendTargets[13];
Expand Down
15 changes: 13 additions & 2 deletions src/character/playable_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
PCharacter::PCharacter(ShaderManager *shaderManager, RenderManager *renderManager, TaskManager *taskManager, SoundEngine *soundEngine, GLFWwindow *window, ResourceManager *resourceManager, PhysicsWorld *physicsWorld, Camera *followCamera)
: Character(renderManager, taskManager, resourceManager, physicsWorld, followCamera),
m_soundEngine(soundEngine),
m_window(window)
m_window(window),
m_followOffsetAim(glm::vec3(-0.4f, 1.6f, -1.f)),
m_followOffsetNormal(glm::vec3(0.0f, 1.8f, -5.f)),
m_followOffset(glm::vec3(0.f)),
m_followOffsetTarget(glm::vec3(0.f)),
m_followOffsetFactor(0.1f)
{
try
{
Expand Down Expand Up @@ -153,6 +158,11 @@ void PCharacter::update(float deltaTime)
resetRagdoll();
}

if (glfwGetKey(m_window, GLFW_KEY_T) == GLFW_PRESS)
{
activateRagdoll();
}

if (glfwGetKey(m_window, GLFW_KEY_E) == GLFW_PRESS)
{
float now = (float)glfwGetTime();
Expand Down Expand Up @@ -200,7 +210,8 @@ void PCharacter::updatePistolModelMatrix()
model2 = glm::scale(model2, glm::vec3(m_pistolScale));

m_pistolModelMatrix = model2;
m_pistolSource->setModelMatrix(m_pistolModelMatrix);
m_pistolSource->transform.setModelMatrix(m_pistolModelMatrix);
m_pistolSource->updateModelMatrix();

// muzzle
model3 = glm::translate(model3, m_pistolOffset + m_muzzleOffset);
Expand Down
10 changes: 5 additions & 5 deletions src/character/playable_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class PCharacter : public Character

bool m_firstPerson = false;

glm::vec3 m_followOffsetAim = glm::vec3(-1.2f, 3.4f, -2.3f);
glm::vec3 m_followOffsetNormal = glm::vec3(0.0f, 3.f, -8.f);
glm::vec3 m_followOffset = glm::vec3(0.0f, 3.f, -8.f);
glm::vec3 m_followOffsetTarget = glm::vec3(0.0f, 3.f, -8.f);
float m_followOffsetFactor = 0.1f;
glm::vec3 m_followOffsetAim;
glm::vec3 m_followOffsetNormal;
glm::vec3 m_followOffset;
glm::vec3 m_followOffsetTarget;
float m_followOffsetFactor;

glm::mat4 m_pistolModelMatrix = glm::mat4(1.f);
// TODO: bound to pistol model
Expand Down
4 changes: 2 additions & 2 deletions src/character_controller/character_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ CharacterController::CharacterController(btDiscreteDynamicsWorld *dynamicsWorld,
btCapsuleShape *shape = (btCapsuleShape *)m_rigidBody->getCollisionShape();
m_halfHeight = shape->getHalfHeight() + shape->getRadius();

m_walkSpeed.m_constantValue = 3.f;
m_walkSpeed.m_constantValue = 1.5f;
m_walkSpeed.m_points.push_back(LimiterPoint(M_PI, 0.7f, 1.f)); // back
m_walkSpeed.m_points.push_back(LimiterPoint(2.0f, 1.5f, 0.1f)); // right-back spot
m_walkSpeed.m_points.push_back(LimiterPoint(4.3f, 1.5f, 0.1f)); // left-back spot

m_runSpeed.m_constantValue = 10.f;
m_runSpeed.m_constantValue = 5.f;
m_runSpeed.m_points.push_back(LimiterPoint(M_PI, 4.f, 1.f));
m_runSpeed.m_points.push_back(LimiterPoint(2.0f, 4.f, 0.1f));
m_runSpeed.m_points.push_back(LimiterPoint(4.3f, 4.f, 0.1f));
Expand Down
9 changes: 4 additions & 5 deletions src/model/model.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "model.h"

Model::Model(ResourceManager *resourceManager, std::string const &path, std::string const &pathRelative)
Model::Model(ResourceManager *resourceManager, std::string const &path)
: m_resourceManager(resourceManager),
m_path(path),
m_pathRelative(pathRelative)
m_path(path)
{
m_importer = new Assimp::Importer();

Expand Down Expand Up @@ -95,8 +94,8 @@ void Model::loadModel(std::string const &path)

void Model::processNode(aiNode *node, glm::mat4 parentTransform)
{
glm::mat4 meshTransform = AssimpToGLM::getGLMMat4(node->mTransformation);
glm::mat4 transform = parentTransform * meshTransform;
glm::mat4 nodeTransform = AssimpToGLM::getGLMMat4(node->mTransformation);
glm::mat4 transform = parentTransform * nodeTransform;

for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
Expand Down
3 changes: 1 addition & 2 deletions src/model/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct BoneInfo
class Model
{
public:
Model(ResourceManager *resourceManager, std::string const &path, std::string const &pathRelative);
Model(ResourceManager *resourceManager, std::string const &path);
~Model();
void draw(Shader shader, bool drawOpaque = true);
void drawInstanced(Shader shader, int instanceCount);
Expand All @@ -43,7 +43,6 @@ class Model
glm::vec3 aabbMin;
glm::vec3 aabbMax;
std::string m_path;
std::string m_pathRelative;
std::string directory;
bool gammaCorrection;

Expand Down
2 changes: 1 addition & 1 deletion src/particle_engine/particle_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ float randomFloat(float min, float max);
ParticleEngine::ParticleEngine(ResourceManager *resourceManager, Model *particleCopy, Camera *viewCamera)
: m_viewCamera(viewCamera)
{
m_model = resourceManager->getModel(particleCopy->m_pathRelative, true);
m_model = resourceManager->getModelFullPath(particleCopy->m_path, true);
setupBuffer();
}

Expand Down
7 changes: 6 additions & 1 deletion src/resource_manager/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ ResourceManager::~ResourceManager()
Model *ResourceManager::getModel(const std::string &path, bool isCopy)
{
std::string fullPath = m_executablePath + '/' + path;
return getModelFullPath(fullPath, isCopy);
}

Model *ResourceManager::getModelFullPath(const std::string &fullPath, bool isCopy)
{
// TODO: sharing same model object is safe?
if (!isCopy && m_models.find(fullPath) != m_models.end())
{
// std::cout << "ResourceManager: found loaded model: path: " << path << std::endl;
return m_models[fullPath];
}

Model *model = new Model(this, fullPath, path);
Model *model = new Model(this, fullPath);
if (!isCopy)
m_models[fullPath] = model;

Expand Down
1 change: 1 addition & 0 deletions src/resource_manager/resource_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ResourceManager
std::unordered_map<std::string, Texture> m_textures;

Model *getModel(const std::string &path, bool isCopy = false);
Model *getModelFullPath(const std::string &fullPath, bool isCopy = false);
void disposeModel(std::string fullPath);
Texture getTexture(const Model &model, const std::string &path, const std::string &typeName);
void textureFromMemory(Texture &texture, const aiTexture *embeddedTexture);
Expand Down
52 changes: 22 additions & 30 deletions src/ui/character/character_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,34 @@ void CharacterUI::render()
if (!ImGui::CollapsingHeader("Character", ImGuiTreeNodeFlags_NoTreePushOnOpen))
return;

ImGui::DragFloat("m_scale", &m_character->m_scale, 0.1f);
// TODO: fps camera - change camera min pitch
ImGui::Checkbox("m_aimLocked", &m_controller->m_aimLocked);
ImGui::Checkbox("m_controlCharacter", &m_character->m_controlCharacter);
ImGui::Checkbox("m_followCharacter", &m_character->m_followCharacter);
ImGui::Checkbox("m_drawBones", &m_drawBones);
ImGui::Checkbox("m_renderLastEnterPath", &m_renderLastEnterPath);
renderLastEnterCarPath();
if (ImGui::CollapsingHeader("m_followOffsetNormal", ImGuiTreeNodeFlags_NoTreePushOnOpen))
VectorUI::renderVec3("m_followOffsetNormal", m_character->m_followOffsetNormal, 0.1f);
VectorUI::renderVec3("m_followOffsetAim", m_character->m_followOffsetAim, 0.1f);
if (ImGui::TreeNode("Pistol"))
{
ImGui::DragFloat("m_followOffsetNormalX", &m_character->m_followOffsetNormal.x, 0.1f);
ImGui::DragFloat("m_followOffsetNormalY", &m_character->m_followOffsetNormal.y, 0.1f);
ImGui::DragFloat("m_followOffsetNormalZ", &m_character->m_followOffsetNormal.z, 0.1f);
}
if (ImGui::CollapsingHeader("m_followOffsetAim", ImGuiTreeNodeFlags_NoTreePushOnOpen))
{
ImGui::DragFloat("m_followOffsetAimX", &m_character->m_followOffsetAim.x, 0.1f);
ImGui::DragFloat("m_followOffsetAimY", &m_character->m_followOffsetAim.y, 0.1f);
ImGui::DragFloat("m_followOffsetAimZ", &m_character->m_followOffsetAim.z, 0.1f);
}
if (ImGui::CollapsingHeader("Pistol", ImGuiTreeNodeFlags_NoTreePushOnOpen))
{
ImGui::DragFloat("m_pistolOffsetX", &m_character->m_pistolOffset.x, 0.1f);
ImGui::DragFloat("m_pistolOffsetY", &m_character->m_pistolOffset.y, 0.1f);
ImGui::DragFloat("m_pistolOffsetZ", &m_character->m_pistolOffset.z, 0.1f);
VectorUI::renderVec3("m_pistolOffset", m_character->m_pistolOffset, 0.1f);
renderQuaternion("m_pistolOrientation", m_character->m_pistolOrientation);
ImGui::DragFloat("m_boneScale", &m_boneScale, 0.01f);
ImGui::DragFloat("m_pistolScale", &m_character->m_pistolScale, 0.01f);
ImGui::TreePop();
}
if (ImGui::CollapsingHeader("Muzzle", ImGuiTreeNodeFlags_NoTreePushOnOpen))
if (ImGui::TreeNode("Muzzle"))
{
ImGui::DragFloat("m_muzzleOffsetX", &m_character->m_muzzleOffset.x, 0.1f);
ImGui::DragFloat("m_muzzleOffsetY", &m_character->m_muzzleOffset.y, 0.1f);
ImGui::DragFloat("m_muzzleOffsetZ", &m_character->m_muzzleOffset.z, 0.1f);
VectorUI::renderVec3("m_pistolOffset", m_character->m_muzzleOffset, 0.1f);
ImGui::TreePop();
}
ImGui::DragFloat("m_fireLimit", &m_character->m_fireLimit, 0.05f);
ImGui::DragFloat("m_fireAnimStartTime", &m_character->m_fireAnimStartTime, 0.05f);
ImGui::DragInt("m_fireAnimTimeMilli", &m_character->m_fireAnimTimeMilli, 25);
ImGui::DragFloat("m_aimStateChangeSpeed", &m_character->m_aimStateChangeSpeed, 0.1f);
if (ImGui::CollapsingHeader("Footstep", ImGuiTreeNodeFlags_NoTreePushOnOpen))
if (ImGui::TreeNode("Footstep"))
{
ImGui::DragFloat("m_walkStepFreq", &m_character->m_walkStepFreq, 0.1f);
ImGui::DragFloat("m_runStepFreq", &m_character->m_runStepFreq, 0.1f);
Expand All @@ -57,17 +46,16 @@ void CharacterUI::render()
ImGui::Text("m_runPerc: %.3f", m_character->m_runPerc);
ImGui::Text("m_walkAnimSpeed: %.3f", m_character->m_walkAnimSpeed);
ImGui::Text("m_runAnimSpeed: %.3f", m_character->m_runAnimSpeed);
ImGui::TreePop();
}
ImGui::DragFloat("m_followOffsetFactor", &m_character->m_followOffsetFactor, 0.1f);
ImGui::DragFloat("m_leftBlendEdge", &m_character->m_leftBlendEdge, 0.01f);
ImGui::DragFloat("m_rightBlendEdge", &m_character->m_rightBlendEdge, 0.01f);
ImGui::DragFloat("m_leftForward", &m_character->m_leftForward, 0.01f);
ImGui::DragFloat("m_rightForward", &m_character->m_rightForward, 0.01f);
ImGui::Separator();
if (ImGui::CollapsingHeader("m_walkSpeed", ImGuiTreeNodeFlags_NoTreePushOnOpen))
renderSpeedLimiter(m_character->m_controller->m_walkSpeed, "m_walkSpeed");
if (ImGui::CollapsingHeader("m_runSpeed", ImGuiTreeNodeFlags_NoTreePushOnOpen))
renderSpeedLimiter(m_character->m_controller->m_runSpeed, "m_runSpeed");
renderSpeedLimiter(m_character->m_controller->m_walkSpeed, "m_walkSpeed");
renderSpeedLimiter(m_character->m_controller->m_runSpeed, "m_runSpeed");
ImGui::Text("m_lookDir: (%.1f, %.1f)", m_controller->m_lookDir.x, m_controller->m_lookDir.z);
ImGui::Text("m_moving: %d", m_controller->m_moving);
ImGui::Text("m_jumping: %d", m_controller->m_jumping);
Expand Down Expand Up @@ -153,11 +141,15 @@ void CharacterUI::render()

void CharacterUI::renderSpeedLimiter(SpeedLimiter &speedLimiter, std::string name)
{
if (!ImGui::TreeNode((name + "##CharacterUI::renderSpeedLimiter").c_str()))
return;

const int numPoints = 100;
float values[numPoints];
const float rangeMin = -M_PI;
const float rangeMax = M_PI;

ImGui::DragFloat((name + "::m_constantValue").c_str(), &speedLimiter.m_constantValue, 0.01f);
for (int i = 0; i < numPoints; ++i)
{
float t = static_cast<float>(i) / (numPoints - 1);
Expand All @@ -181,13 +173,13 @@ void CharacterUI::renderSpeedLimiter(SpeedLimiter &speedLimiter, std::string nam

void CharacterUI::renderLastEnterCarPath()
{
if (!ImGui::TreeNode("Last Enter Path##CharacterUI::renderLastEnterCarPath"))
return;

if (!m_renderLastEnterPath || m_character->m_lastCarEnterPath.empty)
return;

ImGui::SetNextWindowSize(ImVec2(400, 400));
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(200, 200, 200, 255));
ImGui::Begin("m_lastCarEnterPath");
ImVec2 windowSize = ImGui::GetWindowSize();
ImVec2 windowSize = ImVec2(400.f, 400.f);
PathResult &path = m_character->m_lastCarEnterPath;
int mapSize = path.dim.x();
float tileSize = windowSize.x / mapSize;
Expand Down Expand Up @@ -243,7 +235,7 @@ void CharacterUI::renderLastEnterCarPath()
ImVec2 center = ImVec2(startPos.x + pos.x * tileSize, startPos.y + pos.y * tileSize);
drawList->AddCircleFilled(center, 6.f, IM_COL32(120, 120, 120, 255));
}
ImGui::End();
ImGui::TreePop();
ImGui::PopStyleColor();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ui/character/character_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class CharacterUI : public BaseUI
btRigidBody *m_rb;

public:
CharacterUI(PCharacter *character, CharacterController *controller, btRigidBody *rb)
CharacterUI(PCharacter *character)
: m_character(character),
m_controller(controller),
m_rb(rb)
m_controller(character->m_controller),
m_rb(character->m_rigidbody)

{
m_bones.push_back("mixamorig:Hips");
Expand Down

0 comments on commit 0ebf19f

Please sign in to comment.