183 lines · 4.6 KB · C++
RawDownload
////  DetailedMotionState.cpp//  interface/src/avatar/////  Created by Luis Cuenca 1/11/2019//  Copyright 2019 High Fidelity, Inc.////  Distributed under the Apache License, Version 2.0.//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html// #include "DetailedMotionState.h" #include <PhysicsCollisionGroups.h>#include <PhysicsEngine.h>#include <PhysicsHelpers.h>#include "MyAvatar.h"  DetailedMotionState::DetailedMotionState(AvatarPointer avatar, const btCollisionShape* shape, int32_t jointIndex) :     ObjectMotionState(shape), _avatar(avatar), _jointIndex(jointIndex) {    assert(_avatar);    if (!_avatar->isMyAvatar()) {        _otherAvatar = std::static_pointer_cast<OtherAvatar>(_avatar);    }    _type = MOTIONSTATE_TYPE_DETAILED;} void DetailedMotionState::handleEasyChanges(uint32_t& flags) {    ObjectMotionState::handleEasyChanges(flags);    if (flags & Simulation::DIRTY_PHYSICS_ACTIVATION && !_body->isActive()) {        _body->activate();    }     if (flags & Simulation::DIRTY_MASS) {        updateBodyMassProperties();    }} DetailedMotionState::~DetailedMotionState() {    assert(_avatar);    _avatar = nullptr;} // virtualuint32_t DetailedMotionState::getIncomingDirtyFlags() const {    return _body ? _dirtyFlags : 0;} void DetailedMotionState::clearIncomingDirtyFlags(uint32_t mask) {    if (_body) {        _dirtyFlags &= ~mask;    }} PhysicsMotionType DetailedMotionState::computePhysicsMotionType() const {    return MOTION_TYPE_KINEMATIC;} // virtualbool DetailedMotionState::isMoving() const {    return false;} // virtualvoid DetailedMotionState::getWorldTransform(btTransform& worldTrans) const {    worldTrans.setOrigin(glmToBullet(getObjectPosition()));    worldTrans.setRotation(glmToBullet(getObjectRotation()));} // virtualvoid DetailedMotionState::setWorldTransform(const btTransform& worldTrans) {    _body->setWorldTransform(worldTrans);} // These pure virtual methods must be implemented for each MotionState type// and make it possible to implement more complicated methods in this base class. // virtualfloat DetailedMotionState::getObjectRestitution() const {    return 0.5f;} // virtualfloat DetailedMotionState::getObjectFriction() const {    return 0.5f;} // virtualfloat DetailedMotionState::getObjectLinearDamping() const {    return 0.5f;} // virtualfloat DetailedMotionState::getObjectAngularDamping() const {    return 0.5f;} // virtualglm::vec3 DetailedMotionState::getObjectPosition() const {    if (_otherAvatar != nullptr) {        auto bodyLOD = _otherAvatar->getBodyLOD();        if (bodyLOD == OtherAvatar::BodyLOD::Sphere) {            return _avatar->getFitBounds().calcCenter();        }    }    return  _avatar->getJointPosition(_jointIndex);} // virtualglm::quat DetailedMotionState::getObjectRotation() const {    return _avatar->getWorldOrientation() * _avatar->getAbsoluteJointRotationInObjectFrame(_jointIndex);} // virtualglm::vec3 DetailedMotionState::getObjectLinearVelocity() const {    return glm::vec3(0.0f);} // virtualglm::vec3 DetailedMotionState::getObjectAngularVelocity() const {    return glm::vec3(0.0f);} // virtualglm::vec3 DetailedMotionState::getObjectGravity() const {    return glm::vec3(0.0f);} // virtualconst QUuid DetailedMotionState::getObjectID() const {    return _avatar->getSessionUUID();} QString DetailedMotionState::getName() const {    return _avatar->getName() + "_" + _jointIndex;} // virtualQUuid DetailedMotionState::getSimulatorID() const {    return _avatar->getSessionUUID();} // virtualvoid DetailedMotionState::computeCollisionGroupAndMask(int32_t& group, int32_t& mask) const {    group = BULLET_COLLISION_GROUP_DETAILED_AVATAR;    mask = Physics::getDefaultCollisionMask(group);} // virtualfloat DetailedMotionState::getMass() const {    return 0.0f;} void DetailedMotionState::setRigidBody(btRigidBody* body) {    ObjectMotionState::setRigidBody(body);    if (_body) {        // remove angular dynamics from this body        _body->setAngularFactor(0.0f);    }} void DetailedMotionState::setShape(const btCollisionShape* shape) {    if (_shape != shape) {        if (_shape) {            getShapeManager()->releaseShape(_shape);        }        _shape = shape;        if (_body) {            assert(_shape);            _body->setCollisionShape(const_cast<btCollisionShape*>(_shape));        }    } else if (shape) {        // we need to release unused reference to shape        getShapeManager()->releaseShape(shape);    }} void DetailedMotionState::forceActive() {    if (_body && !_body->isActive()) {        _body->setActivationState(ACTIVE_TAG);    }}