Hi
How can I go about creating a spheroid in Processing? I need it to be a solid color.
Hi
How can I go about creating a spheroid in Processing? I need it to be a solid color.
I suppose you mean you want more control than the standard sphere() function gives you?
Doing it by hand, one would basically rotate around the y-axis creating quads that are positioned around a vertical semicircle; eg. spherical coordinates: https://en.wikipedia.org/wiki/Spherical_coordinate_system
You’d define the ‘resolution’ or detail of your sphere and then for each determine its vertices like so by stepping through theta and phi.
float r = sin(theta)*_rad;
float rNext = sin(thetaNext)*_rad;
// coordinates of tile corners
float x, y, z;
// corner 0
x = r*cos(phi);
y = cos(theta)*_rad;
z = r*sin(phi);
v0 = new PVector(x, y, z);
// corner 1
x = r*cos(phiNext);
y = cos(theta)*_rad;
z = r*sin(phiNext);
v1 = new PVector(x, y, z);
// corner 2
x = rNext*cos(phiNext);
y = cos(thetaNext)*_rad;
z = rNext*sin(phiNext);
v2 = new PVector(x, y, z);
// corner 3
x = rNext*cos(phi);
y = cos(thetaNext)*_rad;
z = rNext*sin(phi);
v3 = new PVector(x, y, z);
or use scale() plus sphere() to deform the sphere
import peasy.*;
PeasyCam cam;
ArrayList<SphereClass> list = new ArrayList();
void setup() {
size(1300, 900, P3D);
cam=new PeasyCam(this, 400);
// init all spheres
SphereClass newSphere;
newSphere=new SphereClass(23, 23, -230,
5, 5, 5);
list.add(newSphere);
newSphere=new SphereClass(273, 23, -230,
10, 5, 20);
list.add(newSphere);
newSphere=new SphereClass(3, 153, 30,
3, 3, 3);
list.add(newSphere);
}
void draw() {
background(0);
lights();
// loop over all spheres
for (SphereClass sphere : list) {
// show it
sphere.display();
}
// HUD
cam.beginHUD();
fill(255);
text("use peasycam, click on speroids", 23, 23);
cam.endHUD();
}
void mousePressed() {
// loop over all spheres
for (SphereClass sphere : list) {
// select / unselect depending on mouse pos
sphere.selectWhenMouseOver();
}//for
}
// ===========================================================
class SphereClass {
PVector pos; // 3D vector
PVector sizeSphere; // 3D vector
PVector screenPos=new PVector(0, 0); // 2D vector
boolean selected=false;
// constr (pos and size)
SphereClass(float x, float y, float z,
float w, float h, float d) {
pos = new PVector(x, y, z); // 3D vector
sizeSphere = new PVector(w, h, d); // 3D vector
}// constr
void display() {
// draw sphere at pos (x, y, z) coordinate and store 2D screen pos
pushMatrix();
translate(pos.x, pos.y, pos.z);
noStroke();
// we choose the color depending on selected
if (selected)
fill(255, 0, 0); // red
else
fill(0, 0, 255); // blue
// draw the sphere
scale(sizeSphere.x, sizeSphere.y, sizeSphere.z);
sphere(11);
// we monitor the 2D screen pos throughout and store it
screenPos.set(screenX(0, 0, 0), screenY(0, 0, 0));
popMatrix();
// show the 2D screen pos
if (keyPressed)
drawSignX(screenPos);
}
void drawSignX( PVector pos ) {
// Draw a "X" sign
//
float sizeHalf=60;
float x=pos.x;
float y=pos.y;
float z=pos.z;
stroke(255);
line(x-sizeHalf, y-sizeHalf, z, x+sizeHalf, y+sizeHalf, z);
line(x+sizeHalf, y-sizeHalf, z, x-sizeHalf, y+sizeHalf, z);
}
boolean selectWhenMouseOver() {
// select / unselect
if (mouseOver())
selected=true;
else
selected=false;
return selected;
}
boolean mouseOver() {
return
dist(mouseX, mouseY, screenPos.x, screenPos.y) < 50;
}
//
}//class
//