void Quaternion::setAxis(const Direction3D& axis) { Direction3D axis_ = axis.normalize(); float sha = std::sin(std::acos(w)); x = axis_.x * sha; y = axis_.y * sha; z = axis_.z * sha; }
void Quaternion::setAxisAndAngle(const Direction3D& axis, float angle) { Direction3D axis_ = axis.normalize(); float sha = std::sin(angle / 2.0f); x = axis_.x * sha; y = axis_.y * sha; z = axis_.z * sha; w = std::cos(angle / 2.0f); }
//------------------------------------------------------------------------------------- bool MoveToPointHandler::update() { if(pController_ == NULL) { delete this; return false; } Entity* pEntity = pController_->pEntity(); const Position3D& dstPos = destPos(); Position3D currpos = pEntity->getPosition(); Direction3D direction = pEntity->getDirection(); Vector3 movement = dstPos - currpos; if (!moveVertically_) movement.y = 0.f; bool ret = true; if(KBEVec3Length(&movement) < velocity_ + range_) { float y = currpos.y; currpos = dstPos; if(range_ > 0.0f) { // 单位化向量 KBEVec3Normalize(&movement, &movement); movement *= range_; currpos -= movement; } if (!moveVertically_) currpos.y = y; ret = false; } else { // 单位化向量 KBEVec3Normalize(&movement, &movement); // 移动位置 movement *= velocity_; currpos += movement; } // 是否需要改变面向 if (faceMovement_ && (movement.x != 0.f || movement.z != 0.f)) direction.yaw(movement.yaw()); // 设置entity的新位置和面向 if(pController_) pEntity->setPositionAndDirection(currpos, direction); // 非navigate都不能确定其在地面上 if(pController_) pEntity->isOnGround(isOnGround()); // 通知脚本 if(pController_) pEntity->onMove(pController_->id(), pyuserarg_); // 如果达到目的地则返回true if(!ret) { return !requestMoveOver(); } return true; }
//------------------------------------------------------------------------------------- bool MoveToPointHandler::update(TimerHandle& handle) { if(pEntity_ == NULL) { handle.cancel(); return false; } Entity* pEntity = pEntity_; const Position3D& dstPos = destPos(); Position3D currpos = pEntity->position(); Position3D currpos_backup = currpos; Direction3D direction = pEntity->direction(); Vector3 movement = dstPos - currpos; if (!moveVertically_) movement.y = 0.f; bool ret = true; if(KBEVec3Length(&movement) < velocity_ + distance_) { float y = currpos.y; currpos = dstPos; if(distance_ > 0.0f) { // 单位化向量 KBEVec3Normalize(&movement, &movement); movement *= distance_; currpos -= movement; } if (!moveVertically_) currpos.y = y; ret = false; } else { // 单位化向量 KBEVec3Normalize(&movement, &movement); // 移动位置 movement *= velocity_; currpos += movement; } // 是否需要改变面向 if (faceMovement_ && (movement.x != 0.f || movement.z != 0.f)) direction.yaw(movement.yaw()); // 设置entity的新位置和面向 pEntity_->position(currpos); pEntity_->direction(direction); // 非navigate都不能确定其在地面上 pEntity_->isOnGound(false); // 通知脚本 pEntity->onMove(scriptCallbacks_.getIDForHandle(handle), layer_, currpos_backup, pyuserarg_); // 如果达到目的地则返回true if(!ret) { return !requestMoveOver(handle, currpos_backup); } return true; }