public static Optional<IBody> RaySelect(this IUniverse uni, Vector3 origin, Vector3 dir, IRay rayCastProvider = null) { if (rayCastProvider == null) rayCastProvider = new Ray(); double furthestBodyPos = uni.Bodies.Max(b => (b.Position() - origin).Magnitude) + 100; // need to tune this. need to basically find the max dimension size of the biggest object var res = rayCastProvider.Cast(uni.Bodies.Select(b => new TransformedObj<IEdgeIntersector>(b.Dynamics.Transform, b.CollisionShape)), origin, dir, furthestBodyPos); return res.Match( hit => uni.Bodies.Single(b => b.CollisionShape == hit.HitObject.Obj).ToOptional(), () => Optional<IBody>.Nothing ); }
public void MultiObjectTestTwo() { var closeIntersector = A.Fake<IEdgeIntersector>(); var farIntersector = A.Fake<IEdgeIntersector>(); var closeTrans = new Transform(Vector3.I, Matrix3.Identity); var farTrans = new Transform(5 * Vector3.I, Matrix3.Identity); var closeObj = new TransformedObj<IEdgeIntersector>(closeTrans, closeIntersector); var farObj = new TransformedObj<IEdgeIntersector>(farTrans, farIntersector); A.CallTo(() => closeIntersector.FindIntersections(default(Edge))) .WithAnyArguments() .Returns(new Intersection[] { }); A.CallTo(() => farIntersector.FindIntersections(default(Edge))) .WithAnyArguments() .Returns(new[] { new Intersection() }); Hit? hit = new Ray().Cast(new[] { farObj, closeObj }, Vector3.Zero, Vector3.I, 100).Match<Hit?>(h => h, () => null); Assert.NotNull(hit); Assert.That(hit.Value.HitObject, Is.EqualTo(farObj)); }