Original Post
Hi all
This is probably another of my simple to solve problems, but I'm having some real issues with this code. As the title suggests, it is to do with collision response. At the moment I am writing a very basic test app that has one triangle and one line (representing a vertex before and after a frame update). What I want the app to do is draw the line of reflection from the triangle (the after-collision-line if you like) so I can use that resulting vector to get the line (eventually an object) to move away from the triangle.
I have been following the tutorial at
http://www.gamasutra.com/view/feature/3427/collision_response_bouncy_.php?page=2
specifically the maths at the bottom of that page.
The snippet of code below is a conversion of the maths at the bottom of that page into C++
Hopefully it is fairly self explanatory. I have tried to copy the gamasutra maths as best I can but it returns silly values like:
Sorry for the length, I just wanted it to be comprehensive. The values given for the line and triangles are relatively tiny - 0 to 1 or thereabouts.
My question is this: what have I done wrong with that maths to get mad values like that?
Oh and erm any help would be very very much appreciated :)
Thanks for reading,
James
Ps. I will happily provide more infos if you need it, of course.
This is probably another of my simple to solve problems, but I'm having some real issues with this code. As the title suggests, it is to do with collision response. At the moment I am writing a very basic test app that has one triangle and one line (representing a vertex before and after a frame update). What I want the app to do is draw the line of reflection from the triangle (the after-collision-line if you like) so I can use that resulting vector to get the line (eventually an object) to move away from the triangle.
I have been following the tutorial at
http://www.gamasutra.com/view/feature/3427/collision_response_bouncy_.php?page=2
specifically the maths at the bottom of that page.
The snippet of code below is a conversion of the maths at the bottom of that page into C++
bool intersected = rayIntersectsTriangle(point, point2, colPosition, plane1, plane2, plane3); // Work out normal of plane vector(edge1, plane1, plane2); vector(edge2, plane3, plane2); crossProduct(normal, edge2, edge1); Vector v; vector(v, point, point2); Normalize(normal); // Vn = (N . V) * N Vn.x = (innerProduct(normal, v) * normal.x); Vn.y = (innerProduct(normal, v) * normal.y); Vn.z = (innerProduct(normal, v) * normal.z); // Vt = V - Vn Vt.x = V.x - Vn.x; Vt.y = V.y - Vn.y; Vt.z = V.z - Vn.z; if(intersected) { // V = Vt - Kr * Vn V.x = Vt.x - Kr * Vn.x; cout << V.x << ", " << V.y << ", " << V.z << endl; }Hopefully it is fairly self explanatory. I have tried to copy the gamasutra maths as best I can but it returns silly values like:
0.999993, 0, 01.99999, 0, 02.99998, 0, 03.99997, 0, 04.99996, 0, 05.99996, 0, 06.99995, 0, 07.99994, 0, 08.99994, 0, 09.99993, 0, 010.9999, 0, 011.9999, 0, 012.9999, 0, 013.9999, 0, 014.9999, 0, 015.9999, 0, 016.9999, 0, 017.9999, 0, 018.9999, 0, 019.9999, 0, 020.9998, 0, 021.9998, 0, 022.9998, 0, 023.9998, 0, 024.9998, 0, 025.9998, 0, 026.9998, 0, 027.9998, 0, 028.9998, 0, 029.9998, 0, 030.9998, 0, 031.9998, 0, 032.9998, 0, 033.9997, 0, 034.9997, 0, 035.9997, 0, 036.9997, 0, 037.9997, 0, 038.9997, 0, 039.9997, 0, 040.9997, 0, 041.9997, 0, 042.9997, 0, 043.9997, 0, 044.9997, 0, 045.9997, 0, 046.9996, 0, 047.9996, 0, 048.9996, 0, 049.9996, 0, 050.9996, 0, 051.9996, 0, 052.9996, 0, 053.9996, 0, 054.9996, 0, 055.9996, 0, 056.9996, 0, 057.9996, 0, 058.9996, 0, 059.9995, 0, 060.9995, 0, 061.9995, 0, 062.9995, 0, 063.9995, 0, 064.9995, 0, 065.9995, 0, 066.9995, 0, 067.9995, 0, 068.9995, 0, 069.9995, 0, 070.9995, 0, 071.9995, 0, 072.9995, 0, 073.9995, 0, 074.9995, 0, 075.9995, 0, 076.9995, 0, 077.9995, 0, 078.9995, 0, 079.9995, 0, 080.9995, 0, 081.9995, 0, 082.9995, 0, 083.9995, 0, 084.9995, 0, 085.9995, 0, 086.9995, 0, 087.9995, 0, 088.9995, 0, 089.9995, 0, 090.9995, 0, 091.9995, 0, 092.9995, 0, 093.9995, 0, 094.9995, 0, 095.9995, 0, 096.9995, 0, 097.9995, 0, 098.9995, 0, 099.9995, 0, 0101, 0, 0102, 0, 0103, 0, 0104, 0, 0105, 0, 0106, 0, 0107, 0, 0108, 0, 0109, 0, 0110, 0, 0111, 0, 0112, 0, 0113, 0, 0114, 0, 0115, 0, 0116, 0, 0117, 0, 0118, 0, 0119, 0, 0120, 0, 0121, 0, 0122, 0, 0123, 0, 0124, 0, 0125, 0, 0126, 0, 0127, 0, 0128, 0, 0129, 0, 0130, 0, 0131, 0, 0132, 0, 0133, 0, 0134, 0, 0135, 0, 0136, 0, 0137, 0, 0138, 0, 0139, 0, 0140, 0, 0141, 0, 0142, 0, 0143, 0, 0144, 0, 0145, 0, 0146, 0, 0147, 0, 0148, 0, 0149, 0, 0150, 0, 0151, 0, 0152, 0, 0153, 0, 0154, 0, 0155, 0, 0156, 0, 0157, 0, 0158, 0, 0159, 0, 0160, 0, 0161, 0, 0162, 0, 0163, 0, 0164, 0, 0165, 0, 0166, 0, 0167, 0, 0168, 0, 0169, 0, 0170, 0, 0171, 0, 0172, 0, 0173, 0, 0Sorry for the length, I just wanted it to be comprehensive. The values given for the line and triangles are relatively tiny - 0 to 1 or thereabouts.
My question is this: what have I done wrong with that maths to get mad values like that?
Oh and erm any help would be very very much appreciated :)
Thanks for reading,
James
Ps. I will happily provide more infos if you need it, of course.