We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Rotating objects around a circle
Page Index Toggle Pages: 1
Rotating objects around a circle (Read 333 times)
Rotating objects around a circle
Jan 4th, 2009, 5:56pm
 
Hello everyone.  I am new to Processing and to this forum.  I appreciate the great community everyone has created.

I am trying to create a program that displays objects and rotates them around the same circle.  However, I want each object to be created in a random X and Y location, and rotate from there.  Here is my current source code:
---
Orb[] o;
int numOrbs = 2;
float attractRadius = 50.0;
float attractX = 70.0;
float attractY = 90.0;
float step = 0.001;

void setup() {
 background(240);
 size(485, 200);

 o = new Orb[numOrbs];
 
 for (int i=0; i<numOrbs; i++){
   o[i] = new Orb(random(width), random(height), 10);
 }
}

void draw() {
 background(240);
 stroke(0);
 for (int i=0; i<numOrbs; i++) {
   stroke(100, 50);
   line(attractX, attractY, o[i].px, o[i].py);
   o[i].display();
   o[i].move();

 }
 stroke(0);
 fill(255, 50);
 ellipse(attractX, attractY, attractRadius, attractRadius);    // orbs are attracted to this
}

class Orb {
 float x, y; // x-coordinate, y-coordinate
 float radius;
 
 float angle;
 float px, py;
 float frequency = 2;
 
 // Constructor
 Orb(float xpos, float ypos, float rad) {
   x = xpos;
   y = ypos;
   radius = rad;
 }
 
 void display() {
   smooth();
   noStroke();
   ellipseMode(CENTER);
   stroke(100);
   ellipse(px, py, radius, radius);
 }
 
 void move() {
   
   px = attractX + cos(radians(angle)) * ((attractRadius + x)/2);
   py = attractY + sin(radians(angle)) * ((attractRadius + x)/2);

   angle -= frequency;

   println("xpos: " + px + " , ypos: " + py);
 }
}
---

The problem I am running into is that the objects all form on the same line, not in completely random locations on the screen.  Can anyone offer help?  Thank you!
Re: Rotating objects around a circle
Reply #1 - Jan 4th, 2009, 7:12pm
 
Hey,

Try setting a different angle for each Orb.
Re: Rotating objects around a circle
Reply #2 - Jan 4th, 2009, 8:42pm
 
Thanks Kiza.  I did try that, but it didn't work:

Here is the attempted solution:
---
void setup() {
 background(240);
 size(485, 200);

 o = new Orb[numOrbs];
 
 for (int i=0; i<numOrbs; i++){
   o[i] = new Orb(random(width), random(height), 10, random(TWO_PI));
 }
}
.
.
 // Constructor
 Orb(float xpos, float ypos, float rad, float ang) {
   x = xpos;
   y = ypos;
   radius = rad;
   ang = angle;
 }
.
.
---

Am I making a very obvious mistake here?
Re: Rotating objects around a circle
Reply #3 - Jan 4th, 2009, 8:57pm
 
maybe the angles are too subtle.  hack:

Code:

Orb[] o;
int numOrbs = 100;
float attractRadius = 50.0;
float attractX = 70.0;
float attractY = 90.0;
float step = 0.001;

void setup() {
background(240);
size(485, 500);

o = new Orb[numOrbs];

for (int i=0; i<numOrbs; i++){
o[i] = new Orb(random(width), random(height), 10, random(0, 1000), random(3,5)/10);
}
}

void draw() {
background(240);
stroke(0);
for (int i=0; i<numOrbs; i++) {
stroke(100, 50);
line(attractX, attractY, o[i].px, o[i].py);
o[i].display();
o[i].move();

}
stroke(0);
fill(255, 50);
ellipse(attractX, attractY, attractRadius, attractRadius); // orbs are attracted to this
}

class Orb {
float x, y; // x-coordinate, y-coordinate
float radius;

float angle;
float angle1 = 0;
float px, py;
float frequency = 2;

// Constructor
Orb(float xpos, float ypos, float rad, float ang, float freq) {
x = xpos;
y = ypos;
radius = rad;
angle = ang;
frequency = freq;
}

void display() {
smooth();
noStroke();
ellipseMode(CENTER);
stroke(100);
ellipse(px, py, radius, radius);
}

void move() {

px = attractX + cos(radians(angle+angle1)) * ((attractRadius + x)/2);
py = attractY + sin(radians(angle+angle1)) * ((attractRadius + x)/2);

angle1 -= frequency;

println("xpos: " + px + " , ypos: " + py + ", angle" + angle);
}
}
Re: Rotating objects around a circle
Reply #4 - Jan 4th, 2009, 9:16pm
 
Thank you!
Page Index Toggle Pages: 1