#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Rotating objects in a class

edited March 2018

Hello everybody, I am relatively new to Processing and have come up against a problem that I don't know how to solve. Can anyone please tell me what is the best way to code multiple objects, array of objects, where that object is constructed as a class a manipulate with them through rotation. I wish that every entity individually can be rotated depending on movements of the mouse. Thank you in advance for any help.

Tagged:

• edited March 2018 Answer ✓

Break it down into steps

• Can you make a single rectangle rotate?

• Can you make an array of objects ?

e.g.

For Rotation

Every rotation is done around the origin (which is 0,0 of the coordinate system).

So you need to use translate() first and then rotate().

``````translate (width/2, height/2);

rotate ( angle1 );

angle1+= angleSpeed;
``````

To avoid rotating around corner of rectangle instead of its center

To avoid rotating around corner instead of center:

Since rotate() is around origin you want to use `rectMode(CENTER);`

OR

Place the object on / over the origin:

`rect(-40,-40,80,80);`

To let the objects rotate independently make sure the variables like angle are in the class and NOT defined globally/ before `setup ()`.

For mouse:

``````angle1=map(mouseX, 0,width, 0, TWO_PI);
``````

Tutorial:

There is a tutorial on objects (which is also about classes) and one about arrays

Array versus ArrayList

Array is good when you know how many objects you have, otherwise use ArrayList

Chrisir ;-)

• here is an example of a class and an ArrayList of that class:

``````ArrayList<Ball> balls;

void setup() {

size(400, 800);
balls = new ArrayList();

// for-loop
for (int i = 0; i < 10; i++) {

int rectWidth = 20;
int rectHeight = 20;
float rCol, gCol, bCol;

rCol = random(255);
gCol = random(255);
bCol = random(255);

color newColor = color(rCol, gCol, bCol);

Ball newBall = new Ball(random(width-rectWidth), random(height-rectHeight),
rectWidth, rectHeight,
newColor);

//
} // for
//
} // func

void draw() {

background(255); // white

// for-loop
for (int i = 0; i < balls.size(); i++) {
Ball ball = balls.get(i);
ball.display( false );
} // for

// for-loop backward to remove balls
for (int i = balls.size()-1; i>=0; i--) {
Ball ball = balls.get(i);
if (!ball.isAlive) // ! means not
balls.remove( i );  // remove i from list
} // for

fill(0);  // black
text ("number of balls: "+balls.size(),
14, 14);
//
} // func

// ==================================================

class Ball {

// position
float x;
float y;

// size
float w;
float h;

// color
color colBall;

// Is it still alive?
boolean isAlive = true;

// constr
Ball(float tempX, float tempY,
float tempW, float tempH,
color tempColBall) {

x = tempX;
y = tempY;
w = tempW;
h = tempH;
colBall = tempColBall;
} // constr

void display( boolean withStroke ) {

if (withStroke) {
strokeWeight(3);
stroke(0);
} else {
noStroke();
}
fill(colBall);
rect(x, y, w, h);

y+=3;

if (y > height+12)
isAlive = false;
} // method
//
} // class
//
``````
• rotation example

``````float angle1;

void setup()
{
// runs once
// init
size(800, 600);
background(255);
}

void draw()
{
// runs on and on in a loop
//
background(255);

// the big gray rect
pushMatrix();
noStroke();
translate(width/2, height/2);
rotate(angle1);
fill(111);  // gray
rect (-50, -60, 100, 120);
angle1 += 0.03;
popMatrix();

// the smaller red rect
fill(255, 0, 0);  // red
rect (100, 100, 50, 42);
}
``````