//**************************************************************** // attack void Finger::attack( const D3DXVECTOR3& pos ) { float thre = 2.0f; D3DXVECTOR3 v = pos - coarse_->get_current_center(); float l = D3DXVec3Length( &v ); if( waiting_ && 1.0f < l ) { D3DXVec3Normalize( &v, &v ); v *= -0.5f; D3DXVECTOR3 t = pos + v; coarse_->kill_inertia(); coarse_->teleport( t - coarse_->get_current_center() ); } else { px::tetrahedralmesh_type::points_type& points = mesh_->get_points(); size_t n = points.size(); for( size_t i = 0 ; i < n ; i++ ) { if( 0.2f < points[i].source_position.y ) { D3DXVECTOR3 d = pos - points[i].new_position; if( thre < D3DXVec3Length( &d ) ) { d /= thre; D3DXVec3Normalize( &d, &d ); d *= thre; } points[i].forces += d * 50.0f; } } } coarse_->set_positive( true ); waiting_ = false; }
//================================================================================ //! メソッド名 C3D_MAP::draw // // 機能 描画(見えない物は、描画しない)(ソートしない)(Zテクスチャシェーダ使用) // 引数 inZTexSha Zテクスチャシェーダ // inViewPoint カメラ視点 // 更新 2008/12/14 <新規> //================================================================================ void C3D_MAP::draw(CZTEX_SHA* inZTexSha, VEC3* inViewPoint) { vector<C3D_OBJECT*>* c3dObjvec; //!< vectorポインター vector<C3D_OBJECT*>::iterator p; //!< 作業用イテレータ // 検査しながら描画するため、とりだして描画 // 背景描画 c3dObjvec = backG.getObjects(); p = c3dObjvec->begin(); while(p != c3dObjvec->end()) { // オブジェクトのメッセージをチェックして、描画判定 if( D3DXVec3Length(&(*inViewPoint - (*p)->getLoc())) < fDistance) { (*p)->draw(inZTexSha); } p++; } // 通常描画 c3dObjvec = viewG.getObjects(); p = c3dObjvec->begin(); while(p != c3dObjvec->end()) { // オブジェクトのメッセージをチェックして、描画判定 if( D3DXVec3Length(&(*inViewPoint - (*p)->getLoc())) < fDistance) { (*p)->draw(inZTexSha); } p++; } }
BOOL CheckDuplicatePortal ( PORTAL* CheckPortal, int* index ) { // this is used to check the current portal with the portal already // sharing the same leaves in tree, it is only accepted if the portal is // larger than the one that is ready occupying this place in the master // portal array. long CheckPortalLeaf1 = CheckPortal->LeafOwnerArray [ 0 ]; long CheckPortalLeaf2 = CheckPortal->LeafOwnerArray [ 1 ]; long PALeaf1 = 0; long PALeaf2 = 0; // loop through the portals for ( long i = 0; i < NumberOfPortals; i++ ) { PALeaf1 = PortalArray [ i ]->LeafOwnerArray [ 0 ]; PALeaf2 = PortalArray [ i ]->LeafOwnerArray [ 1 ]; if ( ( CheckPortalLeaf1 == PALeaf1 && CheckPortalLeaf2 == PALeaf2 ) || ( CheckPortalLeaf1 == PALeaf2 && CheckPortalLeaf2 == PALeaf1 ) ) { D3DXVECTOR3 Max1, Min1, Max2, Min2; GetPortalBounds ( CheckPortal, &Min1, &Max1 ); GetPortalBounds ( PortalArray [ i ], &Min2, &Max2 ); // measure the length of the vectors to see which is bigger float NewSize = D3DXVec3Length ( &( Max1 - Min1 ) ); float OldSize = D3DXVec3Length ( &( Max2 - Min2 ) ); if ( fabs ( NewSize ) > fabs ( OldSize ) ) { PORTAL* temp = PortalArray [ i ]; DeletePortal ( temp ); temp = NULL; *index = i; return FALSE; } else { // this portal is already in the array return TRUE; } } } *index = NumberOfPortals; // this portal was not found in the array return FALSE; }
float CRouter::GetDistance(const D3DXVECTOR3 *vPosA, const D3DXVECTOR3 *vPosB) { D3DXVECTOR3 vDif = *vPosB - *vPosA; D3DXVECTOR3 vPos = *vPosA; float fDot = D3DXVec3Dot(&vDif, &vPos); float fCos = fDot/(D3DXVec3Length(&vDif)*D3DXVec3Length(&vPos)); return D3DXVec3Length(&vPos)*(sqrt(1 - fCos*fCos)); }
void CLine::DetectCollision() { if (!m_pMap) return; /// Model 2: Turn back if throughed. /// Cons: Some will break through edges. m_bTurned = m_pMap->GetArea(MT_WALL, m_mapIndex)-> ReflectCollision(m_vPos, m_vDir, m_bTurned); if (m_pEcho.empty()) { m_pEcho.push_back(CPoint(m_vPos, m_pMap->GetRenderEffect(m_vPos))); } else { if (m_bQuiet) { D3DXVECTOR3 *pre = &m_pEcho.back().GetPos(); D3DXVECTOR3 lerp = m_vPos - *pre; FLOAT step = 1.f / D3DXVec3Length(&lerp); for (FLOAT s(0); s < 1.f; s += step) { D3DXVec3Lerp(&lerp, pre, &m_vPos, s); m_pEcho.push_back(CPoint(lerp, m_pMap->GetRenderEffect(lerp))); } } else { D3DXVECTOR3 *pre = &m_pEcho.front().GetPos(); D3DXVECTOR3 lerp = m_vPos - *pre; FLOAT step = 1.f / D3DXVec3Length(&lerp); for (FLOAT s(0); s < 1.f; s += step) { D3DXVec3Lerp(&lerp, pre, &m_vPos, s); m_pEcho.push_front(CPoint(lerp, m_pMap->GetRenderEffect(lerp))); } } } /// Model 1: Roll back to previous if throughed. /// Cons: Some will stay still on egdes and flash. //if (map->GetArea(MT_WALL, index)->ReflectCollision(m_vPos, m_vDir)) { // if (m_bQuiet) m_pEcho.push_back(m_vPos); // else m_pEcho.push_front(m_vPos); //} else { // if (m_bQuiet) { // m_pEcho.pop_back(); // if (!m_pEcho.empty()) m_vPos = m_pEcho.back(); // } else { // m_pEcho.pop_front(); // if (!m_pEcho.empty()) m_vPos = m_pEcho.front(); // } //} /// Model 0: Judging from intersection point. /// Cons: Only tested in early version, unstable. //if (map->GetArea(MT_WALL, index)->Validate(m_vPos)) return; //for (EdgesCIt it(map->GetArea(MT_WALL, index)->edges.begin()); // it != map->GetArea(MT_WALL, index)->edges.end(); ++it) { // if (it->Intersected(m_vPre, m_vPos)) { // if (it->AimingInside(m_vDir)) return; // FLOAT dis(fabsf(it->GetDistance(m_vPos))); // //m_vPre = m_vPos; // m_vPos += it->normal*dis*(it->inside?2.f:-2.f); // m_vDir = it->GetReflectDirection(m_vDir); // return; // } //} }
void Character::UpdateItSelf( float dTime ) { // 가속 if ( IsAccelerating() ) { if ( timeGetTime() - GetAccelerationStartTime() > ACCELERATION_TIME ) { // 가속 끝났다 SetIsAccelerating( false ); SetAcceleration( ZERO_VECTOR3 ); } } if ( !m_CharacterClass->IsAlive() ) { //printf_s( "player %d is dead \n", m_CharacterId ); return; } m_Matrix = GetTransform()->MatrixTransform(); if ( IsSpinning() ) { AddSpinTime( dTime ); // 회전축을 기준으로 물체를 회전시킵니다. D3DXMATRIXA16 spinTransform; D3DXVECTOR3 tmpSpinAxis = GetSpinAxis(); float tmpSpinAngle = GetSpinAngularVelocity(); D3DXMatrixRotationAxis( &spinTransform, &tmpSpinAxis, tmpSpinAngle * GetSpinTime() ); // D3DXMatrixMultiply( &m_Matrix, &m_Matrix, &spinTransform ); D3DXMatrixMultiply( &m_Matrix, &spinTransform, &m_Matrix ); } D3DXVECTOR3 tmpVec3 = GetTransform()->GetPosition(); D3DXVECTOR3 tmpVel = GetVelocity(); Physics::CalcCurrentPosition( &tmpVec3, &tmpVel, GetAcceleration(), dTime, GetSpeedConstant() ); // 최대 이동거리 제한 float currentDistanceFromOrigin = D3DXVec3Length( &tmpVec3 ); if ( currentDistanceFromOrigin > MAX_DISTANCE_FROM_ORIGIN ) m_CharacterClass->SetOxygen( 0.0f ); // 최대 속도 제한 float currentSpeed = D3DXVec3Length( &tmpVel ); if ( currentSpeed > MAX_PLAYER_SPEED ) { D3DXVec3Normalize( &tmpVel, &tmpVel ); tmpVel *= MAX_PLAYER_SPEED; } GetTransform()->SetPosition( tmpVec3 ); SetVelocity( tmpVel ); // 산소량 감소등의 작업 처리 GetClassComponent()->Update( dTime ); }
void StaticCamera::SetFromSunlight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXVECTOR4 Power, float Width, float Height, float Depth) { D3DXVECTOR3 d3dLookAt = D3DXVECTOR3(Width, Height, Depth) * 0.5f;//(*position + *Direction * m_fFarPlane); D3DXVECTOR3 eye = *position / D3DXVec3Length(position); eye *= D3DXVec3Length(&d3dLookAt) * 1.1f; eye += d3dLookAt; CBaseCamera::SetViewParams(&eye, &d3dLookAt); mLightColor = Power; *position = eye; d3dLookAt = d3dLookAt - eye; d3dLookAt /= D3DXVec3Length(&d3dLookAt); *direction = d3dLookAt; D3DXVECTOR3 cornersWorld[] = { D3DXVECTOR3(0,0,0), D3DXVECTOR3(Width, 0, 0), D3DXVECTOR3(Width, Height, 0), D3DXVECTOR3(Width, Height, Depth), D3DXVECTOR3(0, Height, 0), D3DXVECTOR3(0, Height, Depth), D3DXVECTOR3(0,0,Depth), D3DXVECTOR3(Width, 0, Depth) }; D3DXVECTOR3 lightSpaceBoxMin = D3DXVECTOR3(FLT_MAX,FLT_MAX,FLT_MAX); D3DXVECTOR3 lightSpaceBoxMax = D3DXVECTOR3(FLT_MIN, FLT_MIN, FLT_MIN); for (int i = 0; i < 8; ++i) { D3DXVec3TransformCoord(&cornersWorld[i], &cornersWorld[i], GetViewMatrix()); lightSpaceBoxMin.x = lightSpaceBoxMin.x < cornersWorld[i].x ? lightSpaceBoxMin.x : cornersWorld[i].x; lightSpaceBoxMin.y = lightSpaceBoxMin.y < cornersWorld[i].y ? lightSpaceBoxMin.y : cornersWorld[i].y; lightSpaceBoxMin.z = lightSpaceBoxMin.z < cornersWorld[i].z ? lightSpaceBoxMin.z : cornersWorld[i].z; lightSpaceBoxMax.x = lightSpaceBoxMax.x > cornersWorld[i].x ? lightSpaceBoxMax.x : cornersWorld[i].x; lightSpaceBoxMax.y = lightSpaceBoxMax.y > cornersWorld[i].y ? lightSpaceBoxMax.y : cornersWorld[i].y; lightSpaceBoxMax.z = lightSpaceBoxMax.z > cornersWorld[i].z ? lightSpaceBoxMax.z : cornersWorld[i].z; }; SetOrthoParams(lightSpaceBoxMax.x - lightSpaceBoxMin.x, lightSpaceBoxMax.y - lightSpaceBoxMin.y, lightSpaceBoxMin.z, lightSpaceBoxMax.z); //set constant buffer D3DXMATRIX identityMatrix; D3DXMatrixIdentity(&identityMatrix); cameraCBuffer->Data.World = identityMatrix; cameraCBuffer->Data.refractiveIndexETA = 1.0f; cameraCBuffer->Data.View = *GetViewMatrix(); cameraCBuffer->Data.WorldViewProjection = identityMatrix * cameraCBuffer->Data.View * (*GetProjMatrix()); //float test = D3DXMatrixDeterminant(GetProjMatrix()); //test = D3DXMatrixDeterminant(GetViewMatrix()); //test = test +1; }
//first attempt at an implementation - use 'force' as an impulse, and change velocities instantaneously void CRigidBody::OnCollision(const PAVECTOR* pLocation, const PAVECTOR* pForce) { D3DXVECTOR3 dxLocation = DXVector(*pLocation); D3DXVECTOR3 dxForce = DXVector(*pForce); m_velocity += dxForce / m_mass; // find the vector from the origin to the point through which the force is exerted D3DXVECTOR3 radius; // first find some multiplier A for which radius = location + A * force float A; float forceMagnitude = D3DXVec3Length(&dxForce); A = - D3DXVec3Dot(&dxLocation, &dxForce) / (forceMagnitude * forceMagnitude); radius = dxLocation + A * dxForce; if (D3DXVec3Length(&radius) < 0.001) { //the force is effectively acting directly through the COM, and is therefore purely translational return; } // now consider two sections of the mass, divided by the force. One is attmpting to turn one way, the other section attempts to turn the other way. // the plane separating the two sections is described by two vectors. One is the force, the other is perpendicular to the force and the radius. D3DXVECTOR3 rotationAxis; D3DXVec3Cross(&rotationAxis, &radius, &dxForce); // section one: the smaller section /* D3DXVECTOR3 sec1COM; float sec1Size = m_hitbox.GetSplitSection(&sec1COM, &force, &rotationAxis); if (sec1Size < 0.0001) { // the force is right on the edge of the object, and is purely rotational */ D3DXQUATERNION change; D3DXQuaternionRotationAxis(&change, &rotationAxis, D3DXVec3Length(&rotationAxis)); m_angularVelocity += change / (m_mass * GetMomentOfInertia(&PAVector(rotationAxis))); return; /* } { // a combination of translation and rotation D3DXVECTOR3 sec1Radius; } */ }
bool Tank::checknResolveBulletCollision(Bullet* other, D3DXVECTOR3& location) { vector<State*> *bullets = other->GetBullets(); vector<int> *collisions = new vector<int>(); bool hascollision = false; for (unsigned int i = 0; i < bullets->size(); i++) { State *otherState = (*bullets)[i]; D3DXVECTOR3 plane = *otherState->GetPosition()-*m_tankState->GetPosition(); D3DXVECTOR3 plane2 = *otherState->GetPosition()-(*m_tankState->GetPosition() + *m_tankState->GetForward()*0.75f); float distance = min(D3DXVec3Length(&plane),D3DXVec3Length(&plane2)); if (distance < 1) { collisions->push_back(i); D3DXVECTOR3 thisvel,othervel,thisafter,otherafter, thisperp, otherperp; D3DXVec3Normalize(&plane,&plane); thisvel = m_tankState->GetPosVel(); othervel = otherState->GetPosVel(); thisvel = thisvel.x * *m_tankState->GetRight() + thisvel.y * *m_tankState->GetUp() + thisvel.z * *m_tankState->GetForward(); othervel = othervel.x * *otherState->GetRight() + othervel.y * *otherState->GetUp() + othervel.z * *otherState->GetForward(); thisperp = plane * D3DXVec3Dot(&plane,&thisvel); //thispar = thisvel - thisperp; otherperp = plane * D3DXVec3Dot(&plane,&othervel); //otherpar = othervel - otherperp; //result = (thisperp-otherperp) * 0.5f; thisafter = thisvel + D3DXVec3Dot(&(othervel+thisvel),&plane) * plane; //otherafter = othervel + D3DXVec3Dot(&(thisvel-othervel),&plane) * plane; thisafter.y = 0; thisafter = thisafter * 0.1f; m_tankState->SetPosVel(thisafter); otherState->SetPosVel(D3DXVECTOR3(0,0,0)); location = *otherState->GetPosition(); hascollision = true; } } other->removeBullets(collisions); return hascollision; }
//================================================================================ //! メソッド名 C3D_MAP::squeezeSortByCameraPositionWithDrawZTexSha // // 機能 絞り込みしながらZテクスチャで描画をし、ソートする // 引数 inCamera カメラ // inViewPoint カメラ視点 // inZTexSha Zテクスチャシェーダ // 更新 2009/01/31 <新規> //================================================================================ void C3D_MAP::squeezeSortByCameraPositionWithDrawZTexSha( C3D_OBJECT* inCamera, VEC3* inViewPoint, CZTEX_SHA* inZTexSha) { squeezeBackG.clear(); //!< 絞り込み背景グループの消去 squeezeViewG.clear(); //!< 絞り込み描画グループの消去 vector<C3D_OBJECT*>* c3dObjvec; //!< vectorポインター vector<C3D_OBJECT*>* c3dObjvecSqueeze; //!< 絞り込み背景vectorポインター vector<C3D_OBJECT*>::iterator p; //!< 作業用イテレータ // 背景 c3dObjvecSqueeze = squeezeBackG.getObjects(); c3dObjvec = backG.getObjects(); p = c3dObjvec->begin(); while(p != c3dObjvec->end()) { // オブジェクトのメッセージをチェックして、描画判定 if( checkMessage(&p)) { if( D3DXVec3Length(&(*inViewPoint - (*p)->getLoc())) < fDistance) { (*p)->draw(inZTexSha); c3dObjvecSqueeze->push_back(*p); } p++; } } // 通常 c3dObjvecSqueeze = squeezeViewG.getObjects(); c3dObjvec = viewG.getObjects(); p = c3dObjvec->begin(); while(p != c3dObjvec->end()) { // オブジェクトのメッセージをチェックして、描画判定 if( checkMessage(&p)) { if( D3DXVec3Length(&(*inViewPoint - (*p)->getLoc())) < fDistance) { (*p)->draw(inZTexSha); c3dObjvecSqueeze->push_back(*p); } p++; } } // ソート squeezeBackG.sortByCameraPosition(inCamera); squeezeViewG.sortByCameraPosition(inCamera); }
//compute the vertex normals void Terrain::ComputeNormals()const{ for (int i = 0; i < gridWidth; i++){ for (int j = 0; j < gridDepth; j++){ Vector3f v1,v2,v3,v4,v12,v23,v34,v41,v; v1 = v2 = v3 = v4 = v12 = v23 = v34 = v41 = v = Vector3f(0.0f,0.0f,0.0f); //grab 2 vectors for this if (j != gridWidth - 1){ v1 = Vector3f(vertices[i*gridDepth+j+1].pos-vertices[i*gridDepth+j].pos); } if (i != gridWidth - 1){ v2 = Vector3f(vertices[(i+1)*gridDepth+j].pos-vertices[i*gridDepth+j].pos); } if (j > 0){ v3 = Vector3f(vertices[i*gridDepth+j-1].pos-vertices[i*gridDepth+j].pos); } if (i > 0){ v4 = Vector3f(vertices[(i-1)*gridDepth+j].pos-vertices[i*gridDepth+j].pos); } D3DXVec3Cross(&v12,&v1,&v2); D3DXVec3Normalize(&v12,&v12); D3DXVec3Cross(&v23,&v2,&v3); D3DXVec3Normalize(&v23,&v23); D3DXVec3Cross(&v34,&v3,&v4); D3DXVec3Normalize(&v34,&v34); D3DXVec3Cross(&v41,&v4,&v1); D3DXVec3Normalize(&v41,&v41); if (D3DXVec3Length(&v12) > 0.0f) v = v + v12; if (D3DXVec3Length(&v23) > 0.0f) v = v + v23; if (D3DXVec3Length(&v34) > 0.0f) v = v + v34; if (D3DXVec3Length(&v41) > 0.0f) v = v + v41; D3DXVec3Normalize(&v,&v); vertices[i*gridDepth+j].normal = v; } } }
//**関数*************************************************************************** // 概要 : 拡大込み2D矩形と円判定 //********************************************************************************* bool CCalc::IntersectBoxAndCircleScale(CHitRect rect , D3DMATRIX matRect , CHitCircle circle , D3DMATRIX matCircle) { D3DXVECTOR3 ScaleA = D3DXVECTOR3(0,0,0) , ScaleB = D3DXVECTOR3(0,0,0); ScaleA.x = D3DXVec3Length(& D3DXVECTOR3(matRect._11 , matRect._12 , matRect._13)); ScaleA.y = D3DXVec3Length(& D3DXVECTOR3(matRect._21 , matRect._22 , matRect._23)); ScaleB.x = D3DXVec3Length(& D3DXVECTOR3(matCircle._11 , matCircle._12 , matCircle._13)); rect.m_fWidth *= ScaleA.x; rect.m_fHeight *= ScaleA.y; circle.m_fRadius *= ScaleB.x; rect.m_fWidth += circle.m_fRadius * 2; rect.m_fHeight += circle.m_fRadius * 2; // 左上に位置合わせ switch(rect.m_nPointType) { case eHitRect::CENTER: rect.m_Point.x -= rect.m_fWidth / 2.0f; rect.m_Point.y -= rect.m_fHeight / 2.0f; break; case eHitRect::UP_LEFT_2D: break; case eHitRect::DOWN_CENTER: rect.m_Point.x -= rect.m_fWidth / 2.0f; rect.m_Point.y -= rect.m_fHeight; break; case eHitRect::DOWN_RIGHT_2D: rect.m_Point.x -= rect.m_fWidth; rect.m_Point.y -= rect.m_fHeight; break; default: // エラー return false; break; } // 判定 if(rect.m_Point.x < circle.m_Point.x && rect.m_Point.x + rect.m_fWidth > circle.m_Point.x && rect.m_Point.y < circle.m_Point.y && rect.m_Point.y + rect.m_fHeight > circle.m_Point.y) return true; else return false; }
D3DXVECTOR3 HitJudgments::CircleLineCollideCompensation( const D3DXVECTOR3 _center, const float _rad, const D3DXVECTOR3 _Spos, const D3DXVECTOR3 _Epos ) { // スタート座標とエンド座標のベクトル D3DXVECTOR3 StartEndVec = _Epos - _Spos ; // ベクトルを正規化 D3DXVECTOR3 Dir ; D3DXVec3Normalize( &Dir, &StartEndVec ) ; // スタート座標と球の座標のベクトル D3DXVECTOR3 StartCenterVec = _Spos - _center ; float t = 0.0f ; D3DXVECTOR3 p = D3DXVECTOR3( 0,0,0 ) ; // 最近接点を取る D3DXVECTOR3 LineNear = ClosestPtPointSegment( _center, _Spos, _Epos ) ; D3DXVECTOR3 CompDir = _center - LineNear ; CompDir.y = 0.0f ; D3DXVec3Normalize( &CompDir, &CompDir ) ; float b = D3DXVec3Dot( &StartCenterVec, &Dir ) ; float c = D3DXVec3Dot( &StartCenterVec, &StartCenterVec ) - _rad * _rad; float Length = D3DXVec3Length( &StartEndVec ) ; if( c > 0.0f && b > 0.0f ) return _center ; float Discr = b * b - c ; if( Discr < 0.0f ) return _center ; t = -b - sqrt( Discr ) ; if( t < 0.0f ) t = 0.0f ; p = _Spos + t * Dir ; float length = D3DXVec3Length( &StartEndVec ) ; if( t >= 0.0f && t <= Length ) { return LineNear + ( CompDir * _rad ) ; } return _center ; }
//**関数*************************************************************************** // 概要 : 拡大球判定 //********************************************************************************* bool CCalc::IntersectSphereScale(CHitCircle circleA , D3DMATRIX matA , CHitCircle circleB , D3DXMATRIX matB) { // ワールドマトリクスから拡大率取得 D3DXVECTOR3 ScaleA = D3DXVECTOR3(0,0,0) , ScaleB = D3DXVECTOR3(0,0,0); ScaleA.x = D3DXVec3Length(&D3DXVECTOR3(matA._11 , matA._12 , matA._13)); //ScaleA.y = D3DXVec3Length(&D3DXVECTOR3(matA._21 , matA._22 , matA._23)); ScaleB.x = D3DXVec3Length(&D3DXVECTOR3(matB._11 , matB._12 , matB._13)); //ScaleB.y = D3DXVec3Length(&D3DXVECTOR3(matB._21 , matB._22 , matB._23)); float fDist = D3DXVec3LengthSq(&(circleA.m_Point - circleB.m_Point)); float fRadi = powf(circleA.m_fRadius * ScaleA.x + circleB.m_fRadius * ScaleB.x , 2.0f); if(fRadi > fDist) return true; else return false; }
void BSP::addAlphaGeometry(Atomic* atomic, unsigned int subsetId) { // calculate sorting key _objectPos = atomic->getBoundingSphere()->center; D3DXVec3Subtract( &_distanceV, &_objectPos, &Camera::eyePos ); _distance = 0.005f * D3DXVec3Length( &_distanceV ); // retrieve sector BSPSector* firstSector = NULL; if( atomic->_sectors.size() ) { firstSector = reinterpret_cast<BSPSector*>( *atomic->_sectors.begin() ); } // choose alpha pool if( _distance <= 255 ) { _sortedAlpha.add( &AlphaGeometry( _nearest, unsigned char( _distance ), atomic->frame(), atomic->geometry(), firstSector, atomic->getBoneMatrices(), subsetId ) ); }
void WaypointManager::addEdge(int id1, int id2) { EdgePtr edge(new Edge()); edge->destination = id2; edge->distance = D3DXVec3Length(&(waypoints[id1]->_position - waypoints[id2]->_position)); waypoints[id1]->neighbors.insert(edge); }
void CBoxMesh::SetSize() { D3DXVECTOR3 vPos = D3DXVECTOR3(10.f, 0.f, 10.f); //D3DXVECTOR3 vMax; //D3DXVECTOR3 vMin; // //memset(&vMax, 0, sizeof(D3DXVECTOR3)); //memset(&vMin, 0, sizeof(D3DXVECTOR3)); float fRadius = -1.f; if(m_pMesh ) { BYTE* ppVB = NULL; m_pMesh->LockVertexBuffer( 0, (VOID**) &ppVB); if(FAILED(D3DXComputeBoundingBox((D3DXVECTOR3*)ppVB, m_pMesh->GetNumVertices(), m_pMesh->GetNumBytesPerVertex(), &m_Size.vMin, &m_Size.vMax))) { m_fRadius = fRadius; return; } D3DXVECTOR3 vLen = m_Size.vMax - m_Size.vMin; fRadius = D3DXVec3Length(&vLen); fRadius *= 0.5f; } m_fRadius = fRadius; }
void Tank::checknResolveStaticCollision(ModelList *model) { for (int i=0; i< model->GetModelCount(); i++) { D3DXVECTOR3 otherPosition = model->GetModelPosition(i); D3DXVECTOR3 plane = otherPosition-*m_tankState->GetPosition(); float distance = D3DXVec3Length(&plane); if (distance < 6) { D3DXVECTOR3 thisvel,thisafter, thisperp; D3DXVec3Normalize(&plane,&plane); thisvel = m_tankState->GetPosVel(); thisvel = thisvel.x * *m_tankState->GetRight() + thisvel.y * *m_tankState->GetUp() + thisvel.z * *m_tankState->GetForward(); thisperp = plane * D3DXVec3Dot(&plane,&thisvel); thisafter = thisvel - (2*thisperp); D3DXVECTOR3 normal1,normal2; D3DXVec3Normalize(&normal1,&thisafter); D3DXVec3Normalize(&normal2,&thisvel); m_tankState->AddtoPosition((6-distance) * (normal1 - normal2)); m_tankState->SetPosVel(thisafter); } } }
void CStageDynamic::Astar_Move() { list<int>* pBestList = GET_SINGLE(CAstar)->GetBestList(); if (pBestList->empty()) //비어있으면 리턴 { return; } const vector<TILE*>* pVecTile = GET_SINGLE(CObjMgr)->GetTileInfo(); int nDestIdx = pBestList->front(); m_Info.vDir = (*pVecTile)[nDestIdx]->vPos - m_Info.vPos; float fDistance = D3DXVec3Length(&m_Info.vDir); D3DXVec3Normalize(&m_Info.vDir,&m_Info.vDir); m_Info.vPos += m_Info.vDir * 100.f * GET_SINGLE(CTimeMgr)->GetTime(); if (fDistance < 5.f) { pBestList->pop_front(); } }
// ajusta el pto de vista para que el mesh sea visible void CSolidView::scaleToFit() { D3DXVECTOR3 min = D3DXVECTOR3 (10000,10000,10000); D3DXVECTOR3 max = D3DXVECTOR3 (-10000,-10000,-10000); for(int i=0;i<escena.cant_mesh;++i) { D3DXVECTOR3 p0 = escena.m_mesh[i]->m_pos; if(p0.x<min.x) min.x = p0.x; if(p0.y<min.y) min.y = p0.y; if(p0.z<min.z) min.z = p0.z; D3DXVECTOR3 p1 = p0 + escena.m_mesh[i]->m_size; if(p1.x>max.x) max.x = p1.x; if(p1.y>max.y) max.y = p1.y; if(p1.z>max.z) max.z = p1.z; } escena.lookFrom = escena.lookAt = (min + max) * 0.5; escena.lookFrom.x += D3DXVec3Length(&(max - min)) * 2; }
//裁减mesh void GameFootprint::ClipMesh(long triangleCount, const Triangle *triangle, const D3DXVECTOR3 *vertex, const D3DXVECTOR3 *normal) { D3DXVECTOR3 newVertex[9]; D3DXVECTOR3 newNormal[9]; // Clip one triangle at a time for (long a = 0; a < triangleCount; a++) { long i1 = triangle->index[0]; long i2 = triangle->index[1]; long i3 = triangle->index[2]; const D3DXVECTOR3& v1 = vertex[i1]; const D3DXVECTOR3& v2 = vertex[i2]; const D3DXVECTOR3& v3 = vertex[i3]; D3DXVECTOR3 cross; D3DXVec3Cross(&cross, &(v2 - v1), &(v3 - v1)); if (DotProduct(decalNormal, cross) > decalEpsilon * D3DXVec3Length(&cross)) { newVertex[0] = v1; newVertex[1] = v2; newVertex[2] = v3; newNormal[0] = normal[i1]; newNormal[1] = normal[i2]; newNormal[2] = normal[i3]; long count = ClipPolygon(3, newVertex, newNormal, newVertex, newNormal); if ((count != 0) && (!AddPolygon(count, newVertex, newNormal))) break; } triangle++; } }
//============================================================================== // Brief : 衝突判定押し戻し停止処理(球と球) // Return : bool : 衝突判定結果(true : 衝突) // Arg : D3DXVECTOR3* pPosCenter0 : 球0の中心座標 // Arg : float fRadius0 : 球0の半径 // Arg : D3DXVECTOR3* pVel0 : 球0の速度 // Arg : D3DXVECTOR3* pPosCenter1 : 球1の中心座標 // Arg : float fRadius1 : 球1の半径 // Arg : D3DXVECTOR3* pVel1 : 球1の速度 //============================================================================== bool CCollision::IsCollidedPushStop(D3DXVECTOR3* pPosCenter0, float fRadius0, D3DXVECTOR3* pVel0, D3DXVECTOR3* pPosCenter1, float fRadius1, D3DXVECTOR3* pVel1) { D3DXVECTOR3 vecCenter = (*pPosCenter0 + *pVel0) - (*pPosCenter1 + *pVel1); // 中心間のベクトル float fDist; // 対象間の距離 // 判定 fDist = D3DXVec3Length(&vecCenter); if(fDist <= fRadius0 + fRadius1) { // 押し戻し if(fabs(fDist) > 0.0001f) { *pPosCenter0 += vecCenter * 0.51f * ((fRadius0 + fRadius1) - fDist) / fDist; *pPosCenter1 -= vecCenter * 0.51f * ((fRadius0 + fRadius1) - fDist) / fDist; } else { pPosCenter0->x += fRadius0; pPosCenter1->x -= fRadius1; } // 当たっている return true; } // 当たっていない return false; }
void Monai::NormalAttModeStart(){ vLength = pMon->GetPosition()-pCha->GetPosition(); if(D3DXVec3Length(&vLength)<MON_REAL_SIZE+BALL_REAL_SIZE+MON_ATTACK_RANGE){ pUI->DamageUI(); pCha->SetLife(g_fNAttackDamage); } }
int InterpolatePosition(D3DXVECTOR3& vResult, D3DXVECTOR3 const& vL, D3DXVECTOR3 const& vR, float fRatioL, float fRatioR) { int nRetCode = false; int nResult = false; float fRange = 0.0f; float fRangeReciprocal = 0.0f; D3DXVECTOR3 vDelta; fRange = fRatioL + fRatioR; vDelta = vR - vL; nRetCode = D3DXVec3Length(&vDelta) > 1.0f; KG_PROCESS_ERROR(nRetCode); nRetCode = abs(fRange) > FLT_EPSILON; KG_PROCESS_ERROR(nRetCode); fRangeReciprocal = 1.0f / fRange; fRatioL *= fRangeReciprocal; fRatioR *= fRangeReciprocal; vResult = vL * fRatioR + vR * fRatioL; nResult = true; Exit0: if (!nResult) { vResult = vL; } return nResult; }
/************************************************************************* * D3DXComputeBoundingSphere */ HRESULT WINAPI D3DXComputeBoundingSphere(CONST D3DXVECTOR3* pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, FLOAT *pradius) { D3DXVECTOR3 temp, temp1; FLOAT d; unsigned int i; if( !pfirstposition || !pcenter || !pradius ) return D3DERR_INVALIDCALL; temp.x = 0.0f; temp.y = 0.0f; temp.z = 0.0f; temp1 = temp; d = 0.0f; *pradius = 0.0f; for(i=0; i<numvertices; i++) { D3DXVec3Add(&temp1, &temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i)); temp = temp1; } D3DXVec3Scale(pcenter, &temp, 1.0f/((FLOAT)numvertices)); for(i=0; i<numvertices; i++) { d = D3DXVec3Length(D3DXVec3Subtract(&temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i), pcenter)); if ( d > *pradius ) *pradius = d; } return D3D_OK; }
void Diagnostic::UpdateLine(const std::string& id, Diagnostic::Colour color, const D3DXVECTOR3& start, const D3DXVECTOR3& end) { if(sm_diag->m_linemap.find(id) == sm_diag->m_linemap.end()) { sm_diag->m_linemap.insert(LineMap::value_type(id,DiagLine())); } sm_diag->m_linemap[id].color = sm_diag->m_colourmap[color]; D3DXVECTOR3 forward = end-start; D3DXVECTOR3 middle = start + (forward*0.5f); float size = D3DXVec3Length(&forward); forward /= size; sm_diag->m_linemap[id].world.MakeIdentity(); if(fabs(std::acos(D3DXVec3Dot(&ZAXIS, &forward))) > 0) { D3DXVECTOR3 up; D3DXVec3Cross(&up, &ZAXIS, &forward); D3DXVec3Normalize(&up, &up); D3DXVECTOR3 right; D3DXVec3Cross(&right, &up, &forward); D3DXVec3Normalize(&right, &right); forward *= size; sm_diag->m_linemap[id].world.SetAxis(up, forward, right); } sm_diag->m_linemap[id].world.SetPosition(middle); sm_diag->m_linemap[id].draw = true; }
//加入多边形 bool GameFootprint::AddPolygon(long vertexCount, const D3DXVECTOR3 *vertex, const D3DXVECTOR3 *normal) { long count = decalVertexCount; if (count + vertexCount >= maxDecalVertices) return (false); // Add polygon as a triangle fan Triangle *triangle = triangleArray + decalTriangleCount; decalTriangleCount += vertexCount - 2; for (long a = 2; a < vertexCount; a++) { triangle->index[0] = (unsigned short) count; triangle->index[1] = (unsigned short) (count + a - 1); triangle->index[2] = (unsigned short) (count + a); triangle++; } // Assign vertex colors float f = 1.0F / (1.0F - decalEpsilon); for (long b = 0; b < vertexCount; b++) { vertexArray[count] = vertex[b]; const D3DXVECTOR3& n = normal[b]; float alpha = (DotProduct(decalNormal, n) / D3DXVec3Length(&n) - decalEpsilon) * f; colorArray[count] = ColorRGBA(1.0F, 1.0F, 1.0F, (alpha > 0.0F) ? alpha : 0.0F); count++; } decalVertexCount = count; return (true); }
void EFFECT_FIREBALL::Update(float timeDelta) { m_t1.m_rot += D3DXVECTOR3(0.5f, 0.5f, 0.5f) * timeDelta; if(m_time < 1.0f && m_pSrcBone != NULL) //Follow staff { m_time += timeDelta; D3DXMATRIX mat = m_pSrcBone->CombinedTransformationMatrix; m_t1.m_pos = D3DXVECTOR3(mat(3,0), mat(3, 1), mat(3, 2)); //Extract position from bone m_t1.m_sca = D3DXVECTOR3(1.5f, 1.5f, 1.5f) * m_time; m_color.w = m_time; if(m_time > 1.0f) { m_color.w = m_time * 0.5f; origin = m_t1.m_pos; m_length = D3DXVec3Length(&(origin - dest)); } } else if(m_prc < 1.0f) //Fly towards target { m_prc += (m_speed * timeDelta) / m_length; m_t1.m_pos = GetPosition(m_prc); } else //Explode { m_prc += (m_speed * timeDelta) / m_length; m_t1.m_sca += D3DXVECTOR3(5.0f, 5.0f, 5.0f) * timeDelta; m_color.w -= timeDelta * 0.5f; } }
//void Cube::GenerateNormalVertex(Vertex* pVertices, UINT uNumVerticesAlongWidth, UINT uNumVerticesAlongLength) { // for (int z = 0; z < uNumVerticesAlongLength; ++z) { // for (int x = 0; x < uNumVerticesAlongWidth; ++x) { // float halfWidth = ((float)uNumVerticesAlongWidth - 1.0f) / 2.0f; // float halfLength = ((float)uNumVerticesAlongLength - 1.0f) / 2.0f; // pVertices[x + z * uNumVerticesAlongLength] = { // D3DXVECTOR3((float)x - halfWidth, 0.0f, (float)z - halfLength), // D3DXVECTOR3(0.0f, 1.0f, 0.0f), // (float)x / (uNumVerticesAlongWidth - 1), (float)z / (uNumVerticesAlongLength - 1) // }; // } // } //} // //void Cube::GenerateIndices(int* pIndices, UINT uNumVerticesAlongWidth, UINT uNumVerticesAlongLength) { // for (int z = 0, index = -1, x; z < uNumVerticesAlongLength - 1; ++z) { // if (z & 0x01) { // for (x = uNumVerticesAlongWidth - 1; x >= 0; --x) { // pIndices[++index] = x + (z * uNumVerticesAlongWidth); // pIndices[++index] = x + (z * uNumVerticesAlongWidth) + uNumVerticesAlongWidth; // } // if (z != uNumVerticesAlongLength - 2) { // pIndices[++index] = ++x + (z * uNumVerticesAlongWidth); // } // } else { // for (x = 0; x < uNumVerticesAlongWidth; ++x) { // pIndices[++index] = x + (z * uNumVerticesAlongWidth); // pIndices[++index] = x + (z * uNumVerticesAlongWidth) + uNumVerticesAlongWidth; // } // if (z != uNumVerticesAlongLength - 2) { // pIndices[++index] = --x + (z * uNumVerticesAlongWidth); // } // } // } //} bool Cube::IsOnMe(D3DXVECTOR3 position) { D3DXVECTOR3 myPosition = D3DXVECTOR3(m_d3dmTranslate._41, m_d3dmTranslate._42, m_d3dmTranslate._43); float length = D3DXVec3Length(&(position - myPosition)); // incorrect, just estimate return length < sqrtf(2.0f) * m_fWidth / 2.0f; }
void SkinnedMesh::RenderSkeleton(Bone* bone, Bone *parent, D3DXMATRIX world) { //Temporary function to render the bony hierarchy if (world == NULL)return; if (bone == NULL)bone = (Bone*)m_pRootBone; //Draw line between bones if (parent != NULL && bone->Name != NULL && parent->Name != NULL) { //Draw Sphere g_pDevice->SetRenderState(D3DRS_LIGHTING, true); g_pDevice->SetTransform(D3DTS_WORLD, &(bone->CombinedTransformationMatrix * world)); m_pSphereMesh->DrawSubset(0); D3DXMATRIX w1 = bone->CombinedTransformationMatrix; D3DXMATRIX w2 = parent->CombinedTransformationMatrix; //Extract translation D3DXVECTOR3 thisBone = D3DXVECTOR3(w1(3, 0), w1(3, 1), w1(3, 2)); D3DXVECTOR3 ParentBone = D3DXVECTOR3(w2(3, 0), w2(3, 1), w2(3, 2)); if (D3DXVec3Length(&(thisBone - ParentBone)) < 2.0f) { g_pDevice->SetTransform(D3DTS_WORLD, &world); VERTEX vert[] = {VERTEX(ParentBone, 0xffff0000), VERTEX(thisBone, 0xff00ff00)}; g_pDevice->SetRenderState(D3DRS_LIGHTING, false); g_pDevice->SetFVF(VERTEX::FVF); g_pDevice->DrawPrimitiveUP(D3DPT_LINESTRIP, 1, &vert[0], sizeof(VERTEX)); } } if (bone->pFrameSibling)RenderSkeleton((Bone*)bone->pFrameSibling, parent, world); if (bone->pFrameFirstChild)RenderSkeleton((Bone*)bone->pFrameFirstChild, bone, world); }