p2.js icon indicating copy to clipboard operation
p2.js copied to clipboard

Positioning planes at edges of canvas

Open lesliecdubs opened this issue 7 years ago • 3 comments

Hey there - great library.

Is there any way to position planes exactly at the four edges of the canvas (left, bottom, right, top)? For reference, the canvas size is being set on load with JS due to a responsive layout.

lesliecdubs avatar Aug 12 '16 21:08 lesliecdubs

Hi, You need a way to convert a canvas coordinate to the corresponding physics/game world coordinate. For example, if your canvas is 400x300 pixels you want to position one of the planes on the edge center (400,150). You need to convert this to game space and place the plane there.

What game or rendering engine do you use? If you use plain HTML5 canvas, you can check the mouse joint example. In other engines there are often built in stuff for doing this.

schteppe avatar Aug 13 '16 08:08 schteppe

Thanks for the quick response, @schteppe. I've actually been using the demo renderer, which I've altered a bit for our purposes.

Isn't the 'position' attribute using generic units (not pixels) to place planes at a specific location? If I could convert this to pixels it would be easy to do the math to position all the planes properly, but as it is I can't seem to figure out how to use the generic units to do this. Any insight?

lesliecdubs avatar Aug 15 '16 15:08 lesliecdubs

Ok! Looks like I used the built in stuff in Pixi.js to do convert to physics space: https://github.com/schteppe/p2.js/blob/85c93c9d77d3f23145238a541a07902d68615a8c/demos/js/WebGLRenderer.js#L178

Before I was using an own implementation (stagePositionToPhysics) to compute the physics position, but for now this method seem to do nothing.

The stage DisplayObject has the transform you need, from canvas to physics. So if you could use its transform to transform a point, you're done. http://pixijs.github.io/docs/PIXI.DisplayObject.html#toGlobal or http://pixijs.github.io/docs/PIXI.DisplayObject.html#toLocal on the stage object is what you're looking for.

schteppe avatar Aug 16 '16 07:08 schteppe