To display a rotated molecule the rotation must be done brfore the translation. Any way the code below rotates the molecule about the vertical axis.
- import processing.opengl.*;
- H20 myH20;
- void setup() {
- size(800, 600, OPENGL);
- myH20 = new H20();
- }
- void draw() {
- background(0);
- myH20.display();
- myH20.move();
- myH20.turn(0.1);
- }
- class H20 {
- color c;
- float xpos = width/2;
- float ypos = height/2;
- float xspeed =5;
- float yspeed =5;
- float rot = 0;
- H20() {
- c = color(255);
- xpos = width/2;
- ypos = height/2;
- xspeed = 1;
- }
- void display() {
- lights();
- pushMatrix();
- // translate to centre of molecule (oxygen atom)
- translate(xpos, ypos, 50);
- // Rotate the molecule
- rotateY(rot);
- // Oxygen
- noStroke();
- fill(255, 100, 100);
- sphere(50);
- // Hydrogen 1
- pushMatrix();
- lights();
- translate(40, -35, 0);
- noStroke();
- fill(90);
- sphere(35);
- popMatrix();
- // Hydrogen 2
- pushMatrix();
- translate(-40, -35, 0);
- noStroke();
- fill(90);
- sphere(35);
- popMatrix();
- popMatrix();
- }
- void move() {
- xpos = xpos + xspeed;
- ypos = ypos + yspeed;
- if (xpos > width-100 || xpos <=100) {
- xspeed=xspeed*-1;
- }
- if (ypos > height-100 || ypos <= 100) {
- yspeed=yspeed*-1;
- }
- }
- void turn(float deltaRot) {
- rot += deltaRot;
- }
- }