开发者

Box2dweb, shifting the canvas?

I'm currently working on a game with html5/js, using box2dweb for the collision but I'm running into the issue where I am required to scroll the level with the player. Box2d renders directly to the 2d context so I think in it's current state there is no way to shift the render portion of the canvas?

In as3 you could just put everything in a movieclip and change it's position accordingly.

So, is it possible in anyway to have control of a camera of some sorts or the render portion of the canvas object to shift it's 'positio开发者_开发问答n' to keep the player centered at all times?

Thanks in advance,

M0rph3v5


Box2D, by itself, doesn't draw anything, it just calculates positions/collisions.

It offers the so-called "Debug Drawing", but it's purpose is... debug. Anyway you could start from there to add all the needed features.

http://code.google.com/p/box2d/wiki/FAQ


Yeah I figured, turns out I had to use a context.translate right before the debugdraw as well to 'shift' everything. Got it working nicely now.

EDIT:

The code I'm currently using

            context.save();
            context.translate(-1*xpos+(canvas.width/2),-1*ypos+(canvas.height/2));
            context.rotate(cars[carid].angle);
            context.drawImage(carSprite, -carspritewidth/2, -carspriteheight/2); 
            context.restore();

where xpos and ypos are the x and y positions of the car, after that i just draw the actual car sprite at 0,0 (with the carsize divided as the center).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜