CoffeePhysics: A JS/CS Physics Engine
soulwire.co.uk
soulwire.co.uk
Vectors: https://github.com/soulwire/Coffee-Physics/blob/master/sourc...
Collision Detection: https://github.com/soulwire/Coffee-Physics/blob/master/sourc...
edit: hah, just saw who i replied to.
double edit: the renderer hierarchy he created is yet another good example of why I believe coffeescript needs interfaces...
> the renderer hierarchy he created is yet another good example
> of why I believe coffeescript needs interfaces...
What exactly are you thinking of here? Because JS is as flexible as you can be in terms of duck-typing and calling any function with any number of arguments -- I don't see what having an explicit "interface" construct would gain you.For example, a rich "collection" interface that has some helper functions for key:value hash-like collections, but that a more array- or set-like subtype doesn't have to implement.
If you forget to implement a method that you later try to use, you'll find out when you try to use it. Such is the nature of the beast.
https://developers.google.com/closure/compiler/docs/js-for-c...
I guess a better question for me to ask here is:
Is there any good in depth guide to the current javascript compilers so that library design decisions can be made, or are we basically restricted to reading the source code/ making a guess and checking with a profiler?
http://www.pagines.ma1.upc.edu/~susin/files/AdvancedCharacte...
The first demo reminded me of a collision-detection D3 demo I made last year. This one uses a quadtree to accelerate collision-detection, as well as another quadtree for the Barnes–Hut approximation of charge forces:
http://mbostock.github.com/d3/talk/20110921/collision.html
More here: http://vimeo.com/29458354
https://github.com/soulwire/Coffee-Physics/blob/master/sourc...
... compared to your own approach:
https://github.com/mbostock/d3/blob/master/src/layout/force....
https://github.com/mbostock/d3/blob/master/src/layout/force....
The rest of the function is calculating default forces and constraints for graph layout; these can be disabled, and thanks to the resilience of Verlet integration, you can easily implement custom forces or constraints in your "tick" event listener (as Shan Carter did in the budget piece a few weeks ago).
Given that it's a generalized physics engine, I like that @soulwire's code is organized into clean, modular units. That makes it easier to test and modify the internals. D3's force layout is specifically-tailored to graph layout, so I don't consider it necessary to make the implementation so modular; the requirements of the force layout are that it is fast by default, that it can be customized with incremental additional effort, and that it is convenient for the common cases.
Also, with larger graphs numerical integration is one of the few places where JavaScript performance (not just rendering and DOM manipulation) actually matters; while unfortunate, it's often necessary to make a trade-off between generality and performance. Still, I'm considering more modular reusable forces and constraints for a future iteration of the force layout (perhaps similar to the older Protovis force layout). But since custom forces tend to be very… well… custom, I think the best option is simply to modify the nodes' positions as needed and let Verlet do the rest.
The loop should look like this:
for (i = 0; i < n; ++i)
for (j = i + 1; j < n; ++j)TypeError: 'undefined' is not a constructor (evaluating 'new Date.getTime()')
nobody says coffeescript isn't nicer than javascript, but some people say its not so much nicer that it's worth the "abstraction tax" - compare to C vs assembly where nobody questions that the abstraction is worth it.
But my personal answer is that I have, say, 50% more fun writing CS than JS, so there are likely to be personal projects I write in CS that I just wouldn't have bothered with or would have lost steam on before. When you're doing something for the love of it, every moment that makes you think "dammit [Javascript], why are you making me do this?" is a potential moment to walk away and do something more fun. I (begrudge;every;semicolon) in an unnecessary for loop.
So if other people are like me, I expect CS to bring new things to the world, not because it's 10% faster but because the 10% it's taking out was the boring part. If no one's like me, then I hereby award myself one Special Snowflake from the many falling outside my window.
To put it another way: "The single most important lesson that people say they have learned from the Ruby programming language is a lesson that _Why’s work embodies in its code: Programming (or whatever you do) should be fun. There must be joy in your craft, and there is precious value in tinkering and playing around."[1]
Whether CoffeeScript or wire-wrapping individual transistors lights up your eyes is up to you of course -- but we all benefit by giving creators tools they like. Sermon for today over.
[1] http://www.smashingmagazine.com/2010/05/15/why-a-tale-of-a-p...
Traditionally JS applications have never been very "heavy" so it's never been a problem as you could just use the other cores to run JS for other browser tabs etc.
Of course now both cores and JS usage are increasing so this will have to change.
This makes possible share the calculations across the cores, but since postMessage() serializes each transferred object the effectiveness of this might not be that good.
Depending on the how much cross talk there would be between the threads, web workers might not be adequate for some algorithms anyway as the message passing (the only way web workers can communicate, there is no "shared memory" access or other such short cuts to communication) could add noticeable latency. Caveat: I've not used them for anything myself so I don't know if any such latency is large enough to be an issue.
You can create multi-threaded code using web workers, but you exclude your app from browsers that don't support them. The major laggard on the desktop is IE, which is due to gain support for them in version 10. As far as I know the only current mobile browser that supports them is Safari under iOS5, apparently they were present in Android's browser but removed/disabled in recent releases.
Performance question: why does window size affect FPS so drastically?
Hopefully that'll be worked on at some point in the not too distant future as it's one of the biggest problems I encounter when dealing with canvas at the moment.
Similarly, an image of 30kb takes more time to draw than an image of 10kb, even if they take up the same dimensions and space on the screen.
If you think this way, it's quite intuitive.