Заставить сферу вращаться - PullRequest
0 голосов
/ 09 января 2020

Я работаю над анимацией упрощенной солнечной системы и хочу применить вращение к Солнцу. Вроде бы простая задача, но как-то не получится. Я пытаюсь написать свою программу, используя объектно-ориентированную парадигму, поэтому каждый pl anet, луна и Солнце являются объектом класса CelestialBody. Я попытался поместить строку rotate(angle) в следующую выдержку из класса CelestialBody, как показано ниже:

if(Type == 4){
          emissive(255, 255, 255);
          pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
          lightFalloff(0, 0, 0.00020); //light falls off right behind the surface of the sun
          ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun
rotate(angle);
        }

К сожалению, ничего не происходит. Как это исправить и заставить солнце вращаться, например, вдоль оси х? Вот мой код:

Основной файл:

import peasy.*; //<>//

PImage bg;
float angle = 0;
CelestialBody planet1;
CelestialBody planet2;
CelestialBody planet3;
CelestialBody planet4;
CelestialBody planet5;
CelestialBody sun;
PImage sunTexture;
PImage rocketTexture;
PShape rocket;
PeasyCam cam;

void setup() {
  fullScreen(P3D);
  bg = loadImage("background.jpg");
  sunTexture = loadImage("sun.jpg");
  cam = new PeasyCam(this, 1200);
  rocket = loadShape("rocket.obj");

  noStroke();

  sun = new CelestialBody(70, 0, 0, 255, 200, 50, 4);//4 - type: sun
  sun.planet.setTexture(sunTexture);
  //noStroke();
  //emissive(0, 0, 0);
  planet1 = new CelestialBody(16, 150, 0.01, 150, 50, 255, 1);
  planet1.spawnMoon(2);

  planet2 = new CelestialBody(25, 300, 0.014, 50, 100, 255, 1);
  planet2.spawnMoon(3);

  planet3 = new CelestialBody(35, 450, -0.015, 255, 100, 40, 2);
  planet3.spawnMoon(1);

  planet4 = new CelestialBody(50, 650, 0.011, 50, 200, 255, 1);
  planet4.spawnMoon(2);

  planet5 = new CelestialBody(65, 900, -0.014, 0, 100, 0, 1);
  planet5.planet = rocket;
  planet5.planet.scale(0.9);
}

void makeOrbit(int a, int r){
  for(int i=0; i<a; i++){
    stroke(255, 10);
    ellipse(height/2,width/2,r,r);
    r-=180;
  }
    noStroke();
}

void draw() {
  background(bg);
  //lights();
  noFill();
  //translate(width/2, height/2);
  stroke(#FFFFFF);
  //ellipse(0,0,300,300);
  //ellipse(0,0,600,600);
  //ellipse(0,0,900,900);
  //ellipse(0,0,planet4.distance,planet4.distance

  //  int z = 100;
  //for (int i = 0; i<2; i++){
  //  z = -z;
  //  pointLight(255, 255, 255, -100, -100, z);
  //  pointLight(255, 255, 255, 100, -100, z);
  //  pointLight(255, 255, 255, 100, 100, z);
  //  pointLight(255, 255, 255, -100, 100, z);
  //}

  //emissive(255, 255, 255);
  //pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
  //lightFalloff(0, 0, 0.00025); //light falls off right behind the surface of the sun
  //ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun

  sun.show();


  planet1.show();
  planet1.orbit();


  planet2.show();
  planet2.orbit();
  planet3.show();
  planet3.orbit();
  planet4.show();
  planet4.orbit();
  planet5.show();
  planet5.orbit();
}

Класс CelestialBody:

class CelestialBody {
    float radius;
    float angle = random(TWO_PI);
    float distance;
    float orbitSpeed;
    PShape planet;
    CelestialBody[] moons;
    int red;
    int green;
    int blue;
    PVector v;
    //PShape globe;
    int Type;

    CelestialBody(float _radius, float _distance, float _orbitSpeed, int _red, int _green, int _blue, int type) {
        v = PVector.random3D(); 
        radius = _radius;
        distance = _distance;
        v.mult(distance);
        orbitSpeed = _orbitSpeed;
        red = _red;
        green = _green;
        blue = _blue;
        //stroke(red, green, blue);
        fill(red, green, blue);
        if(type == 1 || type == 4){
        planet = createShape(SPHERE, _radius);
        }
        else if(type == 2){
           planet = createShape(BOX, _radius);
        }
        Type = type;
    }

    void orbit() {
        angle += orbitSpeed;
        if (moons != null) {
            for (int i = 0; i < moons.length; i++) {
                moons[i].orbit();
            }
        }
    }

    void spawnMoon(int total){
      moons = new CelestialBody[total];
      for(int i = 0; i < moons.length; i++){
        float r = radius / random(2,5);
        float d = random((radius + r), (radius + r)*2);
        float o = random(0.01, 0.05);
        moons[i] = new CelestialBody(r, d, o, (int)random(0, 256), (int)random(0, 256), (int)random(0, 256), 1);
      }
    }

    void show() {
        pushMatrix();
        PVector v2 = new PVector(1,0,1);
        PVector p = v.cross(v2);

        rotate(angle,p.x, p.y, p.z);

        translate(v.x, v.y, v.z);




        if(Type == 4){
          emissive(255, 255, 255);
          pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
          lightFalloff(0, 0, 0.00020); //light falls off right behind the surface of the sun
          ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun
        }
        //shape(globe);
        shape(planet);
        if (moons != null) {
            for (int i = 0; i < moons.length; i++) {
                moons[i].show();
            }
        }
        noStroke();
        popMatrix();
    }

}
...