FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   rotating 2 cubes individually
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: rotating 2 cubes individually  (Read 317 times)
javito

jburong Email
rotating 2 cubes individually
« on: May 18th, 2004, 5:41pm »

hi, I write this code trying to rotate (dragging the mouse) each box individually, but it doesn't work.. can anybody help me?
thanks
Javi Buron
 
CODE::
 
//2 boxes rotarion
//
//
float vx, vy = 0;  
float girox, giroy = 0;
//  
void setup() {
  size(200,100);
  smooth();
}
void loop() {
  background(0);
  push();
  translate(width/4,height/2,-20);
  if(mousePressed){  
    vx = mouseX-pmouseX;
    vy = mouseY-pmouseY;
  } else {
    vx *= 0.7;
    vy *= 0.7;
  }
  girox+=vx/(width/10);
  giroy-=vy/(height/10);
  rotateX(giroy);
  rotateY(girox);
  box(20);
  pop();
  push();
  translate(3*width/4,height/2,-20);
  if(mousePressed){  
    vx = mouseX-pmouseX;
    vy = mouseY-pmouseY;
  } else {
    vx *= 0.7;
    vy *= 0.7;
  }
  girox+=vx/(width/10);
  giroy-=vy/(height/10);
  rotateX(giroy);
  rotateY(girox);
  box(30);
  pop();
}
 

Javi Burón
JohnG

WWW
Re: rotating 2 cubes individually
« Reply #1 on: May 19th, 2004, 11:41am »

You're always going to get both boxes rotating at the same time with that code.
 
What's happening is that you press the mouse button, the first if(mousePressed) will work, and then the program will carry on and get tot he second one as well, and since the time it takes is a tiny fraction of a second, the mouse button is still being pressed, the second if(mousePressed) will also happen.
 
You're also sharing the variables vw,vy,girox,giroy between both cubes, so the motion of one affects the other.
 
Here's a version that doens't have these problems, but it isn't the cleanest way of doing it:
 
 //2 boxes rotarion
//
//
float vx = 0, vy = 0;  
float wx = 0, wy = 0;
float girox = 0, giroy = 0;
float hirox = 0, hiroy = 0;
int mode=0;
//  
void setup() {
  size(200,100);
  smooth();
}
 
void mouseReleased()
{
  mode++;
  mode%=2;
}
 
void loop() {
  background(0);
  push();
  translate(width/4,height/2,-20);
  if(mousePressed && mode==0){  
    vx = mouseX-pmouseX;
    vy = mouseY-pmouseY;
  } else {
    vx *= 0.7;
    vy *= 0.7;
  }
  girox+=vx/(width/10);
  giroy-=vy/(height/10);
  rotateX(giroy);
  rotateY(girox);
  box(20);
  pop();
  push();
  translate(3*width/4,height/2,-20);
  if(mousePressed && mode==1){  
    wx = mouseX-pmouseX;
    wy = mouseY-pmouseY;
  } else {
    wx *= 0.7;
    wy *= 0.7;
  }
  hirox+=wx/(width/10);
  hiroy-=wy/(height/10);
  rotateX(hiroy);
  rotateY(hirox);
  box(30);
  pop();
}
 
 
A better way of doign it would proabbly be to have a MyCube class, that contains the vx/vy/girox/giroy variables, and so each cube has it's own set, and you don't have to change letters in the names to add new ones
 
Something like: (Typed straight into here, haven't tested in processing)
 
class MyCube
{
  float vx=0, vy=0, girox=0, giroy=0;
  int size;
  MyCube(int _size)
  {
     size=_size;
  }
  void Rotate()
  { ... do stuff ... }
  void Draw()
  { ... push draw pop etc... }
}
 
MyCube[] cubes;
void setup()
{
  cubes=new MyCubes[2];
  cubes[0]=new MyCube(20);
  cubes[1]=new MyCube(30);
}
 
void loop()
{
  if(mousePressed)
  {
    cubes[mode].Rotate();
  }
  for(int i=0;i<cubes.length;i++)
  {
    cubes[i].Draw();
  }
}
 
void mouseReleased()
{
  mode++;
  mode=mode%cubes.length;
}
 
javito

jburong Email
Re: rotating 2 cubes individually
« Reply #2 on: May 20th, 2004, 9:55am »

Thanks JohnG, I never have used OOP so I need to study your code deeply.....
 
Thank you again
 

Javi Burón
Pages: 1 

« Previous topic | Next topic »