/// <summary> /// Checks the mouse's position set in the in-game world plane. /// </summary> /// <param name="mousePosition">Mouse's position on screen</param> /// <param name="camera">Camera object</param> /// <param name="device">Graphics device used in rendering</param> /// <returns></returns> public static Vector3 getMouseWorldPosition(Vector2 mousePosition,CameraAndLights camera,GraphicsDevice device) { Vector3 nearsource = new Vector3(mousePosition,0f); Vector3 farsource = new Vector3(mousePosition,CameraAndLights.nearClip); Vector3 nearPoint = device.Viewport.Unproject(nearsource, camera.projectionMatrix, camera.viewMatrix, Matrix.Identity); Vector3 farPoint = device.Viewport.Unproject(farsource, camera.projectionMatrix, camera.viewMatrix, Matrix.Identity); // Create a ray from the near clip plane to the far clip plane. Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint,direction); Plane floor = new Plane(new Vector3(0f,1f,0f),0f); float denominator = Vector3.Dot(floor.Normal,pickRay.Direction); float numerator = Vector3.Dot(floor.Normal,pickRay.Position) + floor.D; float dist = -(numerator / denominator); Vector3 mouseWorldPos = nearPoint + direction * dist; return mouseWorldPos * new Vector3(1f,0f,1f); }
public override void CalculateLocalMouse(Ray mouseRay, Action<VertexPositionColor, VertexPositionColor> debug) { MouseHover = false; var verts = new Vector3[3]; verts[0] = new Vector3(-0.5f, -0.5f, 0); verts[1] = new Vector3(0.5f, -0.5f, 0); verts[2] = new Vector3(-0.5f, 0.5f, 0); for (int i = 0; i < 3; ++i) verts[i] = Vector3.Transform(verts[i], localTransformation); debug(new VertexPositionColor(verts[0], Color.Red), new VertexPositionColor(verts[1], Color.Red)); debug(new VertexPositionColor(verts[0], Color.Green), new VertexPositionColor(verts[2], Color.Green)); var distance = mouseRay.Intersects(new Plane(verts[0], verts[1], verts[2])); if (distance == null || !distance.HasValue) return; if (distance.Value < 0) return; //GUI plane is behind camera var interesectionPoint = mouseRay.Position + (mouseRay.Direction * distance.Value); debug(new VertexPositionColor(verts[0], Color.Blue), new VertexPositionColor(interesectionPoint, Color.Blue)); var x = ScalarProjection(interesectionPoint - verts[0], verts[1] - verts[0]) / (verts[1] - verts[0]).Length(); var y = ScalarProjection(interesectionPoint - verts[0], verts[2] - verts[0]) / (verts[2] - verts[0]).Length(); LocalMouseX = (int)(x * uiCamera.viewportDimensions.X); LocalMouseY = (int)(y * uiCamera.viewportDimensions.Y); MouseHover = true; }
public override void CalculateLocalMouse(Ray MouseRay, Action<Gem.Render.SceneNode, float> HoverCallback) { MouseHover = false; if (InteractWithMouse == false) return; var localMouse = GetLocalMouseRay(MouseRay); var intersection = Mesh.RayIntersection(localMouse); if (intersection.Intersects) { LocalMouse = Vector2.Transform(intersection.UV, UVTransform); if (AlphaMouse) { var pixel = new Color[] { new Color(1.0f, 1.0f, 1.0f, 1.0f) }; var pX = (int)System.Math.Round(LocalMouse.X * Texture.Width); var pY = (int)System.Math.Round(LocalMouse.Y * Texture.Height); Texture.GetData<Color>(0, new Rectangle(pX, pY, 1, 1), pixel, 0, 1); if (pixel[0].A > 0.01f) HoverCallback(this, intersection.Distance); } else HoverCallback(this, intersection.Distance); } }
public static List<IPickable> GetPickables(Point a_clickPoint) { var camera = CameraManager.ActiveCamera; Vector3 nearSource = camera.Viewport.Unproject(new Vector3(a_clickPoint.X, a_clickPoint.Y, camera.Viewport.MinDepth), camera.Projection, camera.View, Matrix.Identity); Vector3 farSource = camera.Viewport.Unproject(new Vector3(a_clickPoint.X, a_clickPoint.Y, camera.Viewport.MaxDepth), camera.Projection, camera.View, Matrix.Identity); Vector3 direction = farSource - nearSource; direction.Normalize(); var ray = new Ray(nearSource, direction); var pickables = new List<IPickable>(); var rays = new List<Ray>(); var map = new Dictionary<float?, IPickable>(); foreach (var pickable in ComponentManager.Pickables.Values) { BoundingBox boundingBox = pickable.GetBoundingBox(); float? distance; ray.Intersects(ref boundingBox, out distance); if (distance != null) { map[distance] = pickable; pickables.Add(pickable); } } return pickables; }
public float? checkCollisionAgainstEnvironment(Ray ray) { float? collided = null; foreach (Collidable c in stationaryCollidables) { if (c.type == CollisionType.environment && collided == null) { Vector3[] points = new Vector3[2]; points[0] = Vector3.Transform(c.boundingBox.Min, c.getComponent<Spatial>().transform); points[1] = Vector3.Transform(c.boundingBox.Max, c.getComponent<Spatial>().transform); BoundingBox bb = BoundingBox.CreateFromPoints(points); if (ray.Intersects(bb) != null) { if (c.children.Count > 0) { collided = checkAgainstChildren(c, ray); } else { collided = checkAgainstMesh(c.getComponent<Drawable3D>(), ray); } } } } return collided; }
public void Update(float dt) { Position += Velocity * dt; Velocity -= new Vector3(0, Gravity * dt, 0); Velocity *= (1f - Damping); Vector3 direction = Velocity; direction.Normalize(); Ray ray = new Ray(Position, direction);//Vector3.Transform(Vector3.Forward, Orientation)); RayCastResult result; Sector.Redria.Space.RayCast(ray, 2, out result); if (result.HitObject != null) { if (result.HitObject.Tag is ShipObj) { var ship = (ShipObj)result.HitObject.Tag; var magnitude = Velocity.Length(); //var angle = Vector3.Dot(result.HitData.Normal,Velocity)/magnitude; float velocity = magnitude;//(float)(magnitude*angle); ship.BulletStrike(Data,velocity); } Sector.Redria.Bullets.Remove(this); } if (Position.Y < -5) Sector.Redria.Bullets.Remove(this); }
public Shot(Ray Trac, float Caliber, Being Origin) { Tracer = Trac; TimeToDie = Environment.TickCount+100; this.Caliber = Caliber; this.Origin = Origin; }
public Vector3 shading(Vector3 pos, Vector3 norm, Ray ray, Actor actor, bool isShade) { // 点光源 var l = light - pos; var l2 = l.LengthSquared(); l.Normalize(); // 視線 var dir = ray.Position - pos; dir.Normalize(); // ハーフベクトル var h = dir + l; h.Normalize(); var ln = Vector3.Dot(l, norm); var hn = Vector3.Dot(h, norm); if (ln < 0) ln = 0; if (hn < 0) hn = 0; var dst = actor.GetColor(isShade ? 0.0f : ln, hn, pos); // 光源の色の反映 dst.X *= lightColor.X; dst.Y *= lightColor.Y; dst.Z *= lightColor.Z; // 光の強さの適当な補正 //dst *= Math.Min(1.5f, 500000.0f / (10000.0f + l2)); //dst *= Math.Min(1.0f, l.Y + 0.1f); return dst; }
/// <summary> /// Determines whether there is a cliff nearby. /// </summary> /// <param name="position">Position to look from.</param> /// <param name="facingDirection">Direction to check in.</param> /// <param name="filter">Anonymous function to filter out unwanted objects.</param> /// <param name="space">The space to check for a cliff in.</param> /// <param name="distance">The distance to check at.</param> /// <returns>True if a cliff was detected, false otherwise.</returns> public static bool FindCliff(Vector3 position, Vector3 facingDirection, Func<BroadPhaseEntry, bool> filter, Space space, float distance) { // If there is a wall before the requested distance assume there is no cliff. Ray forwardRay = new Ray(position, new Vector3(facingDirection.X, 0, facingDirection.Z)); RayCastResult forwardResult = new RayCastResult(); space.RayCast(forwardRay, filter, out forwardResult); if ((forwardResult.HitData.Location - position).Length() < distance) { return false; } facingDirection.Normalize(); Ray futureDownRay = new Ray(position + new Vector3(facingDirection.X * distance, 0, facingDirection.Z * distance), Vector3.Down); RayCastResult result = new RayCastResult(); space.RayCast(futureDownRay, filter, out result); Vector3 drop = result.HitData.Location - futureDownRay.Position; if (drop.Y < -6.0f) { return true; } else { return false; } }
public Monstre GetSourisBoxIntercept(List<Monstre> monstres) { Vector3 nearScreenPoint = new Vector3(ÉtatSouris.X, ÉtatSouris.Y, 0); Vector3 farScreenPoint = new Vector3(ÉtatSouris.X, ÉtatSouris.Y, 1); Vector3 nearWorldPoint = Jeu.PériphériqueGraphique.GraphicsDevice.Viewport.Unproject(nearScreenPoint, ScèneJeu.CaméraJeu.Projection, ScèneJeu.CaméraJeu.Vue, Matrix.Identity); Vector3 farWorldPoint = Jeu.PériphériqueGraphique.GraphicsDevice.Viewport.Unproject(farScreenPoint, ScèneJeu.CaméraJeu.Projection, ScèneJeu.CaméraJeu.Vue, Matrix.Identity); Vector3 direction = farWorldPoint - nearWorldPoint; Ray raySouris = new Ray(nearWorldPoint, direction); float distancemin = float.MaxValue; float? distance; Monstre cible = null; foreach (Monstre monstre in monstres) { foreach (BoundingBox box in monstre.BoxList) { distance = raySouris.Intersects(box); if (distance != null && distance < distancemin) { distancemin = (float)distance; cible = monstre; } } } return cible; }
private void GenerateRect() { int minX = int.MaxValue; int minZ = int.MaxValue; int maxX = int.MinValue; int maxZ = int.MinValue; var corners = Frustum.GetCorners(); Plane plane = new Plane(new Vector4(0, 1, 0, 0)); for (int i = 4; i < corners.Length; i++) { Ray ray = new Ray(corners[i - 4], corners[i]); var distance = ray.Intersects(plane); if (distance.HasValue) { var pos2 = ray.Position + ray.Direction * distance.Value; if ((int)pos2.X > maxX) maxX = (int)pos2.X; if ((int)pos2.X < minX) minX = (int)pos2.X; if ((int)pos2.Z > maxZ) maxZ = (int)pos2.Z; if ((int)pos2.Z < minZ) minZ = (int)pos2.Z; } // Console.WriteLine("distance[{0}]: {1} pos2:{2}", i, distance, pos2.ToString()); } quadRect = new Rectangle(minX, minZ, Math.Abs(maxX - minX), Math.Abs(maxZ - minZ)); // Console.WriteLine("X: {0},{1} Z:{2},{3}", minX, maxX, minZ, maxZ); }
public bool canSee() { if ((EntityManager.Instance.GetPlayer() as Rabbit).IsHidden) return false; Vector2 vecToRabbit = EntityManager.Instance.GetPlayer().Position - agent.Position; if (vecToRabbit.LengthSquared() < rangeSqrd) { vecToRabbit.Normalize(); float relativeAngle = (float)Angles.AngleFromUToV(agent.Heading, vecToRabbit); if (relativeAngle < angleWidth && relativeAngle > angleWidth * -1) { foreach (Wall wall in WallManager.Instance.Walls) { float? inter = new Ray(new Vector3(agent.Position, 0), new Vector3(vecToRabbit, 0)).Intersects( new BoundingBox(new Vector3(wall.BoundingBox.Left,wall.BoundingBox.Top,-10), new Vector3(wall.BoundingBox.Right,wall.BoundingBox.Bottom,10) )); if (inter != null && (float)inter < range) return false; } return true; } } return false; }
// This selects a particular tile given a ray from the camera public Objects.Tile Intersects(Ray ray) { // It's possible that multiple tiles intersect, so we need to create a list // of potential tiles to select. LinkedList<Objects.Tile> possibles = new LinkedList<Objects.Tile>(); foreach (Objects.Tile t in m_Tiles) { if (t.AABB.Intersects(ray) != null) { possibles.AddLast(t); } } // Now select the tile that is the closest to the start position of the ray Objects.Tile retval = null; float fBestDist = 999999999.0f; foreach (Objects.Tile t in possibles) { float fDist = Vector3.DistanceSquared(t.Position, ray.Position); if (fDist < fBestDist) { retval = t; fBestDist = fDist; } } return retval; }
public override bool Intersect(Ray tRay, out float fDistance) { // calculate the vector from the ray origin to centre of sphere Vector3 tOriginToCentre = Centre - tRay.Position; // get the closest approach of the ray to the sphere float fClosestApproach = Vector3.Dot(tOriginToCentre, tRay.Direction); // if this is less than 0, the sphere is behind the ray origin if (fClosestApproach < 0) { fDistance = 0.0f; return false; } else { // calculate the distance from the centre of the sphere to // the closest approach float fLengthSq = tOriginToCentre.LengthSquared(); float fHalfCordSq = (Radius * Radius) - fLengthSq + (fClosestApproach * fClosestApproach); // ray misses the sphere if (fHalfCordSq < 0) { fDistance = 0.0f; return false; } else { fDistance = fClosestApproach - (float) Math.Sqrt(fHalfCordSq); return true; } } }
/// <summary> /// Tests all models in the scene graph for intersection with the relative mouse coordinates. /// </summary> /// <returns>The ModelNode object that claims to be hit by the mouse</returns> /// <remarks>Note that currently this algorithm uses bounding spheres, so for certain objects it will be rather inaccurate.</remarks> public ModelNode GetIntersectingModel(Matrix projectionMatrix, Matrix viewMatrix, Matrix worldMatrix, GraphicsDevice graphicsDevice, Vector2 mousePosition) { // Create 2 positions in screenspace near and far. Vector3 nearSource = new Vector3(mousePosition, 0f); Vector3 farSource = new Vector3(mousePosition, 1f); // Get equivalent positions in world space. Vector3 nearPoint = graphicsDevice.Viewport.Unproject(nearSource, projectionMatrix, viewMatrix, worldMatrix); Vector3 farPoint = graphicsDevice.Viewport.Unproject(farSource, projectionMatrix, viewMatrix, worldMatrix); // Find direction for cursor ray. Vector3 direction = farPoint - nearPoint; direction.Normalize(); // and then create a new ray using nearPoint as the source. Ray cursorRay = new Ray(nearPoint, direction); foreach (ModelNode modelNode in ModelGraph) { ModelNode modelToReturn = modelNode.GetIntersectingModel(cursorRay); if (modelToReturn != null) return modelToReturn; } return null; }
public Face DetermineSide(BoundingBox box, Ray ray) { Face selectedFace = Face.NONE; float closestDist = float.MaxValue; BoundingBox[] sides = { new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Min.X, box.Max.Y, box.Max.Z)), //-x LEFT new BoundingBox(new Vector3(box.Max.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)), //+x RIGHT new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Min.Y, box.Max.Z)), //-y TOP new BoundingBox(new Vector3(box.Min.X, box.Max.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)), //+y BOTTOM new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Min.Z), new Vector3(box.Max.X, box.Max.Y, box.Min.Z)), //-z BACK new BoundingBox(new Vector3(box.Min.X, box.Min.Y, box.Max.Z), new Vector3(box.Max.X, box.Max.Y, box.Max.Z)) //+z FRONT }; for (int i = 0; i < sides.Length; ++i) { float? d = ray.Intersects(sides[i]); if (d.HasValue) { if (d.Value < closestDist) { closestDist = d.Value; selectedFace = (Face)i; } } } return selectedFace; }
/// <summary> /// Returns the mouse X, Y and Z coordinates on the world so that the Y is always on ground zero (0). /// </summary> /// <param name="game"></param> /// <returns></returns> public static Vector3 GetMouseWorldPosition(Game game) { GraphicsDevice graphicsDevice = game.GraphicsDevice; MouseState mouseState = Mouse.GetState(); Vector3 nearSource = new Vector3((float) mouseState.X, (float) mouseState.Y, 0f); Vector3 farSource = new Vector3((float) mouseState.X, (float) mouseState.Y, 1f); Vector3 nearPoint = graphicsDevice.Viewport.Unproject(nearSource, game.Camera.Projection, game.Camera.View, Matrix.Identity); Vector3 farPoint = graphicsDevice.Viewport.Unproject(farSource, game.Camera.Projection, game.Camera.View, Matrix.Identity); // Create a ray from the near clip plane to the far clip plane. Vector3 direction = farPoint - nearPoint; direction.Normalize(); // Create a ray. Ray ray = new Ray(nearPoint, direction); // Calculate the ray-plane intersection point. Vector3 n = new Vector3(0f, 1f, 0f); Plane p = new Plane(n, 0f); // Calculate distance of intersection point from r.origin. float denominator = Vector3.Dot(p.Normal, ray.Direction); float numerator = Vector3.Dot(p.Normal, ray.Position) + p.D; float t = -(numerator / denominator); // Calculate the picked position on the y = 0 plane. Vector3 pickedPosition = nearPoint + direction * t; return pickedPosition; }
public WaypointsCollection ConnectWaypoints(WaypointsCollection col) { WaypointsCollection resp = new WaypointsCollection(col.MapName); foreach (Waypoint item in col.GetWaypointsList()) { item.NeightBorWaypointsId = new List<int>(); foreach (Waypoint item2 in col.GetWaypointsList()) { if (item.Id != item2.Id) { Vector3 dir = item2.WorldPos - item.WorldPos; if (dir.Length() < maxDistance) { float dist = dir.Length() * 1.2f; dir.Normalize(); Ray raio = new Ray(item.WorldPos + new Vector3(0, altura, 0), dir); SegmentInterceptInfo ri = world.PhysicWorld.SegmentIntersect(raio, (a) => true, dist); if (ri != null) { continue; } else { item.NeightBorWaypointsId.Add(item2.Id); } } } } } resp.State = WaypointsState.Connected; resp.IdWaypoint = col.IdWaypoint; return resp; }
/// <summary> /// Unproject a screen coordinate into a ray /// </summary> public static Ray Unproject(Vector2 Point) { //Acquires the frustum of the area of the screen in view //Then it stores the corners of the area BoundingFrustum VisibleArea = new BoundingFrustum(Manager.View * Manager.Projection); Vector3[] corners = VisibleArea.GetCorners(); Vector3 Position = new Vector3(Point, 0.0f); Ray ray = new Ray(); //Point on the near plane of the visible area ray.Position = corners[0] * (1 - Position.X) * (1 - Position.Y) + corners[1] * Position.X * (1 - Position.Y) + corners[2] * Position.X * Position.Y + corners[3] * (1 - Position.X) * Position.Y; Position = corners[4] * (1 - Position.X) * (1 - Position.Y) + corners[5] * Position.X * (1 - Position.Y) + corners[6] * Position.X * Position.Y + corners[7] * (1 - Position.X) * Position.Y; //Direction between the two points ray.Direction = Vector3.Normalize(Position - ray.Position); return ray; }
/// <summary> /// Defines the closest obstacle /// </summary> public void DetectClosestObstacle() { float closestObstacleDistance = float.MaxValue; _closestObstacle = null; var localAgentMatrix = Matrix.Invert(AutonomousAgent.ParentObject.World); foreach (SceneEntity obstacle in Context.Keys) { Vector3 localPos = Vector3.Transform(obstacle.World.Translation, localAgentMatrix); if (UseLineOfSight) { if (localPos.Z > 0f) continue; } float expandedRadius = obstacle.WorldBoundingSphere.Radius + AutonomousAgent.ParentObject.WorldBoundingSphere.Radius - AllowedPenetration; var ray = new Ray(Vector3.Zero, Vector3.Forward); float? result = ray.Intersects(new BoundingSphere(localPos, expandedRadius)); if (result.HasValue && result.Value < closestObstacleDistance) { closestObstacleDistance = result.Value; _closestObstacle = obstacle; _closestObstacleLocalPosition = localPos; } } }
/// <summary> /// Project a point into 2D space /// </summary> public static Vector2 Project(BoundingFrustum VisibleArea, Vector3 Point) { //Acquires the frustum of the area of the screen in view. //Then it stores the corners of the area. Vector3[] corners = VisibleArea.GetCorners(); Ray ray = new Ray(Point, Point - Manager.CameraFocus - Manager.CameraLocation); float? DistanceToFar = ray.Intersects(VisibleArea.Far); float? DistanceToNear = ray.Intersects(VisibleArea.Near); Vector3 ScreenCoord; if (DistanceToFar.HasValue) { ScreenCoord = ray.Position + ray.Direction * DistanceToFar.Value; ScreenCoord = new Vector3( Vector3.Dot( Vector3.Normalize(corners[5] - corners[4]) , ScreenCoord - corners[4]) / (corners[5] - corners[4]).Length() , Vector3.Dot( Vector3.Normalize(corners[7] - corners[4]) , ScreenCoord - corners[4]) / (corners[7] - corners[4]).Length() , 0); } else { //Make sure this is off the screen return Vector2.One * (Manager.GameWindow.Width + Manager.GameWindow.Height); } return new Vector2(ScreenCoord.X * Manager.GameWindow.Width, ScreenCoord.Y * Manager.GameWindow.Height); }
/// <summary> /// レイの始点からレイに沿って指定の長さにある点を取得します。 /// </summary> /// <param name="ray">レイ。</param> /// <param name="size">サイズ。</param> /// <param name="result">指定の点。</param> public static void GetPoint(ref Ray ray, float size, out Vector3 result) { Vector3 vector; Vector3.Multiply(ref ray.Direction, size, out vector); Vector3.Add(ref ray.Position, ref vector, out result); }
/// <summary> /// Test a ray (in model space) for intersection with individual bones. /// </summary> /// <param name="ray"></param> /// <returns></returns> public IEnumerable<KeyValuePair<string, float>> Intersections(Ray ray) { return _model .Model .SkinningData .Bounds .Select((b, i) => { Matrix transform; Matrix.Invert(ref _worldTransforms[i], out transform); var start = Vector3.Transform(ray.Position, transform); //Transform ray into bone space var direction = Vector3.TransformNormal(ray.Direction, transform); float? depth = b.Intersects(new Ray(start, direction)); //Intersect new ray in bone space var name = _model.Model.SkinningData.Names[i]; return new KeyValuePair<string, float?>(name, depth); }) .Where(a => a.Value.HasValue) //Only pass values which intersect // ReSharper disable PossibleInvalidOperationException .Select(a => new KeyValuePair<string, float>(a.Key, a.Value.Value)) //Select float (now we know it's not null) // ReSharper restore PossibleInvalidOperationException .OrderBy(a => a.Value) //Order by distance along ray .ToArray(); }
public IntersectionInfo(float distance, Ray originalRay, Vector3 baryCoord, Geomertry geomertry) { Distance = distance; BaryCoord = baryCoord; OriginalRay = originalRay; Geomertry = geomertry; }
public override void CalculateLocalMouse(Ray MouseRay, Action<Gem.Render.SceneNode, float> HoverCallback) { MouseHover = false; if (InteractWithMouse == false) return; MouseRay.Direction = Vector3.Normalize(MouseRay.Direction); var inverseTransform = Matrix.Invert(Orientation.Transform); var localMouseSource = Vector3.Transform(MouseRay.Position, inverseTransform); var forwardPoint = MouseRay.Position + MouseRay.Direction; forwardPoint = Vector3.Transform(forwardPoint, inverseTransform); var localMouse = new Ray(localMouseSource, forwardPoint - localMouseSource); var intersection = Mesh.RayIntersection(localMouse); if (intersection.Intersects) { LocalMouse = Vector2.Transform(intersection.UV, UVTransform); if (AlphaMouse) { var pixel = new Color[] { new Color(1.0f, 1.0f, 1.0f, 1.0f) }; var pX = (int)System.Math.Round(LocalMouse.X * Texture.Width); var pY = (int)System.Math.Round(LocalMouse.Y * Texture.Height); Texture.GetData<Color>(0, new Rectangle(pX, pY, 1, 1), pixel, 0, 1); if (pixel[0].A > 0.01f) HoverCallback(this, intersection.Distance); } else HoverCallback(this, intersection.Distance); } }
public void Setup() { r1 = new Ray(); r2 = new Ray(new Vector3(1, 2, 3), new Vector3(4, 5, 6)); r3 = new Ray(new Vector3(2.25f, 3.754321f, 4.387f), new Vector3(-1, -1, -1)); r4 = new Ray(new Vector3(1, 1, 1), new Vector3(1, 1, 1)); }
public override void ProcessMouseDown(object sender, System.Windows.Forms.MouseEventArgs e, System.Drawing.Rectangle bounds) { try { Viewport view = new Viewport(bounds.X, bounds.Y, bounds.Width, bounds.Height); Vector2 mouse = new Vector2(e.Location.X, e.Location.Y); Microsoft.Xna.Framework.Ray r = cameraManager.currentCamera.GetMouseRay(mouse, view); float dist = 0; Vector3 pos; Vector3 norm; CollisionSkin cs = new CollisionSkin(); lock (physicsManager.PhysicsSystem) { if (physicsManager.PhysicsSystem.CollisionSystem.SegmentIntersect(out dist, out cs, out pos, out norm, new Segment(r.Position, r.Direction * 1000), new Helper.Physics.DefaultCollisionPredicate())) { Body b = cs.Owner; if (b == null) { return; } Gobject go = b.ExternalData as Gobject; SelectGameObject(go); } } } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.StackTrace); } }
private void PlayMouseDown(System.Windows.Forms.MouseEventArgs e, System.Drawing.Rectangle bounds) { try { Viewport view = new Viewport(bounds.X, bounds.Y, bounds.Width, bounds.Height); Vector2 mouse = new Vector2(e.Location.X, e.Location.Y); Microsoft.Xna.Framework.Ray r = cameraManager.currentCamera.GetMouseRay(mouse, view); float dist = 0; Vector3 pos; Vector3 norm; CollisionSkin cs = new CollisionSkin(); if (GetClickedPoint(out dist, out cs, out pos, out norm, new Segment(r.Position, r.Direction * 1000))) { Body b = cs.Owner; if (b == null) { return; } Gobject go = b.ExternalData as Gobject; SelectGameObject(go); } } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.StackTrace); } }
public static void CalculateMouse3DPosition() { Plane GroundPlane = new Plane(0, 1, 0, 0); // x - lewo prawo Z- gora dol int mouseX = mouseState.X; int mouseY = mouseState.Y; Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f); Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f); Matrix world = Matrix.CreateTranslation(0, 0, 0); Vector3 nearPoint = device.Viewport.Unproject(nearsource, Projection, View, Matrix.Identity); Vector3 farPoint = device.Viewport.Unproject(farsource, Projection, View, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint, direction); float? position = pickRay.Intersects(GroundPlane); if (position != null) { MousePosition = pickRay.Position + pickRay.Direction * position.Value; MousePosition.Y = 30f; } else MousePosition = new Vector3(0, 0, 0); }
//Binary search of intersection between pointer's ray and terrain //This function returns Vector with coordinates where our pointer collides with terrain public static Vector3 BinarySearch(Ray ray) { //Set all needed variables (accuracy, height etc.) float accuracy = 0.01f; float heightAtStartingPoint = Terrain.GetExactHeightAt(ray.Position.X, ray.Position.Z); float currentError = ray.Position.Y - heightAtStartingPoint; int counter = 0; //This loop will find our collision point while (currentError < accuracy) { ray.Direction /= 2.0f; Vector3 nextPoint = ray.Position + ray.Direction; float heightAtNextPoint = Terrain.GetExactHeightAt(nextPoint.X, nextPoint.Z); if (nextPoint.Y < heightAtNextPoint) { ray.Position = nextPoint; currentError = ray.Position.Y - heightAtNextPoint; } //There is no point to iterate more than 1000 times //Accuracy is still good enough so we can break the loop if (counter++ == 1000) break; } return ray.Position; }
public Vector3? GetCollisionPosition() { MouseState mouseState = Mouse.GetState(); Vector3 nearSource = new Vector3(mouseState.X, mouseState.Y, 0f); Vector3 farSource = new Vector3(mouseState.X, mouseState.Y, 1f); Vector3 nearPoint = device.Viewport.Unproject( nearSource, camera.projection, camera.view, Matrix.Identity); Vector3 farPoint = device.Viewport.Unproject( farSource, camera.projection, camera.view, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint, direction); Nullable<float> result = pickRay.Intersects(new Plane(Vector3.Up, 0f)); Vector3? resultVector = direction * result; Vector3? collisionPoint = resultVector + nearPoint; return collisionPoint; }
public void BoundingCylinder_Ray_Test() { BoundingCylinder cyl = new BoundingCylinder (sideA: Vector3.Zero, sideB: Vector3.Up * 100, radius: 100f); Ray ray; ray = new Ray (position: new Vector3 (0, 0, 0), direction: Vector3.Up); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 0, 0), direction: Vector3.Down); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 0, 0), direction: Vector3.Forward); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 0, 0), direction: Vector3.Left); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, -1, 0), direction: Vector3.Forward); Assert.IsNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, -1, 0), direction: Vector3.Left); Assert.IsNull (ray.Intersects (cyl)); // hier sollte eigentlich (0,100,0) auch noch drin sein, nicht nur (0,99,0), // also wie bei SideA! ray = new Ray (position: new Vector3 (0, 99, 0), direction: Vector3.Forward); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 99, 0), direction: Vector3.Left); Assert.IsNotNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 101, 0), direction: Vector3.Forward); Assert.IsNull (ray.Intersects (cyl)); ray = new Ray (position: new Vector3 (0, 101, 0), direction: Vector3.Left); Assert.IsNull (ray.Intersects (cyl)); }
private bool GetClickedPoint(float x, float y, System.Drawing.Rectangle bounds, out float dist, out CollisionSkin cs, out Vector3 pos, out Vector3 norm) { Viewport view = new Viewport(bounds.X, bounds.Y, bounds.Width, bounds.Height); Vector2 mouse = new Vector2(x, y); Microsoft.Xna.Framework.Ray r = cameraManager.currentCamera.GetMouseRay(mouse, view); return(GetClickedPoint(out dist, out cs, out pos, out norm, new Segment(r.Position, r.Direction * 1000))); }
//Ray public static Ray Convert(Microsoft.Xna.Framework.Ray ray) { Ray toReturn; Convert(ref ray.Position, out toReturn.Position); Convert(ref ray.Direction, out toReturn.Direction); return(toReturn); }
public void OnUse(object actor, Microsoft.Xna.Framework.Ray ray, object item) { //GetBase Vars //get Actor IActor myActor = actor as IActor; //Get GameServer IGameServer Server = myActor.State as IGameServer; //Get systems var SystemsCollection = Server.Biomes.GetSystems(); //Get system the player is in uint currentSystemID = myActor.InstanceID; //Define currentSystems for TryGetValue IBiomeSystem currentSystem; //Find the currentSystem based on its ID SystemsCollection.TryGetValue(currentSystemID, out currentSystem); //Get the chunk's ID, that the player is in uint currentChunkID = myActor.ConnectedChunk; //Search current system for the chunk based on its ID IChunk currentChunk = currentSystem.ChunkCollection.First(sys => sys.ID == currentChunkID); //get Starting Point from ray object (its a local Pos) DoubleVector3 rayStartPointAsLocalPosition = new DoubleVector3(System.Convert.ToDouble(ray.Position.X), System.Convert.ToDouble(ray.Position.Y), System.Convert.ToDouble(ray.Position.Z)); //get direction of the Ray from ray object (essentially, where is the player looking to) DoubleVector3 rayDirection = new DoubleVector3(System.Convert.ToDouble(ray.Direction.X), System.Convert.ToDouble(ray.Direction.Y), System.Convert.ToDouble(ray.Direction.Z)); //calculate true global Pos (its needed for the new ray Object) DoubleVector3 rayStartPointAsTrueGlobalPos = DoubleVector3.Transform(rayStartPointAsLocalPosition, currentChunk.World); //Create new Ray Object with calculated outputs PreciseRay hitRay = new PreciseRay(rayStartPointAsTrueGlobalPos, rayDirection); //execute RayCast //set up return objects for coming operation IChunk hitChunk = null as IChunk; DoubleVector3 hitPoint = DoubleVector3.Zero; DoubleVector3 buildPoint = DoubleVector3.Zero; //Do rayCast with calculated Ray Object Boolean rayCastResult = currentSystem.PreciseRayCast(hitRay, (double)6.0, ref hitChunk, ref hitPoint, ref buildPoint, false); Point3D fakeGlobalPos = Point3D.Zero; if (rayCastResult) //if something was hit by the raycast { fakeGlobalPos = //calculate the position of what has been hit as fakeGlobalPos new Point3D( (int)hitChunk.Position.X + (int)hitPoint.X, (int)hitChunk.Position.Y + (int)hitPoint.Y, (int)hitChunk.Position.Z + (int)hitPoint.Z ); } ; SNScriptUtils._Utils.setPos(myActor, "1", fakeGlobalPos); Server.ChatManager.SendActorMessage("RayCast result is" + hitPoint.ToString(), myActor); }
public static float?intersects(Microsoft.Xna.Framework.Ray ray, IEnumerable <BlockLoc> blockList) { float?minDist = null; foreach (BlockLoc test in blockList) { float?thisIntersection = new BoundingBox(test.toWorldSpaceVector3(), test.toWorldSpaceVector3() + new Vector3(1, 1, 1)).Intersects(ray); if (thisIntersection != null) { if (minDist == null || (float)minDist > (float)thisIntersection) { minDist = thisIntersection; } } } return(minDist); }
public override float?intersects(Microsoft.Xna.Framework.Ray ray) { float?intersection = null; Tree closest = null; //currently found but not used for anything foreach (Tree test in trees) { float?thisIntersecton = Intersection.intersects(ray, test.getTrunkBlocks()); if (thisIntersecton.HasValue) { if (intersection.HasValue == false || (float)thisIntersecton < (float)intersection) { intersection = thisIntersecton; closest = test; } } } return(intersection); }
public override SegmentInterceptInfo SegmentIntersect(Microsoft.Xna.Framework.Ray raio, System.Func <IPhysicObject, bool> filter, float maxDistance) { SegmentInterceptInfo SegmentInterceptInfo = null; foreach (var hits in Scene.RaycastAllShapes(new StillDesign.PhysX.Ray(raio.Position.AsPhysX(), raio.Direction.AsPhysX()), ShapesType.All)) { if (filter(hits.Shape.Actor.UserData as IPhysicObject)) { if (SegmentInterceptInfo == null || SegmentInterceptInfo.Distance > hits.Distance) { SegmentInterceptInfo = new SegmentInterceptInfo(); SegmentInterceptInfo.Distance = hits.Distance; SegmentInterceptInfo.ImpactNormal = hits.WorldNormal.AsXNA(); SegmentInterceptInfo.ImpactPosition = hits.WorldImpact.AsXNA(); SegmentInterceptInfo.PhysicObject = hits.Shape.Actor.UserData as IPhysicObject; } } } return(SegmentInterceptInfo); }
private void BuildMouseDown(Point dPos, System.Windows.Forms.MouseEventArgs e, System.Drawing.Rectangle bounds) { try { Viewport view = new Viewport(bounds.X, bounds.Y, bounds.Width, bounds.Height); Vector2 mouse = new Vector2(WindowLocationX, WindowLocationY); Microsoft.Xna.Framework.Ray r = cameraManager.currentCamera.GetMouseRay(mouse, view); float dist = 0; Vector3 pos; Vector3 norm; CollisionSkin cs = new CollisionSkin(); if (GetClickedPoint(out dist, out cs, out pos, out norm, new Segment(r.Position, r.Direction * 1000))) { buildLastMouseDownPosition = pos; } } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.StackTrace); } }
public override float?intersects(Microsoft.Xna.Framework.Ray ray) { return(Intersection.intersects(ray, blocksToBeRemoved)); }
public bool RayCast(Microsoft.Xna.Framework.Ray ray, float maximumLength, IList <BroadPhaseEntry> outputIntersections) { if (maximumLength == float.MaxValue) { throw new NotSupportedException("The Grid2DSortAndSweep broad phase cannot accelerate infinite ray casts. Consider specifying a maximum length or using a broad phase which supports infinite ray casts."); } //Use 2d line rasterization. //Compute the exit location in the cell. //Test against each bounding box up until the exit value is reached. float length = 0; Int2 cellIndex; Vector3 currentPosition = ray.Position; Grid2DSortAndSweep.ComputeCell(ref currentPosition, out cellIndex); while (true) { float cellWidth = 1 / Grid2DSortAndSweep.cellSizeInverse; float nextT; //Distance along ray to next boundary. float nextTy; //Distance along ray to next boundary along y axis. float nextTz; //Distance along ray to next boundary along z axis. //Find the next cell. if (ray.Direction.Y > 0) { nextTy = ((cellIndex.Y + 1) * cellWidth - currentPosition.Y) / ray.Direction.Y; } else if (ray.Direction.Y < 0) { nextTy = ((cellIndex.Y) * cellWidth - currentPosition.Y) / ray.Direction.Y; } else { nextTy = 10e10f; } if (ray.Direction.Z > 0) { nextTz = ((cellIndex.Z + 1) * cellWidth - currentPosition.Z) / ray.Direction.Z; } else if (ray.Direction.Z < 0) { nextTz = ((cellIndex.Z) * cellWidth - currentPosition.Z) / ray.Direction.Z; } else { nextTz = 10e10f; } bool yIsMinimum = nextTy < nextTz; nextT = yIsMinimum ? nextTy : nextTz; //Grab the cell that we are currently in. GridCell2D cell; if (owner.cellSet.TryGetCell(ref cellIndex, out cell)) { float endingX; if (ray.Direction.X < 0) { endingX = currentPosition.X; } else { endingX = currentPosition.X + ray.Direction.X * nextT; } //To fully accelerate this, the entries list would need to contain both min and max interval markers. //Since it only contains the sorted min intervals, we can't just start at a point in the middle of the list. //Consider some giant bounding box that spans the entire list. for (int i = 0; i < cell.entries.count && cell.entries.Elements[i].item.boundingBox.Min.X <= endingX; i++) //TODO: Try additional x axis pruning? { float?intersects; var item = cell.entries.Elements[i].item; ray.Intersects(ref item.boundingBox, out intersects); if (intersects != null && intersects < maximumLength && !outputIntersections.Contains(item)) { outputIntersections.Add(item); } } } //Move the position forward. length += nextT; if (length > maximumLength) //Note that this catches the case in which the ray is pointing right down the middle of a row (resulting in a nextT of 10e10f). { break; } Vector3 offset; Vector3.Multiply(ref ray.Direction, nextT, out offset); Vector3.Add(ref offset, ref currentPosition, out currentPosition); if (yIsMinimum) { if (ray.Direction.Y < 0) { cellIndex.Y -= 1; } else { cellIndex.Y += 1; } } else if (ray.Direction.Z < 0) { cellIndex.Z -= 1; } else { cellIndex.Z += 1; } } return(outputIntersections.Count > 0); }
public bool RayCast(Microsoft.Xna.Framework.Ray ray, IList <BroadPhaseEntry> outputIntersections) { throw new NotSupportedException("The Grid2DSortAndSweep broad phase cannot accelerate infinite ray casts. Consider specifying a maximum length or using a broad phase which supports infinite ray casts."); }
public static void Convert(ref Microsoft.Xna.Framework.Ray ray, out Ray bepuRay) { Convert(ref ray.Position, out bepuRay.Position); Convert(ref ray.Direction, out bepuRay.Direction); }
public static void Convert(ref Ray ray, out Microsoft.Xna.Framework.Ray xnaRay) { Convert(ref ray.Position, out xnaRay.Position); Convert(ref ray.Direction, out xnaRay.Direction); }
// Algorithm copied from: http://www.cs.utah.edu/~awilliam/box/box.pdf // This is the non-optimized version of: /* * Ray-box intersection using IEEE numerical properties to ensure that the * test is both robust and efficient, as described in: * * Amy Williams, Steve Barrus, R. Keith Morley, and Peter Shirley * "An Efficient and Robust Ray-Box Intersection Algorithm" * Journal of graphics tools, 10(1):49-54, 2005 * */ private bool ComputeEntryAndExitSignedDistances(Ray rtRay, out float tMin, out float tMax) { float yMin = float.MaxValue, zMin = float.MaxValue; float yMax = float.MinValue, zMax = float.MinValue; tMin = float.MaxValue; tMax = float.MinValue; if (rtRay.Direction.X > 0) { tMin = (Bounds.Min.X - rtRay.Origin.X) / rtRay.Direction.X; tMax = (Bounds.Max.X - rtRay.Origin.X) / rtRay.Direction.X; } else if (rtRay.Direction.X < 0) { tMin = (Bounds.Max.X - rtRay.Origin.X) / rtRay.Direction.X; tMax = (Bounds.Min.X - rtRay.Origin.X) / rtRay.Direction.X; } if (rtRay.Direction.Y > 0) { yMin = (Bounds.Min.Y - rtRay.Origin.Y) / rtRay.Direction.Y; yMax = (Bounds.Max.Y - rtRay.Origin.Y) / rtRay.Direction.Y; } else if (rtRay.Direction.Y < 0) { yMin = (Bounds.Max.Y - rtRay.Origin.Y) / rtRay.Direction.Y; yMax = (Bounds.Min.Y - rtRay.Origin.Y) / rtRay.Direction.Y; } if ((tMin > yMax) || (yMin > tMax)) { return(false); } if (yMin > tMin) { tMin = yMin; } if (yMax < tMax) { tMax = yMax; } if (rtRay.Direction.Z > 0) { zMin = (Bounds.Min.Z - rtRay.Origin.Z) / rtRay.Direction.Z; zMax = (Bounds.Max.Z - rtRay.Origin.Z) / rtRay.Direction.Z; } else if (rtRay.Direction.Z < 0) { zMin = (Bounds.Max.Z - rtRay.Origin.Z) / rtRay.Direction.Z; zMax = (Bounds.Min.Z - rtRay.Origin.Z) / rtRay.Direction.Z; } if ((tMin > zMax) || (zMin > tMax)) { return(false); } if (zMin > tMin) { tMin = zMin; } if (zMax < tMax) { tMax = zMax; } return(true); #if THIS_IS_USELESS // apparantly ray/BoundingBox returns _one_ of the intersection positions, but not always the closest one!! // USELESS!! Microsoft.Xna.Framework.Ray r = new Microsoft.Xna.Framework.Ray(rtRay.Origin, rtRay.Direction); float?dist = r.Intersects(Bounds); if (null == dist) { return(null); } float[] distances = new float[2]; if (Bounds.Contains(rtRay.Origin) == ContainmentType.Contains) { // originated from inside, let's find the back-end existing position Microsoft.Xna.Framework.Ray backRay = new Microsoft.Xna.Framework.Ray(rtRay.Origin, -rtRay.Direction); float?backDist = backRay.Intersects(Bounds); distances[0] = (float)-backDist; // this will be a negative value distances[1] = (float)dist; } else { // dist is the entry position, now find the exist position distances[0] = (float)dist; // advance the ray position without going out of this node r.Position = r.Position + (distances[0] + float.Epsilon) * r.Direction; if (Bounds.Contains(r.Position) == ContainmentType.Contains) { distances[1] = (float)r.Intersects(Bounds); } else { distances[1] = distances[0] + float.Epsilon; } } return(distances); #endif }
public Nullable <float> Intersects(Ray ray) { return(ray.Intersects(this)); }
public void Intersects(ref Ray ray, out Nullable <float> result) { result = Intersects(ray); }
public abstract float?intersects(Microsoft.Xna.Framework.Ray ray);
public override float?intersects(Microsoft.Xna.Framework.Ray ray) { return(new BoundingBox(objectLoc.toWorldSpaceVector3() - new Vector3(1, 1, 1), objectLoc.toWorldSpaceVector3() + new Vector3(2, 2, 2)).Intersects(ray)); }
public bool IntersectRay(Microsoft.Xna.Framework.Ray r) { float?res = box.Intersects(r); return(res.HasValue); }
internal JobSite getJobSiteAlongRay(Microsoft.Xna.Framework.Ray ray) { return((JobSite)Intersection.getNearestIntersectableAlongRay(ray, jobSites)); }
public override float?intersects(Microsoft.Xna.Framework.Ray ray) { return(Intersection.intersects(ray, plantBlocks.Keys.ToList())); }