Jump to content

Graphics.containsPoint() from a parent container's coordinates


gurkesaft
 Share

Recommended Posts

Hi all,

I've been poking around the web and documentation for some time now and feel I'm missing something basic, and cannot figure this out:

Suppose I have a "master" container with a bunch of objects (containers) in it, and one of those objects (also a container) has within it a Graphics object in which I've drawn a polygon. Is there an easy way to use the Graphics.containsPoint() to test whether a coordinate from the master container is within the Polygon? 

I'm open to other (especially *faster*) solutions, e.g., if Graphics.containsPoint() is slow compared to using a Polygon or Rectangle object. 

Thanks!

Jack

PS- Sidequest: is there an efficient way to test whether things like Polygons and Graphics in different containers overlap?

Link to comment
Share on other sites

to test whether a coordinate from the master container is within the Polygon? 

The most effective is:

graphics._recursivePostUpdateTransform();
const tempPoint = new Point();
graphics.containsPoint(graphics.transform.localTransform.applyInverse(myPoint, tempPoint));

you may omit updateTransform or call how it works internally if you are sure that matrix is already updated.

otherwise, use "grahics.toLocal(point, container, tempPoint)", it takes care of everything but it requires extra "apply"

PS- Sidequest

No.

Link to comment
Share on other sites

Wow that was fast and insane, thank you. I'll give this a shot.

> otherwise, use "grahics.toLocal(point, container, tempPoint)", it takes care of everything but it requires extra "apply"

Is this slower? If not, how would the code look? I'm a little concerned that the deep code you sent above will change in future implementations of Pixi, so if there's a "cleaner" implementation I'm for it.

Another option I thought of would be to translate all the polygon vertices into the master container's coordinates once, THEN loop over my many, many objects in the master, so that I don't have to waste cpu on every object's transformation. Let me know if you have thoughts on this approach.

Thanks again; I hope you're paid for your prolific work.

Link to comment
Share on other sites

Thanks again; I hope you're paid for your prolific work.
 

My main job pays the bill, I rarely have debts, so I'm in much better position than many others. I support the idea that knowledge has to be free, like https://en.wikipedia.org/wiki/Sci-Hub

Let me know if you have thoughts on this approach.

Its still the same number of vertices that have to be transformed. My old fork of pixijs had lazy calculation of local, world and camera vertices. I think it actually depends on whole algorithm that you want to apply and on bottlenecks. Tell me more and maybe i can tell what you have to do

Link to comment
Share on other sites

I've got hundreds of points in the master container, and I need to check if they're all in a few-point polygon living in the child container's coordinates. My current plan is to do one inverse transform of the polygon and then loop over the hundreds of points to see if they're within it.

--

I agree about free knowledge, and, luckily, scientific grant agencies are moving toward the requirement of open access and open data (it's part of the budget in the applications now). Arxiv is helping with that, and I think combining arxiv with some kind of stack-exchange-like upvoting and peer-review system with tags for sorting would replace journals. But that's a separate discussion...

But don't burn out. ;) Most of the answers I see about pixi seem to come from you and your sticks of dynamite.

Link to comment
Share on other sites

Okay, so it turns out to convert child container coordinates (x,y) to parent container coordinates, one takes 

child_container.localTransform.apply( new PIXI.Point(x,y) );

This returns a PIXI.Point in the parent container coordinates. applyInverse() does some pretty wonky stuff. :)

Thanks again!!

Link to comment
Share on other sites

You asked

Is there an easy way to use the Graphics.containsPoint() to test whether a coordinate from the master container is within the Polygon? 

Ist applyInverse.

For graphics -> parent its "apply"

Dont forget about updateTransform fun )

Edited by ivan.popelyshev
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...