We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using the java.awt.Polygon in my processing sketch because I need its functions "contains" and "intersects" for my code. It's about a Pendulum simulation and the moving body of the pendulum is the Polygon that 'hits' objects and adjusts its motion to them. So I need to move and rotate my Polygon, and for this, after some research I did, I believe I need the PathIterator or the Serialization Methods from java ( https://docs.oracle.com/javase/7/docs/api/java/awt/geom/PathIterator.html OR https://docs.oracle.com/javase/7/docs/api/serialized-form.html#java.awt.Polygon). Can you please give me an example of how these functions can be used in Processing?
I am posting my code in case it helps to understand the problem. At the moment what I am trying is to know when the polygon hits the 'ground'. In the class ground I have the boolean intersects, but it always returns false, and I believe it is because the actual polygon doesn't move. Only its image moves.
Pendulum pendulum;
MyPolygon mypolygon;
Ground ground;
//MYPOLYGON
PVector[] points;
PVector massCenter;
//PENDULUM
PVector origin;
PVector location;
//GROUND
int wdth=260;
int ht=3;
int xRect=-wdth/2;
int yRect=15;
void setup() {
size(600, 400, P2D);
points=new PVector[8];
points[0]= new PVector(0, 0);
points[1]= new PVector(-50, 0);
points[2]= new PVector(-50, -10);
points[3]= new PVector(-75, -20);
points[4]= new PVector(-75, -40);
points[5]= new PVector(25, -40);
points[6]= new PVector(25, -20);
points[7]= new PVector(0, -10);
origin=new PVector(width/2, height/2);
mypolygon=new MyPolygon(points);
mypolygon.createGrid();
massCenter= mypolygon.centerOfMass();
massCenter.add(origin);
pendulum=new Pendulum(origin, massCenter, points);
ground=new Ground(xRect, yRect, wdth, ht);
}
void draw() {
background(0);
mypolygon. drawGrid();
mypolygon.drawCenterOfMass();
mypolygon.drawInitialShape(50);
pendulum.debug();
pendulum.update();
pendulum.display();
ground.intersection();
ground.display();
}
class MyPolygon {
int scl=5;
int rows=50;
int cols=40;
float xAverage;
float yAverage;
java.awt.Polygon p;
PVector[] pts;
ArrayList <PVector> grid;
MyPolygon(PVector[] pts_) {
pts=pts_;
p = new java.awt.Polygon();
for (PVector a : pts) {
p.addPoint(int(a.x), int(a.y));
}
grid=new ArrayList<PVector>();
}
void createGrid() {
for (int i=-rows; i<rows; i++) {
for (int j=-cols; j<cols; j++) {
PVector k=new PVector(i*scl, j*scl);
if (p.contains(i*scl, j*scl)) {
grid.add(k);
}
}
}
}
void drawGrid() {
pushMatrix();
translate(origin.x, origin.y);
fill(0, 0, 200);
noStroke();
for (PVector v : grid) {
ellipse(v.x, v.y, 2, 2);
}
popMatrix();
}
PVector centerOfMass() {
int xcount=0;
int ycount=0;
for (int i=0; i<grid.size(); i++) {
xcount+=grid.get(i).x;
ycount+=grid.get(i).y;
}
xAverage=xcount/grid.size();
yAverage=ycount/grid.size();
PVector r=new PVector (xAverage, yAverage);
return r;
}
void drawCenterOfMass() {
fill(0, 100, 200);
ellipse(massCenter.x, massCenter.y, 10, 10);
}
void drawInitialShape(float alpha) {
pushMatrix();
translate(origin.x, origin.y);
fill(255, alpha);
beginShape();
for (PVector a : points) {
vertex(a.x, a.y);
}
endShape();
popMatrix();
}
void readObject(java.awt.Polygon p){
}
}
class Pendulum {
PVector[] pts;
PVector penOrigin;
PVector penLocation;
float len;
float a;
float aVel;
float aAcc;
float angleThickness;
Pendulum(PVector origin_, PVector location_, PVector[] pts_) {
pts=pts_;
penOrigin=origin_.get();
penLocation=location_.get();
len=dist(penOrigin.x, penOrigin.y, penLocation.x, penLocation.y);
PVector diff=new PVector();
diff=penLocation;
diff.sub(penOrigin);
a=diff.heading2D();
angleThickness=a;
aVel=0;
aAcc=0;
}
void debug() {
println ("P.origin =" + penOrigin + "P. location= " + penLocation + "angle a=" + a);
}
void update() {
aAcc=-0.0001*len*sin(a);
aVel+=aAcc;
aVel*=0.99;
a+=aVel;
penLocation.set(sin(a)*len, cos(a)*len, 0);
}
void display() {
pushMatrix();
translate(origin.x, origin.y);
fill(255, 0, 200);
ellipse(0, 0, 20, 20);
ellipse(penLocation.x, penLocation.y, 20, 20);
pushMatrix();
rotate(-a + (PI/2 -angleThickness));
fill(255, 100);
beginShape();
for (PVector a : pts) {
vertex(a.x, a.y);
}
endShape();
popMatrix();
popMatrix();
}
}
class Ground{
float x;
float y;
float w;
float h;
boolean touch=false;
Ground(float x_, float y_, float w_, float h_){
x=x_;
y=y_;
w=w_;
h=h_;
}
void display(){
pushMatrix();
translate(origin.x, origin.y);
fill(255);
rect(x,y ,w,h);
//rect(10,-200,460,30);
popMatrix();
}
void intersection() {
boolean c=mypolygon.p.intersects(x, y, w, h);
println(c);
}
}
Answers
You are right that is the reason.
You do not need the PathIterator or Serialization to do this you simply need to update the polygon points as the pendulum swings. To do this we use the java.awt.geom.AffineTransform class to transform the original polygon points by translating and rotating them as we do the same for the pendulum. The MyPolygon class has two arrays, one holding the original points and the second the transformed points. It also has an update method to perform the transformation. This method is called from the update method in the Pendulum class so they get updated together.
I have also lowered the ground to make it easier to test the code.
HTH