Monday, December 14, 2009

Precision Collision Detection

I was playing around with Corey O'Neil's collision detection engine and while it is very good for working with most simple shapes; I was missing a crucial piece of information when it came to working with more irregular shapes.

What I'm referring to here is the 'normal of collision'.
With Corey's kit, we can acquire an 'angle of collision'; this angle is useful if working with circles because the angle of collision in this case is equivalent to the angle made by the collision normal.
In some more complex cases, however, the angle of collision doesn't give us the necessary information to determine the 'bounce angle'.

It's a bit difficult to explain, so I'll just show you.
Here is corey's kit (the red line shows the angle of collision):

^ The number of milliseconds it takes to calculate the collision is shown in the white textbox.

Basically, with Corey's kit, we can only get a precise angle of collision between two shapes if one of those shapes is a circle.

Here is my version:

Notice that now we can get an accurate bounce angle from the normal of collision.
Also, check out the improvement in speed from Corey's kit!
I'm considering releasing my code publicly, but I'll have to do a bit of code commenting first ;p

I followed Skinner's concept of using BitmapData to perform the collision testing and used a great deal of bitmap processing and vector math to evaluate the normal of collision. I still have to do a bit of tweaking to optimize performance. But so far it gets the job done.

Tuesday, December 8, 2009

Picking up some momentum

Things are slowly starting to take shape for my latest game.
I now have a fairly complete physics engine that manages collisions and their associated effects between combinations of solid and soft body objects.

I still need to go through my code and do some cleaning up to make it faster... Also, I should probably lower the dot-count for my deformable bodies; right now it's at around 200 points... Which is more than I need.

Anyways, here is what my work looks like thus far:

Press space bar to fire bullets.

I know that the collision detection is less than perfect - sometimes is passes straight through some of the narrow parts of the tree if the ball is moving fast. It'll just take some basic parameter tweaking to get it right (I.e. Limit the speed to a reliable rate and maybe make less air resistance so that it doesn't slow down too fast).

To save myself some time, I used Corey O'Neil's AS3 collision detection kit:

It's based on ideas by Skinner.

It records some useful data about collisions including the objects involved and angle of impact - this was a BIG time-saver.

Wednesday, December 2, 2009

Yet more progress on soft-body physics

Hopefully this is going to be the last update to my soft-body physics engine... I can't think of any ways to make it more realistic and efficient. Right now I'm very pleased with the effect. It's not completely accurate, but it looks very natural compared to some of the other engines I've seen on the web.

In this version, I introduced a concept of 'ambient force' which works on the same premise as 'ambient light' for people who are familiar with 3D. The reason for ambient light is that it would be too computationally expensive to simulate light beams bouncing around surfaces millions of times just to make things look natural; it's must better to just reproduce the effect of the physics as opposed to the physics itself.

Ambient force was made to simulate the effect of inter-molecular bonds that cause a molecule to pull its neighbors when a force is exerted upon that molecule. Instead of going through each point in the deformable body and simulating the effect of the bonds with its neighbors, I thought it would be much easier to just apply a scaled ambient force to every particle in the system; the closer it is to the center of mass, the less ambient force is applied (again this is because of the 'normal' force discussed in my previous post).

Anyway, here's the result: