General Category > General Anim8or Forum
WEEHEEEE oh, oh wait its dead, I thought it was dancing (CRE8OR NOTICE)
cooldude234:
Well still can't figure it out, so I'm going to freely post the code for anyone to take a look at it.
NOTE: this doesn't make the project open source ;) and this is only the portion of the code that checks a ray and plane collision (which is the part not working).
--- Quote from: code ---bool CheckLineTriangle(vector3f Vert1, vector3f Vert2, vector3f Vert3, vector3f Ray1, vector3f Ray2, vector3f &ContactPoint)
{
vector3f Normal;
vector3f IntersectPos;
// Finding Triangle Normal (by using crossproduct)
Normal = crossproduct (subtract_vector(Vert2,Vert1),subtract_vector(Vert3,Vert1));
// Find distance from ray1 and ray2 to the plane defined by the triangle
double Dist1 = dot_product(subtract_vector(Ray1,Vert1),Normal);
double Dist2 = dot_product(subtract_vector(Ray2,Vert1),Normal);
if ( (Dist1 * Dist2) >= 0.0f)
{
// line doesn't cross the triangle.
return false;
}
if ( Dist1 == Dist2)
{
// line and plane are parallel
return false;
}
// Find point on the line that intersects with the plane
IntersectPos.x = Ray1.x + (Ray2.x - Ray1.x) * (-Dist1/(Dist2-Dist1));
IntersectPos.y = Ray1.y + (Ray2.y - Ray1.y) * (-Dist1/(Dist2-Dist1));
IntersectPos.z = Ray1.z + (Ray2.z - Ray1.z) * (-Dist1/(Dist2-Dist1));
ContactPoint = IntersectPos;
}
--- End quote ---
And here's the math functions used in it
--- Quote ---float dot_product (vector3f A, vector3f B)
{
return ( (A.x * B.x) + (A.y * B.y) + (A.z * B.z));
}
vector3f crossproduct(vector3f A, vector3f B)
{
vector3f vector;
vector.x = (A.y*B.z)-(A.z*B.y);
vector.y = (A.x*B.z)-(A.z*B.x);
vector.z = (A.x*B.y)-(A.y*B.x);
return vector;
}
--- End quote ---
Also vector3f are just a 3 floating point vectors (in other words they are a variable that contains a x y and z)
cooldude234:
First off I just noticed this topic has 10901 views (wholy craps :o)
Now onto the original reason for posting; just a little bit on what I plan to do.So I plan on making this engine module based. Meaning you can take out and add in parts to fit your needs.
Because games (and programing) in general don't have one specific way of doing things and instead they have algorithms that they use to fit their specific needs; I'm not going to take a 'one size fits all' motto. However I do want the engine itself to be a universal tool to make a wide range and variety of games (from FPSs to RPGs, 2D plat-formers and even text adventures :D).
So module based is the way to go, and a good example of this would be shadows. For instance you wouldn't compute shadows the same way you would in a 3D First person shoot than a 2D rpg game would you? Of course not. So what I would do is I would make two algorithms for shadows, one that would take 3D space coordinates and one that would take 2D Coordinates and then compute them efficiently for each case.
Now there's a bit more to it than that (on the technical side), and I don't exactly know what direction I want to take this moduleness in :P. I might use separate DLLs for things like 2D physics and 3D lighting, 3D rendering etc. That way you can exclude whatever parts you are not using from the exe folder and it would run fine. However this is truly dependent on how big the exe is in the end (right now all the functions are compiled inside the exe). If the exe is something like 30 megabytes then I will make separate dlls for it.
Now you may be thinking but 30 megabytes is nothing for a hard drive (or ram) but that's not the issue. You see when you run a program, like an exe that program doesn't actually go into the ram (or is ran from the hard drive). It is put onto your CPU, well your CPU's built in memory anyway. And if your exe is too big and can't fit onto the cpu then it will go onto your ram card (and this SLOWS things down BIG time, it's not good to have). So in short the smaller the exe the better.
Well that's the small update I can give you, it's not much but it's a little insight to my plans for the future of this project.
NickE:
cooldude234,
Whenever I have tried to determine whether a line segment (like an edge) intersects a polygon, I have done it in two steps:
1) determine if and where the line segment intersects the plane of the polygon
2) determine if that intersection is actually inside the polygon
It is unclear from your code segment what Ray1 and Ray2 actually are, but if they are points that define a line segment, then this is how I would solve this problem:
facenormal=normalize(crossproduct(vert3-vert2,vert2-vert1)) //get normal of polygon plane
rayvector= ray2 - ray1 //get vector of ray line segment
ray_plane_angle=dot(facenormal,rayvector) //actually the arccos of the angle between
if ray_plane_angle != 0 //if zero, the two are parallel
ray_plane_intersection_point = ray2 + ((dot(vert1 - ray2,facenormal) / ray_plane_angle) * rayvector
//above, calculate the intersection point of vector of the line segment and plane
//next add angles of intersection point to two of the vertices at a time.
//if the angles add to 360 deg (or 2*pi rad) then inside, otherwise the intersection point is outside
total_angles= acos(dot(normalize(vert1-ray_plane_intersection_point),normalize(vert2-ray_plane_intersection_point)))
+acos(dot(normalize(vert2-ray_plane_intersection_point),normalize(vert3-ray_plane_intersection_point)))
+acos(dot(normalize(vert3-ray_plane_intersection_point),normalize(vert1-ray_plane_intersection_point)))
if (abs(total_angles - 2*pi) < 0.001) contact_point=ray_plane_intersection_point
If you are actually dealing with a line segment, rather than an endless line, then you need to check whether the intersection point is actually between ray2 and ray1:
if length(ray2-ray_plane_intersection_point)+length(ray1-ray_plane_intersection_point) > length(ray2-ray1)
//intersection point is not between ray2 and ray1
I wrote the code in pseudo code rather than a particular language.
I hope that helps.
NickE
cooldude234:
Thanks NickE, I'll try that later. ;) cheers
VirtuaroidPilot:
Is there a public beta/alpha release I can try? I'm a cre8or veteran, and I would love to get back to the scene. My best game was Dylan 64.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version