Advertisement

Move side of a polygon

Started by November 26, 2015 02:09 PM
2 comments, last by Finalspace 9 years, 2 months ago

I am building a level editor for my game and doing the implementation for resizing (moving of two vectors by a given delta) of polygons right now. Grabing the side of a polygon and moving the vertices along the given delta works already, so the only problem i have right now is to doing the repositioning so that the body center match the new local center.

How do i calculate the new body center and delta for offseting my local vertices?

See picture for better understanding:

resize%20delta.png

Wouldn't this be simply movedelta / 2?

Hello to all my stalkers.

Advertisement

The easiest way I can think of is the following:

First, compute the new vertex positions in global space (affected vertices + delta). After that just sum them up and divide by vertex count to obtain the center of mass (this is a nifty trick to compute the center of something). You can compute local vertex positions by <global vertex position> - <center of mass> (all this does is shift the center of mass to the origin, which is the local coordinate system in question).

The nice thing is that this works for an arbitrary number of vertices!

For rectangular regions you can calculate Lactose!'s solution analytically, but I don't think it's worth the hassle, unless you have want to see the math.

Yeah it was delta / 2, first move the local vertices for the desired side by the full delta - than move back every local vertex by delta / 2 and then move the body position by delta / 2 too.

But the solution from duckflock i like much more, because it will preserve the center of mass - i will give it a try ;-)

Now it is getting more complex because i want to support scaling for non-rectangulary sides too, in this case you must scale the the other two sides connected too (based on the angle between).

And much more to come:

- Extruding

- Single vertex moving

This topic is closed to new replies.

Advertisement