XMVECTOR MathHelper::RandHemisphereUnitVec3(XMVECTOR n) { XMVECTOR One = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f); XMVECTOR Zero = XMVectorZero(); // Keep trying until we get a point on/in the hemisphere. while(true) { // Generate random point in the cube [-1,1]^3. XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), 0.0f); // Ignore points outside the unit sphere in order to get an even distribution // over the unit sphere. Otherwise points will clump more on the sphere near // the corners of the cube. if( XMVector3Greater( XMVector3LengthSq(v), One) ) continue; // Ignore points in the bottom hemisphere. if( XMVector3Less( XMVector3Dot(n, v), Zero ) ) continue; return XMVector3Normalize(v); } }
VOID DebugDraw::DrawRay( const XMFLOAT3& Origin, const XMFLOAT3& Direction, BOOL bNormalize, D3DCOLOR Color ) { MeshVertexP verts[3]; memcpy( &verts[0], &Origin, 3 * sizeof( FLOAT ) ); XMVECTOR RayOrigin = XMLoadFloat3( &Origin ); XMVECTOR RayDirection = XMLoadFloat3( &Direction ); XMVECTOR NormDirection = XMVector3Normalize( RayDirection ); if( bNormalize ) RayDirection = NormDirection; XMVECTOR PerpVector; XMVECTOR CrossVector = XMVectorSet( 0, 1, 0, 0 ); PerpVector = XMVector3Cross( NormDirection, CrossVector ); if( XMVector3Equal( XMVector3LengthSq( PerpVector ), XMVectorSet( 0, 0, 0, 0 ) ) ) { CrossVector = XMVectorSet( 0, 0, 1, 0 ); PerpVector = XMVector3Cross( NormDirection, CrossVector ); } PerpVector = XMVector3Normalize( PerpVector ); XMStoreFloat3( ( XMFLOAT3* )&verts[1], XMVectorAdd( RayDirection, RayOrigin ) ); PerpVector = XMVectorScale( PerpVector, 0.0625f ); NormDirection = XMVectorScale( NormDirection, -0.25f ); RayDirection = XMVectorAdd( PerpVector, RayDirection ); RayDirection = XMVectorAdd( NormDirection, RayDirection ); XMStoreFloat3( ( XMFLOAT3* )&verts[2], XMVectorAdd( RayDirection, RayOrigin ) ); SimpleShaders::SetDeclPos(); SimpleShaders::BeginShader_Transformed_ConstantColor( g_matViewProjection, Color ); g_pd3dDevice->DrawPrimitiveUP( D3DPT_LINESTRIP, 2, ( const VOID* )verts, sizeof( MeshVertexP ) ); SimpleShaders::EndShader(); }
f32 Vector::lengthSqr() const { XMVECTOR vecLen = XMVector3LengthSq(m_vector); XMFLOAT4 fLen; XMStoreFloat4(&fLen, vecLen); return fLen.x; }
float DistanceSquared(const XMVECTOR& v1, const XMVECTOR& v2) { XMVECTOR& vectorSub = XMVectorSubtract(v1, v2); XMVECTOR& length = XMVector3LengthSq(vectorSub); float Distance = 0.0f; XMStoreFloat(&Distance, length); return Distance; }
_Use_decl_annotations_ bool __vectorcall ShapeCast(const XMFLOAT3& posA, const XMFLOAT3& moveA, SupportMapping* supA, const XMFLOAT3& posB, const XMFLOAT3& moveB, SupportMapping* supB, XMFLOAT3* normal, float* distance) { XMVECTOR s = XMLoadFloat3(&posA); XMVECTOR r = XMVectorSubtract(XMLoadFloat3(&moveA), XMLoadFloat3(&moveB)); XMVECTOR lambda = XMVectorZero(); XMVECTOR x = s; XMVECTOR v = x - XMLoadFloat3(&posB); XMVECTOR vDotR; XMVECTOR p; XMVECTOR w; XMVECTOR simplexP[5] = {}; uint32_t bits = 0; *distance = 0.0f; *normal = XMFLOAT3(0, 0, 0); XMVECTOR e2 = g_XMEpsilon * g_XMEpsilon; uint32_t iterations = 0; while (XMVector2Greater(XMVector3LengthSq(v), e2) && iterations++ < 1000) { v = XMVector3Normalize(v); p = supA->GetSupportPoint(v) + supB->GetSupportPoint(v); w = x - p; if (XMVector2Greater(XMVector3Dot(v, w), XMVectorZero())) { vDotR = XMVector3Dot(v, r); if (XMVector2GreaterOrEqual(vDotR, XMVectorZero())) { return false; } lambda = lambda - XMVector3Dot(v, w) / vDotR; if (XMVector2Greater(lambda, XMVectorSet(1, 1, 1, 1))) { return false; } x = s + lambda * r; XMStoreFloat3(normal, v); } AddPoint(simplexP, p, &bits); v = FindSupportVectorAndReduce(simplexP, x, &bits); } *distance = XMVectorGetX(lambda); return true; }
bool Collider::IsColliding(GameObject* other) { auto otherTransform = other->transform; auto v1 = XMLoadFloat3(&transform->position); auto v2 = XMLoadFloat3(&otherTransform->position); auto vectorDiff = XMVectorSubtract(v1, v2); auto distVector = XMVector3LengthSq(vectorDiff); float dist; XMStoreFloat(&dist, distVector); float radSq = transform->scale.x * transform->scale.x*.56 + otherTransform->scale.x * otherTransform->scale.x*.56; return dist <= radSq; }
void BatchRenderer::DrawRay( const XMFLOAT3& Origin, const XMFLOAT3& Direction, BOOL bNormalize, const FColor& Color ) { XMFLOAT3 verts[3]; memcpy( &verts[0], &Origin, 3 * sizeof( FLOAT ) ); XMVECTOR RayOrigin = XMLoadFloat3( &Origin ); XMVECTOR RayDirection = XMLoadFloat3( &Direction ); XMVECTOR NormDirection = XMVector3Normalize( RayDirection ); if( bNormalize ) RayDirection = NormDirection; XMVECTOR PerpVector; XMVECTOR CrossVector = XMVectorSet( 0, 1, 0, 0 ); PerpVector = XMVector3Cross( NormDirection, CrossVector ); if( XMVector3Equal( XMVector3LengthSq( PerpVector ), XMVectorSet( 0, 0, 0, 0 ) ) ) { CrossVector = XMVectorSet( 0, 0, 1, 0 ); PerpVector = XMVector3Cross( NormDirection, CrossVector ); } PerpVector = XMVector3Normalize( PerpVector ); XMStoreFloat3( ( XMFLOAT3* )&verts[1], XMVectorAdd( RayDirection, RayOrigin ) ); PerpVector = XMVectorScale( PerpVector, 0.0625f ); NormDirection = XMVectorScale( NormDirection, -0.25f ); RayDirection = XMVectorAdd( PerpVector, RayDirection ); RayDirection = XMVectorAdd( NormDirection, RayDirection ); XMStoreFloat3( ( XMFLOAT3* )&verts[2], XMVectorAdd( RayDirection, RayOrigin ) ); // Copy to vertex buffer assert( 3 <= MAX_VERTS ); XMFLOAT3* pVerts = NULL; HRESULT hr; V( g_pVB->Lock( 0, 0, (void**)&pVerts, D3DLOCK_DISCARD ) ) memcpy( pVerts, verts, sizeof(verts) ); V( g_pVB->Unlock() ) // Draw ray D3DXCOLOR clr = Color; g_pEffect9->SetFloatArray( g_Color, clr, 4 ); g_pEffect9->CommitChanges(); pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, 2 ); }
XMVECTOR MathHelper::RandUnitVec3() { XMVECTOR One = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f); XMVECTOR Zero = XMVectorZero(); while (true) { // Generate random point in the cube [-1,1]^3. XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), 0.0f); // Ignore points outside the unit sphere in order to get an even distribution // over the unit sphere. Otherwise points will clump more on the sphere near // the corners of the cube. if (XMVector3Greater(XMVector3LengthSq(v), One))//知道代码意思,但是代码实际执行看不懂。知其然不知其所以然 continue; return XMVector3Normalize(v); } }
float Vector3::length_squared() const { return XMVectorGetX(XMVector3LengthSq(*this)); }
float Vector3::lengthSquared() const { return XMVectorGetX(XMVector3LengthSq(XMLoadFloat3(&mVector))); }