//-----------------------------------------------------------------------------------------
	void CollisionCameraController::update(float timeSinceLast) {
		Ogre::Camera* camera     = mGraphicsSystem->getCamera();
		Ogre::Node*   cameraNode = camera->getParentNode();

		{
			btTransform  transform = mGhostObject->getWorldTransform();
			btQuaternion q         = transform.getRotation();
			q *= btQuaternion(btVector3(0, 1, 0), mCameraYaw);
			mGhostObject->getWorldTransform().setRotation(q);
		}

		camera->pitch(Ogre::Radian(mCameraPitch));

		mCameraYaw   = 0.0f;
		mCameraPitch = 0.0f;

		int camMovementZ = mKeymapState[Backward].second - mKeymapState[Forward].second;
		int camMovementX = mKeymapState[Rightward].second - mKeymapState[Leftward].second;
		int slideUpDown  = mKeymapState[Up].second - mKeymapState[Down].second;

		Ogre::Vector3 camMovementDir(camMovementX, slideUpDown, camMovementZ);
		camMovementDir.normalise();
		camMovementDir *= timeSinceLast * mCameraBaseSpeed * (1 + mSpeedModifier * mCameraSpeedBoost);
		if (camMovementDir.y > 0) {
			mCharacter->jump();
			camMovementDir.y = 0;
		}
		mCharacter->setWalkDirection(Collision::Converter::to(Collision::Converter::to(mGhostObject->getWorldTransform().getRotation()) * camMovementDir));

		{
			Ogre::Node*      cameraNode = camera->getParentNode();
			Ogre::Quaternion q;
			q = q * (Collision::Converter::to(mGhostObject->getWorldTransform().getRotation()));
			q = q * Ogre::Quaternion(cameraNode->getOrientation().getPitch(), Ogre::Vector3(1, 0, 0));

			cameraNode->setOrientation(q);
			cameraNode->setPosition(Collision::Converter::to(mGhostObject->getWorldTransform().getOrigin() /* + btVector3(0,1.8,0)*/));
		}
	}
void InputListener::updateCam(const Ogre::FrameEvent& evt, Worms *_current)
{
  Ogre::Vector3 deplacement = Ogre::Vector3::ZERO;
  if (this->_fixedCam)
  {
    if (this->_Keyboard->isKeyDown(OIS::KC_W))
    {
      this->_CamSupposedPosition = Ogre::Vector3(600, 250.0, 950);
    }
    else
    {
      this->_CamSupposedPosition = _current->getNode()->getPosition() - Ogre::Vector3(0, 0, -300);
    }
  }
  if (!this->_fixedCam)
  {
    Ogre::Vector3 mov = this->_Mouvement;

    if (this->_currentScene->getCamera()->getPosition().x < 100)
      mov.x = 1;
    if (this->_currentScene->getCamera()->getPosition().x > 1200)
      mov.x = -1;
    if (this->_currentScene->getCamera()->getPosition().y < 50)
      mov.y = 1;
    if (this->_currentScene->getCamera()->getPosition().y > 510)
      mov.y = -1;
    if (this->_currentScene->getCamera()->getPosition().z < 50)
      mov.z = 1;
    if (this->_currentScene->getCamera()->getPosition().z > 510)
      mov.z = -1;
    deplacement = mov * this->_Vitesse * evt.timeSinceLastFrame;
    this->_currentScene->getCamera()->moveRelative(deplacement);
  }
  else
    this->_currentScene->getCamera()->setPosition(this->_currentScene->getCamera()->getPosition() + ((this->_CamSupposedPosition - this->_currentScene->getCamera()->getPosition()) * 0.05f));
  Ogre::Node *node = this->_WormsApp->getSceneManager()->getScene("PlayScene")->getScene()->getRootSceneNode()->getChild("CrossHair");
  node->setPosition(_current->getNode()->getPosition().x + 40 * cos(_current->getAngle()), _current->getNode()->getPosition().y + 40 * sin(_current->getAngle()), 30);
}
Example #3
0
    void BtOgreSoftBody::updateOgreMesh()
    {
        Ogre::Node *ogreNode = mEntity->getParentNode();

        //printf("updateOgreMesh %d %s %s\n", internalId, mEntity->getName().c_str(), ogreNode->getName().c_str());

        MeshPtr mesh = mEntity->getMesh();
        Mesh::SubMeshIterator subMeshI = mesh->getSubMeshIterator();
        SubMesh* subMesh = NULL;

        VertexData* vData = NULL;
        VertexDeclaration* vDeclaration = NULL;
        const VertexElement* vPosElement = NULL;

        bool isSharedVerticesAdded = false;
        unsigned short bufferIndex = 0;
        HardwareVertexBufferSharedPtr vBuffer;

        // Can not do arithmetic operations on void*
        unsigned char* lockedMem = NULL;
        float* vPosition;

        btSoftBody::tNodeArray& btNodes = mSoftBody->m_nodes;
        //printf("Bullet nodes size %d\n", btNodes.size());

        int ogreVertexIdx = 0;
        btVector3 btPosOffset;

        while (subMeshI.hasMoreElements())
        {
            subMesh = subMeshI.getNext();

            if (subMesh->useSharedVertices)
            {

                if (isSharedVerticesAdded)
                {
                    continue;
                }

                vData = mesh->sharedVertexData;

                // We need to add shared vertices only once
                isSharedVerticesAdded = true;

            }
            else
            {
                vData = subMesh->vertexData;
            }

            vDeclaration = vData->vertexDeclaration;
            vPosElement = vDeclaration->findElementBySemantic(VES_POSITION);

            bufferIndex = vPosElement->getSource();
            vBuffer = vData->vertexBufferBinding->getBuffer(bufferIndex);

            // Lock the buffer before reading from it
            lockedMem = static_cast<unsigned char*>(vBuffer->lock(HardwareBuffer::HBL_DISCARD));

            // Read each vertex
            for (unsigned int i = 0; i < vData->vertexCount; ++i)
            {
                vPosElement->baseVertexPointerToElement(lockedMem, &vPosition);

                int idx = getBulletIndex(ogreVertexIdx);
                const btVector3 &btPos = btNodes[idx].m_x;

                if (ogreVertexIdx == 0)
                {
                    btPosOffset = btPos;
                }

                *vPosition++ = btPos.x() - btPosOffset.x();
                *vPosition++ = btPos.y() - btPosOffset.y();
                *vPosition = btPos.z() - btPosOffset.z();

                // Point to the next vertex
                lockedMem += vBuffer->getVertexSize();

                ogreVertexIdx++;
            }

            vBuffer->unlock();
        }

        ogreNode->setPosition(btPosOffset.x(), btPosOffset.y(), btPosOffset.z());

    }
Example #4
0
bool OgreSmartBody::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
    if(mWindow->isClosed())
        return false;
 
    //Need to capture/update each device
    mKeyboard->capture();
    mMouse->capture();
 
    if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
        return false;

	// smartbody
	if (!m_pScene)
		return true;

	SmartBody::SBSimulationManager* sim = m_pScene->getSimulationManager();
	sim->setTime((Ogre::Root::getSingleton().getTimer()->getMilliseconds() / 1000.0f) - mStartTime);
	m_pScene->update();

	int numCharacters = m_pScene->getNumCharacters();
	if (numCharacters == 0)
		return true;

	const std::vector<std::string>& characterNames = m_pScene->getCharacterNames();

	for (size_t n = 0; n < characterNames.size(); n++)
	{
		SmartBody::SBCharacter* character = m_pScene->getCharacter(characterNames[n]);
		if (!this->getSceneManager()->hasEntity(characterNames[n]))
			continue;

		Ogre::Entity* entity = this->getSceneManager()->getEntity(characterNames[n]);
		Ogre::Skeleton* meshSkel = entity->getSkeleton();
		Ogre::Node* node = entity->getParentNode();

		SrVec pos = character->getPosition();
		SrQuat ori = character->getOrientation();
		//std::cout << ori.w << ori.x << " " << ori.y << " " << ori.z << std::endl;
		node->setPosition(pos.x, pos.y, pos.z);
		node->setOrientation(ori.w, ori.x, ori.y, ori.z);
	
		// Update joints
		SmartBody::SBSkeleton* sbSkel = character->getSkeleton();
			
		int numJoints = sbSkel->getNumJoints();
		for (int j = 0; j < numJoints; j++)
		{
			SmartBody::SBJoint* joint = sbSkel->getJoint(j);
	
			try
			{
				SrQuat orientation = joint->quat()->value();
	
				Ogre::Vector3 posDelta(joint->getPosition().x, joint->getPosition().y, joint->getPosition().z);
				Ogre::Quaternion quatDelta(orientation.w, orientation.x, orientation.y, orientation.z);
				Ogre::Bone* bone = meshSkel->getBone(joint->getName());
				if (!bone)
					continue;
				bone->setPosition(bone->getInitialPosition() + posDelta);
				bone->setOrientation(quatDelta);
			}
			catch (Ogre::ItemIdentityException& ex)
			{
				// Should not happen as we filtered using m_mValidBones
			}
		}
	}
	

 
    return true;
}
Example #5
0
void EC_Mesh::OnAttributeUpdated(IAttribute *attribute)
{
    if (attribute == &drawDistance)
    {
        if(entity_)
            entity_->setRenderingDistance(drawDistance.Get());
    }
    else if (attribute == &castShadows)
    {
        if(entity_)
        {
            if (entity_)
                entity_->setCastShadows(castShadows.Get());
            //! \todo might want to disable shadows for some attachments
            for (uint i = 0; i < attachment_entities_.size(); ++i)
            {
                if (attachment_entities_[i])
                    attachment_entities_[i]->setCastShadows(castShadows.Get());
            }
        }
    }
    else if (attribute == &nodeTransformation)
    {
        Ogre::Node* adjustmentTarget = adjustment_node_;
        if (bone_tagpoint_)
            adjustmentTarget = bone_tagpoint_;
        
        if (adjustmentTarget)
        {
            Transform newTransform = nodeTransformation.Get();
            adjustmentTarget->setPosition(newTransform.position.x, newTransform.position.y, newTransform.position.z);
            Quaternion adjust(DEGTORAD * newTransform.rotation.x,
                              DEGTORAD * newTransform.rotation.y,
                              DEGTORAD * newTransform.rotation.z);
            // Let's not assume the needed haxor adjustment here, but let user specify it as necessary
            //adjust = Quaternion(PI/2, 0, PI) * adjust;
            adjustmentTarget->setOrientation(Ogre::Quaternion(adjust.w, adjust.x, adjust.y, adjust.z));
            
            // Prevent Ogre exception from zero scale
            if (newTransform.scale.x < 0.0000001f)
                newTransform.scale.x = 0.0000001f;
            if (newTransform.scale.y < 0.0000001f)
                newTransform.scale.y = 0.0000001f;
            if (newTransform.scale.z < 0.0000001f)
                newTransform.scale.z = 0.0000001f;
            
            adjustmentTarget->setScale(newTransform.scale.x, newTransform.scale.y, newTransform.scale.z);
        }
    }
    else if (attribute == &meshRef)
    {
        if (!ViewEnabled())
            return;
            
        //Ensure that mesh is requested only when it's has actually changed.
//        if(entity_)
 //           if(QString::fromStdString(entity_->getMesh()->getName()) == meshRef.Get().ref/*meshResourceId.Get()*/)
  //              return;
/*
        AssetTransferPtr transfer = GetFramework()->Asset()->RequestAsset(meshRef.Get());
        if (transfer)
        {
            connect(transfer.get(), SIGNAL(Loaded(AssetPtr)), SLOT(OnMeshAssetLoaded()), Qt::UniqueConnection);
        }
        else
        {
            RemoveMesh();
        }
        */
        if (meshRef.Get().ref.trimmed().isEmpty())
            LogDebug("Warning: Mesh \"" + this->parent_entity_->GetName().toStdString() + "\" mesh ref was set to an empty reference!");
        meshAsset->HandleAssetRefChange(&meshRef);
    }
    else if (attribute == &meshMaterial)
    {
        if (!ViewEnabled())
            return;
        
        // We won't request materials until we are sure that mesh has been loaded and it's safe to apply materials into it.
        // This logic shouldn't be necessary anymore. -jj.
//        if(!HasMaterialsChanged())
//            return;

        AssetReferenceList materials = meshMaterial.Get();

        // Reallocate the number of material asset reflisteners.
        while(materialAssets.size() > materials.Size())
            materialAssets.pop_back();
        while(materialAssets.size() < materials.Size())
            materialAssets.push_back(boost::shared_ptr<AssetRefListener>(new AssetRefListener));

        for(int i = 0; i < materials.Size(); ++i)
        {
            connect(materialAssets[i].get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnMaterialAssetLoaded(AssetPtr)), Qt::UniqueConnection);
            materialAssets[i]->HandleAssetRefChange(framework_->Asset(), materials[i].ref);
        }
    }
    else if((attribute == &skeletonRef) && (!skeletonRef.Get().ref.isEmpty()))
    {
        if (!ViewEnabled())
            return;
        
        // If same name skeleton already set no point to do it again.
//        if (entity_ && entity_->getSkeleton() && entity_->getSkeleton()->getName() == skeletonRef.Get().ref/*skeletonId.Get()*/.toStdString())
 //           return;

  //      AssetTransferPtr transfer = GetFramework()->Asset()->RequestAsset(skeletonRef.Get().ref);
   //     if (transfer)
    //        connect(transfer.get(), SIGNAL(Loaded(AssetPtr)), SLOT(OnSkeletonAssetLoaded(AssetPtr)), Qt::UniqueConnection);
        skeletonAsset->HandleAssetRefChange(&skeletonRef);
    }
}