本文整理汇总了C#中Plane.Intersects方法的典型用法代码示例。如果您正苦于以下问题:C# Plane.Intersects方法的具体用法?C# Plane.Intersects怎么用?C# Plane.Intersects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Plane
的用法示例。
在下文中一共展示了Plane.Intersects方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WillNotIntersectPlaneIfPointingAway
public void WillNotIntersectPlaneIfPointingAway()
{
var rayPosition = new Vector3(6.66666651f, 3.33333325f, 3.33333325f);
var rayDirection = new Vector3(-0.49999997f, 0.49999997f, 0.49999997f);
var ray = new Ray(rayPosition, rayDirection);
var plane = new Plane(0.0f, 0.707106769f, 0.707106769f, -3.535534f);
float distance;
var result = plane.Intersects(ref ray, out distance);
Assert.IsFalse(result);
}
示例2: RayIntersectsRectangle
/// <summary>
/// Determines whether there is an intersection between a <see cref="Ray"/> and a rectangle (2D).
/// </summary>
/// <param name="ray">The ray to test</param>
/// <param name="rectangleWorldMatrix">The world matrix applied on the rectangle</param>
/// <param name="rectangleSize">The size of the rectangle in 3D</param>
/// <param name="normalAxis">The index of axis defining the normal of the rectangle in the world. This value should be 0, 1 or 2</param>
/// <param name="intersectionPoint">The position of the intersection point in the world</param>
/// <returns><value>true</value> if the ray and rectangle intersects.</returns>
public static bool RayIntersectsRectangle(ref Ray ray, ref Matrix rectangleWorldMatrix, ref Vector3 rectangleSize, int normalAxis, out Vector3 intersectionPoint)
{
bool intersects;
int testAxis1;
int testAxis2;
switch (normalAxis)
{
case 0:
testAxis1 = 1;
testAxis2 = 2;
break;
case 1:
testAxis1 = 2;
testAxis2 = 0;
break;
case 2:
testAxis1 = 0;
testAxis2 = 1;
break;
default:
throw new ArgumentOutOfRangeException("normalAxis");
}
var rectanglePosition = new Vector3(rectangleWorldMatrix.M41, rectangleWorldMatrix.M42, rectangleWorldMatrix.M43);
var normalRowStart = normalAxis << 2;
var plane = new Plane(rectanglePosition, new Vector3(rectangleWorldMatrix[normalRowStart], rectangleWorldMatrix[normalRowStart + 1], rectangleWorldMatrix[normalRowStart + 2]));
// early exist the planes were parallels
if (!plane.Intersects(ref ray, out intersectionPoint))
return false;
// the position of the intersection point with respect to the rectangle center
var intersectionInRectangle = intersectionPoint - rectanglePosition;
// optimization for the simple but very frequent case where the element is not rotated
if (rectangleWorldMatrix.M12 == 0 && rectangleWorldMatrix.M13 == 0 &&
rectangleWorldMatrix.M21 == 0 && rectangleWorldMatrix.M23 == 0 &&
rectangleWorldMatrix.M31 == 0 && rectangleWorldMatrix.M32 == 0)
{
var halfSize1 = Math.Abs(rectangleWorldMatrix[(testAxis1 << 2) + testAxis1] * rectangleSize[testAxis1] / 2f);
var halfSize2 = Math.Abs(rectangleWorldMatrix[(testAxis2 << 2) + testAxis2] * rectangleSize[testAxis2] / 2f);
intersects = -halfSize1 <= intersectionInRectangle[testAxis1] && intersectionInRectangle[testAxis1] <= halfSize1 &&
-halfSize2 <= intersectionInRectangle[testAxis2] && intersectionInRectangle[testAxis2] <= halfSize2;
}
else // general case: decompose the rectangle into two triangles and check that all angles are less than 180 degrees in at least one of the triangles.
{
// find the most significant component of the plane normal
var normalTestIndex = 0;
for (int i = 1; i < 3; i++)
{
if (Math.Abs(plane.Normal[i]) > Math.Abs(plane.Normal[normalTestIndex]))
normalTestIndex = i;
}
var normalSign = Math.Sign(plane.Normal[normalTestIndex]);
// the base vector
var base1 = rectangleSize[testAxis1] * new Vector3(rectangleWorldMatrix[(testAxis1 << 2)], rectangleWorldMatrix[(testAxis1 << 2) + 1], rectangleWorldMatrix[(testAxis1 << 2) + 2]) / 2;
var base2 = rectangleSize[testAxis2] * new Vector3(rectangleWorldMatrix[(testAxis2 << 2)], rectangleWorldMatrix[(testAxis2 << 2) + 1], rectangleWorldMatrix[(testAxis2 << 2) + 2]) / 2;
// build the first triangle and perform the test
var v1 = -base1 - base2 - intersectionInRectangle;
var v2 = +base1 - base2 - intersectionInRectangle;
var v3 = +base1 + base2 - intersectionInRectangle;
intersects = Math.Sign(Vector3.Cross(v1, v2)[normalTestIndex]) == normalSign &&
Math.Sign(Vector3.Cross(v2, v3)[normalTestIndex]) == normalSign &&
Math.Sign(Vector3.Cross(v3, v1)[normalTestIndex]) == normalSign;
// early exit on success
if (intersects)
return true;
// build second triangle and perform the test
v1 = -base1 - base2 - intersectionInRectangle;
v2 = +base1 + base2 - intersectionInRectangle;
v3 = -base1 + base2 - intersectionInRectangle;
intersects = Math.Sign(Vector3.Cross(v1, v2)[normalTestIndex]) == normalSign &&
Math.Sign(Vector3.Cross(v2, v3)[normalTestIndex]) == normalSign &&
Math.Sign(Vector3.Cross(v3, v1)[normalTestIndex]) == normalSign;
}
return intersects;
}
示例3: CheckBoundingSpheres
//for plane collisions, i.e. walls
public bool CheckBoundingSpheres(Plane CheckArg)
{
for (int cntr = 0; cntr < MyModel.Meshes.Count; cntr++)
{
//creates a sphere for one mesh
BoundingSphere Temp = this.MyModel.Meshes[cntr].BoundingSphere;
//adjusts to center of the model
Temp.Center += this.Position;
if (CheckArg.Intersects(Temp) == PlaneIntersectionType.Intersecting)
{
//intersection confimerd, stop checks and prevent model from moving in outer code
return (true);
}
}
//else, return false, no hit detected
return (false);
}
示例4: Intersects
public static void Intersects(ref BoundingFrustum boundingFrustum, ref Plane plane, out PlaneIntersectionType result)
{
var corners = boundingFrustum.GetCorners();
result = plane.Intersects(corners[0]);
for (int i = 1; i < corners.Length; i++)
if (plane.Intersects(corners[i]) != result)
result = PlaneIntersectionType.Intersecting;
}
示例5: ComputePickingDelta
private Vector3 ComputePickingDelta(EngineContext engineContext, Vector3 axis1, Vector3 axis2, ref Matrix viewProj, Vector3 pickingGizmoOrigin, Vector3 pickingObjectOrigin, Vector2 delta)
{
// Build plane along which object will move
var plane = new Plane(pickingGizmoOrigin, pickingGizmoOrigin + axis1, pickingGizmoOrigin + axis2);
plane.Normalize();
// Position difference in screen space to move one unit
var pickingStartTranslationScreenSpace = Vector3.TransformCoordinate(pickingGizmoOrigin, viewProj);
// Build mouse picking ray
var mouseDelta = new Vector3(delta.X / engineContext.RenderContext.Width * 2.0f,
-delta.Y / engineContext.RenderContext.Height * 2.0f,
0.0f);
var mousePickingPosition = pickingStartTranslationScreenSpace + mouseDelta;
var invViewProj = Matrix.Invert(viewProj);
mousePickingPosition.Z = 0.0f;
var picking1 = Vector3.TransformCoordinate(mousePickingPosition, invViewProj);
mousePickingPosition.Z = 0.1f;
var picking2 = Vector3.TransformCoordinate(mousePickingPosition, invViewProj);
// Intersect moving plane with mouse picking ray
var ray = new Ray(picking1, Vector3.Normalize(picking2 - picking1));
Vector3 pickingDelta;
plane.Intersects(ref ray, out pickingDelta);
// Project result along movement axis
pickingDelta = Vector3.Dot(axis1, pickingDelta - pickingGizmoOrigin) * axis1;
return pickingDelta;
}
示例6: checkFooting
//checks if entity boundary intersects with terrain tile boundary
public bool checkFooting(List<Tile> visibles)
{
List<Tile> possibleCollisions = new List<Tile>();
foreach (Tile t in visibles)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Vector3[] boundCornEnt = this.Boundary.GetCorners();
if (boundCornTile[0].Y <= boundCornEnt[2].Y && boundCornTile[1].X > boundCornEnt[0].X && boundCornTile[0].X < boundCornEnt[1].X)
possibleCollisions.Add(t);
}
if (possibleCollisions.Count != 0)
{
foreach (Tile t in possibleCollisions)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Plane topTilePlane = new Plane(boundCornTile[0], boundCornTile[1], boundCornTile[4]);
if (topTilePlane.Intersects(this.Boundary) == PlaneIntersectionType.Intersecting)
{
return true;
}
}
}
return false;
}
示例7: handleTerrainCollisionInDirection
//checks if entity collides with terrain in specified direction and sets it back accordingly
public bool handleTerrainCollisionInDirection(String direction, Vector3 moveVecEntity, List<Tile> visibles)
{
switch (direction)
{
#region left
case "left":
{
List<Tile> possibleCollisions = new List<Tile>();
foreach (Tile t in visibles)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Vector3[] boundCornEnt = this.Boundary.GetCorners();
if (boundCornTile[1].X < boundCornEnt[0].X && boundCornTile[0].Y > boundCornEnt[2].Y && boundCornTile[2].Y < boundCornEnt[0].Y)
possibleCollisions.Add(t);
}
if (possibleCollisions.Count != 0)
{
List<Tile> collisions = new List<Tile>();
foreach (Tile t in possibleCollisions)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Plane rightTilePlane = new Plane(boundCornTile[1], boundCornTile[2], boundCornTile[5]);
if (rightTilePlane.Intersects(this.Boundary) == PlaneIntersectionType.Front)
{
BoundingBox newBound = new BoundingBox(this.Boundary.Min + moveVecEntity, this.Boundary.Max + moveVecEntity);
if (rightTilePlane.Intersects(newBound) == PlaneIntersectionType.Intersecting)
{
collisions.Add(t);
float deltaX = Math.Abs(t.Boundary.Max.X - this.Boundary.Min.X);
this.move(new Vector3(-deltaX,0,0));
Camera2D.realign(new Vector3(-deltaX, 0, 0), this.Position);
}
}
}
if (collisions.Count != 0)
return true;
else
return false;
}
} break;
#endregion
#region right
case "right":
{
List<Tile> possibleCollisions = new List<Tile>();
foreach (Tile t in visibles)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Vector3[] boundCornEnt = this.Boundary.GetCorners();
if (boundCornTile[0].X > boundCornEnt[1].X && boundCornTile[0].Y > boundCornEnt[2].Y && boundCornTile[2].Y < boundCornEnt[0].Y)
possibleCollisions.Add(t);
}
if (possibleCollisions.Count != 0)
{
List<Tile> collisions = new List<Tile>();
foreach (Tile t in possibleCollisions)
{
Vector3[] boundCornTile = t.Boundary.GetCorners();
Plane leftTilePlane = new Plane(boundCornTile[0], boundCornTile[3], boundCornTile[4]);
if (leftTilePlane.Intersects(this.Boundary) == PlaneIntersectionType.Back)
{
BoundingBox newBound = new BoundingBox(this.Boundary.Min + moveVecEntity, this.Boundary.Max + moveVecEntity);
if (leftTilePlane.Intersects(newBound) == PlaneIntersectionType.Intersecting)
{
collisions.Add(t);
float deltaX = Math.Abs(t.Boundary.Min.X - this.Boundary.Max.X);
this.move(new Vector3(deltaX, 0, 0));
Camera2D.realign(new Vector3(deltaX, 0, 0), this.Position);
}
}
}
if (collisions.Count != 0)
return true;
else
return false;
}
//.........这里部分代码省略.........