# Finding the middle of an arbitrary shape

This topic is 5045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Howdy! Just recently I began the massive undertaking of changing thousands of lines of code for my game so that the number of players is now dynamic instead of fixed on 2. Now I've reached the final part: Updating the camera system so instead of tracking the middle point (2D) of the two players (easy) it has to track the middle point of a dynamic number of players (not so easy). There can be any number of players, though usually it'll be between 2 and say, 8. There is no guarentee. The shape that they can create can be non-convex also! I need to find their middle point and I have all their positions stored as x, y floats. Any ideas?

##### Share on other sites
Just add all the points together, and divide by the total number of elements.

Alan

##### Share on other sites
If you need to see them all at once you need to find the minimum and maximum x and y values and adjust the camera to that (assuming your setup allows zooming). If you just average their positions some players may go out of camera range.

##### Share on other sites
http://kspark.kaist.ac.kr/Aesthetic%20Engineering.files/Area%20Centroid.htm

Something like that may help you. Treat all your players as vertexes on a polygon of some sort and then finds its geometric center as described in the link.

Mathworld has a way of doing it for "Point masses" so that maybe better, but that site always confuses the hell out of me.
http://mathworld.wolfram.com/GeometricCentroid.html

and yes, if you allow zooming, you may need to zoom out after panning the camera center on the centoid to make sure that players on the extents are visible. If you do not zoom, you will need to check their extents and shift over a bit from the centroid to get them on the screen.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 48
• ### Forum Statistics

• Total Topics
632637
• Total Posts
3007578
• ### Who's Online (See full list)

There are no registered users currently online

×