Beispiel #1
0
void PhysicsComponent::receiveEvent(Event* event)
{
    if (event->getType() == Event::PHYSICS_UPDATE) {
        btTransform transform;

        transform.setIdentity();
        Ogre::Vector3 position = parent->getNode()->_getDerivedPosition();
        Ogre::Quaternion orientation = parent->getNode()->_getDerivedOrientation();

        if (this->getParent()->getGraphicComponent()->isAnimated() && false) {
            Ogre::Bone* bone = parent->getOgreEntity()->getSkeleton()->getBone(0);
            Ogre::Vector3 bone_converted_pos = parent->getNode()
                    ->convertLocalToWorldPosition(bone->_getDerivedPosition());
            Ogre::Quaternion bone_converted_or = parent->getNode()
                    ->convertLocalToWorldOrientation(bone->_getDerivedOrientation());

            Ogre::Vector3 bonePos =
                    parent->getNode()->_getDerivedPosition() +
                    parent->getNode()->_getDerivedOrientation() *
                    bone_converted_pos;

            Ogre::Quaternion boneRot = parent->getNode()->_getDerivedOrientation() * (bone
                    ->getInitialOrientation().Inverse() * bone->_getDerivedOrientation());

            position = bone_converted_pos;
            orientation = boneRot;

        }

        transform.setOrigin(BtOgre::Convert::toBullet(position));
        transform.setRotation(BtOgre::Convert::toBullet(orientation));

        state->setKinematicPos(transform);
    }
}
Beispiel #2
0
    /** See Ogre::ParticleEmitter. */
    void _initParticle(Ogre::Particle *particle)
    {
        Ogre::Vector3 xOff, yOff, zOff;

        // Call superclass
        ParticleEmitter::_initParticle(particle);

        xOff = Ogre::Math::SymmetricRandom() * mXRange;
        yOff = Ogre::Math::SymmetricRandom() * mYRange;
        zOff = Ogre::Math::SymmetricRandom() * mZRange;

#if OGRE_VERSION >= (1 << 16 | 10 << 8 | 0)
        Ogre::Vector3& position = particle->mPosition;
        Ogre::Vector3& direction = particle->mDirection;
        Ogre::ColourValue& colour = particle->mColour;
        Ogre::Real& totalTimeToLive = particle->mTotalTimeToLive;
        Ogre::Real& timeToLive = particle->mTimeToLive;
#else
        Ogre::Vector3& position = particle->position;
        Ogre::Vector3& direction = particle->direction;
        Ogre::ColourValue& colour = particle->colour;
        Ogre::Real& totalTimeToLive = particle->totalTimeToLive;
        Ogre::Real& timeToLive = particle->timeToLive;
#endif

        Ogre::Node* emitterBone = mEmitterBones.at(OEngine::Misc::Rng::rollDice(mEmitterBones.size()));

        position = xOff + yOff + zOff +
                 mParticleBone->_getDerivedOrientation().Inverse() * (emitterBone->_getDerivedPosition()
                - mParticleBone->_getDerivedPosition());

        // Generate complex data by reference
        genEmissionColour(colour);

        // NOTE: We do not use mDirection/mAngle for the initial direction.
        Ogre::Radian hdir = mHorizontalDir + mHorizontalAngle*Ogre::Math::SymmetricRandom();
        Ogre::Radian vdir = mVerticalDir + mVerticalAngle*Ogre::Math::SymmetricRandom();
        direction = (mParticleBone->_getDerivedOrientation().Inverse()
                     * emitterBone->_getDerivedOrientation() *
                                Ogre::Quaternion(hdir, Ogre::Vector3::UNIT_Z) *
                               Ogre::Quaternion(vdir, Ogre::Vector3::UNIT_X)) *
                              Ogre::Vector3::UNIT_Z;

        genEmissionVelocity(direction);

        // Generate simpler data
        timeToLive = totalTimeToLive = genEmissionTTL();
    }
Beispiel #3
0
Transform GfxBody::getBoneWorldTransform (unsigned n)
{
    checkBone(n);
    Ogre::Bone *bone = skeleton->getBone(n);
    Transform t(from_ogre(bone->_getDerivedPosition()), from_ogre(bone->_getDerivedOrientation()), from_ogre(bone->_getDerivedScale()));
    updateWorldTransform();
    return worldTransform * t;
}
	Quaternion CAnimatedEntity::getBoneWorldOrientation(std::string boneName)
	{
		Ogre::Bone* bone = _entity->getSkeleton()->getBone(boneName);

		Ogre::SceneNode *parent = _entityNode; /*node of the entity that has the skeleton*/;
		Ogre::Quaternion parentQuat = _entityNode->_getDerivedOrientation();
		Ogre::Quaternion boneQuat = bone->_getDerivedOrientation();

		Ogre::Quaternion worldQuat = boneQuat * parentQuat; // actually, it might be parentQuat * boneQuat, I forgot

		return worldQuat;
	}
Beispiel #5
0
void VLogicModel::_updateLocatorPos()
{
    for (VLocatorMap::iterator itr = mLocators.begin(); itr != mLocators.end(); ++itr)
    {
        VLocatorValue &tempValue = itr->second;

        // 只更新创建出来的(已经使用的)locator node
        if (tempValue.mLocatorNode != VNULL)
        {
            if (!tempValue.mBoneName.empty())
            {
                Ogre::Bone *bone = VNULL;
                try
                {
                    bone = mSkeleton->getBone(tempValue.mBoneName);
                }
                catch (const Ogre::Exception &e)
                {
                    Ogre::LogManager::getSingleton().logMessage("LogicModel::_updateLocatorPos " + mName + e.getDescription());
                    continue;
                }

                assert(bone != VNULL);

                Ogre::SceneNode *locatorNode = tempValue.mLocatorNode;
                assert(locatorNode != VNULL);

                locatorNode->setPosition(bone->_getDerivedPosition());
                locatorNode->setOrientation(bone->_getDerivedOrientation());

                if (tempValue.mTranslateFirst)
                {
                    // 先平移,再旋转
                    locatorNode->translate(tempValue.mPosition, Ogre::Node::TS_LOCAL);
                    locatorNode->rotate(tempValue.mOrientation, Ogre::Node::TS_LOCAL);
                }
                else
                {
                    // 先旋转,再平移
                    locatorNode->rotate(tempValue.mOrientation, Ogre::Node::TS_LOCAL);
                    locatorNode->translate(tempValue.mPosition, Ogre::Node::TS_LOCAL);
                }
            }
        }
    }

    if (mLocators.size() != 0)
    {
        mModelMainNode->_update(true, false);
    }
}
Beispiel #6
0
static void updateBoneTree(const Ogre::SkeletonInstance *skelsrc, Ogre::Bone *bone)
{
    if(skelsrc->hasBone(bone->getName()))
    {
        Ogre::Bone *srcbone = skelsrc->getBone(bone->getName());
        if(!srcbone->getParent() || !bone->getParent())
        {
            bone->setOrientation(srcbone->getOrientation());
            bone->setPosition(srcbone->getPosition());
            bone->setScale(srcbone->getScale());
        }
        else
        {
            bone->_setDerivedOrientation(srcbone->_getDerivedOrientation());
            bone->_setDerivedPosition(srcbone->_getDerivedPosition());
            bone->setScale(Ogre::Vector3::UNIT_SCALE);
        }
    }

    Ogre::Node::ChildNodeIterator boneiter = bone->getChildIterator();
    while(boneiter.hasMoreElements())
        updateBoneTree(skelsrc, static_cast<Ogre::Bone*>(boneiter.getNext()));
}
Beispiel #7
0
   void Animation::handleShapes(std::vector<Nif::NiTriShapeCopy>* allshapes, Ogre::Entity* creaturemodel, Ogre::SkeletonInstance *skel){
        shapeNumber = 0;

        if (allshapes == NULL || creaturemodel == NULL || skel == NULL)
        {
            return;
        }

        std::vector<Nif::NiTriShapeCopy>::iterator allshapesiter;
	    for(allshapesiter = allshapes->begin(); allshapesiter != allshapes->end(); allshapesiter++)

                {
                    //std::map<unsigned short, PosAndRot> vecPosRot;

			Nif::NiTriShapeCopy& copy = *allshapesiter;
			std::vector<Ogre::Vector3>* allvertices = &copy.vertices;



			//std::set<unsigned int> vertices;
			//std::set<unsigned int> normals;
			//std::vector<Nif::NiSkinData::BoneInfoCopy> boneinfovector =  copy.boneinfo;
            std::map<int, std::vector<Nif::NiSkinData::IndividualWeight> >* verticesToChange = &copy.vertsToWeights;

			//std::cout << "Name " << copy.sname << "\n";
			Ogre::HardwareVertexBufferSharedPtr vbuf = creaturemodel->getMesh()->getSubMesh(copy.sname)->vertexData->vertexBufferBinding->getBuffer(0);
		            Ogre::Real* pReal = static_cast<Ogre::Real*>(vbuf->lock(Ogre::HardwareBuffer::HBL_NORMAL));


				std::vector<Ogre::Vector3> initialVertices = copy.morph.getInitialVertices();
				//Each shape has multiple indices
				if(initialVertices.size() )
				{

					if(copy.vertices.size() == initialVertices.size())
					{
						//Create if it doesn't already exist
						if(shapeIndexI.size() == static_cast<std::size_t> (shapeNumber))
						{
							std::vector<int> vec;
							shapeIndexI.push_back(vec);
						}
                        if(time >= copy.morph.getStartTime() && time <= copy.morph.getStopTime()){
						float x;
						for (unsigned int i = 0; i < copy.morph.getAdditionalVertices().size(); i++){
							int j = 0;
							if(shapeIndexI[shapeNumber].size() <= i)
								shapeIndexI[shapeNumber].push_back(0);


							if(timeIndex(time,copy.morph.getRelevantTimes()[i],(shapeIndexI[shapeNumber])[i], j, x)){
							int indexI = (shapeIndexI[shapeNumber])[i];
							std::vector<Ogre::Vector3> relevantData = (copy.morph.getRelevantData()[i]);
							float v1 = relevantData[indexI].x;
							float v2 = relevantData[j].x;
							float t = v1 + (v2 - v1) * x;
							if ( t < 0 ) t = 0;
							if ( t > 1 ) t = 1;
						    if( t != 0 && initialVertices.size() == copy.morph.getAdditionalVertices()[i].size())
							{
								for (unsigned int v = 0; v < initialVertices.size(); v++){
									initialVertices[v] += ((copy.morph.getAdditionalVertices()[i])[v]) * t;
								}
							}

							}



						}

						allvertices = &initialVertices;
                        }
						shapeNumber++;
					}
				}


			    if(verticesToChange->size() > 0){

                for(std::map<int, std::vector<Nif::NiSkinData::IndividualWeight> >::iterator iter = verticesToChange->begin();
                    iter != verticesToChange->end(); iter++)
                {
                    std::vector<Nif::NiSkinData::IndividualWeight> inds = iter->second;
                    int verIndex = iter->first;
                    Ogre::Vector3 currentVertex = (*allvertices)[verIndex];
                    Nif::NiSkinData::BoneInfoCopy* boneinfocopy = &(allshapesiter->boneinfo[inds[0].boneinfocopyindex]);
                    Ogre::Bone *bonePtr = 0;



                    Ogre::Vector3 vecPos;
                    Ogre::Quaternion vecRot;
					std::map<Nif::NiSkinData::BoneInfoCopy*, PosAndRot>::iterator result = vecRotPos.find(boneinfocopy);

                    if(result == vecRotPos.end()){
                        bonePtr = skel->getBone(boneinfocopy->bonename);

                        vecPos = bonePtr->_getDerivedPosition() + bonePtr->_getDerivedOrientation() * boneinfocopy->trafo.trans;
                        vecRot = bonePtr->_getDerivedOrientation() * boneinfocopy->trafo.rotation;


                             PosAndRot both;
                            both.vecPos = vecPos;
                            both.vecRot = vecRot;
                            vecRotPos[boneinfocopy] = both;

                    }
                    else{
                        PosAndRot both = result->second;
                        vecPos = both.vecPos;
                        vecRot = both.vecRot;
                    }

                    Ogre::Vector3 absVertPos = (vecPos + vecRot * currentVertex) * inds[0].weight;



                    for(std::size_t i = 1; i < inds.size(); i++){
                        boneinfocopy = &(allshapesiter->boneinfo[inds[i].boneinfocopyindex]);
                        result = vecRotPos.find(boneinfocopy);


                        if(result == vecRotPos.end()){
                            bonePtr = skel->getBone(boneinfocopy->bonename);
                            vecPos = bonePtr->_getDerivedPosition() + bonePtr->_getDerivedOrientation() * boneinfocopy->trafo.trans;
                            vecRot = bonePtr->_getDerivedOrientation() * boneinfocopy->trafo.rotation;

                                PosAndRot both;
                                both.vecPos = vecPos;
                                both.vecRot = vecRot;
                                vecRotPos[boneinfocopy] = both;

                        }
                         else{
                                PosAndRot both = result->second;
                                vecPos = both.vecPos;
                                vecRot = both.vecRot;
                        }


                        absVertPos += (vecPos + vecRot * currentVertex) * inds[i].weight;


                    }
                     Ogre::Real* addr = (pReal + 3 * verIndex);
							  *addr = absVertPos.x;
							  *(addr+1) = absVertPos.y;
				              *(addr+2) = absVertPos.z;

                }




				}
				else
				{
					//Ogre::Bone *bonePtr = creaturemodel->getSkeleton()->getBone(copy.bonename);
					Ogre::Quaternion shaperot = copy.trafo.rotation;
					Ogre::Vector3 shapetrans = copy.trafo.trans;
					float shapescale = copy.trafo.scale;
					std::vector<std::string> boneSequence = copy.boneSequence;

					Ogre::Vector3 transmult;
						Ogre::Quaternion rotmult;
						float scale;
					if(boneSequence.size() > 0){
                        std::vector<std::string>::iterator boneSequenceIter = boneSequence.begin();
                        if(skel->hasBone(*boneSequenceIter)){
					Ogre::Bone *bonePtr = skel->getBone(*boneSequenceIter);




						transmult = bonePtr->getPosition();
						rotmult = bonePtr->getOrientation();
						scale = bonePtr->getScale().x;
						boneSequenceIter++;

					    for(; boneSequenceIter != boneSequence.end(); boneSequenceIter++)
					    {
							if(skel->hasBone(*boneSequenceIter)){
							Ogre::Bone *bonePtr = skel->getBone(*boneSequenceIter);
								// Computes C = B + AxC*scale
								transmult = transmult + rotmult * bonePtr->getPosition();
								rotmult = rotmult * bonePtr->getOrientation();
								scale = scale * bonePtr->getScale().x;
							}
						    //std::cout << "Bone:" << *boneSequenceIter << "   ";
					    }
						transmult = transmult + rotmult * shapetrans;
						rotmult = rotmult * shaperot;
						scale = shapescale * scale;

						//std::cout << "Position: " << transmult << "Rotation: " << rotmult << "\n";
					}
                    }
					else
					{
						transmult = shapetrans;
						rotmult = shaperot;
						scale = shapescale;
					}




					// Computes C = B + AxC*scale
					 // final_vector = old_vector + old_rotation*new_vector*old_scale/

					for(unsigned int i = 0; i < allvertices->size(); i++){
						Ogre::Vector3 current = transmult + rotmult * (*allvertices)[i];
						Ogre::Real* addr = pReal + i * 3;
					    *addr = current.x;
						*(addr+1) = current.y;
						*(addr + 2) = current.z;

					}/*
					for(int i = 0; i < allnormals.size(); i++){
						Ogre::Vector3 current =rotmult * allnormals[i];
						Ogre::Real* addr = pRealNormal + i * 3;
					    *addr = current.x;
						*(addr+1) = current.y;
						*(addr + 2) = current.z;

					}*/

				}
				vbuf->unlock();

		}

    }
Beispiel #8
0
Quaternion GfxBody::getBoneWorldOrientation (unsigned n)
{
    checkBone(n);
    Ogre::Bone *bone = skeleton->getBone(n);
    return from_ogre(bone->_getDerivedOrientation());
}
PhysicsRagDoll::RagBone* PhysicsRagDoll::_addAllBones(PhysicsRagDoll::RagBone* parent, TiXmlElement* boneElement, Actor* parentActor)
{
	// get the information for the bone represented by this element.
    Ogre::Vector3 dir = getAttributeValueAsVector3(boneElement, "dir");
	Ogre::Real length = getAttributeValueAsReal(boneElement, "length");
		
	Ogre::Vector3 size = getAttributeValueAsVector3(boneElement, "size");
	
	
	Ogre::String skeleton_bone = getAttributeValueAsStdString(boneElement, "skeleton_bone");
	Ogre::Bone* ogrebone = mSkeleton->getBone(skeleton_bone);


	Ogre::String shapestr = getAttributeValueAsStdString(boneElement, "shape");
	PhysicsRagDoll::RagBone::BoneShape shape = PhysicsRagDoll::RagBone::BS_BOX;

	if (shapestr=="box")
		shape = PhysicsRagDoll::RagBone::BS_BOX;
	
	if (shapestr=="capsule")
		shape = PhysicsRagDoll::RagBone::BS_CAPSULE;
	
	if (shapestr=="cylinder")
		shape = PhysicsRagDoll::RagBone::BS_CYLINDER;
	
	if (shapestr=="cone")
		shape = PhysicsRagDoll::RagBone::BS_CONE;
	
	if (shapestr=="ellipsoid")
		shape = PhysicsRagDoll::RagBone::BS_ELLIPSOID;

	if (shapestr=="hull")
		shape = PhysicsRagDoll::RagBone::BS_CONVEXHULL;

	Ogre::Real mass = getAttributeValueAsReal(boneElement, "mass");
	
	///////////////////////////////////////////////////////////////////////////////
	RagBone* me = _addBone(mWorld, parent, dir, shape, size, mass, ogrebone,  parentActor);
	///////////////////////////////////////////////////////////////////////////////

	// position the bone.
	Ogre::Quaternion boneorient = mNode->_getDerivedOrientation() * ogrebone->_getDerivedOrientation();
	Ogre::Vector3 bonepos;
	if (shape != PhysicsRagDoll::RagBone::BS_CONVEXHULL)
		bonepos = mNode->_getFullTransform() * ogrebone->_getDerivedPosition() + (boneorient * (dir * (length*0.5f)));
	else
		bonepos = mNode->_getFullTransform() * ogrebone->_getDerivedPosition();

    if (me->getBody())
    	me->getBody()->setPositionOrientation(bonepos, boneorient);

	// set offsets
	if (!parent)
	{
		Ogre::Quaternion offsetorient = (boneorient.Inverse()) * mNode->_getDerivedOrientation();
		Ogre::Vector3 offsetpos = boneorient.Inverse() * (mNode->_getDerivedPosition() - bonepos);
		me->setOffset(offsetorient, offsetpos);
	}


	// get the joint to connect this bone with it's parent.
	if (parent && me->getBody())
	{
		TiXmlElement* jointElement = getChildNamed(boneElement, "Joint");
		if (!jointElement)
		{
			// error!
            LOG_ERROR(Logger::CORE, " Joint not found while creating Ragdoll! ");
			return me;
		}

		Ogre::Vector3 jointpin = getAttributeValueAsVector3(jointElement, "pin");
		Ogre::String jointtypestr = getAttributeValueAsStdString(jointElement, "type");
		PhysicsRagDoll::JointType jointtype = PhysicsRagDoll::JT_BALLSOCKET;

		if (jointtypestr == "ballsocket")
			jointtype = PhysicsRagDoll::JT_BALLSOCKET;

		if (jointtypestr == "hinge")
			jointtype = PhysicsRagDoll::JT_HINGE;


		Ogre::Real limit1 = getAttributeValueAsReal(jointElement, "limit1");
		Ogre::Real limit2 = getAttributeValueAsReal(jointElement, "limit2");

		Ogre::Vector3 jpos = mNode->_getFullTransform() * ogrebone->_getDerivedPosition();
		Ogre::Vector3 jpin = (mNode->_getDerivedOrientation() * parent->getOgreBone()->_getDerivedOrientation()) * jointpin;

		_joinBones(jointtype, parent, me, jpos, jpin, limit1, limit2, mWorld);
	}
	
    LOG_MESSAGE(Logger::CORE, " added bone from '"+ogrebone->getName()+"'.");

	///////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////
	// add all children of this bone.
    for (TiXmlNode* cur = boneElement->FirstChild(); cur; cur = cur->NextSibling())
    {
        if (cur->Type() == TiXmlNode::ELEMENT && hasNodeName(cur, "Bone"))
        {
		    _addAllBones(me, cur->ToElement(), parentActor);
        }
	}

    return me;
}