Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Pixel perfect collision? #112

Closed
MintPaw opened this issue Mar 6, 2017 · 4 comments
Closed

Pixel perfect collision? #112

MintPaw opened this issue Mar 6, 2017 · 4 comments

Comments

@MintPaw
Copy link

MintPaw commented Mar 6, 2017

How do we achieve pixel perfect collision? This seems to be a problem with pixijs, but the common workarounds using WebGLExtract seem to not have externs. I'm considering creating a separate canvas element and using it to get pixel data like this, is this recommended?

EDIT: To clarify my situation, I need to get alpha tolerant pixel testing on animations, and building hitArea polygons isn't really feasible in my setup unless they could be done at runtime.

EDIT 2: I've found this work around for now.
var pixel:Array<Float> = untyped __js__('({0}).extract.pixels({1}).slice({2}, {3})', app.renderer, renderTexture, startInd, startInd+4);

EDIT 3: This actually still won't work because you can't clear RenderTextures on canvas without renderer.clearRenderTexture which is seemingly webgl only.

Can pixi js realistically support pixel colour testing? I've take the feature for granted, but it seems the tech might not be there yet or something??

@ivanpopelyshev
Copy link

WebGL + pixel perfect collision will be very slow thing. I dont think that any GL engine can support it.

@MintPaw
Copy link
Author

MintPaw commented Mar 6, 2017

@ivanpopelyshev If you queried the pixels directly from the shader, but you could also look at the png file after transforming your point with the matrix from the texture, or cache the bitmap data, or any number of things. I guess I'll be writing that. :/

Most applications need some at least some alpha testing, even for just buttons, and almost all games require it. I'd say this hardly usable without it.

@NicolasVereenoghe
Copy link

NicolasVereenoghe commented Apr 25, 2017

if I recall correctly, you can generate a bitmap array this way :

`var lModel : Container = "insert your display object reference"
var lCanvas : CanvasRenderer = new CanvasRenderer( lModel.width, lModel.height, { transparent: true});

lCanvas.render( lModel);

var lImageData : ImageData = lCanvas.context.getImageData( 0, 0, lModel.width, lModel.height);`

And so on, you can work on the ImageData instance. This worked for me at pixi 4.0.2, I hope it still works :) You may generate your bitmap datas before you use them, as I believe it's a bit expensive to generate them at the fly ...

@ivanpopelyshev
Copy link

Oh, we didnt close this? OK, here's answer for v5: https://github.com/pixijs/pixi.js/wiki/v5-Hacks#pixel-perfect-interaction

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants