I previously had an issue with a model not loading correctly (see Processing - loading obj File)
https://stackoverflow.com/users/89766/george-profenza helped me solve the problem in chat, and he wanted to post his optimizations to my code publically.
This also solved the original problem described in the question mentioned above.
You can check out the game at https://github.com/jonlit/spacestarprocessing3d
As mentioned in chat there were a few things slightly off with the existing approach and for visiblity, this are the steps taken to address the issues.
Hope this helps other to debug Processing P3D / OBJ issues:
The first step was to identify the slowest pieces of code. This was done using VisualVM.
This highlighted shape() calls were slow (not not why):
Step 2 was to isolate the problem. Why is loading/displaying a couple of obj files slow.
For reference these are the assets:
rock.obj using rockTexture.png (but currently missing .mtl)
cirno_low.obj using cirno_low_u1_v1.jpeg
This is a test sketch loading/display the .obj files as they are:
PShape rock;
PShape cirno;
void setup(){
size(900, 900, P3D);
cirno = loadShape("cirno_low.obj");
rock = loadShape("rock.obj");
int faces = 0;
int vertices = 0;
for(int i = 0 ; i < rock.getChildCount(); i++){
PShape c = rock.getChild(i);
vertices += c.getVertexCount();
faces++;
}
println("rock faces", faces, "vertices", vertices);
}
void draw(){
background(0);
lights();
translate(width * 0.5, height * 0.5, 0);
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, PI, -PI));
for(int i = 0 ; i < 81; i++){
pushMatrix();
translate(i % 9 * 100 - width * 0.5,
i / 9 * 100 - height * 0.5, -100);
rotate(map(i, 0, 80, -PI, PI), 0.5, 0.5, 0);
scale(0.5);
shape(rock);
popMatrix();
}
pushMatrix();
scale(10);
shape(cirno);
popMatrix();
surface.setTitle((int)frameRate + "fps");
}
It renders pretty fast, without textures though:
The game uses setTexture() and interestingly enough this drops the frame rate:
PShape rock;
PShape cirno;
void setup(){
size(900, 900, P3D);
cirno = loadShape("cirno_low.obj");
cirno.setTexture(loadImage("cirno_low_u1_v1.jpeg"));
rock = loadShape("rock.obj");
rock.setTexture(loadImage("rockTexture.png"));
int faces = 0;
int vertices = 0;
for(int i = 0 ; i < rock.getChildCount(); i++){
PShape c = rock.getChild(i);
vertices += c.getVertexCount();
faces++;
}
println("rock faces", faces, "vertices", vertices);
}
void draw(){
background(0);
lights();
translate(width * 0.5, height * 0.5, 0);
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, PI, -PI));
for(int i = 0 ; i < 81; i++){
pushMatrix();
translate(i % 9 * 100 - width * 0.5,
i / 9 * 100 - height * 0.5, -100);
rotate(map(i, 0, 80, -PI, PI), 0.5, 0.5, 0);
scale(0.5);
shape(rock);
popMatrix();
}
pushMatrix();
scale(10);
shape(cirno);
popMatrix();
surface.setTitle((int)frameRate + "fps");
}
Without checking the PShape source code, the assumption is behind the scenes the PShape has to do more work behind the scenes, because loading an .obj file with an .mtl (which helps load the texture as well) render just fine.
Here's the Processing > Examples > Basics > Shape > LoadDisplayOBJ example tweaked: it renders 1250 instances at 60fps:
/**
* Load and Display an OBJ Shape.
*
* The loadShape() command is used to read simple SVG (Scalable Vector Graphics)
* files and OBJ (Object) files into a Processing sketch. This example loads an
* OBJ file of a rocket and displays it to the screen.
*/
PShape rocket;
float ry;
public void setup() {
size(900, 900, P3D);
rocket = loadShape("rocket.obj");
}
public void draw() {
background(0);
lights();
translate(width/2, height/2 + 100, -200);
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, PI, -PI));
int nc = 1250;
float nr = sqrt(nc);
float sp = 150;
for(int i = 0 ; i < nc; i++){
pushMatrix();
translate(i % nr * sp - width * 0.5,
i / nr * sp - height * 0.5, -sp);
//rotate(map(i, 0, 80, -PI, PI), 0.5, 0.5, 0);
rotateZ(PI + radians(i));
rotateY(ry);
scale(0.5);
shape(rocket);
popMatrix();
}
//rotateZ(PI);
//rotateY(ry);
//shape(rocket);
ry += 0.02;
surface.setTitle((int)frameRate + "fps");
}
This pointed out another issue with how the obj files were used in the game:
each new Star() for example would load the .obj again.
public class Star extends UFO {
public Star (int x, int y, int spd) {
posX = x;
posY = y;
rot = int(random(0, 360));
speed = spd;
symbol = loadShape("rock.obj");
symbol.setTexture(rockTexture); //<>//
...
Ideally these meshes would be loaded once in setup(), with .mtl files and references passed to each instance needing to render them via shape().
This would allow instancing to work as it's the same geometry rendered multiple times. Reloading the same obj file into new memory addresses for each instance would result in many duplicated resources.
One quick fix for the .mtl issue is to simply import the obj in Blender, select it, apply the texture and export it:
(This would also be a good opportunity to rotate/scale models so when they're loaded in Processing, no additional transforms are required and they all can live an in easy to understand coordinate system)
The contents of the exported files I've used are:
cirno_lowWithMTL.mtl
cirno_lowWithMTL.obj
cirno_lowWithMTLDecimated.mtl
cirno_lowWithMTLDecimated.obj
They load/display (with textures) at 60fps (due to the .mtl files)
The recommended optimisation steps (other than using .obj with .mtl files and loading once and re-using mulitple times) are:
avoid extending fixed length arrays (e.g. kryptonit = (Kryptonit[]) append(kryptonit, new Kryptonit(int(random(50, width-150)), int(random(-300, 0))));). ArrayLists are better suited for resizing. In this case in particular a fixed length array is great, as long as it's objects are pre-allocated once, then the positions / states of the objects are updated (e.g. outside of screen objects are marked for re-use and hidden and instead of new objects, existing objects have positions visiblity/reset): in other words Object Pooling)
if meshes are displayed from a single point of view with only rotation on Z axis and position affecting them, they could be images (sprites) instead. (e.g. exporting a static image with alpha channel from Blender at the right scale (or using PGraphics to do this at runtime))
once meshes are loaded, instead of using transformations on them in draw() (e.g. symbol.rotateX(value), which will affect every single vertex in the PShape, use pushMatrix()/popMatrix() call with shape() so simply render the same geometry with different tranformations.
For reference this is the full program with minimal tweaks around loading/using .obj files efficiently (with the old approach commented out and few notes around those regions):
import com.dhchoi.CountdownTimer;
import com.dhchoi.CountdownTimerService;
import controlP5.*;
int zeit;
int punkte;
int leben;
int schwierigkeit = 20;
int zustand = 1;
int boost = 0;
int highscore = 0;
int minuten = 0;
int changeLevel = 0;
boolean paused = true;
boolean gameOver;
JSONArray saves = new JSONArray();
PFont gameOverFont;
PFont gameOverFontSmall;
CountdownTimer timer1 = CountdownTimerService.getNewCountdownTimer(this).configure(1000, 60000);
CountdownTimer kryptonitAnimationTimer1 = CountdownTimerService.getNewCountdownTimer(this).configure(10, 250);
boolean[] keysPressed = new boolean[65536];
ControlP5 cp5;
Star[] stars;
Kryptonit[] kryptonit;
Raumschiff raumschiff;
Player[] players;
String textValue = "";
PImage cirnoTexture;
PImage rockTexture;
PShape cirno;
PShape rock;
void loadMeshes(){
rock = loadShape("rockWithMTL.obj");
rock.scale(0.2);
cirno = loadShape("cirno_lowWithMTL.obj");
cirno.rotateY(HALF_PI);
cirno.rotateZ(HALF_PI * -1);
cirno.scale(5);
}
void settings()
{
//size(800, 400, P3D);
fullScreen(P3D);
smooth(8);
System.setProperty("jogl.disable.openglcore", "true");
}
void setup() {
surface.setResizable(true);
//println("loading textures");
//cirnoTexture = loadImage("cirno_low_u1_v1.jpeg");
//rockTexture = loadImage("rockTexture.png");
//println("finished loading textures: " + cirnoTexture);
loadMeshes();
stars = new Star[0];
kryptonit = new Kryptonit[0];
raumschiff = new Raumschiff(width/2, height/4*3, cirno);
leben = 5;
gameOverFont = createFont("Arial", 36, true);
gameOverFontSmall = createFont("Arial", 16, true);
for (int i = 0; i < schwierigkeit; i++) {
stars = (Star[]) append(stars, new Star(int(random(50, width-150)), int(random(50, height-100)), int(random(5, 15)), rock));
}
players = new Player[0];
cp5 = new ControlP5(this);
cp5.addTextfield("Name")
.setPosition(width/2-100, height/3*2-20)
.setSize(200, 40)
.setFont(gameOverFontSmall)
.setFocus(false)
.setColor(color(255))
.setAutoClear(false)
.setText("Name")
.setLabel("")
.hide()
.lock()
;
}
void draw() {
background(0);
lights();
switch (zustand) {
case 0:
break;
case 1:
fill(255);
text("Zeit:\t" + minuten + ":" + zeit, width-100, 50);
text("Punkte:\t" + punkte, width-100 , 100);
text("Leben:\t" + leben, width-100, 150);
text("Highscore:\t" + highscore, width-100, 200);
text("schwierigkeit:\t" + schwierigkeit, width-100, 250);
try {
for (int i = 0; i < players.length; i++) {
text(players[i].getName() + " " + players[i].getScore(), width-100, 300+15*i);
}
for (int i = 0; i < stars.length; i++) {
stars[i].zeichnen();
stars[i].drehen(random(0, 0.05), random(0, 0.05), random(0, 0.05));
}
for (int i = 0; i < kryptonit.length; i++) {
kryptonit[i].zeichnen();
}
if (kryptonitAnimationTimer1.getTimeLeftUntilFinish() != .0f) {
raumschiff.zeichnen(color(350-kryptonitAnimationTimer1.getTimeLeftUntilFinish(), 100, 100));
} else {
raumschiff.zeichnen(color(100, 100, 100));
}
} catch (Exception e) { e.printStackTrace(); }
noFill();
stroke(100);
rect(50, 50, width-200, height-150);
fill(0);
noStroke();
rect(0, 0, width-150, 48);
if (gameOver) {
pushMatrix();
fill(255);
textAlign(CENTER, CENTER);
textFont(gameOverFont, 36);
textSize(34);
text("GAME OVER!", width/2, height/2);
textFont(gameOverFontSmall, 16);
textSize(16);
text("Press ENTER to resume", width/2, height/2+30);
cp5.get(Textfield.class, "Name").unlock();
cp5.get(Textfield.class, "Name").show();
popMatrix();
}
else if (paused) {
pushMatrix();
fill(255);
textAlign(CENTER, CENTER);
textFont(gameOverFont, 36);
textSize(34);
text("PAUSED!", width/2, height/2);
textFont(gameOverFontSmall, 16);
textSize(16);
text("PRESS ANY KEY TO RESUME", width/2, height/2+30);
popMatrix();
}
break;
default :
background(0);
break;
}
for (int i = 0; i < stars.length; i++) {
if (stars[i].isVisible && sqrt((stars[i].posX - raumschiff.posX) * (stars[i].posX - raumschiff.posX) + (stars[i].posY - raumschiff.posY) * (stars[i].posY - raumschiff.posY) ) < 25){
stars[i].isVisible = false;
punkte+=stars[i].speed;
if (changeLevel > 0) {
changeLevel--;
}
}
}
if (punkte > highscore) {
highscore = punkte;
}
if (kryptonit.length < schwierigkeit / 5) {
//kryptonit = (Kryptonit[]) append(kryptonit, new Kryptonit(int(random(50, width-150)), int(random(-300, 0))));
}
if (stars.length < schwierigkeit) {
stars = (Star[]) append(stars, new Star(int(random(50, width-150)), int(random(-300, 0)), int(random(5, 15)), rock));
}
for (int i = 0; i < kryptonit.length; i++) {
if (kryptonit[i].isVisible && sqrt((kryptonit[i].posX - raumschiff.posX) * (kryptonit[i].posX - raumschiff.posX) + (kryptonit[i].posY - raumschiff.posY) * (kryptonit[i].posY - raumschiff.posY) ) < 25){
kryptonit[i].isVisible = false;
leben-=1;
kryptonitAnimationTimer1.start();
}
}
if (leben < 1){
gameOver = true;
}
if (punkte % 500 <= 20 && punkte % 500 >= 0 && changeLevel == 0 && zustand == 1) {
schwierigkeit+=5;
changeLevel = 5;
}
if (punkte % 500 > 20) {
changeLevel = 0;
}
if (!paused) {
try {
if (!gameOver) {
for (int i = 0; i < stars.length; i++) {
stars[i].bewegen(schwierigkeit/stars[i].speed+boost);
if (stars[i].posY > height-100){
stars[i] = null;
stars[i] = new Star(int(random(58, width-202)), int(random(-300, 0)), int(random(5, 15)), rock);
}
}
for (int i = 0; i < kryptonit.length; i++){
kryptonit[i].bewegen(schwierigkeit/10+boost);
if (kryptonit[i].posY > height-100){
kryptonit[i] = null;
kryptonit[i] = new Kryptonit(int(random(58, width-202)), int(random(-300, 0)));
}
}
}
} catch (Exception e) { e.printStackTrace(); }
}
if (keysPressed[56]){
boost = 5;
}
else {
boost = 0;
}
if (keysPressed[52] && !gameOver && !paused){
try {
raumschiff.bewegen(-7);
if (keysPressed[32]) {
raumschiff.bewegen(-10);
}
} catch (Exception e) { e.printStackTrace(); }
}
if (keysPressed[54] && !gameOver && !paused){
try {
raumschiff.bewegen(7);
if (keysPressed[32]) {
raumschiff.bewegen(10);
}
} catch (Exception e) { e.printStackTrace(); }
}
surface.setTitle((int)frameRate+"fps");
}
void keyPressed() {
if (gameOver && key == ENTER) {
players = (Player[]) append(players, new Player(cp5.get(Textfield.class, "Name").getText(), punkte));
cp5.get(Textfield.class, "Name").lock();
cp5.get(Textfield.class, "Name").hide();
for (int i = 0; i < saves.size(); i++) {
JSONObject playerJSONObject = new JSONObject();
playerJSONObject.setInt("id", i);
playerJSONObject.setString(cp5.get("Name", cp5.get(Textfield.class, "Name").getText()).toString(), "");
playerJSONObject.setInt("score", punkte);
}
saveJSONArray(saves, "data/highscores.json");
schwierigkeit = 20;
paused = true;
gameOver = false;
leben = 5;
punkte = 0;
timer1.reset(CountdownTimer.StopBehavior.STOP_IMMEDIATELY);
timer1.start();
zeit = 0;
stars = null;
stars = new Star[0];
for (int i = 0; i < schwierigkeit; i++) {
stars = (Star[]) append(stars, new Star(int(random(50, width-150)), int(random(50, height-100)), int(random(5, 15)), rock));
}
kryptonit = null;
kryptonit = new Kryptonit[0];
}
keysPressed[key] = true;
}
void keyReleased() {
keysPressed[key] = false;
}
void keyTyped() {
if (key == 'p' || key == 'P') {
if (!gameOver) {
paused = !paused;
if (paused) {
timer1.stop(CountdownTimer.StopBehavior.STOP_IMMEDIATELY);
}
else {
timer1.start();
}
}
}
if (paused && !gameOver && key != 'p' && key != 'P') {
paused = false;
timer1.start();
}
}
void onTickEvent(CountdownTimer t, long timeLeftUntilFinish) {
if (t == timer1) {
zeit++;
}
}
void onFinishEvent(CountdownTimer t) {
if (t == timer1) {
timer1.reset(CountdownTimer.StopBehavior.STOP_AFTER_INTERVAL);
timer1.start();
zeit = 0;
minuten++;
}
}
abstract class Flugobjekt {
public int posX;
public int posY;
public int rot;
public int speed;
boolean isVisible = true;
PShape symbol;
abstract void bewegen (int amount);
}
abstract class UFO extends Flugobjekt {
}
public class Star extends UFO {
float rotationX, rotationY, rotationZ;
public Star (int x, int y, int spd, PShape symbol) {
posX = x;
posY = y;
rot = int(random(0, 360));
speed = spd;
// use a reference to the preloaded PShape (instead of loading a the .obj again for each instance)
this.symbol = symbol;
//symbol = loadShape("rockWithMTL.obj");
//symbol.setTexture(rockTexture);
//symbol.scale(0.2);
/*
fill(255);
stroke(255);
strokeWeight(2);
symbol = createShape();
symbol.beginShape();
symbol.vertex(0, -5);
symbol.vertex(1.4, -2);
symbol.vertex(4.7, -1.5);
symbol.vertex(2.3, 0.7);
symbol.vertex(2.9, 4.0);
symbol.vertex(0, 2.5);
symbol.vertex(-2.9, 4);
symbol.vertex(-2.3, 0.7);
symbol.vertex(-4.7, -1.5);
symbol.vertex(-1.4, -2);
symbol.endShape(CLOSE);
/*/
}
public void zeichnen (){
// skip if PShape (or it's texture) isn't loaded yet)
if(symbol == null){
return;
}
if (isVisible) {
pushMatrix();
translate(posX, posY);
//rotate(rot);
rotateX(rotationX);
rotateY(rotationY);
rotateZ(rotationZ);
//scale(0.2);
shape(symbol);
popMatrix();
}
}
public void bewegen (int amount) {
posY = posY + amount;
}
public void drehen (float xAmount, float yAmount, float zAmount) {
rotationX += xAmount;
rotationY += yAmount;
rotationZ += zAmount;
// symbol.rotateX means all vertices inside the shape will be updated
// use rotateX() then shape() to simply render the same underlying PShape vertex data without updating it all the time
//symbol.rotateX(xAmount);
//symbol.rotateY(yAmount);
//symbol.rotateZ(zAmount);
}
}
public class Kryptonit extends UFO {
public Kryptonit (int x, int y) {
posX = x;
posY = y;
rot = int(random(0, 360));
fill(0);
stroke(255, 0, 0);
strokeWeight(2);
symbol = createShape();
symbol.beginShape();
symbol.vertex(0, -5);
symbol.vertex(1.4, -2);
symbol.vertex(4.7, -1.5);
symbol.vertex(2.3, 0.7);
symbol.vertex(2.9, 4.0);
symbol.vertex(0, 2.5);
symbol.vertex(-2.9, 4);
symbol.vertex(-2.3, 0.7);
symbol.vertex(-4.7, -1.5);
symbol.vertex(-1.4, -2);
symbol.endShape(CLOSE);
}
public void zeichnen (){
if (isVisible) {
pushMatrix();
translate(posX, posY);
rotate(rot);
shape(symbol);
popMatrix();
}
}
public void bewegen (int amount) {
posY = posY + amount;
}
}
public class Raumschiff extends Flugobjekt {
public Raumschiff (int x, int y, PShape symbol) {
posX = x;
posY = y;
fill(100);
noStroke();
this.symbol = symbol;
//symbol = loadShape("cirno_lowWithMTL.obj");//createShape(ELLIPSE, 0, 0, 50, 50);
//symbol.setTexture(cirnoTexture);
//symbol.rotateY(HALF_PI);
//symbol.rotateZ(HALF_PI * -1);
////symbol.rotateX(0.5);
//symbol.scale(5);
/* (Raumschiff)
symbol = createShape();
symbol.beginShape();
symbol.vertex(25, 0);
symbol.vertex(30, 5);
symbol.vertex(30, 5);
symbol.vertex(32, 12);
symbol.vertex(28, 20);
symbol.vertex(31, 28);
symbol.vertex(27, 25);
symbol.vertex(25, 29);
symbol.vertex(23, 25);
symbol.vertex(19, 28);
symbol.vertex(22, 20);
symbol.vertex(18, 12);
symbol.vertex(20, 5);
symbol.endShape(CLOSE);
//*/
}
public void zeichnen (color farbe){
if (isVisible) {
pushMatrix();
symbol.setFill(farbe);
translate(posX, posY);
rotate(rot);
shape(symbol);
popMatrix();
}
}
public void bewegen (int amount) {
posX+=amount;
if (posX < 50) posX = 50;
if (posX > width-150) posX = width-150;
}
}
public class Player {
private String name;
private int score;
public Player (String n, int s) {
name = n;
score = s;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
Here's an example of pre-allocating a number of objects to be reused (a-la object pooling), instead of constant reinstantiation (which has it's costs):
PShape rock;
int numRocks = 25;
Rock[] rocks = new Rock[numRocks];
float halfWidth;
float halfHeight;
void setup(){
size(900, 900, P3D);
rock = loadShape("rockWithMTL.obj");
// ideally the mesh would already been scaled down to avoid this
rock.scale(0.2);
halfWidth = width * 0.5;
halfHeight = height * 0.5;
for(int i = 0 ; i < numRocks; i++){
rocks[i] = new Rock(rock, random(-halfWidth, halfWidth), random(-halfHeight, halfHeight));
}
}
void draw(){
background(0);
lights();
translate(width * 0.5, height * 0.5, 0);
for(int i = 0 ; i < numRocks; i++){
rocks[i].draw();
}
surface.setTitle((int)frameRate + "fps");
}
class Rock{
PShape mesh;
PVector position = new PVector();
PVector velocity = new PVector();
PVector rotationAxis = new PVector();
float rotationAngle = 0;
Rock(PShape mesh, float x, float y){
this.mesh = mesh;
position.x = x;
position.y = y;
velocity.y = random(1, 10);
// pick a random rotation axis
rotationAxis.set(random(1), random(1), random(1));
}
void draw(){
// update
// increment position
position.add(velocity);
// increment rotation
rotationAngle += 0.1;
// object pool behaviour: reset if off screen (no need to re-allocate a new instance)
if(position.y > halfHeight + 100){
position.x = random(-halfWidth, halfWidth);
position.y = -halfHeight - 100;
}
// draw
pushMatrix();
translate(position.x, position.y, position.z);
rotate(rotationAngle, rotationAxis.x, rotationAxis.y, rotationAxis.z);
shape(mesh);
popMatrix();
}
}
Also, here's a super basic demo on encapsulating states. It's a bit hacky because each state know of the other, but shows each could behave as it's own "sketch" that can live in it's own tab and only override it's specific behaviour:
StartScreen start;
GameScreen game;
HighScoreScreen highScore;
StateScreen currentScreen;
void setup(){
size(300, 300);
textAlign(CENTER, CENTER);
textSize(18);
start = new StartScreen();
game = new GameScreen();
highScore = new HighScoreScreen();
currentScreen = start;
}
void draw(){
background(0);
currentScreen.draw();
}
void keyPressed(){
currentScreen.keyPressed();
}
class StateScreen {
StateScreen(){
setup();
}
void setup(){ println(this,"setup()"); }
void draw(){}
void keyPressed(){}
}
class StartScreen extends StateScreen{
void draw(){
fill(sin(frameCount * 0.1) * 127);
text("push any key to\nstart", width * 0.5, height * 0.5);
}
void keyPressed(){
currentScreen = game;
}
}
class GameScreen extends StateScreen{
void draw(){
fill(0, sin(frameCount * 0.1) * 127, 0);
text("push SPACE key to go to\nhigh score screen", width * 0.5, height * 0.5);
}
void keyPressed(){
currentScreen = highScore;
}
}
class HighScoreScreen extends StateScreen{
void draw(){
fill(random(255), random(255), random(255));
text("push SPACE key to go to\nstart screen", width * 0.5, height * 0.5);
}
void keyPressed(){
currentScreen = start;
}
}
I am trying to build a solar system model (only with the Earth, the Sun and the Moon) on Processing (version 3.4), using the Java Mode. I am new to processing and I have only used Java in this context (hence, I am also new to Java).
I have something which is partially working:
That's my code. First tab:
Planet sun;
void setup() {
size(900, 1200);
sun = new Planet(100, 10, 0);
sun.spawnMoons(1,2);
}
void draw() {
background(0);
translate(750, 900/2);
sun.show();
sun.orbit();
}
Second tab:
class Planet {
float radius;
float distance;
Planet[] planets;
float angle;
float orbitspeed;
Planet(float r, float d, float o) {
radius = r;
distance = 400;
angle = PI;
orbitspeed = o;
}
void orbit() {
angle = angle + orbitspeed;
if (planets != null) {
for (int i = 0; i < planets.length; i++) {
planets[i].orbit();
}
}
}
void spawnMoons(int total, int level) {
planets = new Planet[total];
for (int i = 0; i < planets.length; i++) {
float r = radius/(level*2);
float d = distance/(level*4);
float o = 0.01;
planets[i] = new Planet(r, d/(level*8), o);
if (level < 3) {
int num = 2;
planets[i].spawnMoons(num, level+1);
}
}
}
void show() {
pushMatrix();
fill(255, 100);
rotate(angle);
translate(distance, 0);
ellipse(0, 0, radius*2, radius*2);
if (planets != null) {
for (int i = 0; i < planets.length; i++) {
planets[i].show();
}
}
popMatrix();
}
}
However, my "Moon" is too far from my "Earth". I am trying to fix it, but I can't. Considering the way I built it, if I change the value on 11st line (second tab), it won't solve the problem:
distance = 10;
Considering the way I built it, the distance between the Earth and the Sun it is the same as the distance between the Earth and its moon.
I was able to make the radius of each object proportional to each other. Nonetheless, I am failing to do the same with the distance between them. The line bellow was supposed to keep the proportionality on distance but it fails:
float d = distance/(level*4);
How do I fix this?
Thanks.
This is the error:
Planet(float r, float d, float o) {
radius = r;
distance = 400; //<== here
angle = PI;
orbitspeed = o;
}
In the constructor the distance for each new planet is set at 400, so the logic in spawnMoons() does nothing.
If you apply the changes below, it will work as you want and you can start tweaking ;)
//in setup()
sun = new Planet(100, 400, 0);
//in the planet constructor
distance = d;
//in spawnMoons()
float d = distance/level;
planets[i] = new Planet(r, d, o);
I'm new to Processing. I want to learn how to graph some modelling I am doing so I'm using gwoptics to do it. They have an example called RollingGraph. This basically plots out whatever you want along a time dependent scrolling x axis.
The problem is that I don't quite understand how I get it to plot what I want.
I have an array, let's say which plots ellipses randomly on a canvas and these rotate randomly every frame. How can I get the Rolling Graph to plot the sum of all the rotations, which could be circle.rot?
So far I have the best MCVE I could get:
import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;
class eq implements ILine2DEquation{
public double computePoint(double x,int pos) {
return mouseX; //////HOW DO I GET THIS TO RETURN THE SUM OF circle.rot??????
}
}
class eq2 implements ILine2DEquation{
public double computePoint(double x,int pos) {
return mouseY;
}
}
class eq3 implements ILine2DEquation{
public double computePoint(double x,int pos) {
if(mousePressed)
return 400;
else
return 0;
}
}
RollingLine2DTrace roll,roll2,roll3;
Graph2D g;
class Circle{
public float x1;
public float y1;
public float x2;
public float y2;
public color cB;
public float rot;
public float authority;
public float fert = 1;
public float r = x1; //radius
public Circle(float x1, float y1, float x2, float y2, color tempcB, float rot, float authority, float fert){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.cB = tempcB;
this.authority = random(255);
this.fert = random(1);
this.rot= random(360);
}
}
public ArrayList<Circle> circles = new ArrayList<Circle>();
void setup(){
size(1000, 1000);
frameRate(6);
rectMode(CENTER);
ellipseMode(CENTER);
int sec = second();
roll = new RollingLine2DTrace(new eq() ,100,0.01f);
roll.setTraceColour(0, 255, 0);
roll2 = new RollingLine2DTrace(new eq2(),100,0.01f);
roll2.setTraceColour(255, 0, 0);
roll3 = new RollingLine2DTrace(new eq3(),100,0.05f);
roll3.setTraceColour(255, 255, 255);
g = new Graph2D(this, 400, 200, false);
g.setYAxisMax(600);
g.addTrace(roll);
g.addTrace(roll2);
g.addTrace(roll3);
g.position.y = 50;
g.position.x = 100;
g.setYAxisTickSpacing(100);
g.setXAxisMax(5f);
smooth();
background(204);
noStroke();
fill(255, 204,100);
for(int i = 1; i < 48; i++){
float r = random(100,height-200);
float s = random(100,width-200);
float t = 20;
float u = 20;
circles.add(new Circle(r,s,t,u,color(100,14,14),random(360),color(100,14,14),random(10)));
}
}
void draw() {
background(204);
g.draw();
for(Circle circle : circles){
pushMatrix();
translate(circle.x1, circle.y1);
rotate(random(360));
translate(-circle.x1, -circle.y1);
fill(circle.authority);
strokeWeight(0);
stroke(100,0,0);
rect(circle.x1, circle.y1, 24, 36,0, 0, 12, 18);
popMatrix();
}
}
If I understand your question, all you have to do is iterate over the instances and calculate the total.
First, you're going to need a data structure that holds all of your instances. You've said you're using an array, so it sounds like you've go that covered. You have to make sure that this array is in scope for the next step, so this probably means declaring it at the sketch level (not inside a function or class).
Secondly, you're going to need a for loop that iterates over the instances in the data structure. You can use a variable to add up the total. Something like this:
float total = 0;
for(Circle c : yourCircleArray){
total += c.rot;
}
You might put that into a function so that you can call it whenever you want.
Edit: Looking at your code more closely, you actually have an ArrayList, not an array. It looks like it's already initialized at the sketch level, so all you need to do is this:
public double computePoint() {
float total = 0;
for(Circle c : circles){
total += c.rot;
}
return total;
}
If you can't get that working, try creating an MCVE by eliminating your dependencies on whatever library you're importing at the top of your sketch. Remember that an MCVE is supposed to narrow it down to one specific problem (how to get a total from an array), not do your whole end goal.
I can't get around a peculiar problem with SimpleOpenNI for Processing ao I'm asking for your help.
I'd like to store snapshots of pixel depth data (returned by .depthMapRealWorld() method as PVector arrays) on discrete time intervals, then process them further for a presentation. I tried adding them in an ArrayList, but it seems that the depthMapRealWorld() method is returning only a reference to a current depth data, not a real array. I tried in this sequence:
Just getting the data and adding it in an arraylist. On every call of the update() method the whole arraylist contained the same PVector array, even if the array at the zero position was added many iterations away!
Then I made the PVector array, along with its creation time, part of a class. Rewrote the sketch a little, but it didn't help. All of the arrays in the arraylist werw still the same.
Finally, in the constructor of the class, I "manually" copied the xyz coordinates of every vector from the PVector array into a int array. That seemed to solve the problem - the int arrays in the arraylist are now different from each other. But this solution introduced serious performance problems.
The question is: is there a more efficient way of storing these PVector arrays and retaining their value?
code:
import processing.opengl.*;
import SimpleOpenNI.*;
SimpleOpenNI kinect;
float rotation = 0;
int time = 0;
ArrayList dissolver;
ArrayList<Integer> timer;
int pSize = 10;
Past past;
void setup() {
dissolver = new ArrayList();
timer = new ArrayList();
size(1024, 768, OPENGL);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
translate(width/2, height/2, -100);
rotateX(radians(180));
stroke(255);
}
void draw() {
background(0);
translate(width/2, height/2, 500);
rotateX(radians(180));
kinect.update();
stroke (255, 255, 255);
past = new Past (kinect.depthMapRealWorld(), time);
if (dissolver.size() == pSize) { //remove the oldest arraylist element if when list gets full
dissolver.remove(0); //
}
if (time % 20 == 0) {
dissolver.add (past);
Past p1 = (Past) dissolver.get (0);
float [][] o2 = p1.getVector();
println ("x coord of a random point at arraylist position 0: " + o2[50000][0]); //for testing
}
if (dissolver.size() == pSize-1) {
//dissolve ();
}
time ++;
}
void dissolve () { //from the previous nonworking version; ignore
for (int offset = 0; offset < pSize-1; offset ++) {
PVector[] offPoints = (PVector[]) dissolver.get (offset);
int offTime = timer.get(offset);
for (int i = 0; i < offPoints.length; i+=10) {
int col = (time-offTime)*2; //why??
stroke (255, 0, col);
PVector currentPoint = offPoints[i];
if (currentPoint.z <1500) {
point(currentPoint.x, currentPoint.y, currentPoint.z); // - 2*(time-offTime) + random(0, 100)
}
}
}
}
class Past {
private PVector [] depth; //should contain this, not int
private float [][] depth1;
private int time;
Past (PVector [] now, int t) {
//should be like this: depth = now;
//clumsy and performancewise catastrophic solution below
depth1 = new float [now.length][3];
for (int i = 0; i< now.length; i+=10) {
PVector temp = now[i];
depth1 [i][0] = temp.x;
depth1 [i][1] = temp.y;
depth1 [i][2] = temp.z;
}
//arrayCopy(now, depth); this didn't work either
time = t;
}
float [][] getVector () {
return depth1;
}
int getTime () {
return time;
}
}
If I understood correctly, you want to store the 3D positions(ArrayList of PVectors) for each frame, right ?
If so, you should be able to simply store PVectors and reference them later.
Here's a basic sketch to illustrate this:
import processing.opengl.*;
import SimpleOpenNI.*;
SimpleOpenNI kinect;
ArrayList<ArrayList<PVector>> frames = new ArrayList<ArrayList<PVector>>();
ArrayList<PVector> frame;
boolean isRecording = true;
boolean isRecFrame;
void setup() {
size(1024, 768, OPENGL);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
stroke(255);
}
void draw() {
background(0);
translate(width/2, height/2, 500);
rotateX(PI);
translate(0,0,-1000);
kinect.update();
if(isRecording){
isRecFrame = (frameCount % 20 == 0);//record every 20 frames
int[] depthMap = kinect.depthMap();
int steps = 5; // to speed up the drawing, draw every N point
int index;
PVector realWorldPoint;
if(isRecFrame) frame = new ArrayList<PVector>();
for(int y=0;y < kinect.depthHeight();y+=steps)
{
for(int x=0;x < kinect.depthWidth();x+=steps)
{
index = x + y * kinect.depthWidth();
if(depthMap[index] > 0)
{
realWorldPoint = kinect.depthMapRealWorld()[index];
point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
if(isRecFrame) frame.add(realWorldPoint.get());
}
}
}
if(isRecFrame) frames.add(frame);
}else{//playback
ArrayList<PVector> currentFrame = frames.get(frameCount%frames.size());//playback is faster than recording now for testing purposes - add a decent frame counter here at some point
for(PVector p : currentFrame) point(p.x,p.y,p.z);
}
}
void keyPressed(){
if(key == ' ') isRecording = !isRecording;
}
Use the SPACE key to toggle between recording and playback.
The main thing to note is I'm storing a copy of the real world position for each depth pixel (frame.add(realWorldPoint.get());). Another thing to keep in mind is that currently you're storing these coordinates in memory which at some point will fill. If you only store a limited number of frames that should be fine, if not you might want to save to the points to disk. This way you can reuse recordings with other sketches. A basic way would be to sore them in a csv file:
void saveCSV(ArrayList<PVector> pts){
String csv = "x,y,z\n";
for(PVector p : pts) csv += p.x + "," + p.y + "," + p.z + "\n";
saveStrings("frame_"+frameCount+".csv",csv.split("\n"));
}
Another would be to use a more suitable format for point clouds, like PLY.
Saving an ASCII PLY is fairly straight forward:
void savePLY(ArrayList<PVector> pts){
String ply = "ply\n";
ply += "format ascii 1.0\n";
ply += "element vertex " + pts.size() + "\n";
ply += "property float x\n";
ply += "property float y\n";
ply += "property float z\n";
ply += "end_header\n";
for(PVector p : pts)ply += p.x + " " + p.y + " " + p.z + "\n";
saveStrings("frame_"+frameCount+".ply",ply.split("\n"));
}
You can later open/explore/process these files with tools like MeshLab.