SIDEBAR
»
S
I
D
E
B
A
R
«
Creating an asteroid belt
April 23rd, 2016 by Red Lion

Althought it looks a bit boring but I’m writing this post in memory of the first “class” I ever created.

If you want to create a group of objects that have the same features in Processing, you’ll probably need to create a “class” to define the features of this particular object. For example, I am trying to create an asteroid belt in the solar system model we’ve built in class. An asteroid would be a random sized sphere orbiting around the sun, and an asteroid belt would be a group of these asteroids.

My first attempt to create a class ended in failure. I tried to refer to tutorials and some class Tom has written and figure out how the class works, but didn’t get the class working. Then I tried to create a function (void asteroid(){}) instead, but if you draw a function with random numbers, the function will refresh the random number every frame, causing totally ridiculous flashing asteroids. So I guess I still need to stick to writing a class. After nearly 3 hours of banging keyboard in the library, I finally managed to create my asteroid belt. I finally found that the problem was that I wrote the random() in the class, not in the setup.

2016-4-23 16-33-13

 

The class:

class Asteroid{
float col;//colour of asteroid
float spin;//orbit speed
float wiggle;//a tiny Z axis offset effect
float distance;//distance from the sun
float diametre;//diametre of asteroid

Asteroid(float _col,float _spin,float _wiggle,float _distance,float _diametre){
col = _col;
spin = _spin;
wiggle = _wiggle;
distance = _distance;
diametre = _diametre;

}

void spinning(){
pushMatrix();
rotateY(angle*spin);
rotateZ(wiggle/500);
translate(distance,0,0);
fill(col,col,col);
sphere(diametre);
popMatrix();
}

}


Comments are closed

»  Substance:WordPress   »  Style:Ahren Ahimsa