本文整理汇总了C#中Microsoft.Xna.Framework.Ray类的典型用法代码示例。如果您正苦于以下问题:C# Ray类的具体用法?C# Ray怎么用?C# Ray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Ray类属于Microsoft.Xna.Framework命名空间,在下文中一共展示了Ray类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: getMouseWorldPosition
/// <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);
}
开发者ID:ArghyV,项目名称:Peliohjelmointi-s2011,代码行数:37,代码来源:Input.cs
示例2: CalculateLocalMouse
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);
}
}
开发者ID:Blecki,项目名称:GnomeColony,代码行数:26,代码来源:MeshNode.cs
示例3: GetIntersectingModel
/// <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;
}
开发者ID:vsuley,项目名称:Spectro,代码行数:32,代码来源:SceneGraph.cs
示例4: shading
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;
}
开发者ID:furaga,项目名称:CGs,代码行数:33,代码来源:Shader.cs
示例5: Setup
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));
}
开发者ID:sergios1234,项目名称:monoxna,代码行数:7,代码来源:RayTests.cs
示例6: CalculateLocalMouse
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);
}
}
开发者ID:Blecki,项目名称:CCDC,代码行数:34,代码来源:NormalMapMeshNode.cs
示例7: IntersectionInfo
public IntersectionInfo(float distance, Ray originalRay, Vector3 baryCoord, Geomertry geomertry)
{
Distance = distance;
BaryCoord = baryCoord;
OriginalRay = originalRay;
Geomertry = geomertry;
}
开发者ID:bondarchook,项目名称:light-trace,代码行数:7,代码来源:IntersectionInfo.cs
示例8: 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;
}
开发者ID:xuebaibai2,项目名称:game_programming,代码行数:30,代码来源:MousePick.cs
示例9: DetermineSide
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;
}
开发者ID:Brandon-T,项目名称:XNA-FPS,代码行数:28,代码来源:Collider.cs
示例10: GetSourisBoxIntercept
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;
}
开发者ID:ChrisVolkoff,项目名称:DeRPG,代码行数:26,代码来源:InputManager3D.cs
示例11: FindCliff
/// <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;
}
}
开发者ID:thormme,项目名称:Chimera,代码行数:35,代码来源:ObstacleDetector.cs
示例12: canSee
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;
}
开发者ID:Linusa,项目名称:AI_Project,代码行数:28,代码来源:LoS.cs
示例13: 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);
}
开发者ID:powercharger97,项目名称:AsterCorp,代码行数:30,代码来源:Camera.cs
示例14: Intersects
// 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;
}
开发者ID:jaseporter01,项目名称:defense,代码行数:29,代码来源:Level.cs
示例15: 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;
}
开发者ID:kirlianstudios,项目名称:armada,代码行数:30,代码来源:RayPicker.cs
示例16: 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);
}
开发者ID:kaysoky,项目名称:AstronomicalSimulator,代码行数:34,代码来源:Cursor.cs
示例17: Unproject
/// <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;
}
开发者ID:kaysoky,项目名称:AstronomicalSimulator,代码行数:28,代码来源:Cursor.cs
示例18: BinarySearch
//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;
}
开发者ID:patrykos91,项目名称:Laikos,代码行数:28,代码来源:Collisions.cs
示例19: Shot
public Shot(Ray Trac, float Caliber, Being Origin)
{
Tracer = Trac;
TimeToDie = Environment.TickCount+100;
this.Caliber = Caliber;
this.Origin = Origin;
}
开发者ID:narfman0,项目名称:SunshineSlashers,代码行数:7,代码来源:Shot.cs
示例20: 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);
}
开发者ID:MenosGrandes,项目名称:mrowisko-pbl,代码行数:29,代码来源:CreatorController.cs
注:本文中的Microsoft.Xna.Framework.Ray类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论