将扔手雷与人绑定

main
wuyize 2023-04-20 12:21:27 +08:00
parent 47ba586987
commit 20e56d07ee
3 changed files with 27 additions and 11 deletions

View File

@ -25,11 +25,13 @@ DemoWorld::DemoWorld()
shoot->setScale(glm::vec3(45)); shoot->setScale(glm::vec3(45));
throwPerson = std::make_shared<Actor>("Models\\grenade_throw\\scene.gltf"); throwPerson = std::make_shared<Actor>("Models\\grenade_throw\\scene.gltf");
throwPerson->setPosition({ -5,0,-1 }); throwPerson->setPosition({ -7,0,-1 });
throwPerson->setRotaion(glm::angleAxis(glm::radians(90.f), glm::vec3(0, 1, 0)));
throwPerson->setScale(glm::vec3{ 45 }); throwPerson->setScale(glm::vec3{ 45 });
mk2 = std::make_shared<Particle>(10, "Models\\mk2-frag-grenade\\mk2-frag-grenade.gltf"); mk2 = std::make_shared<Particle>(10, "Models\\mk2-frag-grenade\\mk2-frag-grenade.gltf");
mk2->enableGravity(); mk2->setScale(glm::vec3(0.1));
mk2->setPosition({ 0,-100,0 });
addActor(actor); addActor(actor);
addActor(actor2); addActor(actor2);
@ -56,7 +58,7 @@ DemoWorld::DemoWorld()
} }
else else
{ {
particle->enableGravity(); particle->setEnableGravity(true);
} }
particles.push_back(particle); particles.push_back(particle);
addActor(particle); addActor(particle);
@ -88,12 +90,11 @@ void DemoWorld::logicalTick(float deltaTime)
particles[i]->setRotaion(glm::rotation(glm::vec3(0, 0, 1), d)); particles[i]->setRotaion(glm::rotation(glm::vec3(0, 0, 1), d));
} }
if (throwPerson->getAnimatorCurrentTime() - 3.f < 1e-3f) if (abs(throwPerson->getAnimatorCurrentTime() - 3.41f) < 1e-2f)
{ {
mk2->setPosition({ -5.5,2,-1 }); mk2->setPosition(throwPerson->getPosition() + glm::vec3(-0.5, 2.2, 0));
mk2->setScale(glm::vec3(0.1)); mk2->setSpeed(glm::mat3_cast(throwPerson->getRotation() * glm::rotation(glm::vec3(1, 0, 0), glm::normalize(glm::vec3(0, 1, 1)/*方向*/))) * glm::vec3(15/*速度*/, 0, 0));
mk2->setSpeed({ 0,0,2 }); mk2->setEnableGravity(true);
} }
} }

View File

@ -113,7 +113,21 @@ void Particle::setFixed(bool fixed)
this->fixed = fixed; this->fixed = fixed;
} }
void Particle::enableGravity() void Particle::setEnableGravity(bool enable)
{ {
addForce(std::make_shared<Force>(glm::vec3(0.f, -getMass() * 9.80665f, 0.f))); if (enable)
{
if (!gravity)
{
gravity = std::make_shared<Force>(glm::vec3(0.f, -getMass() * 9.80665f, 0.f));
addForce(gravity);
}
}
else
{
if (gravity)
{
removeForce(gravity);
}
}
} }

View File

@ -22,7 +22,7 @@ public:
glm::vec3 getSpeed(); glm::vec3 getSpeed();
void setSpeed(const glm::vec3& speed); void setSpeed(const glm::vec3& speed);
void setFixed(bool fixed); void setFixed(bool fixed);
void enableGravity(); void setEnableGravity(bool enable);
private: private:
bool fixed = false; bool fixed = false;
float mass; float mass;
@ -31,5 +31,6 @@ private:
std::set<std::shared_ptr<Force>> forces; std::set<std::shared_ptr<Force>> forces;
glm::vec3 resultantForce = glm::vec3(0); glm::vec3 resultantForce = glm::vec3(0);
Shader debugShader; Shader debugShader;
std::shared_ptr<Force> gravity;
}; };