本文整理汇总了C#中JigLibX.Geometry.Segment类的典型用法代码示例。如果您正苦于以下问题:C# Segment类的具体用法?C# Segment怎么用?C# Segment使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Segment类属于JigLibX.Geometry命名空间,在下文中一共展示了Segment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SegmentTriangleOverlap
/// <summary>
/// Indicates if a segment intersects a triangle
/// </summary>
/// <param name="seg"></param>
/// <param name="triangle"></param>
/// <returns>bool</returns>
public static bool SegmentTriangleOverlap(Segment seg, Triangle triangle)
{
// the parameters - if hit then they get copied into the args
float u, v, t;
Vector3 e1 = triangle.Edge0;
Vector3 e2 = triangle.Edge1;
Vector3 p = Vector3.Cross(seg.Delta, e2);
float a = Vector3.Dot(e1, p);
if (a > -JiggleMath.Epsilon && a < JiggleMath.Epsilon)
return false;
float f = 1.0f / a;
Vector3 s = seg.Origin - triangle.Origin;
u = f * Vector3.Dot(s, p);
if (u < 0.0f || u > 1.0f)
return false;
Vector3 q = Vector3.Cross(s, e1);
v = f * Vector3.Dot(seg.Delta, q);
if (v < 0.0f || (u + v) > 1.0f)
return false;
t = f * Vector3.Dot(e2, q);
if (t < 0.0f || t > 1.0f)
return false;
return true;
}
示例2: PointSegmentDistance
public static float PointSegmentDistance(out float t, Vector3 pt, Segment seg)
{
Vector3 kDiff;
float fT;
Vector3.Subtract(ref pt, ref seg.Origin, out kDiff);
Vector3.Dot(ref kDiff, ref seg.Delta, out fT);
if (fT <= 0.0f)
{
fT = 0.0f;
}
else
{
float sqrLen = seg.Delta.LengthSquared();
if (fT >= sqrLen)
{
fT = 1.0f;
kDiff = kDiff - seg.Delta;
}
else
{
fT = fT / sqrLen;
kDiff = kDiff - (fT * seg.Delta);
}
}
t = fT;
return kDiff.Length();
}
示例3: Intersects
public bool Intersects(
ref Vector3 position,
ref Vector3 vector,
out Vector3 resultPosition,
out Vector3 resultNormal,
out float resultFraction)
{
var segment = new Segment(position, vector);
return SegmentIntersect(out resultFraction, out resultPosition, out resultNormal, segment);
}
示例4: CollDetect
public override void CollDetect(CollDetectInfo info, float collTolerance, CollisionFunctor collisionFunctor)
{
Vector3 body0Pos = (info.Skin0.Owner != null) ? info.Skin0.Owner.OldPosition : Vector3.Zero;
Vector3 body1Pos = (info.Skin1.Owner != null) ? info.Skin1.Owner.OldPosition : Vector3.Zero;
// todo - proper swept test
Capsule oldCapsule0 = (Capsule)info.Skin0.GetPrimitiveOldWorld(info.IndexPrim0);
Capsule newCapsule0 = (Capsule)info.Skin0.GetPrimitiveNewWorld(info.IndexPrim0);
Segment oldSeg0 = new Segment(oldCapsule0.Position, oldCapsule0.Length * oldCapsule0.Orientation.Backward);
Segment newSeg0 = new Segment(newCapsule0.Position, newCapsule0.Length * newCapsule0.Orientation.Backward);
Capsule oldCapsule1 = (Capsule)info.Skin1.GetPrimitiveOldWorld(info.IndexPrim1);
Capsule newCapsule1 = (Capsule)info.Skin1.GetPrimitiveNewWorld(info.IndexPrim1);
Segment oldSeg1 = new Segment(oldCapsule1.Position, oldCapsule1.Length * oldCapsule1.Orientation.Backward);
Segment newSeg1 = new Segment(newCapsule1.Position, newCapsule1.Length * newCapsule1.Orientation.Backward);
float radSum = newCapsule0.Radius + newCapsule1.Radius;
float oldt0, oldt1;
float newt0, newt1;
float oldDistSq = Distance.SegmentSegmentDistanceSq(out oldt0, out oldt1, oldSeg0, oldSeg1);
float newDistSq = Distance.SegmentSegmentDistanceSq(out newt0, out newt1, newSeg0, newSeg1);
if (System.Math.Min(oldDistSq, newDistSq) < ((radSum + collTolerance) * (radSum + collTolerance)))
{
Vector3 pos0 = oldSeg0.GetPoint(oldt0);
Vector3 pos1 = oldSeg1.GetPoint(oldt1);
Vector3 delta = pos0 - pos1;
float dist = (float)System.Math.Sqrt((float)oldDistSq);
float depth = radSum - dist;
if (dist > JiggleMath.Epsilon)
{
delta /= dist;
}
else
{
// todo - make this not random
delta = Vector3.Transform(Vector3.Backward, Matrix.CreateFromAxisAngle(Vector3.Up, MathHelper.ToRadians(random.Next(360))));
}
Vector3 worldPos = pos1 +
(oldCapsule1.Radius - 0.5f * depth) * delta;
unsafe
{
SmallCollPointInfo collInfo = new SmallCollPointInfo(worldPos - body0Pos, worldPos - body1Pos, depth);
collisionFunctor.CollisionNotify(ref info, ref delta, &collInfo, 1);
}
}
}
示例5: SegmentAABoxOverlap
/// <summary>
/// Indicates if a segment overlaps an AABox
/// </summary>
/// <param name="seg"></param>
/// <param name="AABox"></param>
/// <returns>bool</returns>
public static bool SegmentAABoxOverlap(Segment seg, AABox AABox)
{
Vector3 p0 = seg.Origin;
Vector3 p1 = seg.GetEnd();
float[] faceOffsets = new float[2];
// The AABox faces are aligned with the world directions. Loop
// over the 3 directions and do the two tests.
for (int iDir = 0; iDir < 3; iDir++)
{
int jDir = (iDir + 1) % 3;
int kDir = (iDir + 2) % 3;
// one plane goes through the origin, one is offset
faceOffsets[0] = JiggleUnsafe.Get(AABox.MinPos, iDir);
faceOffsets[1] = JiggleUnsafe.Get(AABox.MaxPos, iDir);
for (int iFace = 0; iFace < 2; iFace++)
{
// distance of each point from to the face plane
float dist0 = JiggleUnsafe.Get(ref p0, iDir) - faceOffsets[iFace];
float dist1 = JiggleUnsafe.Get(ref p1, iDir) - faceOffsets[iFace];
float frac = -1.0f;
if (dist0 * dist1 < -JiggleMath.Epsilon)
frac = -dist0 / (dist1 - dist0);
else if (System.Math.Abs(dist0) < JiggleMath.Epsilon)
frac = 0.0f;
else if (System.Math.Abs(dist1) < JiggleMath.Epsilon)
frac = 1.0f;
if (frac >= 0.0f)
{
//Assert(frac <= 1.0f);
Vector3 pt = seg.GetPoint(frac);
// check the point is within the face rectangle
if ((JiggleUnsafe.Get(ref pt, jDir) > JiggleUnsafe.Get(AABox.MinPos, jDir) - JiggleMath.Epsilon) &&
(JiggleUnsafe.Get(ref pt, jDir) < JiggleUnsafe.Get(AABox.MaxPos, jDir) + JiggleMath.Epsilon) &&
(JiggleUnsafe.Get(ref pt, kDir) > JiggleUnsafe.Get(AABox.MinPos, kDir) - JiggleMath.Epsilon) &&
(JiggleUnsafe.Get(ref pt, kDir) < JiggleUnsafe.Get(AABox.MaxPos, kDir) + JiggleMath.Epsilon))
{
return true;
}
}
}
}
return false;
}
示例6: SegmentCapsuleIntersection
public static bool SegmentCapsuleIntersection(out float tS, Segment seg, Capsule capsule)
{
float bestFrac = float.MaxValue;
tS = 0;
// do the main sides
float sideFrac = float.MaxValue;
if (!SegmentInfiniteCylinderIntersection(out sideFrac, seg,
new Segment(capsule.Position, capsule.Orientation.Backward),
capsule.Radius))
return false; // check this
// only keep this if the side intersection point is within the capsule segment ends
Vector3 sidePos = seg.GetPoint(sideFrac);
if (Vector3.Dot(sidePos - capsule.Position, capsule.Orientation.Backward) < 0.0f)
sideFrac = float.MaxValue;
else if (Vector3.Dot(sidePos - capsule.GetEnd(), capsule.Orientation.Backward) > 0.0f)
sideFrac = float.MaxValue;
// do the two ends
float originFrac = float.MaxValue;
SegmentSphereIntersection(out originFrac, seg, new Sphere(capsule.Position, capsule.Radius));
float endFrac = float.MaxValue; // Check this!
SegmentSphereIntersection(out endFrac, seg, new Sphere(capsule.Position, capsule.Radius));
bestFrac = MathHelper.Min(sideFrac, originFrac);
bestFrac = MathHelper.Min(bestFrac, endFrac);
if (bestFrac <= 1.0f)
{
tS = bestFrac;
return true;
}
return false;
}
示例7: OnFire
public void OnFire(Vector3 muzzlePosition, Vector3 muzzleDir)
{
if (coolDownTimeRemaining <= 0)
{
//fpsModel.SetAnimationLayer("Pistol_Idle", 0.0f);
fpsModel.GetAnimationLayer().AddAnimation("Pistol_Fire", true);
//fpsModel.SetAnimationLayer("Pistol_Fire", 1.0f);
coolDownTimeRemaining = ResourceManager.Inst.GetAnimation("Pistol_Fire").EndTime;
float dist;
CollisionSkin skin;
Vector3 pos, normal;
Segment seg = new Segment(muzzlePosition, muzzleDir * 50);
scene.GetPhysicsEngine().CollisionSystem.SegmentIntersect(out dist, out skin, out pos, out normal, seg, ignorePred);
if (skin != null)
{
Decal decal = new Decal();
decal.SetMaterial("BulletMetal1");
decal.Normal = normal;
decal.Scale = new Vector2(0.25f, 0.25f);
Vector3 posNew = seg.Origin + seg.Delta * dist;
decal.Transformation.SetPosition(posNew);
decal.IsPersistent = false;
scene.AddEntity("bullet", decal);
ParticleEffect bulletEffect = ResourceManager.Inst.GetParticleEffect("BulletEffect");
ParticleEmitter collideEmitter = new ParticleEmitter(bulletEffect, 16);
collideEmitter.EmitOnce = true;
NormalTransform newTransform = new NormalTransform();
newTransform.ConformToNormal(normal);
newTransform.SetPosition(pos);
collideEmitter.Transformation = newTransform;
scene.AddEntity("bulletEmitter", collideEmitter);
}
}
}
示例8: SegmentIntersect
/// <summary>
/// Intersect a segment with the world. If non-zero the predicate
/// allows certain skins to be excluded
/// </summary>
/// <param name="seg"></param>
/// <param name="collisionPredicate"></param>
/// <returns></returns>
public abstract bool SegmentIntersect(out float fracOut, out CollisionSkin skinOut, out Vector3 posOut, out Vector3 normalOut,
Segment seg, CollisionSkinPredicate1 collisionPredicate);
示例9: SegmentTriangleDistanceSq
/// <summary>
/// SegmentTriangleDistanceSq
/// </summary>
/// <param name="segT"></param>
/// <param name="triT0"></param>
/// <param name="triT1"></param>
/// <param name="seg"></param>
/// <param name="triangle"></param>
/// <returns>float</returns>
public static float SegmentTriangleDistanceSq(out float segT, out float triT0, out float triT1, Segment seg, Triangle triangle)
{
// compare segment to all three edges of the triangle
float distSq = float.MaxValue;
if (Intersection.SegmentTriangleIntersection(out segT, out triT0, out triT1, seg, triangle))
{
segT = triT0 = triT1 = 0.0f;
return 0.0f;
}
float s, t, u;
float distEdgeSq;
distEdgeSq = SegmentSegmentDistanceSq(out s, out t, seg, new Segment(triangle.Origin, triangle.Edge0));
if (distEdgeSq < distSq)
{
distSq = distEdgeSq;
segT = s;
triT0 = t;
triT1 = 0.0f;
}
distEdgeSq = SegmentSegmentDistanceSq(out s, out t, seg, new Segment(triangle.Origin, triangle.Edge1));
if (distEdgeSq < distSq)
{
distSq = distEdgeSq;
segT = s;
triT0 = 0.0f;
triT1 = t;
}
distEdgeSq = SegmentSegmentDistanceSq(out s, out t, seg, new Segment(triangle.Origin + triangle.Edge0, triangle.Edge2));
if (distEdgeSq < distSq)
{
distSq = distEdgeSq;
segT = s;
triT0 = 1.0f - t;
triT1 = t;
}
// compare segment end points to triangle interior
float startTriSq = PointTriangleDistanceSq(out t, out u, seg.Origin, triangle);
if (startTriSq < distSq)
{
distSq = startTriSq;
segT = 0.0f;
triT0 = t;
triT1 = u;
}
float endTriSq = PointTriangleDistanceSq(out t, out u, seg.GetEnd(), triangle);
if (endTriSq < distSq)
{
distSq = endTriSq;
segT = 1.0f;
triT0 = t;
triT1 = u;
}
return distSq;
}
示例10: GetInteractNode
InteractNode GetInteractNode(Microsoft.Xna.Framework.Ray lookRay)
{
float collisionDist;
CollisionSkin skin = null;
Vector3 pos, normal;
Segment seg = new Segment(lookRay.Position, lookRay.Direction * MAX_INTERACT_DIST);
scene.GetPhysicsEngine().CollisionSystem.SegmentIntersect(out collisionDist, out skin, out pos, out normal, seg, pred);
float bestDist = (skin != null) ? collisionDist : float.PositiveInfinity;
InteractNode bestNode = null;
for (int i = 0; i < interactables.Count; i++)
{
InteractObject interactObj = interactables[i].GetInteractObject();
BoundingBox bounds = interactObj.Transformation.TransformBounds(interactObj.GetMesh().GetBounds());
float? intersection = lookRay.Intersects(bounds);
if (intersection.HasValue)
{
float dist = intersection.Value;
if (dist < bestDist && dist <= MAX_INTERACT_DIST)
{
InteractNode node = interactObj.GetInteractNode();
if (node.IsEnabled())
{
bestDist = dist;
bestNode = interactObj.GetInteractNode();
}
}
}
}
return bestNode;
}
示例11: CollDetect
/// <summary>
/// CollDetect
/// </summary>
/// <param name="infoOrig"></param>
/// <param name="collTolerance"></param>
/// <param name="collisionFunctor"></param>
public override void CollDetect(CollDetectInfo infoOrig, float collTolerance, CollisionFunctor collisionFunctor)
{
CollDetectInfo info = infoOrig;
// get the skins in the order that we're expecting
if (info.Skin0.GetPrimitiveOldWorld(info.IndexPrim0).Type == this.Type1)
{
CollisionSkin skinSwap = info.Skin0;
info.Skin0 = info.Skin1;
info.Skin1 = skinSwap;
int primSwap = info.IndexPrim0;
info.IndexPrim0 = info.IndexPrim1;
info.IndexPrim1 = primSwap;
}
Vector3 body0Pos = (info.Skin0.Owner != null) ? info.Skin0.Owner.OldPosition : Vector3.Zero;
Vector3 body1Pos = (info.Skin1.Owner != null) ? info.Skin1.Owner.OldPosition : Vector3.Zero;
// todo - proper swept test
Sphere oldSphere = info.Skin0.GetPrimitiveOldWorld(info.IndexPrim0) as Sphere;
Sphere newSphere = info.Skin0.GetPrimitiveNewWorld(info.IndexPrim0) as Sphere;
Capsule oldCapsule = info.Skin1.GetPrimitiveOldWorld(info.IndexPrim1) as Capsule;
Capsule newCapsule = info.Skin1.GetPrimitiveNewWorld(info.IndexPrim1) as Capsule;
Segment oldSeg = new Segment(oldCapsule.Position, oldCapsule.Length * oldCapsule.Orientation.Backward());
Segment newSeg = new Segment(oldCapsule.Position, newCapsule.Length * newCapsule.Orientation.Backward());
float radSum = newCapsule.Radius + newSphere.Radius;
float oldt, newt;
float oldDistSq = Distance.PointSegmentDistanceSq(out oldt, oldSphere.Position, oldSeg);
float newDistSq = Distance.PointSegmentDistanceSq(out newt, newSphere.Position, newSeg);
if (OpenTKHelper.Min(oldDistSq, newDistSq) < (radSum + collTolerance) * (radSum + collTolerance))
{
Vector3 segPos = oldSeg.GetPoint(oldt);
Vector3 delta = oldSphere.Position - segPos;
float dist = (float)System.Math.Sqrt((float)oldDistSq);
float depth = radSum - dist;
if (dist > JiggleMath.Epsilon)
{
delta /= dist;
}
else
{
// todo - make this not random
delta = Vector3Extensions.TransformNormal(Vector3Extensions.Backward, Matrix4.CreateFromAxisAngle(Vector3Extensions.Up, OpenTKHelper.ToRadians(random.Next(360))));
}
Vector3 worldPos = segPos +
((oldCapsule.Radius - 0.5f * depth) * delta);
unsafe
{
SmallCollPointInfo collInfo = new SmallCollPointInfo(worldPos - body0Pos,
worldPos - body1Pos, depth);
collisionFunctor.CollisionNotify(ref info, ref delta, &collInfo, 1);
}
}
}
示例12: SegmentIntersect
/// <summary>
/// Must support intersection with a segment (ray cast)
/// </summary>
/// <param name="frac"></param>
/// <param name="pos"></param>
/// <param name="normal"></param>
/// <param name="seg"></param>
/// <returns>bool</returns>
public abstract bool SegmentIntersect(out float frac,out Vector3 pos,
out Vector3 normal,Segment seg);
示例13: SegmentIntersect
/// <summary>
/// SegmentIntersect
/// </summary>
/// <param name="frac"></param>
/// <param name="pos"></param>
/// <param name="normal"></param>
/// <param name="seg"></param>
/// <returns>bool</returns>
public override bool SegmentIntersect(out float frac, out Vector3 pos, out Vector3 normal, Segment seg)
{
// move segment into octree space
seg.Origin = Vector3.Transform(seg.Origin, invTransform);
seg.Delta = Vector3.TransformNormal(seg.Delta, invTransform);
BoundingBox segBox = BoundingBoxHelper.InitialBox;
BoundingBoxHelper.AddSegment(seg, ref segBox);
unsafe
{
#if USE_STACKALLOC
int* potentialTriangles = stackalloc int[MaxLocalStackTris];
{
#else
int[] potTriArray = DetectFunctor.IntStackAlloc();
fixed (int* potentialTriangles = potTriArray)
{
#endif
int numTriangles = GetTrianglesIntersectingtAABox(potentialTriangles, DetectFunctor.MaxLocalStackTris, ref segBox);
float tv1, tv2;
pos = Vector3.Zero;
normal = Vector3.Zero;
float bestFrac = float.MaxValue;
for (int iTriangle = 0; iTriangle < numTriangles; ++iTriangle)
{
IndexedTriangle meshTriangle = GetTriangle(potentialTriangles[iTriangle]);
float thisFrac;
Triangle tri = new Triangle(GetVertex(meshTriangle.GetVertexIndex(0)),
GetVertex(meshTriangle.GetVertexIndex(1)),
GetVertex(meshTriangle.GetVertexIndex(2)));
if (Intersection.SegmentTriangleIntersection(out thisFrac, out tv1, out tv2, seg, tri))
{
if (thisFrac < bestFrac)
{
bestFrac = thisFrac;
// re-project
pos = Vector3.Transform(seg.GetPoint(thisFrac), transformMatrix);
normal = Vector3.TransformNormal(meshTriangle.Plane.Normal, transformMatrix);
}
}
}
frac = bestFrac;
if (bestFrac < float.MaxValue)
{
DetectFunctor.FreeStackAlloc(potTriArray);
return true;
}
else
{
DetectFunctor.FreeStackAlloc(potTriArray);
return false;
}
#if USE_STACKALLOC
}
#else
}
#endif
}
}
示例14: SegmentIntersect
/// <summary>
/// Every skin must support a ray/segment intersection test -
/// operates on the new value of the primitives
/// </summary>
/// <param name="frac"></param>
/// <param name="pos"></param>
/// <param name="normal"></param>
/// <param name="seg"></param>
/// <returns>bool</returns>
public bool SegmentIntersect(out float frac, out Vector3 pos, out Vector3 normal, Segment seg)
{
Vector3 segEnd = seg.GetEnd();
frac = float.MaxValue;
float thisSegLenRelToOrig = 1.0f;
Segment segCopy = seg;
pos = normal = Vector3.Zero;
for (int prim = primitivesNewWorld.Count; prim-- != 0; )
{
float thisFrac;
Vector3 newPosition = pos;
if (primitivesNewWorld[prim].SegmentIntersect(out thisFrac, out newPosition, out normal, segCopy))
{
pos = newPosition;
frac = thisFrac * thisSegLenRelToOrig;
segCopy.Delta *= thisFrac;
thisSegLenRelToOrig *= frac;
}
}
//System.Diagnostics.Debug.WriteLineIf(frac <= 1.0f, pos);
return (frac <= 1.0f);
}
示例15: SegmentSegmentDistanceSq
/// <summary>
/// Returns the distance of two segments.
/// </summary>
/// <param name="t0">Parametric representation of nearest point on seg0.</param>
/// <param name="t1">Parametric representation of nearest point on seg0.</param>
/// <param name="seg0">First segment to test.</param>
/// <param name="seg1">Second segment to test.</param>
/// <returns>float</returns>
public static float SegmentSegmentDistanceSq(out float t0, out float t1, Segment seg0, Segment seg1)
{
Vector3 kDiff = seg0.Origin - seg1.Origin;
float fA00 = seg0.Delta.LengthSquared();
float fA01 = -Vector3.Dot(seg0.Delta, seg1.Delta);
float fA11 = seg1.Delta.LengthSquared();
float fB0 = Vector3.Dot(kDiff, seg0.Delta);
float fC = kDiff.LengthSquared();
float fDet = System.Math.Abs(fA00 * fA11 - fA01 * fA01);
float fB1, fS, fT, fSqrDist, fTmp;
if (fDet >= JiggleMath.Epsilon)
{
// line segments are not parallel
fB1 = -Vector3.Dot(kDiff, seg1.Delta);
fS = fA01 * fB1 - fA11 * fB0;
fT = fA01 * fB0 - fA00 * fB1;
if (fS >= 0.0f)
{
if (fS <= fDet)
{
if (fT >= 0.0f)
{
if (fT <= fDet) // region 0 (interior)
{
// minimum at two interior points of 3D lines
float fInvDet = 1.0f / fDet;
fS *= fInvDet;
fT *= fInvDet;
fSqrDist = fS * (fA00 * fS + fA01 * fT + 2.0f * fB0) +
fT * (fA01 * fS + fA11 * fT + 2.0f * fB1) + fC;
}
else // region 3 (side)
{
fT = 1.0f;
fTmp = fA01 + fB0;
if (fTmp >= 0.0f)
{
fS = 0.0f;
fSqrDist = fA11 + (2.0f) * fB1 + fC;
}
else if (-fTmp >= fA00)
{
fS = 1.0f;
fSqrDist = fA00 + fA11 + fC + (2.0f) * (fB1 + fTmp);
}
else
{
fS = -fTmp / fA00;
fSqrDist = fTmp * fS + fA11 + (2.0f) * fB1 + fC;
}
}
}
else // region 7 (side)
{
fT = 0.0f;
if (fB0 >= 0.0f)
{
fS = 0.0f;
fSqrDist = fC;
}
else if (-fB0 >= fA00)
{
fS = 1.0f;
fSqrDist = fA00 + (2.0f) * fB0 + fC;
}
else
{
fS = -fB0 / fA00;
fSqrDist = fB0 * fS + fC;
}
}
}
else
{
if (fT >= 0.0f)
{
if (fT <= fDet) // region 1 (side)
{
fS = 1.0f;
fTmp = fA01 + fB1;
if (fTmp >= 0.0f)
{
fT = 0.0f;
fSqrDist = fA00 + (2.0f) * fB0 + fC;
}
else if (-fTmp >= fA11)
{
fT = 1.0f;
fSqrDist = fA00 + fA11 + fC + (2.0f) * (fB0 + fTmp);
}
//.........这里部分代码省略.........