/* distance to hit the ground */ public float Intersect(Vector3 pos, Vector3 dir) { int square = 0; if (!simple) { square = getSquare(pos); if (square == -1) { return(pos.Y); } if ((map[square].mesh == null) || (map[square].loading)) { return(0f); } } IntersectInformation hit = new IntersectInformation(); if (map[square].mesh.Intersect(pos, dir, ref hit)) { return(hit.Dist); } return(0f); }
private Vector3 ComputeIntersectedVector(Mesh sourceMeth, IntersectInformation ii) { // create an array to hold the indices for the intersected face short[] intersectedIndices = new short[3]; // fetch indices for the intersected face from the mesh short[] indices = (short[])sourceMeth.LockIndexBuffer(typeof(short), LockFlags.ReadOnly, sourceMeth.NumberFaces * 3); Array.Copy(indices, ii.FaceIndex * 3, intersectedIndices, 0, 3); sourceMeth.UnlockIndexBuffer(); // create an array to hold the vertices for the intersected face Vector3[] intersectVertices = new Vector3[3]; // extract vertex data from mesh, using our indices we obtained earlier Vector3[] meshVertices = (Vector3[])sourceMeth.LockVertexBuffer(typeof(Vector3), LockFlags.ReadOnly, sourceMeth.NumberVertices); intersectVertices[0] = meshVertices[intersectedIndices[0]]; intersectVertices[1] = meshVertices[intersectedIndices[1]]; intersectVertices[2] = meshVertices[intersectedIndices[2]]; sourceMeth.UnlockVertexBuffer(); //Compute the interset vector on the face Vector3 intersectVector = intersectVertices[0] + ii.U * (intersectVertices[1] - intersectVertices[0]) + ii.V * (intersectVertices[2] - intersectVertices[0]); //Transform it to the world space intersectVector.TransformCoordinate(source.WorldMatrix); return(intersectVector); }
/// <summary> /// Checks whether a ray intersects with the collision mesh. /// </summary> /// <param name="rayPos"></param> /// <param name="rayDir"></param> /// <param name="depth"></param> /// <returns></returns> public bool Intersects(Vector3 rayPos, Vector3 rayDir, out float depth) { if (this.mesh != null) { // Transform the ray to the GameObject coordinates Vector3 rayPosModel = rayPos; Vector3 rayDirModel = rayDir; Matrix inverseTransformMatrix = Matrix.Invert(gameObject.TransformMatrix); rayPosModel.TransformCoordinate(inverseTransformMatrix); rayDirModel.TransformCoordinate(inverseTransformMatrix); IntersectInformation intersectInfo = new IntersectInformation(); if (this.mesh.Intersect(rayPosModel, rayDirModel, out intersectInfo)) { if (intersectInfo.Dist >= 0 && intersectInfo.Dist < rayDir.Length()) { depth = intersectInfo.Dist; return(true); } } } // If arrived here, no intersection occurred depth = 0; return(false); }
public override bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { Matrix inver = Matrix.Invert(model.WorldMatrix); near.TransformCoordinate(inver); far.TransformCoordinate(inver); return(coneMesh.M.Intersect(near, far - near, out closestHit)); }
public override bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { //Copy the vector Vector3 near1 = near; Vector3 far1 = far; Matrix inver = Matrix.Invert(model.WorldMatrix); near.TransformCoordinate(inver); far.TransformCoordinate(inver); return(rectangleMesh.M.Intersect(near, far - near, out closestHit)); }
public List <Vector3> GetIntersectedVectors(Mesh sourceMeth, IntersectInformation[] hits) { List <Vector3> allHitPoints = new List <Vector3>(); //Recover the vector information from the intersection information for (int index = 0; index < hits.Length; index++) { IntersectInformation hit = hits[index]; Vector3 intersectVector = this.ComputeIntersectedVector(sourceMeth, hit); allHitPoints.Add(intersectVector); } return(allHitPoints); }
public Vector3 DXClickToVector(Vector2 click) { if (HBObject == null) { return(new Vector3(0, 0, 0)); } if (HBObject.Model.Mesh == null) { return(new Vector3(0, 0, 0)); } Matrix World = HBObject.CreateWorldMatrix(); Vector3 rayStart, rayDirection; // Convert the mouse point into a 3D point Vector3 v = new Vector3( (((2.0f * click.X) / this.ClientRectangle.Width) - 1) / Proj.M11, -(((2.0f * click.Y) / this.ClientRectangle.Height) - 1) / Proj.M22, 1.0f); // Get the inverse of the composite view and world matrix Matrix m = View * World; m.Invert(); // Transform the screen space pick ray into 3D space rayDirection.X = v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31; rayDirection.Y = v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32; rayDirection.Z = v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33; rayStart.X = m.M41; rayStart.Y = m.M42; rayStart.Z = m.M43; Mesh msh = ((XMesh)HBObject.Model.Mesh).Mesh; IntersectInformation info = new IntersectInformation(); if (!msh.Intersect(rayStart, rayDirection, out info)) { return(new Vector3(0.0f, 0.0f, 0.0f)); } Vector3 hitPos = rayStart + info.Dist * rayDirection; return(new Vector3(HBObject.X + hitPos.X, HBObject.Y - hitPos.Y, hitPos.Z)); }
private void HighlightIntersectedFace(IntersectInformation ii) { short[] intersectedIndices = new short[3]; short[] indices = (short[])scannerMesh.LockIndexBuffer(typeof(short), LockFlags.ReadOnly, scannerMesh.NumberFaces * 3); Array.Copy(indices, ii.FaceIndex * 3, intersectedIndices, 0, 3); scannerMesh.UnlockIndexBuffer(); CustomVertex.PositionTextured[] tempIntersectedVertices = new CustomVertex.PositionTextured[3]; CustomVertex.PositionTextured[] meshVertices = (CustomVertex.PositionTextured[])scannerMesh.LockVertexBuffer(typeof(CustomVertex.PositionTextured), LockFlags.ReadOnly, scannerMesh.NumberVertices); tempIntersectedVertices[0] = meshVertices[intersectedIndices[0]]; tempIntersectedVertices[1] = meshVertices[intersectedIndices[1]]; tempIntersectedVertices[2] = meshVertices[intersectedIndices[2]]; scannerMesh.UnlockVertexBuffer(); this.intersectedVertices = tempIntersectedVertices; }
public override bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { //Copy the vector Vector3 near1 = near; Vector3 far1 = far; Matrix inver = Matrix.Invert(model.WorldMatrix); near.TransformCoordinate(inver); far.TransformCoordinate(inver); IntersectInformation intersectRound; bool roundResult = roundMesh.M.Intersect(near, far - near, out intersectRound); closestHit = intersectRound; return(roundResult); }
/* We try to get the height of the landscape so the airplane * can land with pos.Y = landscape.Y * For that we get the current height on that point compared * to the land, and subtract that value from our pos.Y that way * our pos.Y should be equal to landscape.Y * */ public float RealHeight(Vector3 pos) { int square = 0; if (!simple) { square = getSquare(pos); if (square == -1) { return(pos.Y); } if ((map[square].mesh == null) || (map[square].loading)) { return(0f); } } IntersectInformation hit = new IntersectInformation(); Vector3 dir = new Vector3(0f, -1f, 0f); if (map[square].mesh.Intersect(pos, dir, ref hit)) { return(pos.Y - hit.Dist); } dir.Y = 1f; if (map[square].mesh.Intersect(pos, dir, ref hit)) { if (pos.Y > 0f) { return(pos.Y - hit.Dist); } else { return(pos.Y + hit.Dist); } } // no intersection at all? return the object height return(pos.Y); }
public bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { IntersectInformation primaryModelHitInfo; bool primaryModelHitResult = primaryModelRender.Intersect(near, far, out primaryModelHitInfo); bool eoSymbolModelHitResult = false; IntersectInformation eoSymbolModelHitInfo = new IntersectInformation(); if (eoSymbolRender != null) { eoSymbolModelHitResult = eoSymbolRender.Intersect(near, far, out eoSymbolModelHitInfo); } if (eoSymbolModelHitResult) { if (primaryModelHitResult) { if (eoSymbolModelHitInfo.Dist > primaryModelHitInfo.Dist) { closestHit = primaryModelHitInfo; } else { closestHit = eoSymbolModelHitInfo; } return(true); } else { closestHit = eoSymbolModelHitInfo; return(true); } } else { closestHit = primaryModelHitInfo; return(primaryModelHitResult); } }
public override bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { //Get a clone of this point , because we'll changed its value Vector3 clone = model.Position; //Project the point to the viewport near.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, Matrix.Identity); far.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, Matrix.Identity); clone.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, model.WorldMatrix); closestHit = new IntersectInformation(); //Determine whether the two points is the same by a litte error if (Math.Abs(((short)clone.X - (short)near.X)) <= GeometryRender.DefaultPointWidth && (Math.Abs((short)clone.Y - (short)near.Y)) <= GeometryRender.DefaultPointWidth) { closestHit.Dist = clone.Z; return(true); } else { return(false); } }
public static bool TestIntersectWithMouse(Mesh mshMesh, object objViewport, Matrix mtxProjection, Matrix mtxView, Matrix mtxWorld, int iX, int iY, out IntersectInformation iiInfo) { // Create ray for intersection test Vector3 near = new Vector3(iX, iY, 0); Vector3 far = new Vector3(iX, iY, 1); //unproject the near and far vectors to 3D space near.Unproject(objViewport, mtxProjection, mtxView, mtxWorld); far.Unproject(objViewport, mtxProjection, mtxView, mtxWorld); //subtract the near vector from the far vector to get our ray far.Subtract(near); return mshMesh.Intersect(near,far, out iiInfo); }
/// <summary> /// The bsp collision viewer_ mouse down. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> /// <remarks></remarks> private void BSPCollisionViewer_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { cam.oldx = e.X; cam.oldy = e.Y; } #region SpawnSelection (Mouse Left Button) else if (e.Button == MouseButtons.Left) { if (currentMode == editingModes.Surface) #region Check for collision intersection of Surfaces { // This takes our mouse cursor and creates a line directly into the screen (2D -> 3D) Vector3 s = Vector3.Unproject(new Vector3(e.X, e.Y, 0), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 d = Vector3.Unproject(new Vector3(e.X, e.Y, 1), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 rPosition = s; Vector3 rDirection = Vector3.Normalize(d - s); // Used to find the nearest polygon IntersectInformation iiClosest = new IntersectInformation(); iiClosest.Dist = 10000; // Set a very far off distance for (int x = 0; x < polygons.Count; x++) { //check for intersection IntersectInformation ii; for (int xx = 0; xx < polygons[x].indices.Length - 2; xx++) { Geometry.IntersectTri( coll.Vertices[polygons[x].indices[0]], coll.Vertices[polygons[x].indices[xx + 1]], coll.Vertices[polygons[x].indices[xx + 2]], rPosition, rDirection, out ii); if (ii.Dist != 0) { if (iiClosest.Dist > ii.Dist) iiClosest = ii; else continue; // holds the surface # that is currently nearest currentSurface = x; break; }; } updateInfo(); } } #endregion /* #region DecideUponObjectRotation if ((SelectedSpawn.Count > 0) && (rotationBitMask != 0)) { selectionStart = render.Mark3DCursorPosition(e.X, e.Y, Matrix.Identity); oldx = e.X; oldy = e.Y; itemrotate = true; //return; } #endregion else */ if (currentMode == editingModes.Point) #region Collision point movement mode { Mesh checkSphere = Mesh.Sphere(device, 0.3f, 5, 5); #region CheckSpawnsForIntersection for (int x = 0; x < coll.Vertices.Length; x++) { //int tempcount = SpawnModel[spawnmodelindex[x]].Display.Chunk.Count; int tempcount = 1; for (int yy = 0; yy < tempcount; yy++) { // Check under mouse cursor for object selection/deselection? if (render.MeshPick(e.X, e.Y, checkSphere, TranslationMatrix[x]) == true) { #region TurnSpawnOnOrOff int tempi = SelectedPoints.IndexOf(x); if (tempi != -1) { SelectedPoints.RemoveAt(tempi); } else { SelectedPoints.Add(x); } #endregion updateInfo(); System.Threading.Thread.Sleep(300); break; } } } #endregion CycleThroughSpawns checkSphere.Dispose(); } #endregion } #endregion }
public Vector3 DXClickToVector(Vector2 click) { if (HBObject == null) return new Vector3(0, 0, 0); if (HBObject.Model.Mesh == null) return new Vector3(0, 0, 0); Matrix World = HBObject.CreateWorldMatrix(); Vector3 rayStart, rayDirection; // Convert the mouse point into a 3D point Vector3 v = new Vector3( (((2.0f*click.X)/this.ClientRectangle.Width) - 1)/Proj.M11, -(((2.0f*click.Y)/this.ClientRectangle.Height) - 1)/Proj.M22, 1.0f); // Get the inverse of the composite view and world matrix Matrix m = View*World; m.Invert(); // Transform the screen space pick ray into 3D space rayDirection.X = v.X*m.M11 + v.Y*m.M21 + v.Z*m.M31; rayDirection.Y = v.X*m.M12 + v.Y*m.M22 + v.Z*m.M32; rayDirection.Z = v.X*m.M13 + v.Y*m.M23 + v.Z*m.M33; rayStart.X = m.M41; rayStart.Y = m.M42; rayStart.Z = m.M43; Mesh msh = ((XMesh) HBObject.Model.Mesh).Mesh; IntersectInformation info = new IntersectInformation(); if (!msh.Intersect(rayStart, rayDirection, out info)) return new Vector3(0.0f, 0.0f, 0.0f); Vector3 hitPos = rayStart + info.Dist*rayDirection; return new Vector3(HBObject.X + hitPos.X, HBObject.Y - hitPos.Y, hitPos.Z); }
protected void ProcessLowerSquare(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { RigidBodies.RigidBody_DX rbParent = this.Parent_DX; // Vector3 v3Tmp = this.LocalLocation; // //allow for normal movement with mouse // DoMouseTranslation(AnimatMouseArgs); // // //now make sure the final position of the movement puts the object on the surface of the parent // // //shoot a ray from the center of this body to to the surface of the parent. // //Direction of the ray is -this.Direction // IntersectInformation iiTmp = new IntersectInformation(); // if(rbParent.Mesh.Intersect(this.LocalLocation,-Direction,out iiTmp)) // { // //find the point of intersection // Vector3 m_v3Intersect = Util_DX.FindIntersectionPoint(rbParent.Mesh,iiTmp); // this.LocalLocation = m_v3Intersect + this.FindPointOnSurface(new Vector3(),-Direction); // } // else // { // //MessageBox.Show("There was NOT an intersection"); //// System.Windows.Forms.MouseEventArgs MouseArgs = new System.Windows.Forms.MouseEventArgs(AnimatMouseArgs.Button, AnimatMouseArgs.Clicks, -AnimatMouseArgs.X, -AnimatMouseArgs.Y, AnimatMouseArgs.Delta); //// AnimatTools.Framework.MouseEventArgs ArgsInv = new AnimatTools.Framework.MouseEventArgs(MouseArgs,AnimatMouseArgs.OldX, AnimatMouseArgs.OldY, AnimatMouseArgs.Shift, AnimatMouseArgs.Control, AnimatMouseArgs.XKey, AnimatMouseArgs.YKey, AnimatMouseArgs.ZKey, AnimatMouseArgs.SKey, AnimatMouseArgs.Scale); //// DoMouseTranslation(ArgsInv); // this.LocalLocation = v3Tmp; // } // //Get the spherical coordinates for the current location // Vector3 v3Spherical = Util_DX.Cartesian_To_Spherical(this.LocalLocation); // // //change the r and theta values according to the mouse // v3Spherical.X = 1.5f; // v3Spherical.Z += AnimatMouseArgs.DeltaX * (AnimatMouseArgs.Scale * 2); // v3Spherical.Y += AnimatMouseArgs.DeltaY * (AnimatMouseArgs.Scale * 2); // // // //Convert the sphereical cooridnate back to cartesian // Vector3 v3TmpLoc = Util_DX.Spherical_To_Cartesian(v3Spherical); // this.LocalLocation = v3TmpLoc; // Direction = FindDirection(); // // //shoot a ray from the center of this body to to the surface of the parent. // //Direction of the ray is -this.Direction // IntersectInformation iiTmp = new IntersectInformation(); // if(rbParent.Mesh.Intersect(Vector3.Empty, v3TmpLoc ,out iiTmp)) // { // //find the point of intersection // Vector3 m_v3Intersect = Util_DX.FindIntersectionPoint(rbParent.Mesh,iiTmp); // this.LocalLocation = m_v3Intersect + this.FindPointOnSurface(new Vector3(),-Direction); // this.OrientBody(); // } // else // MessageBox.Show("Doh"); // // this.LocalLocation = Util_DX.Spherical_To_Cartesian(v3Spherical); Vector3 v3Tmp; if(m_v3Intersect != Vector3.Empty) v3Tmp = m_v3Intersect; else v3Tmp = this.LocalLocation; float fX = (AnimatMouseArgs.DeltaX * -AnimatMouseArgs.Scale * 2); float fY = (AnimatMouseArgs.DeltaY * -AnimatMouseArgs.Scale * 2); v3Tmp.TransformCoordinate(Matrix.RotationX(fX) * Matrix.RotationZ(fY)); this.LocalLocation = v3Tmp; Direction = FindDirection(); IntersectInformation iiTmp = new IntersectInformation(); if(rbParent.Mesh.Intersect(Vector3.Empty, this.LocalLocation ,out iiTmp)) { //find the point of intersection m_v3Intersect = Util_DX.FindIntersectionPoint(rbParent.Mesh,iiTmp); this.LocalLocation = m_v3Intersect + this.FindPointOnSurface(new Vector3(),-Direction); //this.LocalLocation = m_v3Intersect; this.OrientBody(); } // else // MessageBox.Show("Damnit"); }
public override void UpdateWithMouse(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { //If no button is pressed..nothing is selected if(AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.None) { m_bCBSelected = false; m_bRBSelected = false; return; } IntersectInformation iiInfo = new IntersectInformation(); Vector3 tmp = new Vector3(); tmp.TransformCoordinate(CombinedTransformationMatrix * Device.Transform.World1); Matrix mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(m_mshCB != null && !m_bRBSelected && Util_DX.TestIntersectWithMouse(m_mshCB, Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bCBSelected = true; tmp = new Vector3(); tmp.TransformCoordinate(Matrix.Translation(0,-Radius,0.06f) * this.CombinedTransformationMatrix * Device.Transform.World1); mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(m_mshCB != null && !m_bCBSelected && Util_DX.TestIntersectWithMouse(m_mshCB, Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bRBSelected = true; if(m_bCBSelected) ProcessCenterSquare(AnimatMouseArgs); else if(m_bRBSelected) ProcessRadius(AnimatMouseArgs); }
public virtual bool TestReceptiveFieldIntersection(int x, int y, ref Vec3d vVertex) { if(m_d3dReceptiveFieldPoint != null) { //a temporary intersect information for the selected bounding box IntersectInformation iiTmp = new IntersectInformation(); Vec3d vSelVertex = new Vec3d(null, 0, 0, 0); float fltSelDistance = -1; //First find all of the vertex points that are intersected. foreach(Vec3d vPoint in m_aryReceptiveFields) { Matrix mWorld = Matrix.Translation((float) vPoint.X, (float) vPoint.Y, (float) vPoint.Z) * CombinedTransformationMatrix * Device.Transform.World1; if(Util_DX.TestIntersectWithMouse(m_d3dReceptiveFieldPoint,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld, x, y, out iiTmp)) { if(fltSelDistance < 0 || iiTmp.Dist < fltSelDistance) { fltSelDistance = iiTmp.Dist; vSelVertex = vPoint; } } } if(fltSelDistance > 0) { vVertex = vSelVertex; return true; } } return false; }
//TODO: Find what causes the graphics to freeze in this code protected void DrawLowerSelectionBox() { RigidBodies.RigidBody_DX rbParent = (RigidBodies.RigidBody_DX)this.Parent; // Vector3 v3D = rbParent.RelativeLocation - this.RelativeLocation; // v3D.Normalize(); IntersectInformation iiInfo = new IntersectInformation(); Vector3 v = new Vector3(); v.TransformCoordinate(this.CombinedTransformationMatrix); this.m_mshSelectedBoundingBox.Intersect(rbParent.RelativeLocation, Direction, out iiInfo); m_v3LB = Util_DX.FindIntersectionPoint(this.m_mshSelectedBoundingBox,iiInfo); //m_v3LB = rbParent.FindPointOnSurface(new Vector3(), -Direction); m_v3LB.TransformCoordinate(CombinedTransformationMatrix * Device.Transform.World1); Matrix m = this.Device.Transform.View; m.Invert(); m.M41 = m_v3LB.X; m.M42 = m_v3LB.Y; m.M43 = m_v3LB.Z; this.Device.Transform.World = m; this.Device.Material = Util_DX.WhiteMaterial(); this.Device.RenderState.ZBufferEnable = false; Device.SetTexture(0,texLB); m_mshLB.DrawSubset(0); Device.SetTexture(0,null); this.Device.RenderState.ZBufferEnable = true; }
public override bool IsSelectionBoxSelected(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { if(this.m_mshCB == null && this.m_mshLB == null) return false; else if (this.m_bUBSelected || this.m_bLBSelected) return true; else { IntersectInformation iiInfo = new IntersectInformation(); //matrix transormation of the world Vector3 tmp = new Vector3(); tmp.TransformCoordinate(CombinedTransformationMatrix * Device.Transform.World1); Matrix mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(Util_DX.TestIntersectWithMouse(m_mshCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo)) return true; mWorld.M41 = m_v3LB.X; mWorld.M42 = m_v3LB.Y; mWorld.M43 = m_v3LB.Z; if(Util_DX.TestIntersectWithMouse(m_mshLB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo)) return true; return false; } }
public override void UpdateWithMouse(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { //If no button is pressed..nothing is selected if(AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.None) { m_bCBSelected = false; m_bMinSelected = false; m_bMaxSelected = false; return; } //set the location for the max constraint box Vector3 tmp = new Vector3(); tmp.TransformCoordinate(Matrix.Translation(0, 0, -MaxMovement + m_fRadius) * CombinedTransformationMatrix * Device.Transform.World1); Matrix m = this.Device.Transform.View; m.Invert(); m.M41 = tmp.X; m.M42 = tmp.Y; m.M43 = tmp.Z; IntersectInformation iiInfo = new IntersectInformation(); if(m_mshCB != null && Util_DX.TestIntersectWithMouse(m_mshCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, m,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && !m_bCBSelected && !m_bMinSelected && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bMaxSelected = true; //set the location for the min constraint box tmp = new Vector3(); tmp.TransformCoordinate(Matrix.Translation(0, 0, -MinMovement - m_fRadius) * CombinedTransformationMatrix * Device.Transform.World1); m = this.Device.Transform.View; m.Invert(); m.M41 = tmp.X; m.M42 = tmp.Y; m.M43 = tmp.Z; if(m_mshCB != null && Util_DX.TestIntersectWithMouse(m_mshCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, m,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && !m_bCBSelected && !m_bMaxSelected && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bMinSelected = true; //set the location of the center box tmp = new Vector3(); tmp.TransformCoordinate(CombinedTransformationMatrix * Device.Transform.World1); Matrix mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(m_mshCB != null && !m_bMaxSelected && !m_bMinSelected && Util_DX.TestIntersectWithMouse(m_mshCB, Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bCBSelected = true; if(m_bCBSelected) ProcessCenterSquare(AnimatMouseArgs); else if(m_bMaxSelected && (AnimatMouseArgs.Button == MouseButtons.Left)) ProcessMaxSquare(AnimatMouseArgs); else if(m_bMinSelected && (AnimatMouseArgs.Button == MouseButtons.Left)) ProcessMinSquare(AnimatMouseArgs); }
/// <summary> /// Creates a new instance of the Intersection class. /// </summary> public Intersection(IntersectInformation input, Vector3 intersectionPosition) : this(intersectionPosition, input.Dist, input.FaceIndex, input.U, input.V) { }
/// <summary> /// The model viewer_ mouse downx. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> /// <remarks></remarks> private void ModelViewer_MouseDownx(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { #region Check for collision intersection of Surfaces // This takes our mouse cursor and creates a line directly into the screen (2D -> 3D) Vector3 s = Vector3.Unproject(new Vector3(e.X, e.Y, 0), render.device.Viewport, render.device.Transform.Projection, render.device.Transform.View, Matrix.Identity); Vector3 d = Vector3.Unproject(new Vector3(e.X, e.Y, 1), render.device.Viewport, render.device.Transform.Projection, render.device.Transform.View, Matrix.Identity); Vector3 rPosition = s; Vector3 rDirection = Vector3.Normalize(d - s); // Used to find the nearest polygon IntersectInformation iiClosest = new IntersectInformation(); iiClosest.Dist = 10000; // Set a very far off distance for (int x = 0; x < bsp.BSPRawDataMetaChunks.Length; x++) { //check for intersection IntersectInformation ii; for (int xx = 0; xx < bsp.BSPRawDataMetaChunks[x].Indices.Length; xx += 3) { Geometry.IntersectTri( bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 0]], bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 1]], bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 2]], rPosition, rDirection, out ii); if (ii.Dist != 0) { if (iiClosest.Dist > ii.Dist) iiClosest = ii; else continue; // holds the surface # that is currently nearest currentSurface = (x << 16) + xx; break; }; } //updateInfo(); } #endregion } if (e.Button == MouseButtons.Right) { cam.change(e.X, e.Y); } }
/// <summary> /// The model viewer_ mouse downx. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> /// <remarks></remarks> private void ModelViewer_MouseDownx(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { #region Check for collision intersection of Surfaces // This takes our mouse cursor and creates a line directly into the screen (2D -> 3D) Vector3 s = Vector3.Unproject(new Vector3(e.X, e.Y, 0), render.device.Viewport, render.device.Transform.Projection, render.device.Transform.View, Matrix.Identity); Vector3 d = Vector3.Unproject(new Vector3(e.X, e.Y, 1), render.device.Viewport, render.device.Transform.Projection, render.device.Transform.View, Matrix.Identity); Vector3 rPosition = s; Vector3 rDirection = Vector3.Normalize(d - s); // Used to find the nearest polygon IntersectInformation iiClosest = new IntersectInformation(); iiClosest.Dist = 10000; // Set a very far off distance for (int x = 0; x < bsp.BSPRawDataMetaChunks.Length; x++) { //check for intersection IntersectInformation ii; for (int xx = 0; xx < bsp.BSPRawDataMetaChunks[x].Indices.Length; xx += 3) { Geometry.IntersectTri( bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 0]], bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 1]], bsp.BSPRawDataMetaChunks[x].Vertices[bsp.BSPRawDataMetaChunks[x].Indices[xx + 2]], rPosition, rDirection, out ii); if (ii.Dist != 0) { if (iiClosest.Dist > ii.Dist) { iiClosest = ii; } else { continue; } // holds the surface # that is currently nearest currentSurface = (x << 16) + xx; break; } ; } //updateInfo(); } #endregion } if (e.Button == MouseButtons.Right) { cam.change(e.X, e.Y); } }
/// <summary> /// Finds a point on the surface of the mesh from intersection test /// </summary> /// <param name="d3dDevice">Direct3D device</param> /// <param name="v3Start">Starting location of the ray to test for intersection with surface</param> /// <param name="v3Direction">Direction of the ray to test for intersection with surface</param> /// <returns>Point on the surface of the mesh from the orgin of mesh in the direction of v3Direction</returns> public virtual Vector3 FindPointOnSurface(Vector3 v3Start, Vector3 v3Direction) { //IntersectInformation of the intersection test IntersectInformation iiII = new IntersectInformation(); //if the mesh hasn't been created..create it if(m_d3dMesh == null) this.CreateBody(); //test for intersection from the orgin to the surface in the v3Direction direction m_d3dMesh.Intersect(new Vector3(), v3Direction, out iiII); //get the face that was intersected Vector3[] v3Verts = Util_DX.FindIntersectionFace(m_d3dMesh, iiII); //get the normal of said face Vector3 v3N = Util_DX.CalculateNormal(v3Verts); //set //the direction of the body which is opposite the surface normal //m_v3Direction = -v3N; //this point will be negative..need to flip it return -1 * RayIntersectWithPlane(v3Start,v3N ,v3Verts); }
public abstract bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit);
/// <summary> /// Test for intersection with the mesh /// </summary> /// <param name="device">The Direct3D device associated with the mesh</param> /// <param name="x">X coordinate of the mouse</param> /// <param name="y">Y coordinate of the mouse</param> /// <param name="cmCommand">Which mode we are in: SelectBodies, SelectJoints, or AddBodies</param> /// <param name="colSelected">A collection of meshes that intersected with the ray from the mouse</param> public override void TestIntersection(int x, int y, AnimatTools.Forms.BodyPlan.Command.enumCommandMode cmCommand, ref System.Collections.ArrayList colSelected) { //If we're not in the right mode the skip this computation if(m_d3dMesh != null && cmCommand != AnimatTools.Forms.BodyPlan.Command.enumCommandMode.SelectJoints) { //matrix transormation of the world Matrix mWorld = CombinedTransformationMatrix * Device.Transform.World1; //a temporary intersect information for the selected bounding box IntersectInformation iiTmp = new IntersectInformation(); //shoot the ray and test for intersection if(Util_DX.TestIntersectWithMouse(m_d3dMesh,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld, x, y, out this.m_iiIntersectInfo)) { //if there was a sucessfull intersection then add this mesh to the selected collection colSelected.Add(this); //Vertices that represent the intersected face Vector3[] vertsFace = Util_DX.FindIntersectionFace(m_d3dMesh, m_iiIntersectInfo); //the normal of the selected face this.m_v3FaceNormal = Util_DX.CalculateNormal(vertsFace); //m_v3SelectedFacePoint = this.RayIntersectWithPlane(new Vector3(),FaceNormal,vertsFace); m_v3SelectedFacePoint = Util_DX.FindIntersectionPoint(this.Mesh,this.IntersectInfo); //Vector3 v = m_v3SelectedFacePoint - m_v3Location; //Debug.WriteLine("Relative Point: (" + v.X + ", " + v.Y + ", " + v.Z + ")"); } else if((this.m_mshSelectedBoundingBox != null && this.m_bSelected && Util_DX.TestIntersectWithMouse(m_mshSelectedBoundingBox,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld, x, y, out iiTmp))) { colSelected.Add(this); } // else // m_bSelected = false; } base.TestIntersection (x, y, cmCommand, ref colSelected); }
public override void UpdateWithMouse(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { //set the location of the upper box Vector3 tmp = new Vector3(); tmp.TransformCoordinate(CombinedTransformationMatrix * Device.Transform.World1); Matrix mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.None) { m_bCBSelected = false; m_bLCBSelected = false; m_bUCBSelected = false; return; } IntersectInformation iiInfo = new IntersectInformation(); if(m_mshCB != null && !this.m_bLCBSelected && !this.m_bUCBSelected && Util_DX.TestIntersectWithMouse(m_mshCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left || AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Right)) m_bCBSelected = true; //set the location of the upper box tmp = new Vector3(); tmp.TransformCoordinate(Matrix.Translation( -m_fRadius * 2, 0,0) * Matrix.RotationAxis(m_v3Axis ,-m_fltMinAngle) * this.CombinedTransformationMatrix * Device.Transform.World1); mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; //Did we click on the lower constraint box? if(m_mshLCB != null && !this.m_bCBSelected && !this.m_bUCBSelected && Util_DX.TestIntersectWithMouse(m_mshLCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left)) m_bLCBSelected = true; //set the location of the upper box tmp = new Vector3(); tmp.TransformCoordinate(Matrix.Translation( -m_fRadius * 2, 0,0) * Matrix.RotationAxis(m_v3Axis ,-m_fltMaxAngle) * this.CombinedTransformationMatrix * Device.Transform.World1); mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(m_mshUCB != null && !this.m_bLCBSelected && !this.m_bCBSelected && Util_DX.TestIntersectWithMouse(m_mshUCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo) && (AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.Left)) m_bUCBSelected = true; if(m_bCBSelected) ProcessCenterSquare(AnimatMouseArgs); else if(m_bLCBSelected && (AnimatMouseArgs.Button == MouseButtons.Left)) ProcessLowerConstraint(AnimatMouseArgs); else if(m_bUCBSelected && (AnimatMouseArgs.Button == MouseButtons.Left)) ProcessUpperConstraint(AnimatMouseArgs); }
public override void UpdateWithMouse(AnimatTools.Framework.MouseEventArgs AnimatMouseArgs) { IntersectInformation iiInfo = new IntersectInformation(); //matrix transormation of the world Vector3 tmp = new Vector3(); tmp.TransformCoordinate(CombinedTransformationMatrix * this.Device.Transform.World1); Matrix mWorld = this.Device.Transform.View; mWorld.Invert(); mWorld.M41 = tmp.X; mWorld.M42 = tmp.Y; mWorld.M43 = tmp.Z; if(AnimatMouseArgs.Button == System.Windows.Forms.MouseButtons.None) { m_bUBSelected = false; m_bLBSelected = false; m_bSelectionBoxSelected = false; return; } //Did we click on the middle selection box? if(m_mshCB != null && Util_DX.TestIntersectWithMouse(m_mshCB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo)) m_bUBSelected = true; mWorld.M41 = m_v3LB.X; mWorld.M42 = m_v3LB.Y; mWorld.M43 = m_v3LB.Z; //Did we click on the lower selection box? if(m_mshLB != null && Util_DX.TestIntersectWithMouse(m_mshLB,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo)) m_bLBSelected = true; if(m_bUBSelected && !m_bSelectionBoxSelected) ProcessCenterSquare(AnimatMouseArgs); else if(m_bLBSelected && !m_bSelectionBoxSelected) ProcessLowerSquare(AnimatMouseArgs); else { mWorld = CombinedTransformationMatrix * this.Device.Transform.World1; if(!m_bSelectionBoxSelected && this.m_mshSelectedBoundingBox != null && Util_DX.TestIntersectWithMouse(this.m_mshSelectedBoundingBox,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld,AnimatMouseArgs.X, AnimatMouseArgs.Y, out iiInfo)) { m_bSelectionBoxSelected = true; Vector3[] vertsFace = Util_DX.FindIntersectionFace(this.m_mshSelectedBoundingBox, iiInfo); //the normal of the selected face this.m_v3SBBN = Util_DX.CalculateNormal(vertsFace); } if(m_bSelectionBoxSelected) ProcessSelectedBoundingBox(AnimatMouseArgs, this.m_v3SBBN); } }
/// <summary> /// Finds the face of a mesh that has been intersected with a ray /// </summary> /// <param name="iiIntInfo">IntersectInformation object from the intersection of a ray and face</param> /// <returns>Vector3[] of vertices that make up a triangle face</returns> public static Vector3[] FindIntersectionFace(Microsoft.DirectX.Direct3D.Mesh mshMesh, IntersectInformation iiInfo) { if(mshMesh != null) { //vertices that make up the face CustomVertex.PositionOnly[] vertsFace = new CustomVertex.PositionOnly[3]; // get the index buffer and lock it GraphicsStream buffer = mshMesh.LockIndexBuffer(LockFlags.ReadOnly); // move to the position of the face index buffer.Position = iiInfo.FaceIndex * System.Runtime.InteropServices.Marshal.SizeOf(new IndexEntry()); // 6 = sizeof(IndexEntry) // read the index entry from the buffer IndexEntry entry = (IndexEntry)buffer.Read(typeof(IndexEntry)); // we've got the info we need from the index buffer... unlock it mshMesh.UnlockIndexBuffer(); buffer.Close(); //now that we have the vertex locations, get each vertex from the vertex buffer // get the vertex buffer and lock it buffer = mshMesh.LockVertexBuffer(LockFlags.ReadOnly); // move to the first vertex position if(buffer.Length > entry.v1 * mshMesh.NumberBytesPerVertex) { buffer.Position = entry.v1 * mshMesh.NumberBytesPerVertex; // grab first (left) vertex of triangle vertsFace[0] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } if(buffer.Length > entry.v2 * mshMesh.NumberBytesPerVertex) { // move to the second vertex position buffer.Position = entry.v2 * mshMesh.NumberBytesPerVertex; // grab second (middle) vertex of triangle vertsFace[1] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } if(buffer.Length > entry.v3 * mshMesh.NumberBytesPerVertex) { // move to the third vertex of triangle buffer.Position = entry.v3 * mshMesh.NumberBytesPerVertex; // grab last (right) vertex of triangle vertsFace[2] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } //unlock the buffer mshMesh.UnlockVertexBuffer(); buffer.Close(); // Vector representation instead of CustomVertex Vector3[] v3Verts = new Vector3[vertsFace.Length]; v3Verts[0] = vertsFace[0].Position; v3Verts[1] = vertsFace[1].Position; v3Verts[2] = vertsFace[2].Position; return v3Verts; } else return null; }
protected Vector3 FindDirection() { Vector3 v3DirToParent = -this.LocalLocation; v3DirToParent.Normalize(); //a temporary intersect information IntersectInformation iiInfo = new IntersectInformation(); //check for intersection with parent this.Parent_DX.Mesh.Intersect(new Vector3(), v3DirToParent, out iiInfo); Vector3[] vertsFace = Util_DX.FindIntersectionFace(this.Parent_DX.Mesh, iiInfo); Vector3 v3Norm = Util_DX.CalculateNormal(vertsFace); return -v3Norm; }
public RadiosityIntersection(IntersectInformation ii, Vector3 position, Vector3 newDirection, float scale, int lightMapIndex) : this(position, ii.Dist, ii.FaceIndex, ii.U, ii.V, newDirection, scale, lightMapIndex) { }
public override void TestIntersection(int x, int y, AnimatTools.Forms.BodyPlan.Command.enumCommandMode cmCommand, ref System.Collections.ArrayList colSelected) { if(CanDraw() && cmCommand == AnimatTools.Forms.BodyPlan.Command.enumCommandMode.SelectJoints) { bool bBBSelected = false; Matrix mBBWorld = Matrix.Translation(0,0,0.045f) * this.CombinedTransformationMatrix * this.Device.Transform.World1; //moves the body to where it is in space Matrix mWorld = Matrix.Translation(0,0,0.045f) * this.CombinedTransformationMatrix * Device.Transform.World1; IntersectInformation iiTmp = new IntersectInformation(); bBBSelected = Util_DX.TestIntersectWithMouse(m_mshSelectedBoundingBox,Device.Viewport,Device.Transform.Projection, Device.Transform.View, mBBWorld,x,y,out iiTmp) && this.m_bSelected; if(Util_DX.TestIntersectWithMouse(m_mshPole,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld, x, y, out iiTmp) || bBBSelected) colSelected.Add(this); } base.TestIntersection (x, y, cmCommand, ref colSelected); }
/// <summary> /// Finds a point of intersection on a mesh /// </summary> /// <param name="mshMesh">Mesh to find the point on</param> /// <param name="iiInfo">Intersect Information of the mesh to find the point on</param> /// <returns>Vector3 representation of the point of intersection</returns> public static Vector3 FindIntersectionPoint(Mesh mshMesh, IntersectInformation iiInfo) { //get barycentric points float fB1 = iiInfo.U; float fB2 = iiInfo.V; float fB3 = FindThirdBarycentricPoint(fB1, fB2); //get the intersected face Vector3[] v3Face = FindIntersectionFace(mshMesh, iiInfo); //convert the barycentric points to a 3d point Vector3 v3Point = (fB2 * v3Face[2]) + (fB1 * v3Face[1]) + (fB3 * v3Face[0]); return v3Point; }
public override void TestIntersection(int x, int y, AnimatTools.Forms.BodyPlan.Command.enumCommandMode cmCommand, ref System.Collections.ArrayList colSelected) { if(m_d3dMesh != null && cmCommand == AnimatTools.Forms.BodyPlan.Command.enumCommandMode.SelectJoints) { Matrix m_World = this.CombinedTransformationMatrix * Device.Transform.World1; Matrix mWorld = m_World; IntersectInformation iiTmp = new IntersectInformation(); if(Util_DX.TestIntersectWithMouse(m_mshPole,Device.Viewport, Device.Transform.Projection, Device.Transform.View, mWorld, x, y, out iiTmp)|| (Util_DX.TestIntersectWithMouse(m_mshSelectedBoundingBox,Device.Viewport,Device.Transform.Projection, Device.Transform.View, mWorld,x,y,out iiTmp) && this.m_bSelected)) colSelected.Add(this); //else // m_bSelected = false; m_World = Matrix.Translation( m_fRadius, 0,0) * Matrix.RotationAxis(new Vector3(0,0,1),m_fltMaxAngle) * mWorld; if(Util_DX.TestIntersectWithMouse(m_mshConstraint,Device.Viewport, Device.Transform.Projection, Device.Transform.View, m_World, x, y, out iiTmp) || (Util_DX.TestIntersectWithMouse(m_mshSelectedBoundingBox,Device.Viewport,Device.Transform.Projection, Device.Transform.View, mWorld,x,y,out iiTmp) && this.m_bSelected)) colSelected.Add(this); //else // m_bSelected = false; m_World = Matrix.Translation( m_fRadius, 0,0) * Matrix.RotationAxis(new Vector3(0,0,1),m_fltMinAngle) * mWorld; if(Util_DX.TestIntersectWithMouse(m_mshConstraint,Device.Viewport, Device.Transform.Projection, Device.Transform.View, m_World, x, y, out iiTmp) || (Util_DX.TestIntersectWithMouse(m_mshSelectedBoundingBox,Device.Viewport,Device.Transform.Projection, Device.Transform.View, mWorld,x,y,out iiTmp) && this.m_bSelected)) colSelected.Add(this); //else // m_bSelected = false; } base.TestIntersection (x, y, cmCommand, ref colSelected); }
public override bool Intersect(Vector3 near, Vector3 far, out IntersectInformation closestHit) { //Get a clone of this point , because we'll changed its value Vector3 clone1 = model.Node1; Vector3 clone2 = model.Node2; float dist = 0f; //Project the point to the viewport near.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, Matrix.Identity); far.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, Matrix.Identity); clone1.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, model.WorldMatrix); clone2.Project(d3d.Dx.Viewport, d3d.Projection, d3d.View, model.WorldMatrix); closestHit = new IntersectInformation(); //These two values is used to detemine whether the near point is betweem the clone1 and clone2 int value3 = (int)(clone1.X - near.X) * (int)(clone2.X - near.X); int value4 = (int)(clone1.Y - near.Y) * (int)(clone2.Y - near.Y); bool intersectResult = false; //These two values is used to detemine whether the near point is on the line of clone1 and clone2 bool isAlongY = (int)Math.Abs((clone1.X - near.X)) <= 1 && (int)Math.Abs((clone2.X - near.X)) <= 1; bool isAlongX = (int)Math.Abs((clone1.Y - near.Y)) <= 1 && (int)Math.Abs((clone2.Y - near.Y)) <= 1; if (isAlongX && isAlongY) { intersectResult = true; } else if (isAlongX && !isAlongY) { intersectResult = value3 < 0; } else if (!isAlongX && isAlongY) { intersectResult = value4 < 0; } else { int value1 = (int)((clone1.X - near.X) / (clone1.Y - near.Y)); int value2 = (int)((near.X - clone2.X) / (near.Y - clone2.Y)); if (value3 < 0 && value4 < 0 && Math.Abs(value1 - value2) <= 1) { intersectResult = true; } } if (intersectResult) { // clone1.Z //point projected by clone1|--------------------clone1 // | / // dis2 | / // | dist / // -|---------------/picked point on the line // dis1 | / // | / // | / //point projected by clone2|-----------/clone2 // clone2.Z float dis1 = (float)Math.Pow((clone1.X - near.X) * (clone1.X - near.X) + (clone1.Y - near.Y) * (clone1.Y - near.Y), 0.5); float dis2 = (float)Math.Pow((clone2.X - near.X) * (clone2.X - near.X) + (clone2.Y - near.Y) * (clone2.Y - near.Y), 0.5); if (clone1.Z > clone2.Z) { dist = dis2 / (dis1 + dis2) * (clone1.Z - clone2.Z) + clone2.Z; } else { dist = dis1 / (dis1 + dis2) * (clone2.Z - clone1.Z) + clone1.Z; } closestHit.Dist = dist; return(true); } else { return(false); } }
/// <summary> /// The bsp collision viewer_ mouse down. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> /// <remarks></remarks> private void BSPCollisionViewer_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { cam.oldx = e.X; cam.oldy = e.Y; } #region SpawnSelection (Mouse Left Button) else if (e.Button == MouseButtons.Left) { if (currentMode == editingModes.Surface) #region Check for collision intersection of Surfaces { // This takes our mouse cursor and creates a line directly into the screen (2D -> 3D) Vector3 s = Vector3.Unproject(new Vector3(e.X, e.Y, 0), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 d = Vector3.Unproject(new Vector3(e.X, e.Y, 1), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 rPosition = s; Vector3 rDirection = Vector3.Normalize(d - s); // Used to find the nearest polygon IntersectInformation iiClosest = new IntersectInformation(); iiClosest.Dist = 10000; // Set a very far off distance for (int x = 0; x < polygons.Count; x++) { //check for intersection IntersectInformation ii; for (int xx = 0; xx < polygons[x].indices.Length - 2; xx++) { Geometry.IntersectTri( coll.Vertices[polygons[x].indices[0]], coll.Vertices[polygons[x].indices[xx + 1]], coll.Vertices[polygons[x].indices[xx + 2]], rPosition, rDirection, out ii); if (ii.Dist != 0) { if (iiClosest.Dist > ii.Dist) { iiClosest = ii; } else { continue; } // holds the surface # that is currently nearest currentSurface = x; break; } ; } updateInfo(); } } #endregion /* #region DecideUponObjectRotation * if ((SelectedSpawn.Count > 0) && (rotationBitMask != 0)) * { * selectionStart = render.Mark3DCursorPosition(e.X, e.Y, Matrix.Identity); * oldx = e.X; * oldy = e.Y; * itemrotate = true; * //return; * } #endregion * else */ if (currentMode == editingModes.Point) #region Collision point movement mode { Mesh checkSphere = Mesh.Sphere(device, 0.3f, 5, 5); #region CheckSpawnsForIntersection for (int x = 0; x < coll.Vertices.Length; x++) { //int tempcount = SpawnModel[spawnmodelindex[x]].Display.Chunk.Count; int tempcount = 1; for (int yy = 0; yy < tempcount; yy++) { // Check under mouse cursor for object selection/deselection? if (render.MeshPick(e.X, e.Y, checkSphere, TranslationMatrix[x]) == true) { #region TurnSpawnOnOrOff int tempi = SelectedPoints.IndexOf(x); if (tempi != -1) { SelectedPoints.RemoveAt(tempi); } else { SelectedPoints.Add(x); } #endregion updateInfo(); System.Threading.Thread.Sleep(300); break; } } } #endregion CycleThroughSpawns checkSphere.Dispose(); } #endregion } #endregion }
public bool IntersectMesh(int id, float ox, float oy, float oz, float dx, float dy, float dz, ref IntersectInformation closesthit) { return(devicemesh.Mesh.IntersectSubset(id, new Vector3(ox, oy, oz), new Vector3(dx, dy, dz), ref closesthit)); }