Can a calculated vector be included in an object's constructor?

Hi Sages,

I am trying to make a game with an ellipse (= antiballistic missile) which takes off from 1 of 3 spots and flies towards a mouse-clicked target leaving a trail and then disappears, along with the trail. I am creating a class called Amiss and was going to construct it as follows:

Amiss(color fillTemp, color strokeTemp, int widthTemp, int heightTemp, int speedTemp, int startXtemp, int startYtemp, int targXtemp, int targYtemp) {

I then figured I would use a move() method to animate the missile toward its target, something like this:

// "vP" stands for the vector path the missile will follow.
vP = new PVector(targX - startX, targY - startY);
vP.normalize();

xPos = xPos + speed * vP.x;
yPos = yPos + speed * vP.y;
ellipse(xPos, yPos, amWidth, amHeight);

It strikes me that having the program calculate the animating vector each time the missile moves is a waste of processing and that each missile should calculate its own normalized vP once when it is created and then store that vector amongst its parameters for quick, repeated use whenever move() is called.

Is this a good impulse?

Can you include an empty parameter within a constructor and then populate it with a value calculated from its other parameters?

If so, how do you do that?

While I'm at it, am I using method/parameter correctly for Processing?

Thanks for any help!

Answers

  • Answer ✓

    each missile should calculate its own normalized vP once when it is created and then store that vector amongst its parameters for quick, repeated use whenever move() is called.

    Since each missile will have its own vP so it should be an attribute/field of the missile class. This is good impulse :)

    While I'm at it, am I using method/parameter correctly for Processing?

    Yes.

    Can you include an empty parameter within a constructor and then populate it with a value calculated from its other parameters?

    If the parameter is a primitive data type e.g. int, float etc. then the answer is no but if it is an instance of a class e.g. PVector then yes (there are some classes that don't allow this)

    Try this program

    void setup() {
      PVector vec = new PVector();
      println(vec);
      changeVector(vec, 1, 2, 3);
      println(vec);
    }
    
    void changeVector(PVector v, int x, int y, int z) {
      v.x = x;
      v.y = y;
      v.z = z;
    }
    

    The output is

    [ 0.0, 0.0, 0.0 ]
    [ 1.0, 2.0, 3.0 ]
    
  • Thanks folks. Though I'm afraid I'm not quite experienced enough to parse all the code help you gave me, your answers did push me in the right direction which I think is this:

    Within an object's constructor lines, I can calculate (from the parameters passed into that instance) the normalized vector I need to use every time move() is called. That vector does not need to become one of the object's defined parameters in order to be available to that object's methods; move() simply uses the calculated vector with each call and does not need to (re)calculate anything itself.

    It seems simple and very obvious now but I sure am grateful for you 2 taking the time to shine some light my way.

  • additionally:

    a class can hold much more vars than those that are copied / filled from the parameters in the constructor

    in the constructor you can calculate and do much more than just those copying

    so you can calc stuff in the class that don't have to be a param in the constructor

Sign In or Register to comment.