void Tank::orientTurret(float deltaX,float deltaY, float time) { float anglelimit = acos(D3DXVec3Dot(&D3DXVECTOR3(0,1,0), m_tankState->GetUp())); yaw += deltaX*0.01f; if ((deltaY < 0 && pitch > -0.60 + anglelimit) || (deltaY > 0 && pitch <0.60 + anglelimit)) pitch += deltaY*0.005f; }
void orthogonalizeAndNormalizeTangent(const D3DXVECTOR3 &tangent, const D3DXVECTOR3& normal, D3DXVECTOR3& tangentOut){ float d = D3DXVec3Dot(&normal, &tangent); D3DXVECTOR3 newTangent = tangent - d * normal; D3DXVec3Normalize(&newTangent, &newTangent); tangentOut = newTangent; }
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)); }
D3DXQUATERNION Sun::QuatFromBallPoints(const D3DXVECTOR3 &vFrom, const D3DXVECTOR3 &vTo) { D3DXVECTOR3 vPart; float fDot = D3DXVec3Dot(&vFrom, &vTo); D3DXVec3Cross(&vPart, &vFrom, &vTo); return D3DXQUATERNION(vPart.x, vPart.y, vPart.z, fDot); }
// Move camera backwards void Camera::MoveBack(GameTime& gameTime) { D3DXVECTOR3 forward; forward = mDirection - (D3DXVec3Dot(&mDirection, &mWorldUp) * mWorldUp); D3DXVec3Normalize(&forward, &forward); mPosition -= forward * C_MOVE_SPEED * (float)gameTime.GetTimeSinceLastTick().Seconds; }
void BoundingBox::getAxisProjection(D3DXVECTOR3 axis, BoundingBox* box, float& min, float& max) { float val; min = D3DXVec3Dot(&axis, &(box->getVertex(0))); max = D3DXVec3Dot(&axis, &(box->getVertex(0))); for (int i=0; i<8; i++) { D3DXVECTOR3 as = box->getVertex(i); val = D3DXVec3Dot(&axis, &(box->getVertex(i))); if (val < min) min = val; if (val > max) max = val; } }
D3DXQUATERNION ArcBall::QuatFromBallPoints(const D3DXVECTOR3 &vFrom, const D3DXVECTOR3 &vTo) { D3DXVECTOR3 vPart; float fDot = D3DXVec3Dot( &vFrom, &vTo ); // rotation angle D3DXVec3Cross( &vPart, &vFrom, &vTo ); // rotation axis return D3DXQUATERNION( vPart.x, vPart.y, vPart.z, fDot ); }
static inline void calcSphereTriangleDistance( Sphere* sphere, Vector* normal, Vector* v0, Vector* v1, Vector* v2, Vector* collPoint, float* distance ) { // project sphere center onto plane of triangle. Vector* center = &sphere->center; Vector projPoint; float dist2plane = D3DXVec3Dot( v0, normal ) - D3DXVec3Dot( center, normal ); D3DXVec3Scale( &projPoint, normal, dist2plane ); D3DXVec3Add( &projPoint, &projPoint, center ); // does the projected point lie within the collision triangle? Vector* vertices[3]; vertices[0] = v0, vertices[1] = v1, vertices[2] = v2; if( isPointWithinTriangle( &projPoint, vertices, normal ) ) { *distance = fabs( dist2plane ); *collPoint = projPoint; return; } // projected point lies outside the triangle, so find the nearest // point on the triangle boundary... else { float currdist; Vector closestPoint, temp; findNearestPointOnLine( &closestPoint, &projPoint, v0, v1 ); D3DXVec3Subtract( &temp, center, &closestPoint ); *distance = D3DXVec3Length(&temp), *collPoint = closestPoint; findNearestPointOnLine( &closestPoint, &projPoint, v1, v2 ); D3DXVec3Subtract(&temp, center, &closestPoint); currdist = D3DXVec3Length(&temp); if( *distance > currdist ) *distance = currdist, *collPoint = closestPoint; findNearestPointOnLine( &closestPoint, &projPoint, v0, v2 ); D3DXVec3Subtract(&temp, center, &closestPoint); currdist = D3DXVec3Length(&temp); if( *distance > currdist ) *distance = currdist, *collPoint = closestPoint; } }
//**関数*************************************************************************** // 概要 : 反射計算 //********************************************************************************* void CCalc::Bound3D(CBoundData* boundA , CBoundData* boundB) { D3DXVECTOR3 VelA , VelB; D3DXVECTOR3 VecAtoB; // オブジェクトの方向 D3DXVECTOR3 VecBtoA; // オブジェクトの方向 D3DXVECTOR3 vecBuf; float fBuf; // 計算用バッファ VelA = boundA->m_Velocity; VelB = boundB->m_Velocity; float fWaA = boundA->m_fMass; float fWaB = boundB->m_fMass; float fBou = boundA->m_fBounds; // オブジェクトの衝突方向を計算 VecAtoB = boundB->m_Pos - boundA->m_Pos; // AからBへのベクトル D3DXVec3Normalize(&VecAtoB , &VecAtoB); VecBtoA = boundA->m_Pos - boundB->m_Pos; // BからAへのベクトル D3DXVec3Normalize(&VecBtoA , &VecBtoA); // Aの速度ベクトルを分解 fBuf = D3DXVec3Dot(&VelA , &VecAtoB); VecAtoB = VecAtoB * fBuf; // 分解したベクトルを保存 VelA -= VecAtoB; // 継続して持つベクトルを逃がす // Bの速度ベクトル fBuf = D3DXVec3Dot(&VelB , &VecBtoA); VecBtoA = VecBtoA * fBuf; VelB -= VecBtoA; // ベクトル計算 boundA->m_Velocity = ((fWaA - fBou * fWaB) * VecAtoB + ((1 + fBou) * fWaB * VecBtoA)) / (fWaA + fWaB); boundB->m_Velocity = ((fWaB - fBou * fWaA) * VecBtoA + ((1 + fBou) * fWaA * VecAtoB)) / (fWaB + fWaA); boundA->m_Velocity += VelA; boundB->m_Velocity += VelB; // 衝突回避位置に移動 D3DXVECTOR3 vec = boundB->m_Pos - boundA->m_Pos; D3DXVec3Normalize(&vec , &vec); boundB->m_Pos.x = boundA->m_Pos.x + vec.x * (boundA->m_fRadius + boundB->m_fRadius + 0.5f); boundB->m_Pos.y = boundA->m_Pos.y + vec.y * (boundA->m_fRadius + boundB->m_fRadius + 0.5f); boundB->m_Pos.z = boundA->m_Pos.z + vec.z * (boundA->m_fRadius + boundB->m_fRadius + 0.5f); }
bool intersect_plane ( const D3DXVECTOR3& start, const D3DXVECTOR3& dir, const D3DXVECTOR3& pt, D3DXVECTOR3& ret ) { const D3DXVECTOR3& n = cache.normal; float denom = D3DXVec3Dot ( &n,&dir ); if ( denom <= -EPSILON || denom >= EPSILON ) { float numer = D3DXVec3Dot ( &n,&( pt - start ) ); float u = numer / denom; if ( u > 0 && u <= 1.0f ) { ret = start + dir * u; return true; } } return false; }
D3DXVECTOR3 Reflect(const D3DXVECTOR3* pN, const D3DXVECTOR3* pD) { D3DXVECTOR3 n = *pN; D3DXVECTOR3 d = *pD; D3DXVec3Normalize( &n, &n ); float fDot = D3DXVec3Dot( &n, &(-d) ); return 2*fDot*n + d; }
void Camera::UpdateViewMatrix() { D3DXVec3Normalize(&m_look, &m_look); D3DXVec3Cross(&m_up, &m_look, &m_right); D3DXVec3Normalize(&m_up, &m_up); D3DXVec3Cross(&m_right, &m_up, &m_look); D3DXVec3Normalize(&m_right, &m_right); float x = -D3DXVec3Dot(&m_right, &m_position); float y = -D3DXVec3Dot(&m_up, &m_position); float z = -D3DXVec3Dot(&m_look, &m_position); m_matView = D3DXMATRIX( m_right.x, m_up.x, m_look.x, 0.0f, m_right.y, m_up.y, m_look.y, 0.0f, m_right.z, m_up.z, m_look.z, 0.0f, x, y, z, 1.0f); }
////////////////////////////////////////////////////////////////////////// // setDirection // (desc) return rotation matrix to match shadow object's normal to input direction // + helper function ////////////////////////////////////////////////////////////////////////// rmatrix ZShadow::setDirection( rvector& dir_ ) { rmatrix xRotMat; rmatrix yRotMat; rvector xVector = dir_; xVector.y = 0; float xtheta = D3DXVec3Dot( &mNormal, &xVector ); rvector yVector = dir_; yVector.x = 0; float yTheta = D3DXVec3Dot( &mNormal, &yVector ); D3DXMatrixRotationX( &xRotMat, xtheta ); D3DXMatrixRotationY( &yRotMat, yTheta ); return xRotMat*yRotMat; }
Camera::Camera(D3DXVECTOR3 position, D3DXVECTOR3 direction, D3DXVECTOR3 worldUp, const Frustum& viewFrustum) : mPosition(position), mDirection(direction), mWorldUp(worldUp), mZoom(-200.0f) { mPosition = mPosition - (D3DXVec3Dot(&mPosition, &mWorldUp) * mWorldUp); mDestination = mPosition; D3DXVec3Normalize(&mDirection, &mDirection); CreateProjectionMatrix(viewFrustum); }
// funkcja z TFQE // kod do backface cullingu zakomentowany, lepiej daæ to jako osonn¹ funkcjê // zwraca ujemn¹ wartoœæ jeœli przecina promieñ od ty³u bool RayToTriangle(const VEC3& _ray_pos, const VEC3& _ray_dir, const VEC3& _v1, const VEC3& _v2, const VEC3& _v3, float& _dist) { ////// Nowy, piêkny algorytm :) // Znaleziony w Google Code Search: // http://www.google.com/codesearch?hl=en&q=+RayTriangleIntersect+show:M63-4o6bYUI:fUr9QIwtaKY:Dw059DARM5E&sa=N&cd=1&ct=rc&cs_p=http://www.angstrom-distribution.org/unstable/sources/ode-snapshot-20060210.tar.bz2&cs_f=ode-snapshot-20060210/ode/src/collision_trimesh_trimesh.cpp#first // Dopisa³em do niego Backface Culling, dzia³a bez problemu :D VEC3 tvec, pvec, qvec; // find vectors for two edges sharing vert0 VEC3 edge1 = _v2 - _v1; VEC3 edge2 = _v3 - _v1; // begin calculating determinant - also used to calculate U parameter D3DXVec3Cross(&pvec, &_ray_dir, &edge2); // if determinant is near zero, ray lies in plane of triangle float det = D3DXVec3Dot(&edge1, &pvec); //if (BackfaceCulling && det < 0.0f) // return false; if (FLOAT_ALMOST_ZERO(det)) return false; float inv_det = 1.0f / det; // calculate distance from vert0 to ray origin tvec = _ray_pos - _v1; // calculate U parameter and test bounds float u = D3DXVec3Dot(&tvec, &pvec) * inv_det; if (u < 0.0f || u > 1.0f) return false; // prepare to test V parameter D3DXVec3Cross(&qvec, &tvec, &edge1); // calculate V parameter and test bounds float v = D3DXVec3Dot(&_ray_dir, &qvec) * inv_det; if (v < 0.0f || u + v > 1.0f) return false; // calculate t, ray intersects triangle _dist = D3DXVec3Dot(&edge2, &qvec) * inv_det; return true; }
CCamera* CObject_Player::OnChangeCamera(ID3D11Device *pd3dDevice, DWORD nNewCameraMode, DWORD nCurrentCameraMode) { CCamera *pNewCamera = NULL; //새로운 카메라의 모드에 따라 카메라를 새로 생성한다. switch (nNewCameraMode) { case FIRST_PERSON_CAMERA: pNewCamera = new CFirstPersonCamera(m_pCamera); break; case THIRD_PERSON_CAMERA: pNewCamera = new CThirdPersonCamera(m_pCamera); break; case SPACESHIP_CAMERA: pNewCamera = new CSpaceShipCamera(m_pCamera); break; } /*현재 카메라의 모드가 스페이스-쉽 모드의 카메라이고 새로운 카메라가 1인칭 또는 3인칭 카메라이면 플레이어의 Up 벡터를 월드좌표계의 y-축 방향 벡터(0, 1, 0)이 되도록 한다. 즉, 똑바로 서도록 한다. 그리고 스페이스-쉽 카메라의 경우 플레이어의 이동에는 제약이 없다. 특히, y-축 방향의 움직임이 자유롭다. 그러므로 플레이어의 위치는 공중(위치 벡터의 y-좌표가 0보다 크다)이 될 수 있다. 이때 새로운 카메라가 1인칭 또는 3인칭 카메라이면 플레이어의 위치는 지면이 되어야 한다. 그러므로 플레이어의 Right 벡터와 Look 벡터의 y 값을 0으로 만든다. 이제 플레이어의 Right 벡터와 Look 벡터는 단위벡터가 아니므로 정규화한다.*/ if (nCurrentCameraMode == SPACESHIP_CAMERA) { m_d3dxvUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f); m_d3dxvRight.y = 0.0f; m_d3dxvLook.y = 0.0f; D3DXVec3Normalize(&m_d3dxvRight, &m_d3dxvRight); D3DXVec3Normalize(&m_d3dxvLook, &m_d3dxvLook); m_fPitch = 0.0f; m_fRoll = 0.0f; /*Look 벡터와 월드좌표계의 z-축(0, 0, 1)이 이루는 각도(내적=cos)를 계산하여 플레이어의 y-축의 회전 각도 m_fYaw로 설정한다.*/ m_fYaw = (float)D3DXToDegree(acosf(D3DXVec3Dot(&D3DXVECTOR3(0.0f, 0.0f, 1.0f), &m_d3dxvLook))); if (m_d3dxvLook.x < 0.0f) m_fYaw = -m_fYaw; } else if ((nNewCameraMode == SPACESHIP_CAMERA) && m_pCamera) { /*새로운 카메라의 모드가 스페이스-쉽 모드의 카메라이고 현재 카메라 모드가 1인칭 또는 3인칭 카메라이면 플레이어의 로컬 축을 현재 카메라의 로컬 축과 같게 만든다.*/ m_d3dxvRight = m_pCamera->GetRightVector(); m_d3dxvUp = m_pCamera->GetUpVector(); m_d3dxvLook = m_pCamera->GetLookVector(); } if (pNewCamera) { //기존 카메라가 없으면 새로운 카메라를 위한 쉐이더 변수를 생성한다. if (!m_pCamera) pNewCamera->CreateShaderVariables(pd3dDevice); pNewCamera->SetMode(nNewCameraMode); //현재 카메라를 사용하는 플레이어 객체를 설정한다. pNewCamera->SetPlayer(this); } if (m_pCamera) delete m_pCamera; return(pNewCamera); }
void r3dComposeLightMatrix( D3DXMATRIX& Mtx, r3dLight* l ) { r3d_assert( l->Type == R3D_SPOT_LIGHT || l->Type == R3D_DIRECT_LIGHT ); D3DXMATRIX w; D3DXVECTOR3 zaxis = l->Direction; D3DXVECTOR3 xaxis; D3DXVECTOR3 yaxis; D3DXVECTOR3 up( 0, 1, 0 ); float dt = D3DXVec3Dot( &up, &zaxis ); if( fabs( dt ) >= 0.9999999f ) { up = D3DXVECTOR3( 1, 0, 0 ); dt = D3DXVec3Dot( &up, &zaxis ); if( fabs( dt ) >= 0.9999999f ) { up = D3DXVECTOR3( 0, 0, 1 ); } } D3DXVec3Normalize( &xaxis, D3DXVec3Cross( &xaxis, &up, &zaxis ) ); D3DXVec3Cross( &yaxis, &zaxis, &xaxis ); D3DXMATRIX res; *(D3DXVECTOR3*) res.m[0] = xaxis; *(D3DXVECTOR3*) res.m[1] = yaxis; *(D3DXVECTOR3*) res.m[2] = zaxis; res.m[0][3] = 0.0f; res.m[1][3] = 0.0f; res.m[2][3] = 0.0f; res.m[3][3] = 1.0f; *(D3DXVECTOR3*)res.m[3] = D3DXVECTOR3( l->x, l->y, l->z ); Mtx = res; }
bool BoundingBox::centerInsideFrustum(D3DXVECTOR3* p, D3DXVECTOR3* n) { for (int i=0; i<6; i++) { float r = D3DXVec3Dot(&(center-p[i]), &n[i]); if (r > 0) return false; } return true; }
bool Picking::Ray_SphereIntersect(const Ray* ray, const BoundingSphere* sphere) { D3DXVECTOR3 v = ray->m_vOrigin - sphere->m_vCenter; float b = 2.0f * D3DXVec3Dot(&ray->m_vDirection, &v); float c = D3DXVec3Dot(&v, &v) - (sphere->m_fRadius * sphere->m_fRadius); float discriminant = (b * b) - (4.0f * c); if(discriminant < 0.0f) return false; discriminant = sqrt(discriminant); float s0 = (-b + discriminant) / 2.0f; float s1 = (-b - discriminant) / 2.0f; return (s0 >= 0.0f || s1 >= 0.0f); }
void FpsCamera::buildView() { m_viewMatrix._11 = m_right.x; m_viewMatrix._21 = m_right.y; m_viewMatrix._31 = m_right.z; m_viewMatrix._12 = m_up.x; m_viewMatrix._22 = m_up.y; m_viewMatrix._32 = m_up.z; m_viewMatrix._13 = m_view.x; m_viewMatrix._23 = m_view.y; m_viewMatrix._33 = m_view.z; m_viewMatrix._41 = -D3DXVec3Dot(&m_pos, &m_right); m_viewMatrix._42 = -D3DXVec3Dot(&m_pos, &m_up); m_viewMatrix._43 = -D3DXVec3Dot(&m_pos, &m_view); m_viewMatrix._44 = 1.0f; }
bool U2IntectRayBox3D::Test() { float aWdU[3], aAWdU[3], aDdU[3], aADdU[3], aAWxDdU[3], fRhs; D3DXVECTOR3 diff = m_pRay->m_vOrigin - m_pBox->m_vCenter; aWdU[0] = D3DXVec3Dot(&m_pRay->m_vDir, &m_pBox->m_avAxis[0]); aAWdU[0] = U2Math::FAbs(aWdU[0]); aDdU[0] = D3DXVec3Dot(&diff, &m_pBox->m_avAxis[0]); aADdU[0] = U2Math::FAbs(aDdU[0]); if(aADdU[0] > m_pBox->m_afExtent[0] && aDdU[0] * aWdU[0] >= 0.f) return false; aWdU[1] = D3DXVec3Dot(&m_pRay->m_vDir, &m_pBox->m_avAxis[1]); aAWdU[1] = U2Math::FAbs(aWdU[1]); aDdU[1] = D3DXVec3Dot(&diff, &m_pBox->m_avAxis[1]); aADdU[1] = U2Math::FAbs(aDdU[1]); if(aADdU[1] > m_pBox->m_afExtent[1] && aDdU[1] * aWdU[1] >= 0.f) return false; aWdU[2] = D3DXVec3Dot(&m_pRay->m_vDir, &m_pBox->m_avAxis[2]); aAWdU[2] = U2Math::FAbs(aWdU[2]); aDdU[2] = D3DXVec3Dot(&diff, &m_pBox->m_avAxis[2]); aADdU[2] = U2Math::FAbs(aDdU[2]); if(aADdU[2] > m_pBox->m_afExtent[2] && aDdU[2] * aWdU[2] >= 0.f) return false; D3DXVECTOR3 WxD; D3DXVec3Cross(&WxD, &m_pRay->m_vDir, &diff); aAWxDdU[0] = U2Math::FAbs(D3DXVec3Dot(&WxD, &m_pBox->m_avAxis[0])); fRhs = m_pBox->m_afExtent[1] *aAWdU[2] + m_pBox->m_afExtent[2]*aAWdU[1]; if(aAWxDdU[0] > fRhs) return false; aAWxDdU[1] = U2Math::FAbs(D3DXVec3Dot(&WxD, &m_pBox->m_avAxis[1])); fRhs = m_pBox->m_afExtent[0] *aAWdU[2] + m_pBox->m_afExtent[2]*aAWdU[0]; if(aAWxDdU[1] > fRhs) return false; aAWxDdU[2] = U2Math::FAbs(D3DXVec3Dot(&WxD, &m_pBox->m_avAxis[2])); fRhs = m_pBox->m_afExtent[0] *aAWdU[1] + m_pBox->m_afExtent[1]*aAWdU[0]; if(aAWxDdU[2] > fRhs) return false; return true; }
bool HitJudgments::IntersectSphereRay( float _sr, D3DXVECTOR3 _sp, D3DXVECTOR3 _rp, D3DXVECTOR3 _rv ) { D3DXVECTOR3 u = _rp - _sp; float a = D3DXVec3Dot( &_rv, &_rv ); float b = D3DXVec3Dot( &_rv, &u ); float c = D3DXVec3Dot( &u, &u ) - _sr ; if ( a - 0.00001f <= 0.0f ) { return false; } float isColli = b * b - a * c; if ( isColli < 0.0f ) { return false; } return true; }
void CCamera::getViewMatrix(D3DXMATRIX *V) { D3DXVec3Normalize(&g_vLook, &g_vLook); D3DXVec3Cross(&g_vUp, &g_vLook, &g_vRight); D3DXVec3Normalize(&g_vUp, &g_vUp); D3DXVec3Cross(&g_vRight, &g_vUp, &g_vLook); D3DXVec3Normalize(&g_vRight, &g_vRight); float x = -D3DXVec3Dot(&g_vRight, &g_vPos); float y = -D3DXVec3Dot(&g_vUp , &g_vPos); float z = -D3DXVec3Dot(&g_vLook , &g_vPos); V->_11 = g_vRight.x; V->_12 = g_vUp.x; V->_13 = g_vLook.x; V->_14 = 0.0f; V->_21 = g_vRight.y; V->_22 = g_vUp.y; V->_23 = g_vLook.y; V->_24 = 0.0f; V->_31 = g_vRight.z; V->_32 = g_vUp.z; V->_33 = g_vLook.z; V->_34 = 0.0f; V->_41 = x ; V->_42 = y ; V->_43 = z ; V->_44 = 1.0f; }
//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; } */ }
void Camera::updateViewMatrix() { D3DXVECTOR3 R = this->right; D3DXVECTOR3 U = this->up; D3DXVECTOR3 L = this->look; D3DXVECTOR3 P = this->position; D3DXVec3Normalize(&L,&L); D3DXVec3Cross(&U,&L,&R); D3DXVec3Normalize(&U,&U); D3DXVec3Cross(&R,&U,&L); float x = -D3DXVec3Dot(&P,&R); float y = -D3DXVec3Dot(&P,&U); float z = -D3DXVec3Dot(&P,&L); this->right = R; this->up = U; this->look = L; view(0,0) = this->right.x; view(1,0) = this->right.y; view(2,0) = this->right.z; view(3,0) = x; view(0,1) = this->up.x; view(1,1) = this->up.y; view(2,1) = this->up.z; view(3,1) = y; view(0,2) = this->look.x; view(1,2) = this->look.y; view(2,2) = this->look.z; view(3,2) = z; view(0,3) = 0.0f; view(1,3) = 0.0f; view(2,3) = 0.0f; view(3,3) = 1.0f; }
bool intersect_triangle(D3DXVECTOR3* orig, D3DXVECTOR3* dir, D3DXVECTOR3* vert0, D3DXVECTOR3* vert1, D3DXVECTOR3* vert2, double& t, double& u, double& v, D3DXVECTOR3* intPoint) { D3DXVECTOR3 edge1, edge2, tvec, pvec, qvec; double det, inv_det; D3DXVec3Subtract(&edge1, vert1, vert0); D3DXVec3Subtract(&edge2, vert2, vert0); D3DXVec3Cross(&pvec, dir, &edge2); det = D3DXVec3Dot(&edge1, &pvec); if (det < EPSILON) return false; D3DXVec3Subtract(&tvec, orig, vert0); u = D3DXVec3Dot(&tvec, &pvec); if (u<0.0 || u>det) return false; D3DXVec3Cross(&qvec, &tvec, &edge1); v = D3DXVec3Dot(dir, &qvec); if (v < 0.0 || u + v > det) return false; t = D3DXVec3Dot(&edge2, &qvec); inv_det = 1.0 / det; t *= inv_det; u *= inv_det; v *= inv_det; intPoint->x = vert0->x; intPoint->y = vert0->y; intPoint->z = vert0->z; intPoint->x += edge1.x*u + edge2.x*v; intPoint->y += edge1.y*u + edge2.y*v; intPoint->z += edge1.z*u + edge2.z*v; return true; }
void Camera::UpdateMatrix() { D3DXVECTOR3 R = mRight; D3DXVECTOR3 U = mUp; D3DXVECTOR3 L = mLook; D3DXVECTOR3 P = mPosition; D3DXVec3Normalize(&L, &L); D3DXVECTOR3 temp; D3DXVec3Cross(&temp, &L, &R); D3DXVec3Normalize(&U, &temp); D3DXVec3Cross(&R, &U, &L); float x = -D3DXVec3Dot(&P, &R); float y = -D3DXVec3Dot(&P, &U); float z = -D3DXVec3Dot(&P, &L); mRight = R; mUp = U; mLook = L; mView(0, 0) = mRight.x; mView(1, 0) = mRight.y; mView(2, 0) = mRight.z; mView(3, 0) = x; mView(0, 1) = mUp.x; mView(1, 1) = mUp.y; mView(2, 1) = mUp.z; mView(3, 1) = y; mView(0, 2) = mLook.x; mView(1, 2) = mLook.y; mView(2, 2) = mLook.z; mView(3, 2) = z; mView(0, 3) = 0.0f; mView(1, 3) = 0.0f; mView(2, 3) = 0.0f; mView(3, 3) = 1.0f; }
//----------------------------------------------------------------------------- // Name: D3DXQuaternionUnitAxisToUnitAxis2 // Desc: Axis to axis quaternion double angle (no normalization) // Takes two points on unit sphere an angle THETA apart, returns // quaternion that represents a rotation around cross product by 2*THETA. //----------------------------------------------------------------------------- inline D3DXQUATERNION* WINAPI D3DXQuaternionUnitAxisToUnitAxis2 ( D3DXQUATERNION *pOut, const D3DXVECTOR3 *pvFrom, const D3DXVECTOR3 *pvTo) { D3DXVECTOR3 vAxis; D3DXVec3Cross(&vAxis, pvFrom, pvTo); // proportional to sin(theta) pOut->x = vAxis.x; pOut->y = vAxis.y; pOut->z = vAxis.z; pOut->w = D3DXVec3Dot( pvFrom, pvTo ); return pOut; }
bool IT_SphereAABB(const D3DXVECTOR3& SpherePos, float SphereRadius, const AABB& Box) { D3DXVECTOR3 ClosestPt = ClosestPtToAABB(SpherePos, Box); D3DXVECTOR3 v = ClosestPt - SpherePos; if(D3DXVec3Dot(&v,&v) <= SphereRadius*SphereRadius) return true; return false; }
void Camera::Update(float deltaTime, D3DXVECTOR2 mouseMovement, float groundPosition) { float DegToRad = (3.14159/180); float RadToDeg = (180/3.14159); float rotRight = 0; float rotUp = 0; D3DXVECTOR3 horzDir = Forward; horzDir.y = 0.0f; float angleRad = D3DXVec3Dot(&horzDir, &Forward); float angleDeg = angleRad * RadToDeg; rotRight += sens * mouseMovement.x; rotUp -= sens * mouseMovement.y; // Limit the pitch if we're looking too far up if(angleRad < 0.2f && Forward.y >= 0) { rotUp = 0.001f; } //and if we're looking too far down else if(angleRad < 0.05f && Forward.y <= 0) { rotUp = -0.001f; } if(!onGround) { Position.y += velocityY*deltaTime; velocityY -= 130*deltaTime; if(Position.y <= groundPosition + characterHeight) { velocityY = 0.0f; onGround = true; } } else { Position.y = groundPosition + currentHeight; } float speed = 30.0f; Movment(speed, deltaTime); D3DXMATRIX rotation; D3DXMatrixRotationY(&rotation, rotRight); D3DXVec3TransformCoord(&Right, &Right, &rotation); D3DXVec3TransformCoord(&Forward, &Forward, &rotation); D3DXMatrixRotationAxis(&rotation, &Right, rotUp); D3DXVec3TransformCoord(&Forward, &Forward, &rotation); D3DXVec3Cross(&Up, &Forward, &Right); }