CirclePath::CirclePath(const Quaternion& baseRotation, const Point4& startPosition, const Point4& orbitPosition, bool clockWise, const Vector3& rotationAxis) : Path(baseRotation), startPosition(startPosition), orbitPosition(orbitPosition), clockWise(clockWise) { Vector3 orbitDirection = orbitPosition - startPosition; radius = orbitDirection.length(); orbitDirection = orbitDirection.normalize(); // Vector3 normalizedRotationAxis = rotationAxis.normalize(); Vector3 testAxis = orbitDirection.cross(normalizedRotationAxis); if (testAxis.length() == 0.0f) { testAxis = Vector3(1.0f, 0.0f, 0.0f).cross(orbitDirection); if (testAxis.length() == 0.0f) { this->rotationAxis = Vector3(0.0f, 1.0f, 0.0f); } else { this->rotationAxis = testAxis; } } else { this->rotationAxis = rotationAxis; } elapsedAngle = 0.0f; }
void Agent::orderPathFollow() { if (mPath->GetLength() > 0) { this->SetMaxSpeed(10); //find closest node float min = 999999; for (int i=0;i<mPath->GetLength();i++) { Vector3 delta = Position - mPath->GetNode(i).getPos(); if (delta.length() < min) { min = delta.length(); PathPosition = i; } } //go this->resetBehavior(); this->pathOn(); //avoid obstacles,walls & seperate //this->avoid1On(); //this->avoid2On(); this->avoid3On(); //this->seperateOn(); } }
//----------------------------------------------------------------------------- //! 外接円を計算する //! @param [in] p1 座標1 //! @param [in] p2 座標2 //! @param [in] p3 座標3 //! @retval 球構造体 //----------------------------------------------------------------------------- Sphere& CascadedShadow::calcCircumscribeCircle(Vector3& p1, Vector3& p2, Vector3& p3) { //---- 外心を求める Vector3 center = calcCircleCenter(p1, p2, p3); #if 1 //---- 半径を求める // 3辺の長さを計算 Vector3 A = (p1 - p2); Vector3 B = (p2 - p3); Vector3 C = (p3 - p1); f32 a = A.length(); f32 b = B.length(); f32 c = C.length(); // 余弦定理で角度を求める Radian radA = calcLawOfCosines(a, b, c); // 正弦定理で半径を求める // 2R = a / sinA f32 R = (a / sinf(radA)) / 2.0f; #else // 求めた外心から半径を計算する f32 R = (center - p1).length(); #endif // 求まった球の情報を返す return Sphere(center, R); }
void testPointUnitSphere( const math::Vector3<real_t> & p ) { static const geometry::Sphere UNIT_SPHERE( math::Vector3<real_t>( 0, 0, 0 ), real_t( 1 ) ); static const real_t EPSILON = real_t(1e-4); real_t q = lbm::intersectionRatioBisection( UNIT_SPHERE, p, -p, EPSILON ); Vector3<real_t> intersectionPoint = p + (-p) * q; real_t intersectionRadius = intersectionPoint.length(); WALBERLA_CHECK_LESS( std::fabs( intersectionRadius - real_t( 1 ) ), EPSILON ); q = lbm::intersectionRatioSphere( UNIT_SPHERE, p, -p ); intersectionPoint = p + ( -p ) * q; intersectionRadius = intersectionPoint.length(); WALBERLA_CHECK_LESS( std::fabs( intersectionRadius - real_t( 1 ) ), EPSILON ); q = lbm::intersectionRatio( UNIT_SPHERE, p, -p, EPSILON ); intersectionPoint = p + ( -p ) * q; intersectionRadius = intersectionPoint.length(); WALBERLA_CHECK_LESS( std::fabs( intersectionRadius - real_t( 1 ) ), EPSILON ); }
void CInterpolation::onFixedTick(unsigned int msecs){ _msecs = msecs; //si no estamos interpolando, gl if(!_interpolating) return; //lo primero de todo, movemos la posición del servidor para poder interpolar con más exactitud Vector3 newPos; //calculamos la direccion en la que debemos interpolar Vector3 direction = (_serverDirection*Vector3(1,0,1)).normalisedCopy(); //calculamos el movimiento que debe hacer el monigote, mucho mas lento del que debe hacer de normal direction*=(_entity->getComponent<CAvatarController>("CAvatarController")->getVelocity()*Vector3(1,0,1)).length()*0.25f; //si nos hemos pasado, debemos moverlo al sitio if(direction.length() > _distance){ direction*=(_distance/direction.length()); } _entity->getComponent<CPhysicController>("CPhysicController")->move(direction,msecs); _distance -= direction.length(); //si hemos terminado de interpolar, lo dejamos if((_distance < _minDistance)){ _interpolating = false; } }
Vector3 TunnelSlice::requestWallMove(Direction dir, float offset) const { Vector3 move = rot * requestWallDistance(dir); move = move * ((move.length() - offset) / move.length()); return move; }
/////////////////////////////////////////////////////////////////////// // Driver int main(int argc, char* argv[]) { if ( argc != 8 ) { printf("Set the grid v = a + b*D^c, where d is the D distance to the nearest atom.\n"); printf("For D < cutDistance, v = a + b*cutDistance^c.\n"); printf("Usage: %s srcGrid pointFile aOffset bScale cExponent cutDistance outFile\n", argv[0]); return 0; } const char* srcFile = argv[1]; const char* pointFile = argv[2]; const double aOffset = strtod(argv[3], NULL); const double bScale = strtod(argv[4], NULL); const double cExp = strtod(argv[5], NULL); const double cutDist = strtod(argv[6], NULL); const char* outFile = argv[argc-1]; // Load the grids. Grid src(srcFile); src.zero(); int n = src.length(); printf("Loaded `%s' which contains %d nodes.\n", srcFile, n); // Load the coordinates. printf("Loading the coordinates.\n"); Scatter pos(pointFile); int posNum = pos.length(); printf("Loaded %d points from `%s'.\n", pos.length(), pointFile); int complete = 0; for (int i = 0; i < n; i++) { Vector3 r = src.getPosition(i); Vector3 dr = pos.get(0)-r; double minDist = dr.length(); for (int j = 1; j < posNum; j++) { Vector3 dr = pos.get(j)-r; double dist = dr.length(); if (dist < minDist) minDist = dist; } if (minDist < cutDist) minDist = cutDist; double v = powerLaw(minDist, aOffset, bScale, cExp); src.setValue(i, v); // Write the progress. int comp = (100*i)/n; if (abs(complete - comp) >= 5) { printf("%d percent complete\n", comp); complete = comp; } } char comments[256]; sprintf(comments, "%s distance map", srcFile); src.write(outFile, comments); printf("Wrote `%s'.\n", outFile); return 0; }
void CameraBehaviorVehicleSpline::update(const CameraManager::CameraContext &ctx) { if ( ctx.mCurrTruck->free_camerarail <= 0 ) { gEnv->cameraManager->switchToNextBehavior(); return; } Vector3 dir = (ctx.mCurrTruck->nodes[ctx.mCurrTruck->cameranodepos[0]].smoothpos - ctx.mCurrTruck->nodes[ctx.mCurrTruck->cameranodedir[0]].smoothpos).normalisedCopy(); targetPitch = 0.0f; if ( camPitching ) { targetPitch = -asin(dir.dotProduct(Vector3::UNIT_Y)); } if ( ctx.mCurrTruck->getAllLinkedBeams().size() != numLinkedBeams ) { createSpline(ctx); } updateSpline(); updateSplineDisplay(); camLookAt = spline->interpolate(splinePos); if ( RoR::Application::GetInputEngine()->isKeyDown(OIS::KC_LSHIFT) && RoR::Application::GetInputEngine()->isKeyDownValueBounce(OIS::KC_SPACE) ) { autoTracking = !autoTracking; #ifdef USE_MYGUI if ( autoTracking ) { RoR::Application::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_NOTICE, _L("auto tracking enabled"), "camera_go.png", 3000); } else { RoR::Application::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_NOTICE, _L("auto tracking disabled"), "camera_go.png", 3000); } #endif // USE_MYGUI } if ( autoTracking ) { Vector3 centerDir = ctx.mCurrTruck->getPosition() - camLookAt; if ( centerDir.length() > 1.0f ) { centerDir.normalise(); Radian oldTargetDirection = targetDirection; targetDirection = -atan2(centerDir.dotProduct(Vector3::UNIT_X), centerDir.dotProduct(-Vector3::UNIT_Z)); if ( targetDirection.valueRadians() * oldTargetDirection.valueRadians() < 0.0f && centerDir.length() < camDistMin) { camRotX = -camRotX; } } } CameraBehaviorOrbit::update(ctx); }
void UtilGL::drawArrow(const Vector3 &a,const Vector3 &u,double radius,const string &s1,const string &s2,double arrowRatio) { Vector3 to; to.set(a+u*(1.0-arrowRatio)); glPushMatrix(); glPushMatrix(); glTranslatef(a.x(),a.y(),a.z()); if (!s1.empty()) { draw(s1,Vector3(0,0,0)); } glPopMatrix(); glPushMatrix(); Quaternion q; q.set(Vector3(0,0,1),u); double angle;Vector3 axe; q.copyToAngleAxis(&angle,&axe); glTranslatef(a.x(),a.y(),a.z()); glRotatef(angle,axe.x(),axe.y(),axe.z()); glScalef(radius,radius,(to-a).length()); drawCylinder(); glPopMatrix(); if (u.length()>0.0001) { glPushMatrix(); glTranslatef((a+u).x(),(a+u).y(),(a+u).z()); glPushMatrix(); if (!s2.empty()) { draw(s2,Vector3(0.2*arrowRatio*u.length(),0.03,0)); } glPopMatrix(); glPopMatrix(); glPushMatrix(); glTranslatef(to.x(),to.y(),to.z()); Matrix4 m; m.setRotation(Vector3(0,0,1),u); glMultMatrixf(m.fv()); drawCone(radius*(2.0-arrowRatio),u.length()*arrowRatio); glPopMatrix(); } glPopMatrix(); }
bool PawnMovementAnimation::animate(const Real& timeSinceLastFrame) { Real distanceMoved = MOVEMENT_SPEED * timeSinceLastFrame; Vector3 path = mDestination - mAnimatedNode->getPosition(); if (path.length() > distanceMoved) { if (path.length() < 280 && mAttackDuration > 0) { if (!mParticleNode) { createBlasts(); mAnimationManager->addAnimation( AnimationFactory::createDyingAnimation( mTargetPiece, mSceneMgr, 2, 1.5)); mAnimationManager->addAnimation( AnimationFactory::createBleedingAnimation( mTargetPiece, mSceneMgr, 1, 1.5)); } if (mParticleNode->getPosition().y + mTargetPiece->getPosition().y >= 0) { if (mParticleNode->getPosition().y < 200) { mTargetPiece->translate(0, -timeSinceLastFrame * 600, 0); mParticleNode->translate(0, -timeSinceLastFrame * 400, 0); } else { mParticleNode->translate(0, -timeSinceLastFrame * 1000, 0); } } else { playAnimation("eat", timeSinceLastFrame, false, mParticleNode, false); } mAttackDuration -= timeSinceLastFrame; } else { // Normalising the vector so the speed remains constant. path.normalise(); mAnimatedNode->translate(path * distanceMoved); Vector3 src = mAnimatedNode->getOrientation() * Vector3::UNIT_Z; mAnimatedNode->rotate(src.getRotationTo(path)); playAnimation("walk", timeSinceLastFrame); } return true; // Animation still running. } resetAnimation("walk"); mAnimatedNode->setPosition(mDestination); mAnimatedNode->setOrientation(mAnimatedNode->getInitialOrientation()); return false; // Animation finished. }
/// Pick marker //--------------------------------------------------------------------------------------------------------------- void SplineRoad::Pick(Camera* mCamera, Real mx, Real my, bool bRay, bool bAddH, bool bHide) { iSelPoint = -1; //if (vMarkNodes.size() != getNumPoints()) // return; // assert Ray ray = mCamera->getCameraToViewportRay(mx,my); // 0..1 const Vector3& pos = mCamera->getDerivedPosition(), dir = ray.getDirection(); const Plane& pl = mCamera->getFrustumPlane(FRUSTUM_PLANE_NEAR); Real plDist = FLT_MAX; const Real sphR = 2.4f; //par for (int i=0; i < (int)getNumPoints(); ++i) { // ray to sphere dist const Vector3& posSph = getPos(i); const Vector3 ps = pos - posSph; Vector3 crs = ps.crossProduct(dir); Real dist = crs.length() / dir.length(); // dist to camera Real plD = pl.getDistance(posSph); if (dist < sphR && plD > 0 && plD < plDist) { plDist = plD; iSelPoint = i; } } SelectMarker(bHide); // hide/show all markers int iHide = bHide ? 1 : 0; if (iHide != iOldHide) { iOldHide = iHide; for (size_t i=0; i < vMarkNodes.size(); ++i) vMarkNodes[i]->setVisible(bAddH); } // ray terrain hit pos if (bRay && ndHit && mTerrain) { std::pair<bool, Vector3> p = mTerrain->rayIntersects(ray); bHitTer = p.first; //ndHit->setVisible(bHitTer); posHit = p.second; if (bHitTer) { Vector3 pos = posHit; //if (iChosen == -1) // for new if (!newP.onTer && bAddH) pos.y = newP.pos.y; ndHit->setPosition(pos); } } }
void Matrix3<T>::normalize(void) { const float error = a * b; const Vector3<T> t0 = a - (b * (0.5f * error)); const Vector3<T> t1 = b - (a * (0.5f * error)); const Vector3<T> t2 = t0 % t1; a = t0 * (1.0f / t0.length()); b = t1 * (1.0f / t1.length()); c = t2 * (1.0f / t2.length()); }
/** * @param planeNormal Plane normal normalized * @param distanceToOrigin Distance to the origin. Positive if dot product between a vector from plane to origin and the normal is positive. */ template<class T> Plane<T>::Plane(const Vector3<T> &normal, T distanceToOrigin) : normal(normal), d(distanceToOrigin) { const T normalLength = normal.length(); if(normalLength < 0.9999f || normalLength > (T)1.0001) { std::ostringstream normalLengthStr; normalLengthStr << normal.length(); throw std::invalid_argument("Impossible to build a plane. Wrong normal length: " + normalLengthStr.str() + "."); } }
void RangeScanner::depth_image(SparseRangeScan* _scan, string _filename) { double** r_scan = new double*[sensor.resy()]; double** g_scan = new double*[sensor.resy()]; double** b_scan = new double*[sensor.resy()]; for(int y = 0; y < sensor.resy(); y++) { r_scan[y] = new double[sensor.resx()]; g_scan[y] = new double[sensor.resx()]; b_scan[y] = new double[sensor.resx()]; for(int x = 0; x < sensor.resx(); x++) { r_scan[y][x] = 0; g_scan[y][x] = 0; b_scan[y][x] = 0; } } double min_depth = 1e10, max_depth = -1e10; for(int p = 0; p < _scan->size(); p++) { PixelCoord pix = _scan->getPixel(p); Vector3 pt = _scan->getPt(p); double depth = pt.length(sensor.camera_pos()); min_depth = depth < min_depth ? depth : min_depth; max_depth = depth > max_depth ? depth : max_depth; } for(int p = 0; p < _scan->size(); p++) { PixelCoord pix = _scan->getPixel(p); Vector3 pt = _scan->getPt(p); double depth = pt.length(sensor.camera_pos()); double normalized_depth = 1.0 - ((depth-min_depth) / (max_depth-min_depth)); r_scan[pix.y][pix.x] = normalized_depth; g_scan[pix.y][pix.x] = normalized_depth; b_scan[pix.y][pix.x] = normalized_depth; } PNGImage png_image(_filename, sensor.resx(), sensor.resy()); for(int y = 0; y < sensor.resy(); y++) { for(int x = 0; x < sensor.resx(); x++) { double r = r_scan[y][x], g = g_scan[y][x], b = b_scan[y][x]; png_image.set_png_pixel(x,(sensor.resy()-y)-1, r, g, b); } } png_image.write_to_file(); for(int y = 0; y < sensor.resy(); y++) { delete [] r_scan[y]; delete [] g_scan[y]; delete [] b_scan[y]; } delete [] r_scan; delete [] g_scan; delete [] b_scan; }
void Vector3::interpolateDirection(const Vector3 &u,const Vector3 &v,double t) { // TODO : heavy computation Vector3 axis=p3d::cross(u,v); Quaternion q; if (axis.length()<0.00001) q.setIdentity(); else { double angle=u.angle(v,u.cross(v)); angle*=t; q=Quaternion::fromAngleAxis(toDegree(angle),axis); } double ilength=(1.0-t)*u.length()+t*v.length(); Vector3 uu=u; uu.normalize(); *this=(q*uu)*ilength; }
/* -------------------------------------------------------------------------- */ void LensFlare::update() { if (mHidden || !mLightNode) return; /// If the Light is out of the Camera field Of View, the lensflare is hidden. if (!mCamera->isVisible(getLightPosition())) { mHaloSet->setVisible(false); mBurstSet->setVisible(false); return; } Vector3 lightToCamera = mCamera->getDerivedPosition() - getLightPosition(); Vector3 CameraVect = lightToCamera.length() * mCamera->getDerivedDirection(); CameraVect += mCamera->getDerivedPosition(); // The LensFlare effect takes place along this vector. Vector3 LFvect = (CameraVect - getLightPosition()); // LFvect += Vector3(-64,-64,0); // sprite dimension (to be adjusted, but not necessary) // The different sprites are placed along this line. mHaloSet->getBillboard(0)->setPosition( LFvect); mHaloSet->getBillboard(1)->setPosition( LFvect*0.725); mHaloSet->getBillboard(2)->setPosition( LFvect*0.250); mBurstSet->getBillboard(0)->setPosition( LFvect*0.833); mBurstSet->getBillboard(1)->setPosition( LFvect*0.500); mBurstSet->getBillboard(2)->setPosition( LFvect*0.320); // We redraw the lensflare (in case it was previouly out of the camera field, and hidden) this->setVisible(true); }
AITackleAction::AITackleAction(const Player* p) : AIAction(mActionName, p) { // action to tackle the ball/opponent currently holding the ball float maxdist = 3.0f; Vector3 tacklevec = p->getMatch()->getBall()->getPosition() + p->getMatch()->getBall()->getVelocity() * 0.5f - p->getPosition(); float dist = tacklevec.length(); mScore = -1.0f; if(dist < maxdist) { Player* nearestopp = MatchHelpers::nearestOppositePlayerToBall(*p->getTeam()); if(nearestopp->standing()) { float oppdist = MatchEntity::distanceBetween(*p, *nearestopp); const float maxOppDist = 2.0f; if(oppdist < maxOppDist) { float distToOwnGoal = (MatchHelpers::ownGoalPosition(*p) - p->getMatch()->getBall()->getPosition()).length(); if(distToOwnGoal > 10.0f && distToOwnGoal < 80.0f) { mScore = 1.0f - (oppdist / maxOppDist); mScore *= mPlayer->getTeam()->getAITacticParameters().TackleActionCoefficient; } } } } mAction = boost::shared_ptr<PlayerAction>(new TacklePA(tacklevec)); }
void GameEngine::spawnProjectile(Vector3 dir) { //Creates a projectile! Projectile* obj; bool foundProjectile = false; for (unsigned int i = 0; i < m_projectiles->size(); i ++) { if (!m_projectiles->at(i)->getIsAlive()) { obj= m_projectiles->at(i); foundProjectile = true; break; } } if (!foundProjectile) return; obj->setIsAlive(true); obj->resetLifetime(); obj->getEmitter()->setPosition(m_camera->center); obj->getEmitter()->initParticles(); obj->getEmitter()->setIsAlive(true); //setting up rotation angle for drawing Vector3 orthVec = dir.cross(Vector3(5,0,0)); float angle = -acos(dir.dot(Vector3(1,0,0)) / dir.length()) * 180.0 / 3.14 + 180.0; obj->setRotation(orthVec, angle); obj->setPosition(m_camera->center); obj->setVelocity(dir); obj->setIsProjectile(); }
////////////////////////////////////////////////////////////////////////////////// //// Compute the current modelview matrix ////////////////////////////////////////////////////////////////////////////////// Matrix3 getModelviewMatrix() { up /= up.length(); // ensure the up vector is normalized // compute x, y, and z of modelview transform Vector3 foc = focus; //foc[1]*=-1; Vector3 f = (foc)-eye; f/=f.length(); Vector3 s = f.cross(up); Vector3 u = (s/s.length()).cross(f); // correct dimensions f = -f; return Matrix3(s, u, f); }
void construct_SD(size_t rId, size_t cId) const { complex<REAL> sij(0,0); complex<REAL> dij(0,0); for(size_t gi = 0;gi < spec_->nGaussPts;++ gi) { // gaussian point --> tri[rId] center Vector3<REAL> r = spec_->triCenters[rId] - spec_->gaussPts[cId][gi]; REAL lenr = r.length(); REAL lenr2 = lenr*lenr; // r^2 s_->derCacheS_[rId][cId][gi] = spec_->gaussWeights[cId][gi]* s_->crho_ / (4.*M_PI*lenr); /* ik*c*rho*exp(ikr)/(4*PI*r)*/ sij += complex<REAL>(0, s_->k_)*std::exp(complex<REAL>(0,s_->k_*lenr))* s_->derCacheS_[rId][cId][gi]; s_->derCacheD_[rId][cId][gi] = -spec_->gaussWeights[cId][gi] * r.dot(spec_->triNormals[cId]) / (4.*M_PI*lenr2*lenr); /* (-1+ikr)*exp(ikr)*-<r.n>/(4*PI*r^3) */ dij += complex<REAL>(-1,s_->k_*lenr)*std::exp(complex<REAL>(0,s_->k_*lenr))* s_->derCacheD_[rId][cId][gi]; } s_->B_(rId,cId) = sij; s_->A_(rId,cId) = dij; }
//----------------------------------------------------------------------------- //! 更新(プレイヤーの周りに常にいる) //! @param [in] player プレイヤー座標 //----------------------------------------------------------------------------- void AllyHealer::UpdateNormal(Vector3& playerPos) { // プレイヤーへのベクトル Vector3 dir = playerPos - _position; // 長さ取得 f32 length = dir.length(); Circle goalCircle(playerPos, 100.0f); Circle myCircle(_position, _radius); // プレイヤーの近くにいるかどうか if( goalCircle.isHit(myCircle) ){ // 近くにいる _speed = 0.0f; _keyUpdate = true; }else{ // 近くにいない _speed = 3.0f; dir = dir.normalize(); _rotation._y = atan2f(dir._x, dir._z); _keyUpdate = false; } }
bool BSPBoxSplitSelector::getSplitPlane( const util::Vector<BSPPolygon*>& polys, ProgressIndicator* progress, Vector4* splitPlane ) const { assert( polys.size() > 0 ); double workUnit = polys.size(); if ( progress ) progress->addProgress( workUnit ); OBBoxBuilder builder; while ( builder.nextPass() ) { for ( int i = 0 ; i < polys.size() ; ++i ) { const BSPPolygon& poly = *polys[i]; for ( int k = 0 ; k < poly.vertices() ; ++k ) builder.addPoints( &poly.getVertex(k), 1 ); } } OBBox box = builder.box(); Vector3 center = box.translation(); Vector3 normal = box.rotation().getColumn(0); *splitPlane = Vector4( normal.x, normal.y, normal.z, -center.dot(normal) ); return Math::abs(normal.length()-1.f) < 1e-3f && splitPlane->finite(); }
void operator() (const tbb::blocked_range<size_t>& range) { complex<REAL> localsum(0,0); const BIESpec* spec = s_->spec_; const complex<REAL> ik = complex<REAL>(0,s_->k_); for(size_t ii = range.begin(); ii != range.end(); ++ ii) { const complex<REAL>& P = s_->x_(ii); const complex<REAL>& dP = spec->normalVel(ii)*ik*s_->crho_; // triangle ii for(size_t gi = 0; gi < spec->nGaussPts; ++ gi) { // gaussian point --> tri[rId] center Vector3<REAL> r = pt_ - spec->gaussPts[ii][gi]; const REAL lenr = r.length(); const REAL lenr2 = r.length_sqr(); // r^2 const complex<REAL> expikr = std::exp(complex<REAL>(0,s_->k_*lenr)); localsum += expikr*dP * spec->gaussWeights[ii][gi] / (4.*M_PI*lenr); const REAL rdotny = r.dot(spec->triNormals[ii]); localsum += complex<REAL>(-1,s_->k_*lenr) * expikr * P * rdotny * spec->gaussWeights[ii][gi] / (4.*M_PI*lenr2*lenr); } } this->ret = localsum; }
void GravitationalForce::apply_fun(float d_t){ for (unsigned int i = 0; i < mInfluencedPhysics.size()-1; i++){ Physics* lhs = mInfluencedPhysics[i]; for (unsigned int j = i+1 ; j < mInfluencedPhysics.size(); j++){ Physics* rhs = mInfluencedPhysics[j]; Vector3 distanceVector = rhs->getPosition()-lhs->getPosition(); float distance = distanceVector.length(); //Distanz in Ordnung if (distance < getMaxDistance() && distance >= 1.0){ if(distance < 1){ distance = 1; } distanceVector.normalize(); //Anziehungskraft berechnen float force = mScale * (rhs->getMass()*lhs->getMass() / (distance*distance)); //Anziehungskraft in Ordnung if (force > getMinForce()){ Vector3 Rhs_forceVector = distanceVector; Rhs_forceVector *= force; //Anziehungskraft um d_t skaliert anwenden lhs->applyForce(Rhs_forceVector*d_t); Vector3 Lhs_forceVector = (-1.0) * distanceVector; Lhs_forceVector *= force; //Anziehungskraft um d_t skaliert anwenden rhs->applyForce(Lhs_forceVector*d_t); } } } } }
void SphereSphereCollisionAlgorithm::doProcessCollisionAlgorithm(const CollisionObjectWrapper &object1, const CollisionObjectWrapper &object2) { const CollisionSphereShape &sphere1 = static_cast<const CollisionSphereShape &>(object1.getShape()); const CollisionSphereShape &sphere2 = static_cast<const CollisionSphereShape &>(object2.getShape()); Vector3<float> diff = object2.getShapeWorldTransform().getPosition().vector(object1.getShapeWorldTransform().getPosition()); float length = diff.length(); float radius1 = sphere1.getRadius(); float radius2 = sphere2.getRadius(); if(length - getContactBreakingThreshold() < (radius1 + radius2)) { //collision detected //compute depth penetration float depth = length - (radius1+radius2); //compute normal (if both spheres have same origin: default normal is (1,0,0)) Vector3<float> normalFromObject2(1.0, 0.0, 0.0); if(length > std::numeric_limits<float>::epsilon()) { //normalize normal (from object2 to object1) normalFromObject2 = diff / length; } //compute intersection point Point3<float> pointOnObject2 = object2.getShapeWorldTransform().getPosition().translate(radius2 * normalFromObject2); addNewContactPoint(normalFromObject2, pointOnObject2, depth); } }
void MeshBuilder::position(const Vector3& pos) { assert(m_bIsSharedVertices || !m_currentSubMesh.strName.empty() && "You must call begin() before you call position()"); // If a temporary vertex is pending, add it to the list if (m_bTempVertexPending) copyTempVertexToBuffer(); if (m_bAutomaticDeclaration) { if (m_bFirstVertex) { declarePosition(); m_bAutomaticDeclaration = true; m_bFirstVertex = false; } else { m_bAutomaticDeclaration = false; } } m_currentVertex.position = pos; // Update bounds m_AABB.merge(pos); m_radius = std::max(m_radius, pos.length()); // Reset current texture coord m_usTextureCoordsIndex = 0; m_bTempVertexPending = true; }
/*! * This method merges this bounding sphere with another bounding sphere, so * that afterwards this bounding sphere includes all the space that was * previously included by the other bounding sphere. */ void BoundingSphere::mergeWith(const BoundingSphere* other) { Vector3 span = other->getCenterVector() - getCenterVector(); real distance = span.length(); if ( (mRadius >= (distance + other->getRadius())) || ( (fabs(distance) <= 0.0001) && (fabs(other->getRadius() - mRadius) <= 0.0001))) { // nothing to do since this bounding sphere includes the other one, // or the bounding spheres are identical } else if (other->getRadius() > (distance + mRadius)) { mRadius = other->getRadius(); mCenterVertex = other->getCenterVertex(); //FIXME mCenterVertex can be a null pointer } else { real radius, length; Vector3 direction; radius = distance + mRadius + other->getRadius(); radius /= 2; length = (radius - mRadius) / distance; mRadius = radius; mCenterVector += span * length; //the old center in the Vertex must be deleted mCenterVertex = 0; } }
void Branch::generateSection(Vector3* coords, const Vector3& start, const Vector3& end) { ZENIC_ASSERT(coords); Vector3 up(0.0f, 1.0f, 0.0f); Vector3 rel = end - start; Vector3 n = rel.normalize(); Vector3 right = up.cross(n); float rightLength = right.length(); if (rightLength) right *= 1.0f / rightLength; else right = Vector3(1.0f, 0, 0); up = n.cross(right); for (uint i = 0; i < s_settings->m_sides; ++i) { Vector3 temp = up * (s_cosTable[i] * m_radius) + right * (s_sinTable[i] * m_radius); coords[i] = start + temp; } m_radius = m_radius * (s_settings->m_contact * 0.01f) + (s_settings->m_contactVariation * 0.01f * floatRandom()); }
bool Building::collisionCheck(const Vector3 &before, const Vector3 &after, const Character* chara, Vector3 &collisionPos, Vector3 &normal) const { //最初から内部に居る場合は, スルー if(before.distanceTo(position) < radius + chara->getRadius()) return false; const Vector3 dir = after - before; const Vector3 dR = position - before; const float a = dir.length(); const float b = dir.dot(dR); const float c = position.distSquared(before) - chara->getRadius() - radius; const float D = b*b - a*c; if( D < 0 || a == 0) return false; float t = -b/a; if( t<0 || t > 1) return false; collisionPos = before + t*dir; normal = collisionPos - position; normal.normalize(); return true; }
void Tower::connectSubtower( SceneManager *scene, Tower *subtower ) { char name[30]; sprintf( name, "Connector%d_%d\n", m_towerIndex, subtower->m_towerIndex ); Entity *ent1 = scene->createEntity( name, "tube.mesh"); sprintf( name, "Connector%d_%d\n", m_towerIndex, subtower->m_towerIndex ); subtower->m_connectNode = scene->getRootSceneNode()->createChildSceneNode( name ); subtower->m_connectNode->attachObject( ent1 ); Vector3 pos = getNode()->getPosition(); Vector3 subPos = subtower->getNode()->getPosition(); // align connector Vector3 connectPos = pos + subPos; connectPos *= 0.5; connectPos.y = 1.0; subtower->m_connectNode->setPosition( connectPos); Vector3 connectDir = pos - subPos; subtower->m_connectNode->setScale( 1.0, 1.0, connectDir.length() ); connectDir.normalise(); subtower->m_connectNode->setDirection( connectDir ); subtower->m_upstreamTower = this; }