# Moving a Polygon based on Camera position

I am in the process of implementing TMX Maps (made in Tiled) for my game. In Tiled, I can create polygons, which leaves a tag, containing the points.

Loading in these points to a Polygon (java.awt.Polygon) isn't a problem. When all points are loaded into a Polygon (which also had an x and y position, which is the origin of the Polygon), and I draw it, it looks perfect.

However, when I was to move it the troubles started. Every tick, I want to set the position of this Polygon according to the x, y position (the origin), and the Camera position, so that when I draw it, it will draw on position.x - Camera.x and position.y - Camera.y. So every tick, I want to set the (x, y) position of the Polygon equal to position - Camera.position.

I thought that this would be as simple as calling the translate(position.x - Camera.x, position.y - Camera.y) function in the polygon every tick, but the problem here is that this function adds a delta, instead of setting the position. Because of this, the Polygon won't stay still when the Camera is still, because it adds this delta, so the Polygon just moves off the screen.

So, the question is really how to set the position of a polygon every tick, instead of adding a delta to it every tick?

You have to keep separated your world and screen coordinates.

Store only world coordinates of your object (position inside the whole map).

Then, when you need to test collisions (object with another object) you can check both object in worldcoordinates. If you apply rotation or scaling to object you will have to go from object coordinates in world to screen coordinates and then to another object world coordinates to account for object or another object changes.

When you want to draw it on the screen use the transform matrix to go from world coordinates to screen coordinates.

A graphical explanation about transformations can be found here and an explanation to understand how to use matrix here.

Store the x points, y points and any other polygon related data elsewhere. Then in the render function you can save the data to a local scope set of variables (or an array or List if you want) and apply the transformations using good old mathematical operators. Then you can draw the polygon using this new data.

• But then the position actually doesn't change? Because when I do collision detection, I use the intersects() function, and that won't really work if the position isn't right. Commented Apr 14, 2015 at 20:28

I'm not sure I completely understand, but if you say you want to update your polygons coordinates to polygon.x - Camera.x, polygon.y - Camera.y and translate expects a delta, wouldn't then

translate(-Camera.x, -Camera.y)

be what you're after?

However, if what you want is to compensate for the camera movement, i.e., have the polygons always be drawn at the same screen coordinates independent of where the camera is, you could try recording the Camera's delta and use that as the parameter for translate:

// compute camera delta
int deltax = Camera.x - prevCameraX;
int deltay = Camera.y - prevCameraY;

// translate all polygons
for (Polygon polygon : polygons) {
polygon.translate(deltax, deltay);
}

...

// store current camera position for next tick
prevCameraX = Camera.x
prevCameraY = Camera.y