本文整理汇总了C#中Microsoft.Xna.Framework.Ray.Intersects方法的典型用法代码示例。如果您正苦于以下问题:C# Ray.Intersects方法的具体用法?C# Ray.Intersects怎么用?C# Ray.Intersects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Ray
的用法示例。
在下文中一共展示了Ray.Intersects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Project
/// <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);
}
示例2: checkCollisionAgainstEnvironment
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;
}
示例3: GetCollisionPosition
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;
}
示例4: BoundingCylinder_Ray_Test
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));
}
示例5: CalculateMouse3DPosition
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);
}
示例6: DetectClosestObstacle
/// <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;
}
}
}
示例7: GetOuter
public static VoxLocation? GetOuter(Ray camRay, VoxState state)
{
camRay.Position -= new Vector3(state.World.worldMin.X * Region.WIDTH, 0, state.World.worldMin.Y * Region.DEPTH);
VRay vr = new VRay(camRay.Position, camRay.Direction);
Vector3I loc = vr.GetNextLocation();
// Check For World Intersect
BoundingBox bb = new BoundingBox(Vector3.Zero, new Vector3(VoxWorld.WIDTH * Region.WIDTH, Region.HEIGHT, VoxWorld.DEPTH * Region.DEPTH));
if(!camRay.Intersects(bb).HasValue) return null;
// Move In World
while(!IsInBounds(loc))
loc = vr.GetNextLocation();
// Move Through World
VoxLocation pvl = new VoxLocation(loc);
while(IsInBounds(loc)) {
VoxLocation vl = new VoxLocation(loc);
Region region = state.World.regions[vl.RegionIndex];
if(region != null) {
ushort id = region.voxels[vl.VoxelIndex].ID;
if(id != 0) return pvl;
}
loc = vr.GetNextLocation();
pvl = vl;
}
return null;
}
示例8: CalculateLocalMouse
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;
}
示例9: GenerateRect
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);
}
示例10: GetPickables
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;
}
示例11: GetRayPlaneIntersectionPoint
public static Vector3? GetRayPlaneIntersectionPoint(Ray ray, Plane plane)
{
float? distance = ray.Intersects(plane);
if (distance.HasValue)
return ray.Position + ray.Direction * distance.Value;
else
return null;
}
示例12: FindIntersection
// überprüft, ob ein Strahl ein Dreieck schneidet
protected float? FindIntersection(Ray ray, ITriangle triangle)
{
// überprüfen, ob der Strahl die vom Dreieck aufgespannte Ebene schneidet
float? f = ray.Intersects(new Plane(triangle.P0, triangle.P1, triangle.P2));
// wenn ja, überprüfen ob Schnittpunkt im Dreieck liegt
if (f != null && IsInsideTriangle(triangle, ray.Position + ray.Direction * f.Value))
return f;
return null;
}
示例13: CastBulletRay
public static float? CastBulletRay(Entity targetEntity, GraphicsDevice GraphicsDevice)
{
Vector3 near = new Vector3(GraphicsDevice.Viewport.Width * 0.5f, GraphicsDevice.Viewport.Height * 0.5f, 0.0f);
Vector3 far = new Vector3(GraphicsDevice.Viewport.Width * 0.5f, GraphicsDevice.Viewport.Height * 0.5f, 1.0f);
near = GraphicsDevice.Viewport.Unproject(near, Projection(GraphicsDevice), LookAt(), Matrix.Identity);
far = GraphicsDevice.Viewport.Unproject(far, Projection(GraphicsDevice), LookAt(), Matrix.Identity);
Ray ray = new Ray(near, Vector3.Normalize(far - near));
return ray.Intersects(targetEntity.model.boundingbox);
}
开发者ID:SamOatesUniversity,项目名称:Year-2---Game-Software-Development---Out-Of-Scope,代码行数:11,代码来源:Camera.cs
示例14: CheckRayIntersection
public bool CheckRayIntersection(Ray ray, BoundingSphere sphere)
{
if (ray.Intersects(sphere) != null)
{
drawSpheres = sphere;
return true;
}
else
{
return false;
}
}
示例15: getIntersectedQuadNode
public static Vector3 getIntersectedQuadNode(Ray intersected)
{
Vector3 v = Vector3.Zero;
BoundingBox tmp;
foreach(QuadNode q in QuadNodeController.QuadNodeList)
{
if((intersected.Intersects(q.Bounds))!=null)
{
// prezri vsetky bunky terenu v patchi a zisti ktoru malu bunku pretina
int minX = (int)q.Bounds.Min.X;
int minZ = (int)q.Bounds.Min.Z;
int maxX = (int)q.Bounds.Max.X;
int maxZ = (int)q.Bounds.Max.Z;
for (int j = minX; j < maxX; j++)
{
for (int k = minZ; k < maxZ; k++)
{
v.X = j;
v.Y = StaticHelpers.StaticHelper.GetHeightAt(k, j);
v.Z = k;
tmp.Min = v;
tmp.Max = v + new Vector3(1);
if (intersected.Intersects(tmp) != null)
{
return v;
}
}
}
return q.Bounds.Min + (q.Bounds.Max - q.Bounds.Min) /2;
}
}
return Vector3.Zero;
}