开发者

Does any one know is there any collision detection API for HTML5 Canvas..?

I have been experimenting HTML5 canvas by coding basic mind-mapping application. I have tried to find out if there is any javascript API used for managing object in canvas like co开发者_StackOverflow社区llision detection between images or shapes. I think it is not a good idea to write my own since there might be some good API around. Anyone have clue or some information on this. I would very much appreciate.


Lately I've been experimenting with this myself. I found that Box2DJS is a great Newtonian physics engine, if you want to try it out. http://box2d-js.sourceforge.net/

It's kind of hard to grasp the concept at first, but it gets easier as you go on, and it also comes with some nice examples. Box2D has been ported to many languages and most use the same, or very similar methods, so documentation for any port will likely suit your needs.

Good luck :).

EDIT: I found a link to this documentation on the previously stated site: http://www.kyucon.com/doc/box2d/ (Couldn't make it a hyper link due to my low reputation limiting me to one per post, I'm a noob ;P)

It's for Box2DFlashAS3, but they claim that "The Box2DJS APIs are completely [the] same as those of Box2DFlashAS3"

EDIT: Oh, I just noticed you were looking for collision detection, not physics, my bad... Perhaps Box2D could still work, but perhaps not. Sorry about that...


Take a look at Cake. It's a scene graph plug-in for canvas elements. I don't think it'll do collision detection, but it will maybe help get you in the right direction. You can at least attach event handlers to canvas objects with it.


Box2DWeb has collision detection using Box2D.Dynamics.b2ContactListener.

Box2DWeb is a port of the Box2DFlash 2.1a (so it has DebugDraw and ContactListeners). There's an example below that dispatches all contact events.

var contactListener = new Box2D.Dynamics.b2ContactListener();           
    contactListener.BeginContact = function(contact) {
        console.log('something just hit something else');

        // example to narrow the field
        if( contact instanceof Box2D.Dynamics.Contacts.b2PolyAndCircleContact) {
            // collision of circle to polygon
        }
    };

world.SetContactListener(contactListener);


Take a look at GameJS, it's a Javascript port of PyGame (well known game library for Python). It has a concept of sprites and sprite groups and has fairly advanced collision detection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜