Jump to content
  • Advertisement
Sign in to follow this  
jamwaffles

Collision response maths problems

This topic is 2909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++

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, 0
1.99999, 0, 0
2.99998, 0, 0
3.99997, 0, 0
4.99996, 0, 0
5.99996, 0, 0
6.99995, 0, 0
7.99994, 0, 0
8.99994, 0, 0
9.99993, 0, 0
10.9999, 0, 0
11.9999, 0, 0
12.9999, 0, 0
13.9999, 0, 0
14.9999, 0, 0
15.9999, 0, 0
16.9999, 0, 0
17.9999, 0, 0
18.9999, 0, 0
19.9999, 0, 0
20.9998, 0, 0
21.9998, 0, 0
22.9998, 0, 0
23.9998, 0, 0
24.9998, 0, 0
25.9998, 0, 0
26.9998, 0, 0
27.9998, 0, 0
28.9998, 0, 0
29.9998, 0, 0
30.9998, 0, 0
31.9998, 0, 0
32.9998, 0, 0
33.9997, 0, 0
34.9997, 0, 0
35.9997, 0, 0
36.9997, 0, 0
37.9997, 0, 0
38.9997, 0, 0
39.9997, 0, 0
40.9997, 0, 0
41.9997, 0, 0
42.9997, 0, 0
43.9997, 0, 0
44.9997, 0, 0
45.9997, 0, 0
46.9996, 0, 0
47.9996, 0, 0
48.9996, 0, 0
49.9996, 0, 0
50.9996, 0, 0
51.9996, 0, 0
52.9996, 0, 0
53.9996, 0, 0
54.9996, 0, 0
55.9996, 0, 0
56.9996, 0, 0
57.9996, 0, 0
58.9996, 0, 0
59.9995, 0, 0
60.9995, 0, 0
61.9995, 0, 0
62.9995, 0, 0
63.9995, 0, 0
64.9995, 0, 0
65.9995, 0, 0
66.9995, 0, 0
67.9995, 0, 0
68.9995, 0, 0
69.9995, 0, 0
70.9995, 0, 0
71.9995, 0, 0
72.9995, 0, 0
73.9995, 0, 0
74.9995, 0, 0
75.9995, 0, 0
76.9995, 0, 0
77.9995, 0, 0
78.9995, 0, 0
79.9995, 0, 0
80.9995, 0, 0
81.9995, 0, 0
82.9995, 0, 0
83.9995, 0, 0
84.9995, 0, 0
85.9995, 0, 0
86.9995, 0, 0
87.9995, 0, 0
88.9995, 0, 0
89.9995, 0, 0
90.9995, 0, 0
91.9995, 0, 0
92.9995, 0, 0
93.9995, 0, 0
94.9995, 0, 0
95.9995, 0, 0
96.9995, 0, 0
97.9995, 0, 0
98.9995, 0, 0
99.9995, 0, 0
101, 0, 0
102, 0, 0
103, 0, 0
104, 0, 0
105, 0, 0
106, 0, 0
107, 0, 0
108, 0, 0
109, 0, 0
110, 0, 0
111, 0, 0
112, 0, 0
113, 0, 0
114, 0, 0
115, 0, 0
116, 0, 0
117, 0, 0
118, 0, 0
119, 0, 0
120, 0, 0
121, 0, 0
122, 0, 0
123, 0, 0
124, 0, 0
125, 0, 0
126, 0, 0
127, 0, 0
128, 0, 0
129, 0, 0
130, 0, 0
131, 0, 0
132, 0, 0
133, 0, 0
134, 0, 0
135, 0, 0
136, 0, 0
137, 0, 0
138, 0, 0
139, 0, 0
140, 0, 0
141, 0, 0
142, 0, 0
143, 0, 0
144, 0, 0
145, 0, 0
146, 0, 0
147, 0, 0
148, 0, 0
149, 0, 0
150, 0, 0
151, 0, 0
152, 0, 0
153, 0, 0
154, 0, 0
155, 0, 0
156, 0, 0
157, 0, 0
158, 0, 0
159, 0, 0
160, 0, 0
161, 0, 0
162, 0, 0
163, 0, 0
164, 0, 0
165, 0, 0
166, 0, 0
167, 0, 0
168, 0, 0
169, 0, 0
170, 0, 0
171, 0, 0
172, 0, 0
173, 0, 0



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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!