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:

Monday, November 30, 2009

More progress on soft-body physics

I made a lot of progress with my soft-body physics engine.
Now the motion looks a lot more natural (if you get the settings right) and you can also do some seriously weird effects.

Now, I've made it so that points which are further away from the center of mass will be affected by more force (this is similar to real-world situations where there is usually more normal force to counterbalance external forces near the center of mass than other areas). This gives the Soft-body a 'wavy' feel.

Press any key to tone-down motion:

The 3 parameters that you can edit here are only a very small fraction of the properties that can now be modified through this engine. In this case the plant was generated such that there is a significant, linear distribution of weight from top to bottom; most of the weight is at the bottom (thus the center of mass is brought closer to the base). This makes things really wavy and it looks as if it is underwater. Each point on the shape is affected by tiny amounts of gravity, but the gravity on the shape itself was set to 0 (so that it would not fly-off the screen).

Here's a more realistic version:

The plant was drawn with a very basic engine which converts hand-drawn lines to a set of soft-body points in code. I plan to make this soft-body generator more robust after I finish the game that I'm currently working on.

Saturday, November 28, 2009

Back to Flash

I've been doing some work in Flash using ActionScript 3.
I've started planning for a whole series of online games that will revolve around a common theme.

So far, I've created some engines including a flexible application page-manager and a pretty cool soft-body physics library that allows soft-body objects to react to physical 'force fields'.

Here's a link (Press any key to reset)

The math used there is fairly heavy. It uses many physical concepts surrounding motion, mass, elasticity (Hooke's law) and linear algebra. It's somewhat physically accurate except for air resistance (I cheated a bit there)... Physicists hate air resistance anyway so who cares.

As you might notice, the body is in a zero-gravity environment with so-called 'air resistance'.
The body is made up of special points of a given mass which react to force vectors, the body itself reacts to 'force beams' which vary in magnitude, area of effect and dissipation effects (distance from source and distance from center of beam).

Obviously there are missing physical elements such as dissipation of force via contact but on the whole it's a nice addition to my portfolio.

Sunday, April 26, 2009

Introduction

Hello World.
I will be using this blog as a means of logging and reflecting upon my software engineering work and also to put forward some theories I may have about software design, programming etc...