Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Thu Jul 24, 2014 5:40 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Jul 03, 2013 2:35 am 
Rookie

Joined: Thu Jul 05, 2007 5:45 pm
Posts: 2
Hi guys

Ive been using xna for a while and have been trying to achieve the effect of particles flying from
a 3d car model as it moves across a multitextured heightmap terrain(2D texture)the way I was told to achieve this was to shoot a ray into the ground and have it return the coordinates of that specific texture I feel as though Im pretty close to achieving this, but one of the next things I was advised on to do is to transform a ray position into the object space of the terrain but with the research Ive done Ive only seen samples done with meshes. if anyone could advise me on how this is done using a heightmap terrain it would be much appreciated.(code below of project Ive been working on but wasnt sure on how to modify the the ray transform portion of it)

Thankyou





Code Listing: Expand
float? RayIntersectsModel(Ray ray,  Matrix modelTransform,
out Vector3 vertex1, out Vector3 vertex2,
out Vector3 vertex3)
{

vertex1 = vertex2 = vertex3 = Vector3.Zero;

Matrix inverseTransform = Matrix.Invert(modelTransform);

ray.Position = Vector3.Transform(ray.Position, inverseTransform);
ray.Direction = Vector3.TransformNormal(ray.Direction, inverseTransform);







// Keep track of the closest triangle we found so far,
// so we can always return the closest one.
float? closestIntersection = null;



for (int i = 0; i < terrain.TerrainVertices.Length -1; i += 3)
{
// Perform a ray to triangle intersection test.
float? intersection;

RayIntersectsTriangle(ref ray,
ref terrain.TerrainVertices1[i],
ref terrain.TerrainVertices1[i + 1],
ref terrain.TerrainVertices1[i + 2],
out intersection);

// Does the ray intersect this triangle?
if (intersection != null)
{
// If so, find the cloest one
if ((closestIntersection == null) ||
(intersection < closestIntersection))
{
// Store the distance to this triangle.
closestIntersection = intersection;

// Transform the three vertex positions into world space,
// and store them into the output vertex parameters.
Vector3.Transform(ref terrain.TerrainVertices1[i],
ref modelTransform, out vertex1);

Vector3.Transform(ref terrain.TerrainVertices1[i + 1],
ref modelTransform, out vertex2);

Vector3.Transform(ref terrain.TerrainVertices1[i + 2],
ref modelTransform, out vertex3);
}
}
}

return closestIntersection;
}


void RayIntersectsTriangle(ref Ray ray,
ref Vector3 vertex1,
ref Vector3 vertex2,
ref Vector3 vertex3, out float? result)
{
// Compute vectors along two edges of the triangle.
Vector3 edge1, edge2;

Vector3.Subtract(ref vertex2, ref vertex1, out edge1);
Vector3.Subtract(ref vertex3, ref vertex1, out edge2);

// Compute the determinant.
Vector3 directionCrossEdge2;
Vector3.Cross(ref ray.Direction, ref edge2, out directionCrossEdge2);

float determinant;
Vector3.Dot(ref edge1, ref directionCrossEdge2, out determinant);

// If the ray is parallel to the triangle plane, there is no collision.
if (determinant > -float.Epsilon && determinant < float.Epsilon)
{
result = null;
return;
}

float inverseDeterminant = 1.0f / determinant;

// Calculate the U parameter of the intersection point.
Vector3 distanceVector;
Vector3.Subtract(ref ray.Position, ref vertex1, out distanceVector);

float triangleU;
Vector3.Dot(ref distanceVector, ref directionCrossEdge2, out triangleU);
triangleU *= inverseDeterminant;

// Make sure it is inside the triangle.
if (triangleU < 0 || triangleU > 1)
{
result = null;
return;
}

// Calculate the V parameter of the intersection point.
Vector3 distanceCrossEdge1;
Vector3.Cross(ref distanceVector, ref edge1, out distanceCrossEdge1);

float triangleV;
Vector3.Dot(ref ray.Direction, ref distanceCrossEdge1, out triangleV);
triangleV *= inverseDeterminant;

// Make sure it is inside the triangle.
if (triangleV < 0 || triangleU + triangleV > 1)
{
result = null;
return;
}

// Compute the distance along the ray to the triangle.
float rayDistance;
Vector3.Dot(ref edge2, ref distanceCrossEdge1, out rayDistance);
rayDistance *= inverseDeterminant;

// Is the triangle behind the ray origin?
if (rayDistance < 0)
{
result = null;
return;
}
result = rayDistance;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group