I never know exactly where to post my questions, but this seems as good a place as anywhere. Maybe Java2D because it’s about Polygons… Regardless.
Hey guys. I’ve got a an issue I just can’t wrap my brain around. I’m trying to make a game like Worms: Armageddon that has weapons which fire and destroy the ground dynamically. I decided to use Java’s built-in Polygon objects
to do this, although if anyone knows of a much better way I’m all ears.
WHAT’S GOING ON:
The ground is a big dynamic Polygon, or a list of Polygons (either or, it doesn’t really matter). Say we have a piece that looks like a mountain: it has points starting from the base and going to the peak, then back down to the other side. This forms a complete Polygon, and is drawn as such. Simple enough. But my problem is not making these Polygons, it’s dynamically changing them.
Let’s introduce an explosion. This takes away a big chunk of the landscape. To do this, I’ve figured a few steps (which work). First, the explosion is also a polygon, I am currently using a 100-gon to look like a circle. If there is polygon interception between the 100-gon and the ground, then part of the ground should be removed. To do this, first all points of the explosion that individually intercept the ground are stored – they will form a new border later, replacing a chunk of the ground. Next, any points of the ground that aren’t within the explosion are stored, effectively removing points from the ground that were within the explosion.
Now we have all points from the ground excluding any that were within the explosion radius in one list, and another list with any points of the explosion that encroached on the old ground. In a perfect world, put these points together in one Polygon and you’ve got the original ground with a circle-shaped chunk taken out of it. I thought it was a perfect world.
THE PROBLEM:
When Polygons are drawn they do it according to some technique I forget the name of. The way that works is that if some points overlap others then that area becomes “negative,” not being drawn. Because of this, the order the points are added into the Polygon is INCREDIBLY important. It can’t just find the overlying shape and draw that. I’ve tried to come up with and tested every possible way I could think of to have an algorithm that works in every case, but I can’t get it to work. Even if I insert the 100-gon points into the right place order-wise, the 100-gon points also need to moving in the exact same direction the points it replaced were going. I can’t figure it out.
Hopefully that’s followable. If not, I’ll try to include some pictures or something. What follows is my code for factoring explosions in. You can’t see the code for the Ground or the generation of the points for the explosion here, but they shouldn’t make much of a difference.
/**
* This method "explodes" a Polygon, removing/adding points from it to form a "hole" or
* explosion in that Polygon. This is called by the Ground and the ground's Polygon rendering
* is passed into this method. The passed Polygon itself is modified, thus void is returned.
* @param p The Polygon (generally Ground) to explode.
*/
public void explode(Polygon p)
{
//Cycle through each of the explosion's points and find which ones
//intersect the passed Polygon p. These intersecting points are added
//to the list to be used later.
java.util.ArrayList addedPoints = new java.util.ArrayList();
for (int i = 0; i < explosion.npoints; i++)
{
if (p.contains(explosion.xpoints[i],explosion.ypoints[i]))
addedPoints.add(new Point(explosion.xpoints[i],explosion.ypoints[i]));
}
//Once we have our added points, we can remove any points from the ground
//that fall within the explosion Polygon, effectively doing the opposite.
//The index knows where the explosion should be inserted.
java.util.ArrayList newPolygon = new java.util.ArrayList();
int index = -1;
for (int i = 0; i < p.npoints; i++)
{
if (!explosion.contains(p.xpoints[i],p.ypoints[i]))
newPolygon.add(new Point(p.xpoints[i],p.ypoints[i]));
else if (index < 0)
index = i;
}
//The newPolygon will be all of the addedPoints included with the points
//that did not intersect the explosion.
if (index != -1)
newPolygon.addAll(index,addedPoints);
else
newPolygon.addAll(addedPoints);
int[] pxs = new int[newPolygon.size()];
int[] pys = new int[newPolygon.size()];
for (int i = 0; i < newPolygon.size(); i++)
{
pxs[i] = ((Point)newPolygon.get(i)).x;
pys[i] = ((Point)newPolygon.get(i)).y;
}
//Finally, change the original Polygon to the new one.
p.xpoints = pxs;
p.ypoints = pys;
p.npoints = newPolygon.size();
arena.remove(this);
}
That you very much anyone who cared to read all that crap and attempted to help me out.