Thursday, December 16, 2010

Buggy buggy buggy

In implementing some semblance of physical motion simulation, I started bungling together a few different methods for performing various tasks, and subsequently they didn't meld very well. I came upon a particular bug which would eventuate when an object collided with the ground. It didn't occur for every ground collision, just a few random ones. What would happen is that the object would disappear, but I was able to track that it still had a position and velocity that should render it on the screen.

Turns out that the angular velocity was undefined as a result of a divide-by-zero, and rotating the object at an undefined angle rendered it... unrenderable... I think that's a word. So I put in a catch so that it wouldn't do that anymore.

I had another bug in which multiple points of an object would be underground (thus triggering a collision). What one would want most ideally would be to use the vertex that has penetrated the ground deepest as the point of impact. What was occurring in practice was that once a submerged vertex was found, the method would exit and use that vertex as the point of impact.

The problem with this is that when a collision is detected with the ground, I bump the object up out of the ground by how deeply it's penetrated the ground (based on the vertex used as the point of collision). As well, the velocity is adjusted per a bounce. After that, though, there is still a deeper point that is submerged in the ground. In the next frame, that will be detected as a point of collision, and when the velocity is reflected off the ground, it moves down instead of up.


This results in the object infinitely bouncing up and down within the ground and flying off to infinity in the x-z plane.

But I fixed it! Now, instead of breaking as soon as a colliding vertex is found, I continue with the rest and find which of them has penetrated the ground deepest, and use that as the point of collision. Magnifique.

Objects are bouncing around with improved behavior, but they're still not perfect. For instance, the imbued rotation from a collision is inaccurate, largely because I'm not implementing it with any degree of physical accuracy. I may or may not be using an entirely ill-conjured and wholly suppositious equation based not on physics but on convenience. And, by that, I mean that I am using an entirely ill-conjured and wholly suppositious equation based not on physics but on convenience.

I need to fix that.

No comments:

Post a Comment