15
10/2010
Softbody Physics - Demo
Written by arthur
I have always been fascinated by physics simulation - in real time. And when games are mixing physics simulation with traditional gameplay, it give often really good games! My main references are locoroco, gish, world of goo and even bridge builder, this is what I tried to make possible with my own physics engine. I made a few things with that engine, but I didn't release the source code, mainly because it's not clean enough, but you can still enjoy the demos.
Blob - Nintendo DS
The goal of this demo was to be the closest of what we find in locoroco, but for the Nintendo DS. Unfortunately it doesn't succeed, but it's still nice to see physics simulation running on such limited hardware.
Download the demo.
Bridge Builder - World of Goo like - Nintendo DS
Well, this is an unfinished bridge-builder/world of goo like for the nintendo ds, you place points with the stylus and all the structure is moving.
Download the demo.
Blob Volley - Windows
Another example of what my engine is capable of. It was a game which was targeted to be released on gp2x, but I have never finished it. But you can still enjoy the windows work in progress!
Download the demo.
Technical tips
It is all about a spring/mass physics problem. I use a simple euler method for integration, it can be inacurate but the result is almost the same as a Runge-Kutta 4 method in a game. I also use a fixed timestep for the simulation, it is often more convenient if you don't think your simulation could have a drop in performance.
I think I can sum up a softbody physics engine by the code below. If you have any question or tips you want to share, don't hesitate to post a comment!
Blob - Nintendo DS
Download the demo.
Bridge Builder - World of Goo like - Nintendo DS
Download the demo.
Blob Volley - Windows
Download the demo.
Technical tips
It is all about a spring/mass physics problem. I use a simple euler method for integration, it can be inacurate but the result is almost the same as a Runge-Kutta 4 method in a game. I also use a fixed timestep for the simulation, it is often more convenient if you don't think your simulation could have a drop in performance.
I think I can sum up a softbody physics engine by the code below. If you have any question or tips you want to share, don't hesitate to post a comment!
typedef struct {
float px, py, vx, vy, ax, ay, mass, fx, fy, last_px, last_py;
} Mass;
typedef struct {
float d, k;
Mass* a;
Mass* b;
} Spring;
void mass_update(Mass *m, float dt)
{
float inv_mass = 1.0f / m->mass;
m->ax = m->fx * inv_mass;
m->ay = m->fy * inv_mass;
m->vx += m->ax * dt;
m->vy += m->ay * dt;
m->last_px = m->px;
m->last_py = m->py;
m->px += m->vx * dt;
m->py += m->vy * dt;
m->fx = m->fy = 0;
}
void spring_update(Spring* s)
{
float distance = sqrtf((s->a->px - s->b->px) * (s->a->px - s->b->px) + (s->a->py - s->b->py) * (s->a->py - s->b->py));
float delta_distance = distance - s->d;
if(distance > EPSILON)
{
float inv_distance = 1.0f / distance;
float ex = (s->a->px - s->b->px) * inv_distance;
float ey = (s->a->py - s->b->py) * inv_distance;
s->a->fx -= ex * s->k * delta_distance;
s->a->fy -= ey * s->k * delta_distance;
s->b->fx += ex * s->k * delta_distance;
s->b->fy += ey * s->k * delta_distance;
}
}
Classified in : Projects - Tags : physics, soft, simulation, blob, pc, c++ - no comments