Do PVectors reset after being modified?
in
Programming Questions
•
5 months ago
Hi Guys. I'm working on an n-player pong, in which I need to dynamically generate paddles for each player. I'm currently translating the coordinate system to the center (width/2, height/2), and basing all coordinates off of that origin (to make it more similar to a normal coordinate grid). Then, I generate coordinates for each of the end points based on the number of players, by rotating a radius around the origin. That portion works fine, and adds to an ArrayList with each of the points in the form of a PVector in that arraylist.
I then (attempt to) generate paddles.
Paddles are generated by using the same method I used to create endpoints, but providing a smaller radius (so each coordinate endpoint is inside the polygon generated by the boundaryGen function. So, here's the paddle class:
- class Paddle {
- PVector endPointA;
- PVector endPointB;
- PVector smallPosA;
- PVector smallPosB;
- PVector midpoint;
- PVector slope;
- Paddle(PVector point1, PVector point2, int rIndexI) {
- endPointA = new PVector(point1.x, point1.y);
- endPointB = new PVector(point2.x, point2.y);
- slope = new PVector(endPointB.x - endPointA.x,endPointB.y - endPointA.y);
- slope.setMag(1);
- slope.mult(5);
- midpoint = new PVector((endPointA.x + endPointB.x )/2, (endPointA.y + endPointB.y)/2);
- smallPosA = midpoint;
- smallPosB = midpoint;
- smallPosA.add(slope);
- smallPosB.add(slope);
- }
- void display(int i) {
- line (smallPosA.x, smallPosA.y, smallPosB.x, smallPosB.y);
- }
- }
So, the paddle class had endPointA and endPointB which define the line that bounds the axis that the paddle can move around. Those points are passed from the paddle edge generation function, as defined above. If needed, I can post that as well.
So, then it defines a vector as a slope between those two points. That slope seems to work okay. A midpoint is then generated between the two endpoints, to which I figured I'd add or subtract the slope (multiplied by a width) to the midpoint, which should hopefully generate two points that are slightly away from the midpoint. The problem is, those points are somehow equal when the time to create the line comes around, and it becomes just a point (which is not what I want to happen). I've been gnawing at this for 5 hours and I still haven't been able to figure out what is wrong. I'd really appreciate any pointers.
Thanks,
Hades
EDIT: The main class is below:
- ArrayList<PVector> bPoints = new ArrayList<PVector>(); //boundary Points arraylist
- ArrayList<Paddle> paddles = new ArrayList<Paddle>();
- final int numberOfSides = 8;
- final int buffer = 8; //distance to keep away from the sides
- final float innerBuffer = .95; //modifier for the distance the paddles are away from the boundary
- final float pAngle = (180*(numberOfSides-2))/numberOfSides; //inner angle between the shape
- final float innerAngle = 360/numberOfSides; //distance to rotate the coordinate system to generate each point
- void setup() {
- size (650, 650);
- bPoints = boundaryGen();
- paddles = makePaddles();
- }
- float smallerOf(float one, float two) {
- if (one > two) return two;
- else return one;
- }
- ArrayList<PVector> boundaryGen () {
- pushMatrix();
- translate(width/2, height/2);
- float degrees = 0;
- float degreesToAdd = 360/numberOfSides;
- float radius = smallerOf(width/2-buffer, height/2 -buffer);
- ArrayList<PVector> temp = new ArrayList<PVector>();
- for (int i = 0; i < numberOfSides; i++) {
- temp.add(new PVector(radius*cos(radians(degrees)), radius*sin(radians(degrees))));
- degrees += degreesToAdd;
- }
- popMatrix();
- return temp;
- }
- ArrayList<Paddle> makePaddles () {
- pushMatrix();
- translate(width/2, height/2);
- float degrees = 0;
- float degreesToAdd = 360/numberOfSides;
- float radius = smallerOf(width/2-buffer, height/2 -buffer)*innerBuffer;
- ArrayList<PVector> points = new ArrayList<PVector>();
- ArrayList<Paddle> paddlesTemp = new ArrayList<Paddle>();
- //makes a smaller set of coordinates inside
- for (int i = 0; i < numberOfSides; i++) {
- points.add(new PVector(radius*cos(radians(degrees)), radius*sin(radians(degrees))));
- degrees += degreesToAdd;
- }
- popMatrix();
- //adds to the paddlesTemp array for each point on the previously generated array
- for (int i = 0; i < points.size(); i++) {
- paddlesTemp.add(new Paddle(points.get(i%points.size()), points.get((i+1)%points.size()), i));
- }
- return paddlesTemp;
- }
- void draw() {
- background(100);
- //bunch of drawing stuff goes here
- translate(width/2, height/2);
- stroke(0);
- noFill();
- strokeWeight(2);
- stroke(color(100, 255, 150));
- //stroke(random(150), random(150), random (150));
- //create the outside border
- beginShape();
- {
- for (int i = 0; i < bPoints.size(); i++) {
- vertex(bPoints.get(i).x, bPoints.get(i).y);
- }
- //need to manually assign the last/first vertex again or the loop won't be closed
- vertex(bPoints.get(0).x, bPoints.get(0).y);
- }
- endShape();
- //reprint the paddles every round
- for (int i = 0; i < paddles.size(); i++) {
- paddles.get(i).display(i);
- }
- }
Note: I've removed a good bit of code from this program that isn't relevant to the question, including collision and the ball.
1