بتاريخ 01/ربيع الأول/1433 05:56 م، قطب Ali Amin حاجبيه بشدة وهو يقول:
مثل ما قال الاخ فراسفي 02/ربيع الأول/1433 05:03 ص، قال 16mofed84 بهدوء وتؤدة:
في فكره ..لتقليل الحساباتWorldToModel = ModelToWorld.Inverse();
public bool RayCastTestFast(Vector3 pos, Vector3 target, float dist, out Vector3 v, out Vector3 normal, bool fl)
{
v = Vector3.Zero;
float min = 9999999f;
Triangle Tmin = null;
Ray r = new Ray(pos, -Vector3.Normalize(target - pos));
Matrix TriangleTransfrom = new Matrix();
if (fl) r.Direction = -r.Direction;
BoundingSphere RSp = new BoundingSphere(pos, 4f);
foreach (CCMEntity ent in CCMEntities)
{
if (!ent.Rigid.BoundingBox.RayIntersect(eHelper.ToJVector(pos), eHelper.ToJVector(r.Direction))) continue;
Matrix trans = eHelper.ToXnaMatrix(ent.Rigid.Orientation);
trans.Translation = eHelper.ToXnaVector(ent.Rigid.Position);
Ray rr = new Ray(Vector3.Transform(r.Position, Matrix.Invert(trans)),
Vector3.Transform(r.Direction, Matrix.Invert(trans)));
for (int meshCount = 0; meshCount < ent.Meshes.Length; meshCount++)
{
float? rtest = r.Intersects(ent.Meshes[meshCount].Sphere);
if (rtest.HasValue)
{
for (int triCount = 0; triCount < ent.Meshes[meshCount].Triangles.Length; triCount++)
{
if(ent.Meshes[meshCount].Triangles[triCount].Sphere.Intersects(rr).HasValue)
{
Vector3 intersect = Vector3.Zero;
if (eHelper.RayToTriangle(ref r, out intersect,
ref ent.Meshes[meshCount].Triangles[triCount]))
{
if (Vector3.Distance(intersect, rr.Position) < min)
{
TriangleTransfrom = trans;
Tmin = ent.Meshes[meshCount].Triangles[triCount];
min = Vector3.Distance(intersect, rr.Position);
v = intersect;
}
}
}
}
}
}
}
if (Tmin != null)
{
Triangle ttt = new Triangle();
ttt.P1 = Tmin.P1;
ttt.P2 = Tmin.P2;
ttt.P3 = Tmin.P3;
ttt.Transform(TriangleTransfrom);
normal = (ttt.P1.Normal + ttt.P2.Normal + ttt.P3.Normal) / 3f;
return true;
}
else
{
v = Vector3.Zero;
normal = Vector3.Zero;
return false;
}
}
////////////////////////////////////////////////////////////////// ehelper class
public static bool RayToTriangle(ref Ray cursorRay, out Vector3 intersection,ref GameA1.Triangle t)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
float pickDist = 0.0f; // distance from ray origin to intersection
float pickU = 0.0f; // barycentric coordinate of intersection
float pickV = 0.0f; // barycentric coordinate of intersection
if (RayIntersectTriangle(ref cursorRay.Position,
ref cursorRay.Direction,ref t.P3.Position,
ref t.P2.Position,ref t.P1.Position, ref pickDist,
ref pickU, ref pickV))
{
if (pickDist > 0.0f)
{
intersection = pickU * t.P2.Position +
pickV * t.P1.Position +
(1 - pickU - pickV) * t.P3.Position;
return true;
}
}
intersection = Vector3.Zero;
return false;
}
private static bool RayIntersectTriangle(ref Vector3 rayPosition,
ref Vector3 rayDirection,
ref Vector3 tri0,
ref Vector3 tri1,
ref Vector3 tri2,
ref float pickDistance,
ref float barycentricU,
ref float barycentricV)
{
// Find vectors for two edges sharing vert0
Vector3 edge1 = tri1 - tri0;
Vector3 edge2 = tri2 - tri0;
// Begin calculating determinant - also used to calculate barycentricU parameter
Vector3 pvec = Vector3.Cross(rayDirection, edge2);
// If determinant is near zero, ray lies in plane of triangle
float det = Vector3.Dot(edge1, pvec);
if (det < 0.0001f)
return false;
// Calculate distance from vert0 to ray origin
Vector3 tvec = rayPosition - tri0;
// Calculate barycentricU parameter and test bounds
barycentricU = Vector3.Dot(tvec, pvec);
if (barycentricU < 0.0f || barycentricU > det)
return false;
// Prepare to test barycentricV parameter
Vector3 qvec = Vector3.Cross(tvec, edge1);
// Calculate barycentricV parameter and test bounds
barycentricV = Vector3.Dot(rayDirection, qvec);
if (barycentricV < 0.0f || barycentricU + barycentricV > det)
return false;
// Calculate pickDistance, scale parameters, ray intersects triangle
pickDistance = Vector3.Dot(edge2, qvec);
float fInvDet = 1.0f / det;
pickDistance *= fInvDet;
barycentricU *= fInvDet;
barycentricV *= fInvDet;
return true;
}