# Scaling Convex Polygon Paths

## 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:

The paths of these polygons are stored like so:

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!

##### 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
##### 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:

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
##### Share on other sites

I went and learned how to do matrix math again and now it works

I ended up just using the transform my sprite to transform my points! actually so simple if you dont try to stitch together 50 libs.

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.