Jump to content

Scaling Convex Polygon Paths


peterje
 Share

Recommended Posts

This question is not strictly PIXI related but I hope someone here would be able to provide some insight.

 

In my project I am attempting to create accurate collision detection for PIXI sprites. So far, I have taken a sprite and deconstructed it into convex polygons like so:

image.png.0fad99f5f568e728b5f9939c78a2a449.png

 

The paths of these polygons are stored like so:

 

image.thumb.png.1fb542b2528fa89e176f2687344df3a9.png

 

These points represent the paths of all 12 polygons in the balloon. I am using https://github.com/davidfig/intersects to detect collision between two sets of these polygons. It is the simplest library I could find. Pretty much the pseudocode for collision goes like: 

 

function isTouching(other){
    for(polyPath1 in this.shapes){
        for(polyPath2 in other.shapes){
            if(intersects.polygonPolygon(poly1, poly2){
                return true;
            }
       }
    }
   return false;
}

 

Definitely not the most efficient approach...but that is besides the point.

 

As these polygons are entirely decouples from my PIXI Sprite, I have to update the points of each array every time my PIXI sprite is changed. Translating the points as my sprite changes its x and y is trivial, I can just go through the list and update the even-indexed values for a change of x, and the odd-indexed values for change of the y.

 

However, I am stuck when I want to change the scale of my PIXI Sprite. If I change the scale of my Sprite to 0.5, I unfortunately cannot just scale every point in my matrix by 0.5. Rotation is also an issue.

 

Does anyone know of an algorithm or library I can use to scale and rotate polygons from their point paths? It would ideally return a new array of the transformed polygon.

 

Thanks!

Link to comment
Share on other sites

Does anyone know of an algorithm or library I can use to scale and rotate polygons from their point paths?

Sounds like you dont know how to work with 2x3 matrices. And that is all over pixi source code, in mesh.containsPoint() , in many places in Bounds class, e.t.c.

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

12 hours ago, ivan.popelyshev said:

Does anyone know of an algorithm or library I can use to scale and rotate polygons from their point paths?

Sounds like you dont know how to work with 2x3 matrices. And that is all over pixi source code, in mesh.containsPoint() , in many places in Bounds class, e.t.c.

Yeah I used https://github.com/chrvadala/transformation-matrix#transform Affine Transform library.

 

Translating my points works fine but using the scale matrix did not...

 

It looks something like this after transforming each point of my shape by an affine scale matrix:

image.png.0099100732ddc3758d1bf9dd12d8b94b.png

 

I think it is because each polygon needs a different reference point to scale from. At first they were all scaling with (0,0) as a reference point but that didnt work. I tried using the centroid of each polygon as a reference point but that did not work either.

Edited by peterje
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...