Suggested Workflow - PGraphics and Class instances

edited March 2016 in Programming Questions

Hello,

I have a sketch that uses instances of a class where the appearance of each iteration is generative(different). I am using an arraylist as the instances of the class have a lifespan and an unknown number of instances are used.

My question is: Can/how I generate the each instances and then instead of displaying it and having to recalculate the shape as it moves around with every frame, can I generate the shape and then display it as a pGraphic so that the sketch runs faster?

Having tried to implement this my issues are:

Which parts of the code need to be within the pg.beginDraw() and pg.endDraw(); - i.e is it only things that refer to the shape and colour (so not the parts for moving the object around).

How do I set the initial area for creating the PGraphic: PGraphics pg = createGraphics(100, 100); - This refers to a 100X100 square from 0, but my instances are being created at random locations.

Answers

    • Sketch's canvas is itself a PGraphics.
    • If we createGraphics() of the same dimensions as canvas', we don't need to worry 'bout positioning it.
    • Otherwise it behaves as some "sprite". And thus it needs coordinates.
  • What I am trying to do is, stop having to calculate a complex shape in each frame. My sketch becomes very slow when more than 20 instances of the class are created.

    Can I not generate the visuals of the class instance then display it as an image and use the acceleration, velocity etc in my class to position an image of the generated shape? Essentially generating a sprite as you mentioned.

  • edited March 2016

    If the PGraphics got its content static, it's as easy as choosing which coordinate pair to display it.
    It's hard to try helping out w/ words only. It becomes a game of guesses. :-<

Sign In or Register to comment.