当前位置: 首页>>代码示例>>C++>>正文


C++ PhysicsObject::getVelocity方法代码示例

本文整理汇总了C++中PhysicsObject::getVelocity方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsObject::getVelocity方法的具体用法?C++ PhysicsObject::getVelocity怎么用?C++ PhysicsObject::getVelocity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhysicsObject的用法示例。


在下文中一共展示了PhysicsObject::getVelocity方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: respondToCollisions

void PhysicsSimulator::respondToCollisions(std::vector<PhysicsCollisionTuple> collisions, float delta) {
    for (size_t i = 0; i < collisions.size(); i++) {
        PhysicsObject *first = std::get<0>(collisions[i]);
        PhysicsCollider *firstCollider = std::get<1>(collisions[i]);
        PhysicsObject *second = std::get<2>(collisions[i]);
        PhysicsCollider *secondCollider = std::get<3>(collisions[i]);
        //PhysicsCollisionData data = std::get<4>(collisions[i]);

        // Perfectly Elastic Collision
        if (first->getElasticity() == 1 && second->getElasticity() == 1) {
            bool reflection = false;
            if (firstCollider->getType() == PhysicsColliderTypePlane && ((PhysicsColliderPlane *)firstCollider)->getReflective() == true) {
                reflection = true;
                glm::vec3 normal = ((PhysicsColliderPlane *)firstCollider)->getNormal();
                float magnitude = glm::length(second->getVelocity());
                glm::vec3 initial = second->getVelocity();

                glm::vec3 newVelocity = initial - (2.0f * normal * glm::dot(initial, normal));
                second->setVelocity(glm::normalize(newVelocity) * magnitude);
            }

            if (secondCollider->getType() == PhysicsColliderTypePlane && ((PhysicsColliderPlane *)secondCollider)->getReflective() == true) {
                reflection = true;
                glm::vec3 normal = ((PhysicsColliderPlane *)secondCollider)->getNormal();
                float magnitude = glm::length(first->getVelocity());
                glm::vec3 initial = first->getVelocity();

                glm::vec3 newVelocity = initial - (2.0f * normal * glm::dot(initial, normal));
                first->setVelocity(glm::normalize(newVelocity) * magnitude);
            }

            if (reflection == false) {
                float m1 = first->getMass();
                float m2 = second->getMass();

                // This is the collision normal (line of collision) and the normal of the tangent plane
                glm::vec3 normal = glm::normalize(second->getPosition() - first->getPosition());

                // Get scalar projections onto the normal
                double _v1ns = glm::dot(first->getVelocity(), normal);
                double _v2ns = glm::dot(second->getVelocity(), normal);

                double v1ns = ((1 * m2 * (_v2ns - _v1ns)) / (m1 + m2)) + ((m1 * _v1ns) / (m1 + m2)) + ((m2 * _v2ns) / (m1 + m2));
                double v2ns = ((1 * m1 * (_v1ns - _v2ns)) / (m1 + m2)) + ((m2 * _v2ns) / (m1 + m2)) + ((m1 * _v1ns) / (m1 + m2));

                glm::vec3 v1n = float(v1ns) * normal;
                glm::vec3 v2n = float(v2ns) * normal;

                glm::vec3 v1t = first->getVelocity() - (glm::dot(first->getVelocity(), normal) * normal);
                glm::vec3 v2t = second->getVelocity() - (glm::dot(second->getVelocity(), normal) * normal);

                glm::vec3 v1 = v1n + v1t;
                glm::vec3 v2 = v2n + v2t;

                first->setVelocity(v1);
                second->setVelocity(v2);
            }
        }
    }
}
开发者ID:jonathanbuchanan,项目名称:Sunglasses,代码行数:60,代码来源:PhysicsSimulator.cpp


注:本文中的PhysicsObject::getVelocity方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。