public BoundingSphere Transform(XMMatrix m) { // Load the center of the sphere. XMVector v_center = this.center; // Transform the center of the sphere. XMVector c = XMVector3.Transform(v_center, m); XMVector dX = XMVector3.Dot(((XMVector *)&m)[0], ((XMVector *)&m)[0]); XMVector dY = XMVector3.Dot(((XMVector *)&m)[1], ((XMVector *)&m)[1]); XMVector dZ = XMVector3.Dot(((XMVector *)&m)[2], ((XMVector *)&m)[2]); XMVector d = XMVector.Max(dX, XMVector.Max(dY, dZ)); BoundingSphere result; // Store the center sphere. result.center = c; // Scale the radius of the pshere. float scale = (float)Math.Sqrt(d.X); result.radius = this.radius * scale; return(result); }
public ContainmentType Contains(XMVector point) { XMVector boxCenter = this.center; XMVector boxExtents = this.extents; return(XMVector3.InBounds(point - boxCenter, boxExtents) ? ContainmentType.Contains : ContainmentType.Disjoint); }
protected void UpdateVelocity(double fElapsedTime) { XMVector vMouseDelta = m_vMouseDelta; XMVector vRotVelocity = vMouseDelta * m_fRotationScaler; m_vRotVelocity = vRotVelocity; XMVector vKeyboardDirection = m_vKeyboardDirection; XMVector vAccel = vKeyboardDirection; // Normalize vector so if moving 2 dirs (left & forward), // the camera doesn't move faster than if moving in 1 dir vAccel = XMVector3.Normalize(vAccel); // Scale the acceleration vector vAccel *= m_fMoveScaler; if (m_bMovementDrag) { // Is there any acceleration this frame? if (XMVector3.LengthSquare(vAccel).X > 0) { // If so, then this means the user has pressed a movement key // so change the velocity immediately to acceleration // upon keyboard input. This isn't normal physics // but it will give a quick response to keyboard input m_vVelocity = vAccel; m_fDragTimer = m_fTotalDragTimeToZero; m_vVelocityDrag = vAccel / (float)m_fDragTimer; } else { // If no key being pressed, then slowly decrease velocity to 0 if (m_fDragTimer > 0) { // Drag until timer is <= 0 XMVector vVelocity = m_vVelocity; XMVector vVelocityDrag = m_vVelocityDrag; vVelocity -= vVelocityDrag * (float)fElapsedTime; m_vVelocity = vVelocity; m_fDragTimer -= fElapsedTime; } else { // Zero velocity m_vVelocity = XMVector.Zero; } } } else { // No drag, so immediately change the velocity m_vVelocity = vAccel; } }
public BoundingBox Transform(float scale, XMVector rotation, XMVector translation) { Debug.Assert(Internal.XMQuaternionIsUnit(rotation), "Reviewed"); // Load center and extents. XMVector boxCenter = this.center; XMVector boxExtents = this.extents; XMVector vectorScale = XMVector.Replicate(scale); // Compute and transform the corners and find new min/max bounds. XMVector corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[0], boxCenter); corner = XMVector3.Rotate(corner * vectorScale, rotation) + translation; XMVector min, max; min = max = corner; for (int i = 1; i < BoundingBox.CornerCount; i++) { corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], boxCenter); corner = XMVector3.Rotate(corner * vectorScale, rotation) + translation; min = XMVector.Min(min, corner); max = XMVector.Max(max, corner); } // Store center and extents. return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
public ContainmentType Contains(BoundingOrientedBox box) { if (!box.Intersects(this)) { return(ContainmentType.Disjoint); } XMVector boxCenter = this.center; XMVector boxExtents = this.extents; // Subtract off the AABB center to remove a subtract below XMVector o_center = box.Center - boxCenter; XMVector o_extents = box.Extents; XMVector o_orientation = box.Orientation; Debug.Assert(Internal.XMQuaternionIsUnit(o_orientation), "Reviewed"); XMVector inside = XMVector.TrueInt; for (int i = 0; i < BoundingOrientedBox.CornerCount; i++) { XMVector c = XMVector3.Rotate(o_extents * CollisionGlobalConstants.BoxOffsets[i], o_orientation) + o_center; XMVector d = c.Abs(); inside = XMVector.AndInt(inside, XMVector.LessOrEqual(d, boxExtents)); } return(XMVector3.EqualInt(inside, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public static void FastIntersectOrientedBoxPlane( XMVector center, XMVector extents, XMVector axis0, XMVector axis1, XMVector axis2, XMVector plane, out XMVector outside, out XMVector inside) { // Compute the distance to the center of the box. XMVector dist = XMVector4.Dot(center, plane); // Project the axes of the box onto the normal of the plane. Half the // length of the projection (sometime called the "radius") is equal to // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w)) // where h(i) are extents of the box, n is the plane normal, and b(i) are the // axes of the box. XMVector radius = XMVector3.Dot(plane, axis0); radius.Y = XMVector3.Dot(plane, axis1).Y; radius.Z = XMVector3.Dot(plane, axis2).Z; radius = XMVector3.Dot(extents, radius.Abs()); // Outside the plane? outside = XMVector.Greater(dist, radius); // Fully inside the plane? inside = XMVector.Less(dist, -radius); }
public bool Intersects(BoundingSphere sh) { XMVector sphereCenter = sh.Center; XMVector sphereRadius = XMVector.Replicate(sh.Radius); XMVector boxCenter = this.center; XMVector boxExtents = this.extents; XMVector boxMin = boxCenter - boxExtents; XMVector boxMax = boxCenter + boxExtents; //// Find the distance to the nearest point on the box. //// for each i in (x, y, z) //// if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2 //// else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2 XMVector d = XMGlobalConstants.Zero; // Compute d for each dimension. XMVector lessThanMin = XMVector.Less(sphereCenter, boxMin); XMVector greaterThanMax = XMVector.Greater(sphereCenter, boxMax); XMVector minDelta = sphereCenter - boxMin; XMVector maxDelta = sphereCenter - boxMax; // Choose value for each dimension based on the comparison. d = XMVector.Select(d, minDelta, lessThanMin); d = XMVector.Select(d, maxDelta, greaterThanMax); // Use a dot-product to square them and sum them together. XMVector d2 = XMVector3.Dot(d, d); return(XMVector3.LessOrEqual(d2, XMVector.Multiply(sphereRadius, sphereRadius))); }
public ContainmentType Contains(BoundingOrientedBox box) { if (!box.Intersects(this)) { return(ContainmentType.Disjoint); } XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); XMVector radiusSq = v_radius * v_radius; XMVector boxCenter = box.Center; XMVector boxExtents = box.Extents; XMVector boxOrientation = box.Orientation; Debug.Assert(Internal.XMQuaternionIsUnit(boxOrientation), "Reviewed"); XMVector insideAll = XMVector.TrueInt; for (int i = 0; i < BoundingOrientedBox.CornerCount; i++) { XMVector c = XMVector3.Rotate(boxExtents * CollisionGlobalConstants.BoxOffsets[i], boxOrientation) + boxCenter; XMVector d = XMVector3.LengthSquare(XMVector.Subtract(v_center, c)); insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq)); } return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public ContainmentType Contains(BoundingBox box) { if (!box.Intersects(this)) { return(ContainmentType.Disjoint); } XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); XMVector radiusSq = v_radius * v_radius; XMVector boxCenter = box.Center; XMVector boxExtents = box.Extents; XMVector insideAll = XMVector.TrueInt; XMVector offset = boxCenter - v_center; for (int i = 0; i < BoundingBox.CornerCount; i++) { XMVector c = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], offset); XMVector d = XMVector3.LengthSquare(c); insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq)); } return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public static XMVector XMPlaneTransform(XMVector plane, XMVector rotation, XMVector translation) { XMVector v_normal = XMVector3.Rotate(plane, rotation); XMVector vD = XMVector.SplatW(plane) - XMVector3.Dot(v_normal, translation); return(new XMVector(v_normal.X, v_normal.Y, v_normal.Z, vD.W)); }
public BoundingBox Transform(XMMatrix m) { // Load center and extents. XMVector boxCenter = this.center; XMVector boxExtents = this.extents; // Compute and transform the corners and find new min/max bounds. XMVector corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[0], boxCenter); corner = XMVector3.Transform(corner, m); XMVector min, max; min = max = corner; for (int i = 1; i < BoundingBox.CornerCount; i++) { corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], boxCenter); corner = XMVector3.Transform(corner, m); min = XMVector.Min(min, corner); max = XMVector.Max(max, corner); } // Store center and extents. return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
private void InitScene() { this.cascadedShadow.InitScene(this.selectedMesh, this.cascadeConfig); XMVector vMeshExtents = this.cascadedShadow.SceneAABBMax - this.cascadedShadow.SceneAABBMin; XMVector vMeshLength = XMVector3.Length(vMeshExtents); float fMeshLength = vMeshLength.GetByIndex(0); this.Settings.MeshLength = fMeshLength; }
public ContainmentType Contains(XMVector point) { XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); XMVector distanceSquared = XMVector3.LengthSquare(point - v_center); XMVector radiusSquared = XMVector.Multiply(v_radius, v_radius); return(XMVector3.LessOrEqual(distanceSquared, radiusSquared) ? ContainmentType.Contains : ContainmentType.Disjoint); }
public static void FastIntersectFrustumPlane( XMVector point0, XMVector point1, XMVector point2, XMVector point3, XMVector point4, XMVector point5, XMVector point6, XMVector point7, XMVector plane, out XMVector outside, out XMVector inside) { // Find the min/max projection of the frustum onto the plane normal. XMVector min, max, dist; min = max = XMVector3.Dot(plane, point0); dist = XMVector3.Dot(plane, point1); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point2); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point3); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point4); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point5); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point6); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); dist = XMVector3.Dot(plane, point7); min = XMVector.Min(min, dist); max = XMVector.Max(max, dist); XMVector planeDist = -XMVector.SplatW(plane); // Outside the plane? outside = XMVector.Greater(min, planeDist); // Fully inside the plane? inside = XMVector.Less(max, planeDist); }
public bool Intersects(XMVector v0, XMVector v1, XMVector v2) { // Load the sphere. XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); // Compute the plane of the triangle (has to be normalized). XMVector n = XMVector3.Normalize(XMVector3.Cross(v1 - v0, v2 - v0)); // Assert that the triangle is not degenerate. Debug.Assert(!XMVector3.Equal(n, XMGlobalConstants.Zero), "Reviewed"); // Find the nearest feature on the triangle to the sphere. XMVector dist = XMVector3.Dot(v_center - v0, n); // If the center of the sphere is farther from the plane of the triangle than // the radius of the sphere, then there cannot be an intersection. XMVector noIntersection = XMVector.Less(dist, -v_radius); noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(dist, v_radius)); // Project the center of the sphere onto the plane of the triangle. XMVector point = v_center - (n * dist); // Is it inside all the edges? If so we intersect because the distance // to the plane is less than the radius. XMVector intersection = Internal.PointOnPlaneInsideTriangle(point, v0, v1, v2); // Find the nearest point on each edge. XMVector radiusSq = v_radius * v_radius; // Edge 0,1 point = Internal.PointOnLineSegmentNearestPoint(v0, v1, v_center); // If the distance to the center of the sphere to the point is less than // the radius of the sphere then it must intersect. intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq)); // Edge 1,2 point = Internal.PointOnLineSegmentNearestPoint(v1, v2, v_center); // If the distance to the center of the sphere to the point is less than // the radius of the sphere then it must intersect. intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq)); // Edge 2,0 point = Internal.PointOnLineSegmentNearestPoint(v2, v0, v_center); // If the distance to the center of the sphere to the point is less than // the radius of the sphere then it must intersect. intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq)); return(XMVector4.EqualInt(XMVector.AndComplementInt(intersection, noIntersection), XMVector.TrueInt)); }
public void Update(ITimer timer) { float t = timer == null ? 0.0f : (float)timer.TotalSeconds; float d = timer == null ? 0.0f : (float)timer.ElapsedSeconds; this.worldMatrix = XMMatrix.RotationY(t); // Rotate the second light around the origin XMMatrix rotate = XMMatrix.RotationY(-2.0f * d); this.lightDirs[1] = XMVector3.Transform(this.lightDirs[1], rotate); }
public static BoundingBox CreateFromSphere(BoundingSphere sh) { XMVector sp_Center = sh.Center; XMVector sh_Radius = XMVector.Replicate(sh.Radius); XMVector min = XMVector.Subtract(sp_Center, sh_Radius); XMVector max = XMVector.Add(sp_Center, sh_Radius); Debug.Assert(XMVector3.LessOrEqual(min, max), "Reviewed"); return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
public ContainmentType Contains(BoundingSphere sh) { XMVector center1 = this.center; float r1 = this.radius; XMVector center2 = sh.center; float r2 = sh.radius; XMVector v = XMVector.Subtract(center2, center1); XMVector dist = XMVector3.Length(v); float d = dist.X; return((r1 + r2 >= d) ? ((r1 - r2 >= d) ? ContainmentType.Contains : ContainmentType.Intersects) : ContainmentType.Disjoint); }
public bool Intersects(XMVector origin, XMVector direction, out float distance) { Debug.Assert(Internal.XMVector3IsUnit(direction), "Reviewed"); XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); // l is the vector from the ray origin to the center of the sphere. XMVector l = v_center - origin; // s is the projection of the l onto the ray direction. XMVector s = XMVector3.Dot(l, direction); XMVector l2 = XMVector3.Dot(l, l); XMVector r2 = v_radius * v_radius; // m2 is squared distance from the center of the sphere to the projection. XMVector m2 = l2 - (s * s); XMVector noIntersection; // If the ray origin is outside the sphere and the center of the sphere is // behind the ray origin there is no intersection. noIntersection = XMVector.AndInt(XMVector.Less(s, XMGlobalConstants.Zero), XMVector.Greater(l2, r2)); // If the squared distance from the center of the sphere to the projection // is greater than the radius squared the ray will miss the sphere. noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(m2, r2)); // The ray hits the sphere, compute the nearest intersection point. XMVector q = (r2 - m2).Sqrt(); XMVector t1 = s - q; XMVector t2 = s + q; XMVector originInside = XMVector.LessOrEqual(l2, r2); XMVector t = XMVector.Select(t1, t2, originInside); if (XMVector4.NotEqualInt(noIntersection, XMVector.TrueInt)) { // Store the x-component to *pDist. t.StoreFloat(out distance); return(true); } distance = 0.0f; return(false); }
public MainGameComponent() { var vLightDir = new XMFloat3(-1, 1, -1); this.LightDirection = XMVector3.Normalize(vLightDir); XMVector eye = new XMVector(0.0f, 0.0f, -100.0f, 0.0f); XMVector at = new XMVector(0.0f, 0.0f, -0.0f, 0.0f); XMVector up = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); float fObjectRadius = 378.15607f; XMVector radius = XMVector3.Normalize(at - eye).Scale(fObjectRadius * 3.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up) * XMMatrix.TranslationFromVector(radius); this.WorldMatrix = XMMatrix.Identity; }
public ContainmentType Contains(BoundingFrustum fr) { if (!fr.Intersects(this)) { return(ContainmentType.Disjoint); } XMVector v_center = this.center; XMVector v_radius = XMVector.Replicate(this.radius); XMVector radiusSq = v_radius * v_radius; XMVector v_origin = fr.Origin; XMVector v_orientation = fr.Orientation; Debug.Assert(Internal.XMQuaternionIsUnit(v_orientation), "Reviewed"); // Build the corners of the frustum. XMVector v_rightTop = new XMVector(fr.RightSlope, fr.TopSlope, 1.0f, 0.0f); XMVector v_rightBottom = new XMVector(fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f); XMVector v_leftTop = new XMVector(fr.LeftSlope, fr.TopSlope, 1.0f, 0.0f); XMVector v_leftBottom = new XMVector(fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f); XMVector v_near = XMVector.Replicate(fr.Near); XMVector v_far = XMVector.Replicate(fr.Far); XMVector[] corners = new XMVector[BoundingFrustum.CornerCount]; corners[0] = v_rightTop * v_near; corners[1] = v_rightBottom * v_near; corners[2] = v_leftTop * v_near; corners[3] = v_leftBottom * v_near; corners[4] = v_rightTop * v_far; corners[5] = v_rightBottom * v_far; corners[6] = v_leftTop * v_far; corners[7] = v_leftBottom * v_far; XMVector insideAll = XMVector.TrueInt; for (int i = 0; i < BoundingFrustum.CornerCount; i++) { XMVector c = XMVector3.Rotate(corners[i], v_orientation) + v_origin; XMVector d = XMVector3.LengthSquare(XMVector.Subtract(v_center, c)); insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq)); } return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public static void FastIntersectAxisAlignedBoxPlane(XMVector center, XMVector extents, XMVector plane, out XMVector outside, out XMVector inside) { // Compute the distance to the center of the box. XMVector dist = XMVector4.Dot(center, plane); // Project the axes of the box onto the normal of the plane. Half the // length of the projection (sometime called the "radius") is equal to // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w)) // where h(i) are extents of the box, n is the plane normal, and b(i) are the // axes of the box. In this case b(i) = [(1,0,0), (0,1,0), (0,0,1)]. XMVector radius = XMVector3.Dot(extents, plane.Abs()); // Outside the plane? outside = XMVector.Greater(dist, radius); // Fully inside the plane? inside = XMVector.Less(dist, -radius); }
public BoundingSphere Transform(float scale, XMVector rotation, XMVector translation) { // Load the center of the sphere. XMVector v_center = this.center; // Transform the center of the sphere. v_center = XMVector3.Rotate(v_center * XMVector.Replicate(scale), rotation) + translation; BoundingSphere result; // Store the center sphere. result.center = v_center; // Scale the radius of the pshere. result.radius = this.radius * scale; return(result); }
public override void SetViewParams(XMVector vEyePt, XMVector vLookatPt) { base.SetViewParams(vEyePt, vLookatPt); // Propogate changes to the member arcball XMMatrix mRotation = XMMatrix.LookAtLH(vEyePt, vLookatPt, XMVector.FromFloat(0.0f, 1.0f, 0.0f, 0.0f)); XMVector quat = XMQuaternion.RotationMatrix(mRotation); m_ViewArcBall.SetQuatNow(quat); // Set the radius according to the distance XMVector vEyeToPoint = XMVector.Subtract(vLookatPt, vEyePt); float len = XMVector3.Length(vEyeToPoint).X; SetRadius(len); // View information changed. FrameMove should be called. m_bDragSinceLastUpdate = true; }
public static BoundingBox CreateMerged(BoundingBox b1, BoundingBox b2) { XMVector b1_Center = b1.center; XMVector b1_Extents = b1.extents; XMVector b2_Center = b2.center; XMVector b2_Extents = b2.extents; XMVector min = XMVector.Subtract(b1_Center, b1_Extents); min = XMVector.Min(min, XMVector.Subtract(b2_Center, b2_Extents)); XMVector max = XMVector.Add(b1_Center, b1_Extents); max = XMVector.Max(max, XMVector.Add(b2_Center, b2_Extents)); Debug.Assert(XMVector3.LessOrEqual(min, max), "Reviewed"); return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); this.mainGameComponent.CreateDeviceDependentResources(this.DeviceResources); float fObjectRadius = 378.15607f; // Setup the camera's view parameters XMFloat3 vecEye = new XMFloat3(0.0f, 0.0f, -100.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, -0.0f); this.camera.SetViewParams(vecEye, vecAt); this.camera.SetRadius(fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f); var vLightDir = new XMFloat3(-1, 1, -1); vLightDir = XMVector3.Normalize(vLightDir); this.lightControl.SetLightDirection(vLightDir); }
public ContainmentType Contains(XMVector v0, XMVector v1, XMVector v2) { if (!this.Intersects(v0, v1, v2)) { return(ContainmentType.Disjoint); } XMVector boxCenter = this.center; XMVector boxExtents = this.extents; XMVector d = XMVector.Subtract(v0, boxCenter).Abs(); XMVector inside = XMVector.LessOrEqual(d, boxExtents); d = XMVector.Subtract(v1, boxCenter).Abs(); inside = XMVector.AndInt(inside, XMVector.LessOrEqual(d, boxExtents)); d = XMVector.Subtract(v2, boxCenter).Abs(); inside = XMVector.AndInt(inside, XMVector.LessOrEqual(d, boxExtents)); return(XMVector3.EqualInt(inside, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public static XMVector PointOnLineSegmentNearestPoint(XMVector s1, XMVector s2, XMVector p) { XMVector dir = s1 - s1; XMVector projection = XMVector3.Dot(p, dir) - XMVector3.Dot(s1, dir); XMVector lengthSq = XMVector3.Dot(dir, dir); XMVector t = projection * lengthSq.Reciprocal(); XMVector point = s1 + (t * dir); // t < 0 XMVector selectS1 = XMVector.Less(projection, XMGlobalConstants.Zero); point = XMVector.Select(point, s1, selectS1); // t > 1 XMVector selectS2 = XMVector.Greater(projection, lengthSq); point = XMVector.Select(point, s2, selectS2); return(point); }
private static void LoadParticles(Random rand, Particle[] pParticles, int startIndex, XMFloat3 center, XMFloat4 velocity, float spread, int numParticles) { for (int i = 0; i < numParticles; i++) { XMFloat3 delta = new XMFloat3(spread, spread, spread); while (XMVector3.LengthSquare(delta).X > spread * spread) { delta.X = RPercent(rand) * spread; delta.Y = RPercent(rand) * spread; delta.Z = RPercent(rand) * spread; } pParticles[startIndex + i].pos.X = center.X + delta.X; pParticles[startIndex + i].pos.Y = center.Y + delta.Y; pParticles[startIndex + i].pos.Z = center.Z + delta.Z; pParticles[startIndex + i].pos.W = 10000.0f * 10000.0f; pParticles[startIndex + i].velo = velocity; } }
public static XMVector PointOnPlaneInsideTriangle(XMVector p, XMVector v0, XMVector v1, XMVector v2) { // Compute the triangle normal. XMVector n = XMVector3.Cross(v2 - v0, v1 - v0); // Compute the cross products of the vector from the base of each edge to // the point with each edge vector. XMVector c0 = XMVector3.Cross(p - v0, v1 - v0); XMVector c1 = XMVector3.Cross(p - v1, v2 - v1); XMVector c2 = XMVector3.Cross(p - v2, v0 - v2); // If the cross product points in the same direction as the normal the the // point is inside the edge (it is zero if is on the edge). XMVector zero = XMGlobalConstants.Zero; XMVector inside0 = XMVector.GreaterOrEqual(XMVector3.Dot(c0, n), zero); XMVector inside1 = XMVector.GreaterOrEqual(XMVector3.Dot(c1, n), zero); XMVector inside2 = XMVector.GreaterOrEqual(XMVector3.Dot(c2, n), zero); // If the point inside all of the edges it is inside. return(XMVector.AndInt(XMVector.AndInt(inside0, inside1), inside2)); }