Ogre::Vector3 plane_segment_intersection(const Ogre::Vector3& P1, const Ogre::Vector3& P2, const Ogre::Vector3& P3, const Ogre::Vector3& L1, const Ogre::Vector3& L2) { Ogre::Vector3 T(P2 - P1); Ogre::Vector3 N = T.crossProduct(P3 - P1); Ogre::Vector3 result = L1 + (N.dotProduct(P1 - L1) / N.dotProduct(L2 - L1)) * (L2 - L1); return result; }
void FPSViewController::setPropertiesFromCamera( Ogre::Camera* source_camera ) { Ogre::Quaternion quat = source_camera->getOrientation() * ROBOT_TO_CAMERA_ROTATION.Inverse(); float yaw = quat.getRoll( false ).valueRadians(); // OGRE camera frame looks along -Z, so they call rotation around Z "roll". float pitch = quat.getYaw( false ).valueRadians(); // OGRE camera frame has +Y as "up", so they call rotation around Y "yaw". Ogre::Vector3 direction = quat * Ogre::Vector3::NEGATIVE_UNIT_Z; if ( direction.dotProduct( Ogre::Vector3::NEGATIVE_UNIT_Z ) < 0 ) { if ( pitch > Ogre::Math::HALF_PI ) { pitch -= Ogre::Math::PI; } else if ( pitch < -Ogre::Math::HALF_PI ) { pitch += Ogre::Math::PI; } yaw = -yaw; if ( direction.dotProduct( Ogre::Vector3::UNIT_X ) < 0 ) { yaw -= Ogre::Math::PI; } else { yaw += Ogre::Math::PI; } } pitch_property_->setFloat( pitch ); yaw_property_->setFloat( mapAngleTo0_2Pi( yaw )); position_property_->setVector( source_camera->getPosition() ); }
bool InteractiveMarkerControl::intersectSomeYzPlane( const Ogre::Ray& mouse_ray, const Ogre::Vector3& point_on_plane, const Ogre::Quaternion& plane_orientation, Ogre::Vector3& intersection_3d, Ogre::Vector2& intersection_2d, float& ray_t ) { Ogre::Vector3 normal = plane_orientation * control_orientation_.xAxis(); Ogre::Vector3 axis_1 = plane_orientation * control_orientation_.yAxis(); Ogre::Vector3 axis_2 = plane_orientation * control_orientation_.zAxis(); Ogre::Plane plane(normal, point_on_plane); Ogre::Vector2 origin_2d(point_on_plane.dotProduct(axis_1), point_on_plane.dotProduct(axis_2)); std::pair<bool, Ogre::Real> intersection = mouse_ray.intersects(plane); if (intersection.first) { intersection_3d = mouse_ray.getPoint(intersection.second); intersection_2d = Ogre::Vector2(intersection_3d.dotProduct(axis_1), intersection_3d.dotProduct(axis_2)); intersection_2d -= origin_2d; ray_t = intersection.second; return true; } ray_t = 0; return false; }
bool PhysicsEngine::PerformRaySphereCollisionTest(const Ogre::FrameEvent &fe, PhysicsEntity *ray, PhysicsEntity *sphere) { Ogre::Vector3 rayDirection = ray->getDerivedOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z; Ogre::Vector3 rayPosition = ray->getDerivedPosition(); float ab2 = rayDirection.dotProduct(rayDirection); Ogre::Vector3 p = sphere->getDerivedPosition(); Ogre::Vector3 ap = p - rayPosition; float ap_dot_dir = ap.dotProduct(rayDirection); float t = ap_dot_dir / ab2; if (t < 0.0f) t = 0.0f; Ogre::Vector3 q = rayPosition + rayDirection * t; Ogre::Vector3 pq = q - p; float radius = sphere->GetRadius(); float r2 = radius * radius; if (pq.dotProduct(pq) < r2) { ray->Collide(fe, sphere); if (ray->GetBodyType() == ENTITY_BODY_RAY) { // sphere only detects collisions with physical rays sphere->Collide(fe, ray); } return true; } return false; }
void FPSCamera::setOrientation( float x, float y, float z, float w ) { Ogre::Quaternion quat( w, x, y, z ); yaw_ = quat.getYaw( false ).valueRadians(); pitch_ = quat.getPitch( false ).valueRadians(); Ogre::Vector3 direction = quat * Ogre::Vector3::NEGATIVE_UNIT_Z; if ( direction.dotProduct( Ogre::Vector3::NEGATIVE_UNIT_Z ) < 0 ) { if ( pitch_ > Ogre::Math::HALF_PI ) { pitch_ = -Ogre::Math::HALF_PI + (pitch_ - Ogre::Math::HALF_PI); } else if ( pitch_ < -Ogre::Math::HALF_PI ) { pitch_ = Ogre::Math::HALF_PI - (-pitch_ - Ogre::Math::HALF_PI); } yaw_ = -yaw_; if ( direction.dotProduct( Ogre::Vector3::UNIT_X ) < 0 ) { yaw_ -= Ogre::Math::PI; } else { yaw_ += Ogre::Math::PI; } } normalizePitch(); normalizeYaw(); update(); }
Ogre::Vector3 ParticleChain::computeFrictionDamping(const Ogre::Vector3 &normal, const Ogre::Vector3 &velocity) { int minAxis = 0; if (normal.y*normal.y < normal.x*normal.x) minAxis = 1; Ogre::Vector3 axis(0, 0, 0); axis[minAxis] = 1; Ogre::Vector3 u = normal.crossProduct(axis); Ogre::Vector3 v = u.crossProduct(normal); return mFriction * (velocity.dotProduct(u) * u + velocity.dotProduct(v) * v); }
void Agent::updateLocomote(Ogre::Real deltaTime) { //This is the update locomotion from the previous assignment // Set idle animation if (mDirection == Ogre::Vector3::ZERO) { if (nextLocation()) { Ogre::Vector3 src = mBodyNode->getOrientation() * Ogre::Vector3::UNIT_Z; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { mBodyNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); mBodyNode->rotate(quat); } setBaseAnimation(ANIM_RUN_BASE); setTopAnimation(ANIM_RUN_TOP); } } else { Ogre::Real move = mWalkSpeed * deltaTime; mDistance -= move; if (mDistance <= 0.0f) { mBodyNode->setPosition(mDestination); mDirection = Ogre::Vector3::ZERO; if (!nextLocation()) { setBaseAnimation(ANIM_IDLE_BASE); setTopAnimation(ANIM_IDLE_TOP); } else { Ogre::Vector3 src = mBodyNode->getOrientation() * Ogre::Vector3::UNIT_Z; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { mBodyNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); mBodyNode->rotate(quat); } } } else { mBodyNode->translate(mDirection * move); } } }
void AnimalThirdPersonControler::alignAnimalToCamera(double i_timeSinceLastFrame) { if (!(m_pAnimal != nullptr && m_pCamera != nullptr)) return; // Find new orientation of avatar Ogre::Vector3 camY = m_pCamera->getDirection(); Ogre::Vector3 aniX = m_pAnimal->m_pNode->getOrientation().xAxis(); Ogre::Vector3 aniY = m_pAnimal->m_pNode->getOrientation().yAxis(); Ogre::Vector3 newAniX = camY.dotProduct(aniX) * aniX + camY.dotProduct(aniY) * aniY; if (newAniX.isZeroLength()) return; newAniX.normalise(); m_pAnimal->turn(newAniX, i_timeSinceLastFrame); }
bool PhysicsEngine::PerformSphereSphereCollisionTest(const Ogre::FrameEvent &fe, PhysicsEntity *sphere1, PhysicsEntity *sphere2) { float overlapMagnitude = (sphere1->GetRadius() + sphere2->GetRadius()) - sphere1->getPosition().distance(sphere2->getPosition()); if (overlapMagnitude >= 0.0f) { Ogre::Vector3 d = sphere2->getPosition() - sphere1->getPosition(); Ogre::Vector3 relativeVelocity = d * (d.dotProduct(sphere1->GetVelocity() - sphere2->GetVelocity()) / d.squaredLength()); Ogre::Vector3 impulse1 = (((sphere2->GetRestitution() + 1.0f) * relativeVelocity) / ((1 / sphere1->GetMass()) + (1 / sphere2->GetMass()))); Ogre::Vector3 impulse2 = -(((sphere1->GetRestitution() + 1.0f) * relativeVelocity) / ((1 / sphere1->GetMass()) + (1 / sphere2->GetMass()))); //Ogre::Vector3 currentImpulse1 = d * (d.dotProduct(sphere1->GetAppliedForce()) / d.squaredLength()); //Ogre::Vector3 currentImpulse2 = -d * (d.dotProduct(sphere2->GetAppliedForce()) / d.squaredLength()); if (sphere1->GetBodyType() == ENTITY_BODY_SPHERE && sphere2->GetBodyType() == ENTITY_BODY_SPHERE) { sphere1->translate(-d.normalisedCopy() * (overlapMagnitude / 2.0f)); sphere2->translate(d.normalisedCopy() * (overlapMagnitude / 2.0f)); sphere2->ApplyForce(impulse1);// + currentImpulse1); sphere1->ApplyForce(impulse2);// + currentImpulse2); } sphere1->Collide(fe, sphere2); sphere2->Collide(fe, sphere1); return true; } return false; }
void CharacterController::UpdateAI(float dt) { _CurrentPathAge += dt; //std::cerr << _CurrentPathIndex << " / " << _CurrentPath.size() << "\n"; if (_CurrentPathIndex + 2 <= _CurrentPath.size()) { Ogre::Vector3 const & a = _CurrentPath[_CurrentPathIndex]; Ogre::Vector3 const & b = _CurrentPath[_CurrentPathIndex+1]; Ogre::Vector3 const & m = GetPosition(); Ogre::Vector3 const ab = b - a; Ogre::Vector3 const am = m - a; Ogre::Vector3 const mb = b - m; Ogre::Vector3 const mb_unit = mb.normalisedCopy(); float remaining = mb.length(); for(size_t i = _CurrentPathIndex + 1; i + 2 <= _CurrentPath.size(); ++i) { remaining += _CurrentPath[i].distance(_CurrentPath[i+1]); } //std::cerr << "Remaining distance: " << remaining << " m\n"; float lambda = am.dotProduct(ab) / ab.squaredLength(); //const float tau = 0.1; SetVelocity(_CurrentVelocity * mb_unit); if (lambda > 1) _CurrentPathIndex++; /*if (_CurrentPathIndex + 1 == _CurrentPath.size()) SetVelocity(Ogre::Vector3(0.));*/ } }
void OrientationControlet::injectMousePress(int _absx, int _absy, MyGUI::MouseButton _id) { if( _id == MyGUI::MouseButton::Left ){ string name = Game::getSingleton().pickMovableObject( _absx,_absy ); if( name == mName ){ mPick = true; mMouseX = _absx; mMouseY = _absy; InputFilter::getSingleton().setCapture(); /*判断开始时候,控制球是在球的正面还是背面 */ Game& game = Game::getSingleton(); int sx = game.getScreenWidth(); int sy = game.getScreenHeight(); Ogre::Ray B = game.getCamera()->getCameraToViewportRay( (Ogre::Real)_absx/(Ogre::Real)sx, (Ogre::Real)_absy/(Ogre::Real)sy ); Ogre::Matrix3 m3 = mNode->getLocalAxes(); Ogre::Vector3 axis = m3.GetColumn(2); if( axis.dotProduct(B.getDirection())>0 ) mNearFar = false; else mNearFar = true; } } }
bool DeferredLight::isCameraInsideLight(Ogre::Camera* camera) { switch (parentLight->getType()) { case Ogre::Light::LT_DIRECTIONAL: return false; case Ogre::Light::LT_POINT: { Ogre::Real distanceFromLight = camera->getDerivedPosition().distance(parentLight->getDerivedPosition()); // Small epsilon fix to account for the fact that we aren't a true sphere. return distanceFromLight <= radius + camera->getNearClipDistance() + 0.1; } case Ogre::Light::LT_SPOTLIGHT: { Ogre::Vector3 lightPos = parentLight->getDerivedPosition(); Ogre::Vector3 lightDir = parentLight->getDerivedDirection(); Ogre::Radian attAngle = parentLight->getSpotlightOuterAngle(); // Extend the analytic cone's radius by the near clip range by moving its tip accordingly. Ogre::Vector3 clipRangeFix = -lightDir * (camera->getNearClipDistance() / Ogre::Math::Tan(attAngle/2)); lightPos = lightPos + clipRangeFix; Ogre::Vector3 lightToCamDir = camera->getDerivedPosition() - lightPos; Ogre::Real distanceFromLight = lightToCamDir.normalise(); Ogre::Real cosAngle = lightToCamDir.dotProduct(lightDir); Ogre::Radian angle = Ogre::Math::ACos(cosAngle); // Check whether we will see the cone from our current POV. return (distanceFromLight <= (parentLight->getAttenuationRange() + clipRangeFix.length())) && (angle <= attAngle); } default: return false; } }
//-------------------------------------------------------------------------------- bool CMultiSelEditor::_setScale(OgitorsPropertyBase* property, const Ogre::Vector3& val) { if(val.x == 0.0f || val.y == 0.0f || val.z == 0.0f) return false; Ogre::Vector3 scale; Ogre::Vector3 diff = val / mNode->getScale(); mNode->setScale(val); Ogre::Vector3 groupPos = mNode->getPosition(); NameObjectPairList::const_iterator it = mModifyList.begin(); while(it != mModifyList.end()) { if(!it->second->getLocked()) { Ogre::Vector3 newpos = it->second->getDerivedPosition() - groupPos; Ogre::Vector3 AxisX = mNode->getOrientation() * Ogre::Vector3::UNIT_X; Ogre::Vector3 AxisY = mNode->getOrientation() * Ogre::Vector3::UNIT_Y; Ogre::Vector3 AxisZ = mNode->getOrientation() * Ogre::Vector3::UNIT_Z; Ogre::Vector3 vPos1 = (AxisX.dotProduct(newpos) * AxisX); Ogre::Vector3 vPos2 = (AxisY.dotProduct(newpos) * AxisY); Ogre::Vector3 vPos3 = (AxisZ.dotProduct(newpos) * AxisZ); newpos = (vPos1 * diff.x) + (vPos2 * diff.y) + (vPos3 * diff.z) + groupPos; it->second->setDerivedPosition(newpos); if(it->second->getProperties()->hasProperty("scale")) { it->second->getProperties()->getValue("scale",scale); scale = it->second->getDerivedOrientation().Inverse() * scale; scale = mNode->getOrientation() * scale; scale *= diff; scale = mNode->getOrientation().Inverse() * scale; scale = it->second->getDerivedOrientation() * scale; it->second->getProperties()->setValue("scale", scale); } } it++; } return true; }
void GameObject::setDirection(const Ogre::Vector3 direction) { Ogre::Radian r = initialForward.angleBetween(direction); Ogre::Vector3 initialRight = initialForward.crossProduct(Ogre::Vector3::UNIT_Y); if (initialRight.dotProduct(direction) > 0.f) r *= -1; setYaw(r); }
//helper function inline Ogre::Real intersect(Ogre::Plane* plane,const Ogre::Vector3 &p1, const Ogre::Vector3 &p2) { Ogre::Vector3 dir = p2 - p1; Ogre::Real den = dir.dotProduct(plane->normal); if(den == 0) return 1e20f; return -plane->getDistance(p1) / den; }
//----------------------------------------------------------------------------- bool IntermediateTutorial1::frameRenderingQueued(const Ogre::FrameEvent &evt) { if (mDirection == Ogre::Vector3::ZERO) { if (nextLocation()) { // Set walking animation mAnimationState = mEntity->getAnimationState("Walk"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); rotateRobotToDirection(); } } else { Ogre::Real move = mWalkSpeed * evt.timeSinceLastFrame; mDistance -= move; if (mDistance <= 0.0f) { mNode->setPosition(mDestination); mDirection = Ogre::Vector3::ZERO; // Set animation based on if the robot has another point to walk to. if (!nextLocation()) { // Set Idle animation mAnimationState = mEntity->getAnimationState("Idle"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); } else { // Rotation Code will go here later Ogre::Vector3 src = mNode->getOrientation() * Ogre::Vector3::UNIT_X; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { mNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); mNode->rotate(quat); } // else } } else { mNode->translate(mDirection * move); } // else } // if mAnimationState->addTime(evt.timeSinceLastFrame); return BaseApplication::frameRenderingQueued(evt); }
void SkyDome::setSunDirection (const Ogre::Vector3& sunDir) { float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y); elevation = elevation * 0.5 + 0.5; Ogre::Pass* pass = mMaterial->getBestTechnique()->getPass(0); if (mShadersEnabled) { mParams.sunDirection.set(mParams.vpParams, sunDir.normalisedCopy()); mParams.offset.set(mParams.fpParams, elevation); } else { Ogre::TextureUnitState* gradientsTus = pass->getTextureUnitState(0); gradientsTus->setTextureUScroll (elevation); } }
void LandVehicleSimulation::UpdateVehicle(Actor* vehicle, float seconds_since_last_frame) { if (vehicle->isBeingReset() || vehicle->ar_physics_paused || vehicle->ar_replay_mode) return; #ifdef USE_ANGELSCRIPT if (vehicle->ar_vehicle_ai && vehicle->ar_vehicle_ai->IsActive()) return; #endif // USE_ANGELSCRIPT EngineSim* engine = vehicle->ar_engine; if (engine && engine->HasStarterContact() && engine->GetAutoShiftMode() == SimGearboxMode::AUTO && engine->getAutoShift() != EngineSim::NEUTRAL) { Ogre::Vector3 dirDiff = vehicle->getDirection(); Ogre::Degree pitchAngle = Ogre::Radian(asin(dirDiff.dotProduct(Ogre::Vector3::UNIT_Y))); if (std::abs(pitchAngle.valueDegrees()) > 2.0f) { if (engine->getAutoShift() > EngineSim::NEUTRAL && vehicle->ar_avg_wheel_speed < +0.02f && pitchAngle.valueDegrees() > 0.0f || engine->getAutoShift() < EngineSim::NEUTRAL && vehicle->ar_avg_wheel_speed > -0.02f && pitchAngle.valueDegrees() < 0.0f) { // anti roll back in SimGearboxMode::AUTO (DRIVE, TWO, ONE) mode // anti roll forth in SimGearboxMode::AUTO (REAR) mode float g = std::abs(App::GetSimTerrain()->getGravity()); float downhill_force = std::abs(sin(pitchAngle.valueRadians()) * vehicle->getTotalMass()) * g; float engine_force = std::abs(engine->GetTorque()) / vehicle->getAvgPropedWheelRadius(); float ratio = std::max(0.0f, 1.0f - (engine_force / downhill_force)); if (vehicle->ar_avg_wheel_speed * pitchAngle.valueDegrees() > 0.0f) { ratio *= sqrt((0.02f - vehicle->ar_avg_wheel_speed) / 0.02f); } vehicle->ar_brake = sqrt(ratio); } } else if (vehicle->ar_brake == 0.0f && !vehicle->ar_parking_brake && engine->GetTorque() == 0.0f) { float ratio = std::max(0.0f, 0.2f - std::abs(vehicle->ar_avg_wheel_speed)) / 0.2f; vehicle->ar_brake = ratio; } } if (vehicle->cc_mode) { LandVehicleSimulation::UpdateCruiseControl(vehicle, seconds_since_last_frame); } if (vehicle->sl_enabled) { LandVehicleSimulation::CheckSpeedLimit(vehicle, seconds_since_last_frame); } }
void Tank::rotateTank(void){ Ogre::Vector3 src = mTankBodyNode->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_X; if ((1.0 + src.dotProduct(mDirection)) < 0.0001) { mTankBodyNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); mTankBodyNode->rotate(quat); } }
void Robot::updateLocomote(Ogre::Real deltaTime) { if (mDirection == Ogre::Vector3::ZERO) { if (this->nextLocation()) // a check to see if there is another destination { // Set walking animation this->setBaseAnimation(ANIM_WALK, true); this->setTopAnimation(ANIM_WALK, true); } } else { Ogre::Real move = mWalkSpeed * deltaTime; mDistance -= move; if (mDistance <= 0.0f) // over shooting target { this->mBodyNode->setPosition(mDestination); mDirection = Ogre::Vector3::ZERO; if (!this->nextLocation()) // a check to see if there is another destination { this->setBaseAnimation(ANIM_IDLE, true); this->setTopAnimation(ANIM_IDLE, true); } else { Ogre::Vector3 src = this->mBodyNode->getOrientation() * Ogre::Vector3::UNIT_X; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { this->mBodyNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); this->mBodyNode->rotate(quat); this->mBodyNode->yaw(Ogre::Degree(angleOffset)); // To correct for models facing different directions } } } else { this->mBodyNode->translate(mDirection * move); } } }
void PlayerCharacter::updateCharDirMoving(Ogre::Vector3* moveDir, unsigned long timeSinceLastFrame, Ogre::Radian &moveDirRotAngle) { // TODO: Running backwards goes crazy, turn too fast // http://www.ogre3d.org/tikiwiki/Intermediate+Tutorial+1&structure=Tutorials // http://www.ogre3d.org/tikiwiki/Quaternion+and+Rotation+Primer#Q._How_can_I_make_my_objects_rotate_smoothly_You_mentioned_slerp_etc_ if(!moveDir->isZeroLength()) { Ogre::Vector3 src = this->node->getOrientation() * Ogre::Vector3::UNIT_Z; if ((1.0f + src.dotProduct(*moveDir)) < 0.0001f) { this->node->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(*moveDir); this->node->rotate(quat); } } /*if(!moveDir->isZeroLength()) { //Ogre::Vector3 targetDirection = Quaternion(moveDirRotAngle, Vector3::UNIT_Y) * (*moveDir); Ogre::Vector3 targetDirection = this->node->getOrientation() * *moveDir; Ogre::Radian rotationDifferenceToLastValue = m_lastTargetDirection.angleBetween(targetDirection); Radian targetRotation; if(rotationDifferenceToLastValue > Ogre::Radian(Ogre::Real(0.001))) { Quaternion rotationQuat = (Vector3::UNIT_X).getRotationTo(targetDirection, Vector3::UNIT_Y); Radian targetRotation = rotationQuat.getYaw(); //m_moveRotation.setTargetValueWithAdjustedSmoothtime(targetRotation, true); m_moveRotation = targetRotation; } //m_moveRotation.smoothValue(timeSinceLastFrame); this->node->setDirection(0,0,-1, Node::TS_WORLD); this->node->yaw(m_moveRotation); m_lastTargetDirection = targetDirection; }*/ }
QColor SparseOccupancyGridArrayDisplay::axisColor(const Ogre::Quaternion& q, const Ogre::Vector3& p) { Ogre::Vector3 zaxis = q.zAxis(); Ogre::Vector3 reference = p.normalisedCopy(); double dot = zaxis.dotProduct(reference); if (dot < -1) { dot = -1.0; } else if (dot > 1) { dot = 1.0; } double scale = (dot + 1) / 2.0; return gridColor(scale); }
Ogre::Vector3 InteractiveMarkerControl::closestPointOnLineToPoint( const Ogre::Vector3& line_start, const Ogre::Vector3& line_dir, const Ogre::Vector3& test_point ) { // Find closest point on projected ray to mouse point // Math: if P is the start of the ray, v is the direction vector of // the ray (not normalized), and X is the test point, then the // closest point on the line to X is given by: // // (X-P).v // P + v * ------- // v.v // where "." is the dot product. double factor = ( test_point - line_start ).dotProduct( line_dir ) / line_dir.dotProduct( line_dir ); Ogre::Vector3 closest_point = line_start + line_dir * factor; return closest_point; }
//------------------------------------------------------------------------------------- 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 Tank::wanderMovement(const float & deltaTime) { if(wanderPathCreated) { if (mWanderDirection == Ogre::Vector3::ZERO) { wanderNextLocation(); } else { Ogre::Real move = mMoveSpd * (deltaTime); mWanderDistance -= move; Ogre::Vector3 src = mTankBodyNode->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_X; //http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Quaternion+and+Rotation+Primer //this is used for rotation of the tank if ((1.0 + src.dotProduct(mWanderDirection)) < 0.0001) { mTankBodyNode->yaw(Ogre::Degree(180)); } else { mWanderDirection.y = 0; Ogre::Quaternion quat = src.getRotationTo(mWanderDirection); Ogre::Quaternion mOrientSrc = mTankBodyNode->getOrientation(); Ogre::Quaternion mOrientDest = quat * mOrientSrc; Ogre::Quaternion delta = Ogre::Quaternion::nlerp(deltaTime * 2.0, mOrientSrc, mOrientDest, true); mTankBodyNode->setOrientation(delta); } //for movement if (mWanderDistance <= 0) { mTankBodyNode->setPosition(mWanderDestination); mWanderDirection = Ogre::Vector3::ZERO; } else { mTankBodyNode->translate(move * mWanderDirection); } } } }
Ogre::Vector3 SteeringBehaviors::Pursuit(Character* evader) { Ogre::Vector3 ToEvader = evader->GetSceneNode()->getPosition() - mCharacter->GetSceneNode()->getPosition(); double RelativeHeading = mCharacter->GetHeading().dotProduct(evader->GetHeading()); //if evader is in front and moving forward if((ToEvader.dotProduct(mCharacter->GetHeading())) > 0 && (RelativeHeading < -0.95)) return Seek(evader->GetSceneNode()->getPosition()); //not ahead so look forward double LookAheadTime = ToEvader.length() / (mCharacter->GetMaxSpeed() + evader->GetMaxSpeed()); Ogre::Vector3 currentVelocity = evader->GetVelocity(); Ogre::Real currentSpeed = currentVelocity.normalise(); Ogre::Vector3 desiredVelocity = Seek(evader->GetSceneNode()->getPosition() + evader->GetHeading() * currentSpeed * LookAheadTime); desiredVelocity.y = 0; return desiredVelocity; }
void BaseNpc::update(double timeSinceLastFrame) { mAnimationState->addTime(timeSinceLastFrame); if (m_pCurrentState) { m_pCurrentState->Execute(this); } if (isMoving == false) { if (nextLocation(m_pNpcNode)) { // Set walking animation mAnimationState = m_pNPC->getAnimationState("Walk"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); isMoving = true; } else { // Not moving and no locations in list } } else { Ogre::Real move = mWalkSpeed * timeSinceLastFrame; mDistance -= move; if (mDistance <= 0.0f) { m_pNpcNode->setPosition(mDestination); mDirection = Ogre::Vector3::ZERO; // Set animation based on if the robot has another point to walk to. if (! nextLocation(m_pNpcNode)) { // Set Idle animation mAnimationState = m_pNPC->getAnimationState("Idle"); mAnimationState->setLoop(true); mAnimationState->setEnabled(true); } else { Ogre::Vector3 src = m_pNpcNode->getOrientation() * Ogre::Vector3::UNIT_X; if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) { m_pNpcNode->yaw(Ogre::Degree(180)); } else { Ogre::Quaternion quat = src.getRotationTo(mDirection); m_pNpcNode->rotate(quat); } } } else { m_pNpcNode->translate(mDirection * move); } } }
void OrbitCamera::calculatePitchYawFromPosition( const Ogre::Vector3& position ) { float x = position.x - focal_point_.x; float y = position.y - focal_point_.y; pitch_ = acos( y / distance_ ); normalizePitch(); float val = x / ( distance_ * sin( pitch_ ) ); yaw_ = acos( val ); Ogre::Vector3 direction = focal_point_ - position; if ( direction.dotProduct( Ogre::Vector3::NEGATIVE_UNIT_Z ) < 0 ) { yaw_ = Ogre::Math::TWO_PI - yaw_; } }
void OrbitCamera( ::Ogre::Camera* a_pCam, const::Ogre::Sphere& a_subjSphr, const Ogre::Vector2& a_curTouch, const Ogre::Vector2& a_oldTouch ) { const ::Ogre::Vector3& center = a_subjSphr.getCenter() ; const ::Ogre::Vector3& camPos = a_pCam->getPosition() ; // gets the current looked-at point. const ::Ogre::Vector3 camDir = a_pCam->getDirection().normalisedCopy() ; const ::Ogre::Vector3 lookAt = camDir.dotProduct(center - camPos) * camDir + camPos ; // makes the ray from the subjet's center to the camera. const ::Ogre::Ray rayToCam( center, (camPos - center).normalisedCopy() ) ; // makes the tangent plane to the sphere whose normal is toward the camera. ::Ogre::Real fDist = center.distance( camPos ) ; if( a_subjSphr.getRadius() < fDist ) fDist = a_subjSphr.getRadius() ; const ::Ogre::Plane touchPlane( rayToCam.getDirection(), fDist ) ; // gets the touch points on the plane. ::Ogre::Ray ray ; a_pCam->getCameraToViewportRay( a_curTouch.x, a_curTouch.y, &ray ) ; const ::Ogre::Vector3 curPoint = ray.getPoint( ray.intersects( touchPlane ).second ) ; a_pCam->getCameraToViewportRay( a_oldTouch.x, a_oldTouch.y, &ray ) ; const ::Ogre::Vector3 oldPoint = ray.getPoint( ray.intersects( touchPlane ).second ) ; // gets the quaternion. const ::Ogre::Quaternion qtnn = (curPoint - center).getRotationTo( oldPoint - center ) ; // gets the new camera position. ::Ogre::Vector3 newCamPos = camPos - center ; newCamPos = qtnn * newCamPos ; newCamPos += center ; // gest the new look-at. ::Ogre::Vector3 newLookAt = lookAt - center ; newLookAt = qtnn * newLookAt ; newLookAt += center ; // sets the camera to the new properties. a_pCam->setPosition( newCamPos ) ; a_pCam->lookAt( newLookAt ) ; }
void InteractiveMarkerControl::rotate( Ogre::Ray &mouse_ray ) { Ogre::Vector3 intersection_3d; Ogre::Vector2 intersection_2d; float ray_t; Ogre::Vector3 rotation_axis = control_frame_orientation_at_mouse_down_ * control_orientation_.xAxis(); // Find rotation_center = 3D point closest to grab_point_ which is // on the rotation axis, relative to the reference frame. Ogre::Vector3 rotation_center = closestPointOnLineToPoint( control_frame_node_->getPosition(), rotation_axis, grab_point_ ); // Find intersection of mouse_ray with plane centered at rotation_center. if( intersectSomeYzPlane( mouse_ray, rotation_center, control_frame_orientation_at_mouse_down_, intersection_3d, intersection_2d, ray_t )) { // Find rotation Ogre::Vector3 grab_rel_center = grab_point_ - rotation_center; Ogre::Vector3 mouse_rel_center = intersection_3d - rotation_center; Ogre::Quaternion orientation_change_since_mouse_down = grab_rel_center.getRotationTo( mouse_rel_center, rotation_axis ); Ogre::Radian rot; Ogre::Vector3 axis; orientation_change_since_mouse_down.ToAngleAxis( rot, axis ); // Quaternion::ToAngleAxis() always gives a positive angle. The // axis it emits (in this case) will either be equal to // rotation_axis or will be the negative of it. Doing the // dot-product then gives either 1.0 or -1.0, which is just what // we need to get the sign for our angle. Ogre::Radian rotation_since_mouse_down = rot * axis.dotProduct( rotation_axis ); rotation_ = rotation_at_mouse_down_ + rotation_since_mouse_down; parent_->setPose( parent_->getPosition(), orientation_change_since_mouse_down * parent_orientation_at_mouse_down_, name_ ); } }