Motor Flash Physics: fancy buoyancy

August 30, 2006 on 2:33 pm | In Actionscript, Physics | 2 Comments

Buoyancy is a force that acts on a body which is immersed in a fluid. This force can be divided into a static and a dynamic component. The static component acts on a body that rests in a fluid - it determines if a body floats or sinks and it always points in the opposite direction of gravity, whereas the dynamic component is always directed against the movement of the body, so it can be used for a ship that moves horizontally or a body that drops from a height into the water.

The static force is equal to the weight of the displaced water, which can be calculated by clipping the polygon against the plane that defines the water level. An efficient way of doing this is to triangulate the polygon and clip each triangle to the water plane, which is faster than clipping the complete polygon against the plane. Thanks to Erin Catto for pointing that out.

The dynamic force acts as a damper and is proportional to the velocity of the body in the fluid. To calculate this physically correct would be overkill because you would have to use drag. So its just approximated by using the submerged area. And of course all forces act on the centroid (center of mass).

Since I am still using the slow (non-optimized ;-)) AS2 version of the motor engine I provided a flash-projector for the demo so you can play around with the demo at a descent speed (try to stack the boxes on top of each other). When the AS3 port is finished (hopefully soon) I will recompile and enhance all demos.

Now watch (popup) or download the buoyancy demo!

Operator overloading

August 29, 2006 on 2:40 pm | In Actionscript | No Comments

I am updating and converting my physics code to AS3 at the moment, and one thing I really miss in AS3 is operator overloading which would make your code more legible, especially when you are using vectors and matrices a lot.

Here you sum two vectors, multiply the result by 5 and finally make the vector perpendicular to itself. This not only invokes multiple function calls, but it produces “spaghetti-code” that is hard to read.

vec_a.getSum(vec_b).getMul(5).getPerp(); // PERP((A + B) * 5)

Hence static functions are better and the parenthesis structure is easier to recognize:

Vector.perp(Vector.mul(Vector.sum(vec_a, vec_b), 5));

This approach has the benefit that you can store the function in a local variable (or pass as a parameter) and gain a little speed in AS2 if it is invoked a lot.

var mul:Function = Vector.mul;
var sum:Function = Vector.sum;
var perp:Function = Vector.perp;

for (var i:Number = 0; i < 100; i++)
{
    perp(mul(sum(vec_a, vec_b), 5));
}

Again, this becomes very nested if many expressions are concatenated.

On the other hand some languages like C++ come with a feature called operator overloading, where you assign a function to an operator:

// adding two vectors
Vector operator + (const Vector &V) const {return Vector(x+V.x, y+V.y);}
// multiply by scalar
Vector operator * (float  s)        const {return Vector(x*s, y*s);}
...

So now the expression simply becomes:

perp((vec_a + vec_b) * 5);

Sadly, AS3 doesn’t support such a thing, so you have to stick with static functions and simply expand the vector into its components when it gets messy:

result.x = (vec_a.x + vec_b.x) * 5;
result.y = (vec_a.y + vec_b.y) * 5;

result.perp();

This is OK for 2 dimensions, but in 3 dimensions, you start to write many lines… ;-)

Fun with springy strings

August 22, 2006 on 11:36 am | In Actionscript, Physics | 6 Comments

Yesterday I realized how easy it is to create deformable bodies. It’s just a bunch of mass points, and each mass point is connected by one or more springs. Although traversing the spring/node system is a bit tricky, the overall implementation is simple.

I created a demo showing a string (or rope). The same concept applies to cloth (2D grid-like structure of spring nodes) and jelly (3D grid-like structure of spring nodes). You can drag the end-points around, and adjust two basic properties: stiffness and damping.

Stiffness (spring constant k) defines how much force each spring can apply to its nodes, and damping is used to simulate energy loss. A value of 1 means no energy loss (spring will swing forever), and 0 that the system’s energy will decay rapidly.

It gets a bit unstable for extreme values because I am using an Euler integrator for simplicity, which has limited use for springs. If the system is getting crazy, apply a high damping and low stiffness.

String demo (popup)

Calculating the Moment of Inertia of a non-regular convex Polygon

August 17, 2006 on 10:00 pm | In Actionscript, Physics | 20 Comments

The moment of inertia is needed to calculate the rotational movement of a body. You can think of it as the rotational equivalent of mass. To calculate the moment of inertia you have to integrate the area (volume in 3D) of the polygon in respect to the rotational axis (most often than not an axis through the center of mass). Integrating is slow and hard to do with an arbitrary polygon, so here is an easy way to do just that.

First the polygon is triangulated by fanning around the centroid (center of mass), so that the centroid is part of every triangle.

convex polygon inertia

triangle inertia

Now the centroid of the triangle and the moment of inertia of the triangle about an axis through the center of mass of the triangle ( try to say that ten times fast in a row) has to be calculated.

convex polygon inertia

But the original polygon rotates about an axis through the centroid of the original polygon, so we have to use the parallel axis rule. The rule states that the moment of inertia about an axis parallel to one through the centroid can be calculated by adding the mass ( in this case we use the area of the triangle as mass) multiplied by the squared shortest distance to the parallel axis.

convex polygon inertia

The last step is to sum-up all moments of inertia of the triangles and you are done.

Motor flash physics development update

August 14, 2006 on 3:15 pm | In Actionscript, Physics | 11 Comments

What I have done the last days:

- contact point calculations
- two collision response models (simple & fast + physical accurate)
- different integrators (Euler, Verlet, Mid-Point, RK4)
- basic forces (gravity, spring, attractor, repulsor..)
- dynamic and static friction

So basically the engine is working now. There are some small bugs when handling multiple simultaneous contacts which I’ll solve later. Now it’s time to build a framework, optimize and clean-up the code and convert it to AS3.

I’ll post some new demos the next day, but here is a small preview: Scene A, Scene B

Next Page »

Proudly powered by WordPress Theme based upon Pool theme by Borja Fernandez.
Entries and comments feeds.