本文整理汇总了C#中Ray.Intersects方法的典型用法代码示例。如果您正苦于以下问题:C# Ray.Intersects方法的具体用法?C# Ray.Intersects怎么用?C# Ray.Intersects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ray
的用法示例。
在下文中一共展示了Ray.Intersects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPickedPoint
public Vector2 GetPickedPoint()
{
var screenW = MyGame.GraphicsDevice.BackBuffer.Width;
var screenH = MyGame.GraphicsDevice.BackBuffer.Height;
var mouseX = MyGame.MouseState.X * screenW;
var mouseY = MyGame.MouseState.Y * screenH;
var viewProjection = ViewMatrix * ProjectionMatrix;
var p1 = new Vector3(mouseX, mouseY, 0);
var p2 = new Vector3(mouseX, mouseY, -1);
var q1 = Vector3.Unproject(p1, 0, 0, screenW, screenH, 0, 1, viewProjection);
var q2 = Vector3.Unproject(p2, 0, 0, screenW, screenH, 0, 1, viewProjection);
var diff = Vector3.Normalize(q2 - q1);
var r = new Ray(Position, -diff);
var p = new Plane(-Vector3.UnitZ, 0);
Vector3 result;
var q = r.Intersects(ref p, out result);
return new Vector2(result.X, result.Y);
}
示例2: Mouse_Move
public override void Mouse_Move(MouseEventArgs e)
{
_endMouseRay = Editor.Camera.GetWorldRay(new Vector2(e.X, e.Y));
if (!_startSet)
{
_startPositions.Clear();
foreach (var control in Editor.Selection)
{
_startPositions.Add(control, control.Position);
}
_startMouseRay = _endMouseRay;
_cameraPlane = new Plane(Editor.Camera.LookAt, Editor.Camera.LookAt + Editor.Camera.Right, Editor.Camera.LookAt + Editor.Camera.UpEffective);
_startSet = true;
}
float? distanceStart, distance;
_startMouseRay.Intersects(ref _cameraPlane, out distanceStart);
_endMouseRay.Intersects(ref _cameraPlane, out distance);
if (distanceStart != null && distance != null)
{
var deltaMouse = (_endMouseRay.Position + Vector3.Multiply(_endMouseRay.Direction, distance.Value))
- (_startMouseRay.Position + Vector3.Multiply(_startMouseRay.Direction, distanceStart.Value));
foreach (var control in Editor.Selection)
{
control.Position = _startPositions[control] + deltaMouse;
}
}
}
示例3: CalculateLocalMouse
public override void CalculateLocalMouse(Ray MouseRay, Action<Gem.Render.SceneNode, float> HoverCallback)
{
MouseHover = false;
var verts = new Vector3[3];
verts[0] = new Vector3(0.0f, 0.0f, 0);
verts[1] = new Vector3(1.0f, 0.0f, 0);
verts[2] = new Vector3(0.0f, 1.0f, 0);
for (int i = 0; i < 3; ++i)
verts[i] = Vector3.Transform(verts[i], WorldTransform);
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);
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 * DivisionsX);
LocalMouseY = (int)(y * DivisionsY);
MouseHover = true;
}
示例4: RayPointingThroughBoundingSphereFromRightShouldBeIntersected
public void RayPointingThroughBoundingSphereFromRightShouldBeIntersected()
{
var boundingSphere = new BoundingSphere(new Vector(0, 0, 0), 1);
var ray = new Ray(new Vector(10, 0, 0), new Vector(-1, 0, 0));
var result = ray.Intersects(boundingSphere);
Assert.That(result, Is.Not.Null);
}
示例5: RayPointingAwayFromBoundingSphereShouldBeIntersected
public void RayPointingAwayFromBoundingSphereShouldBeIntersected()
{
var boundingSphere = new BoundingSphere(new Vector(0, 0, 0), 1);
var ray = new Ray(new Vector(0, -10, 0), new Vector(0, -1, 0));
var result = ray.Intersects(boundingSphere);
Assert.That(result, Is.Null);
}
示例6: IsIntersectedBy
public bool IsIntersectedBy(Ray ray3, out float distance)
{
var v1 = Face.ModelPart.Vertices[V1VertexIndex];
var v2 = Face.ModelPart.Vertices[V2VertexIndex];
var v3 = Face.ModelPart.Vertices[V3VertexIndex];
return ray3.Intersects(ref v1, ref v2, ref v3, out distance);
}
示例7: GetScreenPosition
public static PositionInfo GetScreenPosition(Vector3 position3D)
{
var position = position3D.To2D();
var worldCenter = GetWorldCenter();
var screenCenter = Drawing.WorldToScreen(worldCenter.To3D().SetZ(0));
if (position3D.IsOnScreen())
{
var screenPosition = Drawing.WorldToScreen(position3D);
return new PositionInfo
{
screenPosition = screenPosition,
direction = (screenPosition - screenCenter).Normalized(),
distance = worldCenter.Distance(position),
screenCollisionDistance = worldCenter.Distance(position)
};
}
var worldDir = (position - worldCenter).Normalized();
var worldClosePosition = worldCenter + worldDir * 100;
var screenClosePosition = Drawing.WorldToScreen(worldClosePosition.To3D().SetZ(0));
var dir = (screenClosePosition - screenCenter).Normalized();
var screenFarPosition = screenCenter + dir * (Math.Max(Drawing.Width, Drawing.Height) + 100);
var ray = new Ray(screenFarPosition.To3D().SetZ(0), -dir.To3D().SetZ(0));
var boundingBox = new BoundingBox(new Vector3(0, 0, -1),
new Vector3(Drawing.Width, Drawing.Height, 1));
float dist;
var hasIntersection = ray.Intersects(ref boundingBox, out dist);
if (hasIntersection)
{
var rayDirection = dir;
var distance = worldCenter.Distance(position);
var finalScreenPos = screenFarPosition - dir * (dist);
return new PositionInfo
{
screenPosition = position3D.IsOnScreen() ?
Drawing.WorldToScreen(position3D) : finalScreenPos,
direction = rayDirection,
distance = distance,
screenCollisionDistance = dist
};
}
//Console.WriteLine("no intersect");
return null;
}
示例8: Mouse_Move
public override void Mouse_Move(MouseEventArgs e)
{
_endMouseRay = _camera.GetWorldRay(new Vector2(e.X, e.Y));
float? distanceStart, distance;
_startMouseRay.Intersects(ref _cameraPlane, out distanceStart);
_endMouseRay.Intersects(ref _cameraPlane, out distance);
if (distanceStart != null && distance != null)
{
var deltaMouse = (_endMouseRay.Position + Vector3.Multiply(_endMouseRay.Direction, distance.Value))
- (_startMouseRay.Position + Vector3.Multiply(_startMouseRay.Direction, distanceStart.Value));
Editor.Camera.LookAt = _camera.LookAt - deltaMouse;
Editor.Camera.Position = _camera.Position - deltaMouse;
}
}
示例9: IsCustomLookAtTarget
private static bool IsCustomLookAtTarget(MySmallShip smallShip, MyEntity otherEntity, float radius)
{
if (!MUST_LOOK_AT_TARGET)
{
return true;
}
BoundingSphere sphere = new BoundingSphere(otherEntity.WorldVolume.Center, radius);
Ray ray = new Ray(smallShip.GetPosition(), smallShip.WorldMatrix.Forward);
float? rayIntersection = ray.Intersects(sphere);
if (rayIntersection.HasValue)
{
MyLine line = new MyLine(smallShip.WorldVolume.Center, otherEntity.WorldVolume.Center, true);
MyIntersectionResultLineTriangleEx? result = MyEntities.GetIntersectionWithLine(ref line, smallShip, null, ignoreChilds: true);
return !result.HasValue || !result.HasValue || result.Value.Entity == otherEntity;
}
return false;
}
示例10: GetIntersectingPolygon
public bool GetIntersectingPolygon(ref Ray ray, ref OCTreeIntersection closest)
{
if (Indices != null)
{
var nullable = ray.Intersects(Bounds);
if (nullable.HasValue)
{
var num = (closest.IntersectType == OCTreeIntersectionType.None) ? float.MaxValue : closest.DistanceSquared;
if (nullable.Value < num)
{
for (var i = 0; i < (Indices.Length / 3); i++)
{
float num2;
float num3;
float num4;
if (Intersection.RayTriangleIntersect(ray.Position, ray.Direction, Vertices[Indices[i * 3]], Vertices[Indices[(i * 3) + 1]], Vertices[Indices[(i * 3) + 2]], out num2, out num3, out num4))
{
var vector = ray.Position + ((ray.Direction * num2));
var num6 = Vector3.DistanceSquared(vector, ray.Position);
if (num6 < num)
{
num = num6;
closest.IntersectionNormal = MathExtensions.NormalFromPoints(Vertices[Indices[i * 3]], Vertices[Indices[(i * 3) + 1]], Vertices[Indices[(i * 3) + 2]]);
closest.IntersectionPoint = vector;
closest.Node = this;
closest.PolygonIndex = i;
closest.DistanceSquared = num6;
closest.IntersectType = OCTreeIntersectionType.Inside;
}
}
}
}
}
}
else if (Children != null)
{
for (var j = 0; j < Children.Length; j++)
{
Children[j].GetIntersectingPolygon(ref ray, ref closest);
}
}
return (closest.IntersectType != OCTreeIntersectionType.None);
}
示例11: Cast
// Thanks to http://gamedev.stackexchange.com/questions/47362/cast-ray-to-select-block-in-voxel-game
public static Tuple<Coordinates3D, BlockFace> Cast(ReadOnlyWorld world,
Ray ray, IBlockRepository repository, int posmax, int negmax)
{
// TODO: There are more efficient ways of doing this, fwiw
double min = negmax * 2;
var pick = -Coordinates3D.One;
var face = BlockFace.PositiveY;
for (int x = -posmax; x <= posmax; x++)
{
for (int y = -negmax; y <= posmax; y++)
{
for (int z = -posmax; z <= posmax; z++)
{
var coords = (Coordinates3D)(new Vector3(x, y, z) + ray.Position).Round();
if (!world.IsValidPosition(coords))
continue;
var id = world.GetBlockID(coords);
if (id != 0)
{
var provider = repository.GetBlockProvider(id);
var box = provider.InteractiveBoundingBox;
if (box != null)
{
BlockFace _face;
var distance = ray.Intersects(box.Value.OffsetBy(coords), out _face);
if (distance != null && distance.Value < min)
{
min = distance.Value;
pick = coords;
face = _face;
}
}
}
}
}
}
if (pick == -Coordinates3D.One)
return null;
return new Tuple<Coordinates3D, BlockFace>(pick, face);
}
示例12: Hero_OnNewPath
private void Hero_OnNewPath(Obj_AI_Base hero, GameObjectNewPathEventArgs args)
{
if (ObjectCache.menuCache.cache["AutoSetPingOn"].GetValue<bool>() == false)
{
return;
}
if (!hero.IsMe)
{
return;
}
var path = args.Path;
if (path.Length > 1 && !args.IsDash)
{
var movePos = path.Last().To2D();
if (checkPing
&& lastIssueOrderArgs.Process == true
&& lastIssueOrderArgs.Order == GameObjectOrder.MoveTo
&& lastIssueOrderArgs.TargetPosition.To2D().Distance(movePos) < 3
&& myHero.Path.Count() == 1
&& args.Path.Count() == 2
&& myHero.IsMoving)
{
//Draw.RenderObjects.Add(new Draw.RenderPosition(myHero.Path.Last().To2D(), 1000));
Draw.RenderObjects.Add(new Draw.RenderLine(args.Path.First().To2D(), args.Path.Last().To2D(), 1000));
Draw.RenderObjects.Add(new Draw.RenderLine(myHero.Position.To2D(), myHero.Path.Last().To2D(), 1000));
//Draw.RenderObjects.Add(new Draw.RenderCircle(lastMoveToServerPos, 1000, System.Drawing.Color.Red, 10));
var distanceTillEnd = myHero.Path.Last().To2D().Distance(myHero.Position.To2D());
float moveTimeTillEnd = 1000 * distanceTillEnd / myHero.MoveSpeed;
if (moveTimeTillEnd < 500)
{
return;
}
var dir1 = (myHero.Path.Last().To2D() - myHero.Position.To2D()).Normalized();
var ray1 = new Ray(myHero.Position.SetZ(0), new Vector3(dir1.X, dir1.Y, 0));
var dir2 = (args.Path.First().To2D() - args.Path.Last().To2D()).Normalized();
var pos2 = new Vector3(args.Path.First().X, args.Path.First().Y, 0);
var ray2 = new Ray(args.Path.First().SetZ(0), new Vector3(dir2.X, dir2.Y, 0));
Vector3 intersection3;
if (ray2.Intersects(ref ray1, out intersection3))
{
var intersection = intersection3.To2D();
var projection = intersection.ProjectOn(myHero.Path.Last().To2D(), myHero.Position.To2D());
if (projection.IsOnSegment && dir1.AngleBetween(dir2) > 20 && dir1.AngleBetween(dir2) < 160)
{
Draw.RenderObjects.Add(new Draw.RenderCircle(intersection, 1000, System.Drawing.Color.Red, 10));
var distance = //args.Path.First().To2D().Distance(intersection);
lastMoveToServerPos.Distance(intersection);
float moveTime = 1000 * distance / myHero.MoveSpeed;
//Console.WriteLine("waa: " + distance);
if (moveTime < 1000)
{
if (numExtraDelayTime > 0)
{
sumExtraDelayTime += moveTime;
avgExtraDelayTime = sumExtraDelayTime / numExtraDelayTime;
pingList.Add(moveTime);
}
numExtraDelayTime += 1;
if (maxExtraDelayTime == 0)
{
maxExtraDelayTime = ObjectCache.menuCache.cache["ExtraPingBuffer"].GetValue<Slider>().Value;
}
if (numExtraDelayTime % 100 == 0)
{
pingList.Sort();
var percentile = ObjectCache.menuCache.cache["AutoSetPercentile"].GetValue<Slider>().Value;
int percentIndex = (int)Math.Floor(pingList.Count() * (percentile / 100f)) - 1;
maxExtraDelayTime = Math.Max(pingList.ElementAt(percentIndex) - Game.Ping,0);
ObjectCache.menuCache.cache["ExtraPingBuffer"].SetValue(new Slider((int)maxExtraDelayTime, 0, 200));
pingList.Clear();
Console.WriteLine("Max Extra Delay: " + maxExtraDelayTime);
}
Console.WriteLine("Extra Delay: " + Math.Max(moveTime - Game.Ping,0));
}
}
}
}
//.........这里部分代码省略.........
示例13: GetIntersectionWithLine
public override bool GetIntersectionWithLine(ref MyLine line, out Vector3? v, bool useCollisionModel = true, IntersectionFlags flags = IntersectionFlags.ALL_TRIANGLES)
{
BoundingSphere boundingSphere = new BoundingSphere(GetPosition(), BoundingSphereRadius);
Ray ray = new Ray(line.From, line.Direction);
float? result = ray.Intersects(boundingSphere);
v = result.HasValue ? ray.Position + ray.Direction * result.Value : (Vector3?)null;
return result.HasValue;
}
示例14: Hero_OnNewPath
private void Hero_OnNewPath(Obj_AI_Base hero, GameObjectNewPathEventArgs args)
{
if (!Config.Properties.GetBool(ConfigValue.AutoSetPing))
{
return;
}
if (!hero.IsMe)
{
return;
}
var path = args.Path;
if (path.Length > 1 && !args.IsDash)
{
var movePos = path.Last().To2D();
if (_checkPing
&& _lastIssueOrderArgs.Process == true
&& _lastIssueOrderArgs.Order == GameObjectOrder.MoveTo
&& _lastIssueOrderArgs.TargetPosition.To2D().Distance(movePos) < 3
&& MyHero.Path.Count() == 1
&& args.Path.Count() == 2
&& MyHero.IsMoving)
{
//Draw.RenderObjects.Add(new Draw.RenderPosition(myHero.Path.Last().To2D(), 1000));
RenderObjects.Add(new RenderLine(args.Path.First().To2D(), args.Path.Last().To2D(), 1000));
RenderObjects.Add(new RenderLine(MyHero.Position.To2D(), MyHero.Path.Last().To2D(), 1000));
//Draw.RenderObjects.Add(new Draw.RenderCircle(lastMoveToServerPos, 1000, System.Drawing.Color.Red, 10));
var distanceTillEnd = MyHero.Path.Last().To2D().Distance(MyHero.Position.To2D());
float moveTimeTillEnd = 1000 * distanceTillEnd / MyHero.MoveSpeed;
if (moveTimeTillEnd < 500)
{
return;
}
var dir1 = (MyHero.Path.Last().To2D() - MyHero.Position.To2D()).Normalized();
var ray1 = new Ray(MyHero.Position.SetZ(0), new Vector3(dir1.X, dir1.Y, 0));
var dir2 = (args.Path.First().To2D() - args.Path.Last().To2D()).Normalized();
var pos2 = new Vector3(args.Path.First().X, args.Path.First().Y, 0);
var ray2 = new Ray(args.Path.First().SetZ(0), new Vector3(dir2.X, dir2.Y, 0));
Vector3 intersection3;
if (ray2.Intersects(ref ray1, out intersection3))
{
var intersection = intersection3.To2D();
var projection = intersection.ProjectOn(MyHero.Path.Last().To2D(), MyHero.Position.To2D());
if (projection.IsOnSegment && dir1.AngleBetween(dir2) > 20 && dir1.AngleBetween(dir2) < 160)
{
RenderObjects.Add(new RenderCircle(intersection, 1000, Color.Red, 10));
var distance = //args.Path.First().To2D().Distance(intersection);
_lastMoveToServerPos.Distance(intersection);
float moveTime = 1000 * distance / MyHero.MoveSpeed;
//ConsoleDebug.WriteLine("waa: " + distance);
if (moveTime < 1000)
{
if (_numExtraDelayTime > 0)
{
_sumExtraDelayTime += moveTime;
_avgExtraDelayTime = _sumExtraDelayTime / _numExtraDelayTime;
_pingList.Add(moveTime);
}
_numExtraDelayTime += 1;
if (_maxExtraDelayTime == 0)
{
_maxExtraDelayTime = Config.Properties.GetInt(ConfigValue.ExtraPingBuffer);
}
if (_numExtraDelayTime % 100 == 0)
{
_pingList.Sort();
var percentile = ConfigValue.AutoSetPingPercentile.GetInt();
int percentIndex = (int)Math.Floor(_pingList.Count() * (percentile / 100f)) - 1;
_maxExtraDelayTime = Math.Max(_pingList.ElementAt(percentIndex) - Game.Ping,0);
_maxExtraDelayTime.SetTo(ConfigValue.ExtraPingBuffer);
_pingList.Clear();
ConsoleDebug.WriteLine("Max Extra Delay: " + _maxExtraDelayTime);
}
ConsoleDebug.WriteLine("Extra Delay: " + Math.Max(moveTime - Game.Ping,0));
}
}
}
}
//.........这里部分代码省略.........
示例15: IsHitByRay
public bool IsHitByRay(Ray ray, out float distance)
{
bool result = false;
distance = float.MaxValue;
float closestHit = float.MaxValue;
if (ray.Intersects(BoundingSphere))
{
//Check if the ray intersects with a triangle from the mesh
for (int j = 0; j < IndexCount / 3; j++)
{
Vector3 v0 = VertexList[(int)IndexList[j * 3]];
Vector3 v1 = VertexList[(int)IndexList[j * 3 + 1]];
Vector3 v2 = VertexList[(int)IndexList[j * 3 + 2]];
var ScaleMatrix = Matrix.Scaling(Scale.Vector3);
if (ray.Intersects(ref v0, ref v1, ref v2, out distance))
{
if (closestHit > distance * (ray.Direction * Scale.Vector3).Length())
{
closestHit = distance * (ray.Direction * Scale.Vector3).Length();
distance = float.MaxValue;
}
result = true;
}
}
}
distance = closestHit;
return result;
}