void OgreCharacterController::update(Ogre::Real elapsedTime, OIS::Keyboard * input) { Ogre::SceneNode * node = getMainNode(); OGRE_ANIMATION_STATE state = IDLE; if(input->isKeyDown(OIS::KC_W)) { node->translate(node->getOrientation() * Ogre::Vector3(0, 0, elapsedTime*100)); state = RUN; } if(input->isKeyDown(OIS::KC_S)) { node->translate(node->getOrientation() * Ogre::Vector3(0, 0, elapsedTime*-50)); state = RUN; } if(input->isKeyDown(OIS::KC_A)) { node->yaw(Ogre::Radian(2*elapsedTime)); } if(input->isKeyDown(OIS::KC_D)) node->yaw(Ogre::Radian(-2*elapsedTime)); if(input->isKeyDown(OIS::KC_RETURN)) { state = DRAWSWORDS; } if(input->isKeyDown(OIS::KC_SPACE)) { state = SLICE; } if(input->isKeyDown(OIS::KC_PGUP)) { //mRigidBody->setLinearVelocity(btVector3(0, 50, 0)); } animate(elapsedTime, state); }
void TranslationAnm::Fly() { Ogre::SceneNode* node = m_pGameObj->getNode(); Ogre::Vector3 pos = node->getPosition(); if(m_pAnmState->getEnabled()) return; Ogre::TransformKeyFrame* k_t0_0=m_pAnmTrack->getNodeKeyFrame(ANM_KEYFRAME_0); k_t0_0->setRotation(node->getOrientation()); Ogre::TransformKeyFrame* k_t0_2=m_pAnmTrack->getNodeKeyFrame(ANM_KEYFRAME_1); k_t0_2->setRotation(node->getOrientation()); m_pAnmState->setTimePosition(0); m_pAnmState->setEnabled(true); }
void GameState::setUpQuaternion() { //设置跑道标准开始方向 Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()->createChildSceneNode(); Ogre::Vector3 dir = mSceneMgr->getSceneNode("R1PS")->_getDerivedPosition() - mSceneMgr->getSceneNode("R1P1")->_getDerivedPosition(); sn->setDirection(dir); mStdQuaternion = sn->getOrientation(); }
void PhysicsSystem::insertObjectPhysics(const MWWorld::Ptr& ptr, const std::string model){ Ogre::SceneNode* node = ptr.getRefData().getBaseNode(); // unused //Ogre::Vector3 objPos = node->getPosition(); addObject (node->getName(), model, node->getOrientation(), node->getScale().x, node->getPosition()); }
void Cube::save(Ogre::String file) { if (rndCounter == 0) { std::ofstream output(file.c_str()); // Write cube size output << size << std::endl; for (int i = 0; i < size; ++i) for (int j = 0; j < size; ++j) for (int k = 0; k < size; ++k) { CubeElement *e = data[i][j][k]; Ogre::Entity *ent = static_cast<Ogre::Entity*>(e->node->getAttachedObject(0)); Ogre::SceneNode *node = e->node; // Write name, indexes and position output << node->getName() << "\n" << "\t" << i << " " << j << " " << k << "\n" // index in data array << "\t" << node->getPosition().x << " " << node->getPosition().y << " " << node->getPosition().z << "\n"; // position // Write orientation Ogre::Vector3 orient_axis; Ogre::Degree orient_angle; node->getOrientation().ToAngleAxis(orient_angle, orient_axis); output << "\t" << orient_axis.x << " " << orient_axis.y << " " << orient_axis.z << " " // orientation axis << orient_angle.valueDegrees() << "\n"; // orientation angle output << "\t" << ent->getSubEntity(0)->getMaterialName() << "\n" << "\t" << ent->getSubEntity(1)->getMaterialName() << "\n" << "\t" << ent->getSubEntity(2)->getMaterialName() << "\n" << "\t" << ent->getSubEntity(3)->getMaterialName() << "\n" << "\t" << ent->getSubEntity(4)->getMaterialName() << "\n" << "\t" << ent->getSubEntity(5)->getMaterialName() << "\n"; // Write pivot info output << "\t" << e->isPivot << "\n"; if (e->isPivot) { // pivot indexes output << "\t" << e->pivotArrays[0] << " " << e->pivotArrayIndexes[0] << "\n" << "\t" << e->pivotArrays[1] << " " << e->pivotArrayIndexes[1] << "\n"; } // flush output << std::endl; } output.close(); } }
float3x4 EC_Mesh::LocalToParent() const { if (!entity_) { LogError(QString("EC_Mesh::LocalToParent failed! No entity exists in mesh \"%1\" (entity: \"%2\")!").arg(meshRef.Get().ref).arg(ParentEntity() ? ParentEntity()->Name() : "(EC_Mesh with no parent entity)")); return float3x4::identity; } Ogre::SceneNode *node = entity_->getParentSceneNode(); if (!node) { LogError(QString("EC_Mesh::LocalToParent failed! Ogre::Entity is not attached to a Ogre::SceneNode! Mesh \"%1\" (entity: \"%2\")!").arg(meshRef.Get().ref).arg(ParentEntity() ? ParentEntity()->Name() : "(EC_Mesh with no parent entity)")); return float3x4::identity; } return float3x4::FromTRS(node->getPosition(), node->getOrientation(), node->getScale()); }
float3x4 EC_Mesh::LocalToParent() const { if (!entity_) { LogError("EC_Mesh::LocalToParent failed! No entity exists in this mesh!"); return float3x4::identity; } Ogre::SceneNode *node = entity_->getParentSceneNode(); if (!node) { LogError("EC_Mesh::LocalToParent failed! Ogre::Entity is not attached to a Ogre::SceneNode!"); return float3x4::identity; } return float3x4::FromTRS(node->getPosition(), node->getOrientation(), node->getScale()); }
//------------------------------------------------------------------------------------- bool PlayersManager::moving(zappy::Player *p, int i) { OPlayer *OPlayer = this->mOPlayers.at(i); this->speed = Constants::SquareSize / ((Constants::timeUnit / static_cast<Ogre::Real>(time))); Ogre::SceneNode *node = OPlayer->getSceneNode(); Ogre::Vector3 &direction = OPlayer->getDirection(); Ogre::Real &distance = OPlayer->getDistance(); Ogre::Real move = this->speed * this->tslf; Ogre::Vector3 destination(p->getX() * Constants::SquareSize, 0, p->getY() * Constants::SquareSize); Ogre::AnimationState *anim = OPlayer->getEntity()-> getAnimationState(distance <= 0.0f ? "Idle" : "Walk"); anim->setLoop(true); anim->setEnabled(true); if (direction == Ogre::Vector3::ZERO) { Ogre::Vector3 src = node->getOrientation() * Ogre::Vector3::UNIT_X; direction = destination - node->getPosition(); distance = direction.normalise(); if ((1.0f + src.dotProduct(direction)) < 0.0001f) node->yaw(Ogre::Degree(180)); else node->rotate(src.getRotationTo(direction)); if (distance > Constants::SquareSize) distance = 0.0f; } else { distance -= move; if (distance <= 0.0f) { node->setPosition(destination); direction = Ogre::Vector3::ZERO; } else node->translate(direction * move); } if (OPlayer->stateHasChanged()) OPlayer->detachAnim(); anim->addTime(this->tslf); return true; }
void PhysicsSystem::rotateObject (const Ptr& ptr) { Ogre::SceneNode* node = ptr.getRefData().getBaseNode(); std::string handle = node->getName(); Ogre::Quaternion rotation = node->getOrientation(); if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) { //Needs to be changed act->setRotation(rotation); } if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle)) { if(dynamic_cast<btBoxShape*>(body->getCollisionShape()) == NULL) body->getWorldTransform().setRotation(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w)); else mEngine->boxAdjustExternal(handleToMesh[handle], body, node->getScale().x, node->getPosition(), rotation); } }
Entity Factory::createBlock(entityx::ptr<entityx::EntityManager> entityMgr, int x, int y, int z, std::string material) { Entity block = entityMgr->create(); Ogre::SceneManager* sceneMgr = RenderManager::getPtr()->getSceneManager(); Ogre::Entity* wall = sceneMgr->createEntity("Cube.mesh"); wall->setMaterialName(material); Ogre::SceneNode* wallNode = sceneMgr->getRootSceneNode()->createChildSceneNode(); wallNode->attachObject(wall); block.assign<Position>(x,y,z); wallNode->setPosition(x,y,z); block.assign<Orientation>(wallNode->getOrientation()); block.assign<Renderable>(wallNode); block.assign<Name>("Block"); if(material == "Wall"){ block.assign<Destroyable>(200); } return block; }
void MenuScene::createAnimation(void) { Ogre::SceneNode *titleNode = static_cast<Ogre::SceneNode *>(mSceneMgr->getRootSceneNode()->getChild("boomb_title")); Ogre::Animation *anim = mSceneMgr->createAnimation("title_animation", 3); anim->setInterpolationMode(Ogre::Animation::IM_SPLINE); Ogre::NodeAnimationTrack *track = anim->createNodeTrack(0, titleNode); Ogre::TransformKeyFrame *key = track->createNodeKeyFrame(0); key->setTranslate(Ogre::Vector3(0,0,100)); Ogre::Quaternion q1 = titleNode->getOrientation(); Ogre::Quaternion q2(Ogre::Degree(45), Ogre::Vector3::UNIT_X); key->setRotation(q2); key = track->createNodeKeyFrame(1.5); key->setTranslate(Ogre::Vector3(0,0,120)); key->setRotation(q2); key = track->createNodeKeyFrame(3); key->setTranslate(Ogre::Vector3(0,0,100)); key->setRotation(q2); mTitleAnimState = mSceneMgr->createAnimationState("title_animation"); mTitleAnimState->setEnabled(true); mTitleAnimState->setLoop(true); Ogre::SceneNode *backgroundNode = static_cast<Ogre::SceneNode *>(mSceneMgr->getRootSceneNode()->getChild("boomb_background")); anim = mSceneMgr->createAnimation("background_animation", 30); anim->setInterpolationMode(Ogre::Animation::IM_SPLINE); track = anim->createNodeTrack(0, backgroundNode); key = track->createNodeKeyFrame(0); key->setTranslate(Ogre::Vector3(20,0,20)); key = track->createNodeKeyFrame(15); key->setTranslate(Ogre::Vector3(-20,0,-20)); key = track->createNodeKeyFrame(30); key->setTranslate(Ogre::Vector3(20,0,20)); mBackgroundAnimState = mSceneMgr->createAnimationState("background_animation"); mBackgroundAnimState->setEnabled(true); mBackgroundAnimState->setLoop(true); }
void TutorialApplication::evilShot(ShipProject::GameObject* enemy, double speed) { Ogre::Entity* ball = mSceneMgr->createEntity("projectile"); ball->setMaterialName("Projectile/Red"); Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode(); Ogre::Vector3 dir = player->entity()->getParentSceneNode()->getPosition() - enemy->entity()->getParentSceneNode()->getPosition(); dir.normalise(); Ogre::Quaternion orient = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo(dir); node->setPosition(enemy->entity()->getParentSceneNode()->getPosition() ); node->setOrientation(orient); node->translate( orient * (Ogre::Vector3::UNIT_Z * -0.5) ); node->attachObject(ball); ShipProject::GameObject* oBall = new ShipProject::GameObject(ball, 0.25); oBall->SetupCollision(new btSphereShape(PROJECTILE_RADIUS), CollisionGroup::PROJECTILES_BALLS, CollisionGroup::PLAYER | CollisionGroup::WALLS); objects_.push_back( oBall ); // orientatation * (original model forward vector) = direction vector oBall->body()->setLinearVelocity( BtOgre::Convert::toBullet(node->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z * speed)); oBall->body()->setRestitution(1.0); oBall->body()->activate(true); projectiles_[oBall->entity_name()] = Projectile(oBall, 3.0); }
void TutorialApplication::shoot() { //Ogre::InstancedEntity* ball = balls->createInstancedEntity("Ogre/Skin"); Ogre::Entity* ball = mSceneMgr->createEntity("projectile"); //ball->setCastShadows(true); ball->setMaterialName("Projectile/Blue"); Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode(); Ogre::Quaternion orient = camera->actual_orientation(); node->setPosition(player->entity()->getParentSceneNode()->getPosition() ); node->setOrientation(orient); node->translate( orient * (Ogre::Vector3::UNIT_Z * -0.5) ); node->attachObject(ball); ShipProject::GameObject* oBall = new ShipProject::GameObject(ball, 0.25); oBall->SetupCollision(new btSphereShape(PROJECTILE_RADIUS), CollisionGroup::PROJECTILES_PLAYER, CollisionGroup::BALLS | CollisionGroup::WALLS); objects_.push_back( oBall ); // orientatation * (original model forward vector) = direction vector oBall->body()->setLinearVelocity( BtOgre::Convert::toBullet(node->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z * 100)); oBall->body()->setRestitution(1.0); oBall->body()->activate(true); projectiles_[oBall->entity_name()] = Projectile(oBall, 3.0); }
//------------------------------------------------------------------------------------- void RollerCoaster::mouseMoveEvent( QMouseEvent *event ) { lastPos=event->pos(); //bLMouseDown = true; float RatioX = float (lastPos.x())/mCamera->getViewport()->getActualWidth(); float RatioY = float (lastPos.y())/mCamera->getViewport()->getActualWidth(); if(bLMouseDown){ using namespace Ogre; Ray mouseRay=mCamera->getCameraToViewportRay(RatioX,RatioY); std::pair<bool, Real>point =mouseRay.intersects(mPlane); if(point.first) { Vector3 pos= mouseRay.getPoint(point.second); SceneNode *tempSn = mSceneMgr->getRootSceneNode()->createChildSceneNode(); Entity *tempEn = mSceneMgr->createEntity("ogrehead.mesh"); tempSn->attachObject(tempEn); tempSn->setPosition(pos); tempSn->setScale(0.1,0.1,0.1); } } if( bLMouseDown ) { mCurrentObjName = objControl->mouseDragged( lastPos, event->pos(), mCamera->getViewport()->getActualWidth(), mCamera->getViewport()->getActualHeight() ); if( !mCurrentObjName.empty() ) { Ogre::SceneNode *currObj = mSceneMgr->getSceneNode(mCurrentObjName); if (currObj) { bReplan = true; mTrack->mMove(currObj->getName(), currObj->getPosition()); mTrack->mRotate(currObj->getName(), currObj->getOrientation().yAxis()); } } } //==== 當按下滑鼠右鍵 ====// if( event->buttons().testFlag(Qt::RightButton) && lastPos != invalidMousePoint ) { if( currCamType != eWorld ) return; mCamWorldNode = mCamera->getParentSceneNode(); Ogre::Degree rotX(0); Ogre::Degree rotY(0); int dx = event->pos().x()-lastPos.x(); int dy = event->pos().y()-lastPos.y(); rotX += Ogre::Degree(-dx * 0.0025f * ROT_SCALE); // Rotate camera horizontaly rotY += Ogre::Degree(-dy * 0.0025f * ROT_SCALE); // Rotate camera vertical mCamWorldNode->yaw ( rotX, Ogre::Node::TS_WORLD ); mCamWorldNode->pitch( rotY, Ogre::Node::TS_LOCAL ); objControl->update(); } lastPos = event->pos(); }
Entity Factory::createTank(entityx::ptr<entityx::EntityManager> entityMgr, std::string prefix,Ogre::Real velocity,Ogre::Real angularVelocity ,Ogre::Vector3 overHating,int health,bool ai) { DotSceneLoader loader; Ogre::SceneManager* sceneMgr = RenderManager::getPtr()->getSceneManager(); loader.parseDotScene("tank.scene", "General", sceneMgr, 0, prefix); Ogre::SceneNode* ctl = sceneMgr->getSceneNode(prefix + "TankControl"); Ogre::SceneNode* body = sceneMgr->getSceneNode(prefix + "TankBody"); Ogre::SceneNode* turret = sceneMgr->getSceneNode(prefix + "TankTurret"); Ogre::SceneNode* cannon = sceneMgr->getSceneNode(prefix +"TankCannon"); Entity tankEmptyControl = entityMgr->create(); Entity tankTurret = entityMgr->create(); Entity tankBody = entityMgr->create(); Entity tankCannon = entityMgr->create(); tankEmptyControl.assign<Position>(ctl->getPosition()); tankEmptyControl.assign<Orientation>(ctl->getOrientation()); tankEmptyControl.assign<Velocity>(0, 0, velocity); tankEmptyControl.assign<AngularVelocity>(0, angularVelocity, 0); tankEmptyControl.assign<Renderable>(ctl); tankEmptyControl.assign<OverHeating>(overHating.x,overHating.y,overHating.z); tankEmptyControl.assign<Destroyable>(health,health); tankEmptyControl.assign<Collidable>(); tankEmptyControl.assign<Name>(prefix); if(ai){ tankEmptyControl.assign<AI>(); Ogre::Entity *model = static_cast<Ogre::Entity*>(body->getAttachedObject(0)); model->getSubEntity(1)->setMaterialName("Red"); model = static_cast<Ogre::Entity*>(turret->getAttachedObject(0)); model->getSubEntity(1)->setMaterialName("Red"); model = static_cast<Ogre::Entity*>(cannon->getAttachedObject(0)); model->getSubEntity(1)->setMaterialName("Red"); } ptr<Children> child = tankEmptyControl.assign<Children>(); child->children["body"] = tankBody; child->children["turret"] = tankTurret; //child->children.push_back(tankBody); //child->children.push_back(tankTurret); tankTurret.assign<Position>(turret->getPosition()); tankTurret.assign<Orientation>(turret->getOrientation()); tankTurret.assign<Renderable>(turret); child = tankTurret.assign<Children>(); child->children["cannon"] = tankCannon; tankBody.assign<Position>(body->getPosition()); tankBody.assign<Orientation>(body->getOrientation()); tankBody.assign<Renderable>(body); tankCannon.assign<Position>(cannon->getPosition()); tankCannon.assign<Renderable>(cannon); tankCannon.assign<Orientation>(cannon->getOrientation()); ctl->scale(.35, .55, .35); return tankEmptyControl; }
Quaternion getOgreOrientation() const { return fromOgre(mSceneNode->getOrientation()); }
void PhysicsSystem::moveObject (const Ptr& ptr) { Ogre::SceneNode* node = ptr.getRefData().getBaseNode(); std::string handle = node->getName(); Ogre::Vector3 position = node->getPosition(); if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle)) { // TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow // start positions others than 0, 0, 0 if(dynamic_cast<btBoxShape*>(body->getCollisionShape()) == NULL){ btTransform tr = body->getWorldTransform(); tr.setOrigin(btVector3(position.x,position.y,position.z)); body->setWorldTransform(tr); } else{ //For objects that contain a box shape. //Do any such objects exist? Perhaps animated objects? mEngine->boxAdjustExternal(handleToMesh[handle], body, node->getScale().x, position, node->getOrientation()); } } if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) { // TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow // start positions others than 0, 0, 0 if (handle == "player") { playerphysics->ps.origin = position; } else { act->setPosition(position); } } }
void PhysicsSystem::addActor (const Ptr& ptr) { std::string mesh = MWWorld::Class::get(ptr).getModel(ptr); Ogre::SceneNode* node = ptr.getRefData().getBaseNode(); //TODO:optimize this. Searching the std::map isn't very efficient i think. mEngine->addCharacter(node->getName(), mesh, node->getPosition(), node->getScale().x, node->getOrientation()); }
void PhysicsSystem::addObject (const Ptr& ptr) { std::string mesh = MWWorld::Class::get(ptr).getModel(ptr); Ogre::SceneNode* node = ptr.getRefData().getBaseNode(); handleToMesh[node->getName()] = mesh; OEngine::Physic::RigidBody* body = mEngine->createAndAdjustRigidBody(mesh, node->getName(), node->getScale().x, node->getPosition(), node->getOrientation()); mEngine->addRigidBody(body); }