BrainBlitz.org

September 18, 2008

Verlet Forays

Filed under: BoxCAD, Programming — Anthony @ 8:18 am

Before there was BoxCAD…

There was…

RBS! (Rigid Body Simulator)

About a year ago, I had an idea for a funny little game in Visual Basic (yes, Visual Basic). What was supposed to take a few hours ended up taking most of 2008…

My game concept depended on realistic physics – but I didn’t know the first thing about physics. So I decided to tackle that problem separately, before trying to build the game. So, out came my notebook, and dozens of arduous trig equations later – I’d gotten nowhere.  So – to Google I went (I usually make an effort to figure stuff out before hitting the The Oracle – it’s good for my self-esteem).

It wasn’t long before I stumbled upon the infamous Paper by Thomas Jakobsen on Verlet integration (as a fast, though not-as-accurate alternative to Euler).

Euler integration is what you get if you take the physics equations out of a physics textbook and plunk ‘em down in your code. It’s conceptually straightforward (though devilish details abound). Verlet integration was immediately fascinating to me because it was very non-intuitive. I had to work through several iterations of a point’s movement just to see what was going on.

Verlet integration simplifies a whole lotta crap in certain areas. The major factor is – a node can be manually translated arbitrarily, and the algorithm will factor in the new position, automatically handling velocity, rotation (for a multi-node body), etc. This differs from Euler where – if you manually translate a body, you’ve gone done screwed up the equations, and the results are unpredictable. The correct way to modify position in a Euler system is indirectly, by applying forces to the node as necessary to change it’s acceleration, therefore velocity, therefore position.

Two Nodes with a Distance ContraintWhat does this ability to manually translate nodes mean for us? For me, the important factor was – massively simplified constraints. My objective with the verlet engine was to be able to create distance constraints, thus ragdoll systems. Looking at the image to the left, a distance constraint is an interaction between two nodes that attempts to maintain a constant separation. The blue line in the image is the constraint. The basic idea is – after each constraint-free position-computation by the engine, all nodes/bodies will be in a new location; the next step in the physics engine is to check how those new positions conform to the constraints. Position correction is applied to those nodes/bodies who are more or less than the specified distance apart.

In a Euler system, you would apply a corrective forceto the offending nodes, since you’re  not allowed to manually move them. Performing the necessary computations is no walk in the park. Verlet, on the other hand, doeslet you manually translate nodes – which means, to maintain the constraint, you do something like this:

Step 1: Find the difference between the current distance between the nodes, and the specified constraint distanced.
Step 2: Distribute half of that difference to each node (assuming node masses are the same).

If you’re constraint is specified as 5 units, and after computing position, the distance between the nodes is 6 units – then you pull both nodes in by 0.5 units (half of the difference).

And that’s it. The integration takes care of forces, acceleration, momentum, rotation – everything.

I very distinctly the first run after getting my verlet implementation to compile for the first time. It was truly eerie, seeing such complex behavior come out of so little code!

In a system with many inter-related constraints (see image to the right), it requires several passes over the correction algorithm to converge on the system’s solution satisfactorily. That’s not really a problem though, as the algorithms are very fast.

ANYway, after plugging away at my verlet engine for a couple of months, I had RBS to show. At that point, I was in love with physics engines, and had completely forgotten my “few hours of coding” game idea.

With the original, more advanced RBS (Visual Basic), there are some ideas I’d actually like to explore, if I ever get back to it. In the image (at the top of the post), the nodes are colored by velocity. Playing with different configurations of colors/constraints/physics can produce some very mesmorizing and interesting effects.

Somewhere in there, I took on the task of learning ActionScript – Flash’s language. Before long there was the flash-based, simplified RBS (link to source at the bottom of this post). It didn’t take long to realize I was wasting my time with AS2. So I upgraded to AS3. That opened up a few doors. Among them was the availability of open-source physics engines. Out of curiosity, I downloaded and played with a couple – APE and Box2D. I didn’t care for APE, as it had some quirky collision handling, with shapes getting stuck inside one-another frequently (if they were moving fast). For that simple – and probably easily correctable - reason, I tilted towards Box2D.

I built a small “practice” project to experiment with. Suddenly, it is 7 months later, and BoxCAD consists of about 37 files and thousands of lines of code – by far the most complex project I’ve experienced.

BoxCAD is a very exciting project, and I have big plans for it. But sometimes – it’s therapeutic to mess around with the very simple RBS flash app. Try it. :D   And if you’re curious, here’s the verlet source code. Just  keep in mind – this was a “Flash Initiation” project, so it’s pretty durn sloppy.

External Links:
http://www.teknikus.dk/tj/gdc2001.htm - Advanced Character Physics (Thomas Jakobson)
http://box2d.org/ - home of the Box2D Physics Engine – courtesy of Erin Catto
http://www.cove.org/ape/ - APE physics engine – which I have yet to really explore.

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

September 12, 2008

I have donated money to something

Filed under: Uncategorized — Anthony @ 12:38 am

I couldn’t help it. My hands involuntarily moved and clicked the mouse, entered my paypal login stuff, clicked some more, and suddenly – just as I realized the gravity of what was occurring, it was over.

 And what, you ask, prompted appreciation so intense – so primal – that my response was executed as a low-level brain-stem function rather than up there in the prefrontal cortex?

 Icons baby. 700 fantastic, generic, free, CC licensed Icons.

 I will now go build sweet gui’s for all of my websites. Even websites that don’t need guis.

 Thank you Mark!

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks

September 6, 2008

The Information SuperLandfill (and other assorted thoughts)

Filed under: Uncategorized — Tags: , , , , — Anthony @ 1:59 am

We commercialize the goodness out of EVERYthing. First Christmas, and now search engines. 

Search results for most topics would instantly become far more valuable by simply removing all results that contained anything like “store,” “catalog,” “shop,” “dealer,” etc. Even a simple algorithm that threw lots and lots of babies out with the bathwater would make the search engines far more useful.

***

If you go to Google and type “yahoo”, Google lists search results no differently than usual. But if you go to Yahoo and type “google” – you get a big “Search the Web with Yahoo!” with a conveniently placed search box, inserted obtrusively-prior to the actual search listings. I think someone is feeling insecure.

 ***

I came across this post by Dave Winer a few days ago – who apparently is considered the “father” of XML. I got to it via the official “JSON” website. I’ve never liked XML – but for pretty superficial reasons. I know very little about it. And I know nothing about JSON… or rather, knew nothing. That’s why I was ON the json site - to see what the buzz was all about. But anyway, that’s all beside the point. Dave’s post is what stole the show. And I quote:

“Gotta love em, because there’s no way they’re going to stop breaking what works, and fixing what don’t need no fixing.”

“No doubt I can write a routine to parse this, but look at how deep they went to re-invent, XML itself wasn’t good enough for them, for some reason (I’d love to hear the reason). Who did this travesty? Let’s find a tree and string them up. Now.”

“Ladies and gentlemen, boys and girls, this is why I’m fed up with Mountain View, Cupertino, Redwood Shores and Redmond. Give me Berkeley and New York any day….”

Then, a few days ago, I was reading a book (can’t quite remember the name….) on “Intelligent Design.” Again, the topic itself is completely beside the point. What struck me was a quote by Richard Dawkins, among the most prominent and outspoken of evolutionary biologists:

“It is absolutely safe to say that if you meet somebody who claims not to believe in evolution, that person is ignorant, stupid or insane (or wicked, but I’d rather not consider that).”

I have a very vivid mental image pop into my head when I hear adults reacting like that… of a two year old - snot dripping down over his upper lip; a kind of dumb, belligerent look on his face… defending his shiny plastic toy from another two-year old (who may or may not have that dumb, belligerent look).

“And that’s all I’ve got to say about that.”

***

There is a very distinct difference in the way I learned QBasic (when I was 14 or so) , and the way I currently learn new languages/topics.

AutoCAD is a very irritating program.

Even trusty ole’ PHP, with it’s $_POST and $HTTP_POST_VARS… and str_pad() versus strlen()…

Lack of authority, you see, is the common thread. When I learned QBasic, I had maybe heardthe term “internet” or “world wide web” – but when it was time to sit down and do some hardcore nibbles coding – it was just me and the help file. Sometimes maybe an outdated book from the library. Amazingly, I learned so much faster. I didn’t produce as much, mind you – at least not initially.

NOW I have this tendency to grab & hack the first example I find where someone did something similar to what I’m going for. Yes, it does get that little chuck of functionality out of the way real quick. But it’s like building a structure with an assortment of Lego’s, erector set pieces and wooden blocks. The pieces might stack a little at first… but wait till you’re trying to build stairs on the fourth story. That’s when it all starts to fall apart.

AutoCAD is a huge application. Part of it’s hugeness is a result of a massively open API and customization capability. I mean, NOTHING is hard-coded. Menu items, menu accelerators… every last function in AutoCAD can be goofed with, using tools within the application (“meta-tools, we shall call them), or by editing script and data files (which are sprinkled to the four corners of the hard-drive… seriously, it’s maddening trying to remember where all the buggers are).

ANYway, that massive openness to outside development mean a lot of third-parties developed rather large applications that ran within AutoCAD. Naturally, AutoDESK bought them all out, and assimilated them directly into several main applications, all built on top of AutoCAD.

And when you stick that many pieces together from that many different sources – many with overlapping functionality and no standard interface – you get a mess. A buggy, crash-prone mess. There are at least 4 ways to perform most functions in AutoCAD.

The final result is that a modern set of architectural/civil/structural plans looks like crap, because no one does it the same way. Heck, most people don’t even do it the same way twice.

And PHP – I don’t really know the story there, and it’s far more subtle than my learning patterns and AutoCAD – but that disorganization is there.

Decentralization – or de-authoritization – or whatever you want to call it - does lead to theoretically better design (combining the brains of many). But the problem is – doing something one way – even if not the most optimistic way – seems to generally produce better results than producing things the “best” way, but without standards or “best practices.”

One last example – very recent. I had a rather large javascript application to build, and decided to tackle a framework rather than paste it all together like I usually do. After researching several – I settled on jQuery.

I LOVE that almost any functionality I realize I need has been implemented as a plug-in. But by the time I had everything I need, I had a complex web of dependencies, and many of the add-ons have quirky usage.

I still love it of course, it was just annoying.

 Hmmm, this post just got to long.

  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Bookmarks
« Newer Posts

Powered by WordPress