So in this program, you make spheres with the click of a button. Im trying to add a texture to each sphere that I make, but nothing I try seems to work, even if I put it in the class.
Here is the texture Im trying to use. Here is the original code:
//SPHRE PROGRAM MADE BY MILES JORDAN
// This program is used to creat sphere objects that drop and bounce on the floor.
// The lighting of the spheres change color when you press ENTER (RETURN if you are on a Mac).
// You must make a ball with the mouse to change the color fist, so that the computer keyboard may recognise you are on the program.
// The green, directional lights slightly change according to where the cursor is.
// The program now can increase the size of the spheres
// Pressing the R key can now generate spheres at random areas
// Spheres now dissapear after 7 seconds
// A string is used to make several spheres appear in certain coordinates
ArrayList<Ball> balls = new ArrayList<Ball>();
float gravity = 0.1;
//arrays are used to store objects, and an array list can be used to make multiple objects, as it can increase better then a normal array
float lightvalue = 0;
//the light value will be used to change the light type, 0 for regular lights function, 1 for directional, 2 for ambient, 3 for spot light. To change the light, press Enter
float ballsize = 10;
//the size of the ball will increase from 1-0
float rX;
float rY;
// two variables specifying random values in the x and y value. rX is x value and rY is y value
PFont f;
//String[] lines = loadStrings("balls.csv");
void setup() {
size(1500, 700, P3D);
// start with one ball
balls.add( new Ball(width/2, 0, 0)); //
background(0);
frameRate(60);
f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
// Ball is the datatypye of the stuff in the Array, b is the temporary name for each element in the array, balls is the name of the array to iterate through
for( int i=balls.size()-1; i>=0; i--){ //update() and dislay() are methods, in which b is using, and the dot "." connects the two, allowing b to use the two methods
balls.get(i).update(); //update is used to improve the falling of the balls
balls.get(i).display(); //display is used to make the balls, constantly
if( balls.get(i).isDead() ){
balls.remove(i);
}
}
}
void mousePressed() {
balls.add(new Ball( mouseX, mouseY, 0));
//this adds the object
}
class Ball {
float x;
float y;
float z;
float speed;
int t;
// a constructor:
Ball(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
speed = 0;
t = millis() + 7000;
}
void display() {
noStroke();
pushMatrix();
translate(x, y, z);
sphere(ballsize);
popMatrix();
}
void update() {
y = y + speed;
speed = speed + gravity;
if (y > height) {
speed = speed * -0.65;
}
}
boolean isDead(){
return( millis() > t );
}
}
So how would I go about adding textures into the spheres?
So, my program makes spheres that drop down to the ground. After fiddlign around with strings, I can make them appear based on the coordinates I put in the loaded string file. In addition to that, it waits every second to start making another sphere when I use the loaded String.
Now what I want to do is make it so itll load a sphere in a certain coordinate, based on 4 different letters.
A = 500, 100
G = 750, 100
C = 1000, 100
T = 1250, 100
What i want to be able to do is be able to type in each letter into the loaded file in any order I want.
For example:
a
t
c
g
a
t
t
g
a
c
And each time, it would load a sphere in that exact location. How would I do that?
// The program now can increase the size of the spheres
// Pressing the R key can now generate spheres at random areas
ArrayList<Ball> balls = new ArrayList<Ball>();
float gravity = 0.1;
//arrays are used to store objects, and an array list can be used to make multiple objects, as it can increase better then a normal array
float lightvalue = 0;
//the light value will be used to change the light type, 0 for regular lights function, 1 for directional, 2 for ambient, 3 for spot light. To change the light, press Enter
float ballsize = 10;
//the size of the ball will increase from 1-0
float rX;
float rY;
// two variables specifying random values in the x and y value. rX is x value and rY is y value
PFont f;
void setup() {
size(1500, 700, P3D);
// start with one ball
balls.add( new Ball(width/2, 0, 0)); //
background(0);
frameRate(60);
f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
}
//The key pressing here is used to try and change the the lightvalue, to later change the lights
void keyPressed() {
if (key == ENTER || key == RETURN) {
lightvalue = lightvalue + 1;
}
if (lightvalue > 3) {
lightvalue = 0;
}
if (key == '1') {
ballsize = 10;
}
if (key == '2') {
ballsize = 20;
}
if (key == '3') {
ballsize = 30;
}
if (key == '4') {
ballsize = 40;
}
if (key == '5') {
ballsize = 50;
}
if (key == '6') {
ballsize = 60;
}
if (key == '7') {
ballsize = 70;
}
if (key == '8') {
ballsize = 80;
}
if (key == '9') {
ballsize = 90;
}
if (key == '0') {
ballsize = 100;
}
if (key == 'r' || key == 'R' ) {
rX = random(width);
rY = random(height);
balls.add( new Ball(rX, rY, 0));
}
}
void draw() {
background(0);
textFont(f,24);
text ( "Welcome to the Sphere Simulator!" ,0,24);
text ( "Click on the screen to generate a sphere!" ,0,48);
text ( "Press the keys 1 through 0 to increase the size!" ,0,72);
text ( "Press the R key to generate a sphere randomly!" ,0,96);
text ( "Press Enter/Return to change the lights!" ,0,120);
// Ball is the datatypye of the stuff in the Array, b is the temporary name for each element in the array, balls is the name of the array to iterate through
for (Ball b:balls) { //update() and dislay() are methods, in which b is using, and the dot "." connects the two, allowing b to use the two methods
b.update(); //update is used to improve the falling of the balls
b.display(); //display is used to make the balls, constantly
}
//gonna try and use frameRate and remove() to remove objects from the array
}
void mousePressed() {
balls.add(new Ball( mouseX, mouseY, 0));
//this adds the object
}
class Ball {
float x;
float y;
float z;
float speed;
// a constructor:
Ball(float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
speed = 0;
}
void display() {
noStroke();
pushMatrix();
translate(x, y, z);
sphere(ballsize);
popMatrix();
}
void update() {
y = y + speed;
speed = speed + gravity;
if (y > height) {
speed = speed * -0.65;
}
}
}
Now what im trying to do is over time, get the sphere objects I create to be deleted over time. Lets say, for each ball I create, I want each one of them to last 10 seconds, and then be deleted. How do I go about this?
So I have a text file called list.txt, and it has this in it:
750
500
600
300
1100
550
600
20
899
500
750
350
and I used the loadStrings() function to import it into the file. But I have a problem.
What Im trying to do is use those values for the x and y coordinates for an object I make at the setup.
void setup() {
size(1500, 700, P3D);
// start with one ball
balls.add( new Ball(width/2, 0, 0));
background(0);
}
Where it says Ball(width/2, 0, 0)) Im trying to replace width/2, 0 with two values from the txt file, such as the first two (750, 500). How would I go about doing this?
Hello everyone. Im having trouble, as you can see. You see, I have a code that makes a single sphere drop down, and bounce back up, until finally stopping. Here:
float x = 750; // x location of square
float y = 0; // y location of square
float z = 0; // z location of square
float speed = 0; // speed of square
// A new variable, for gravity (i.e. acceleration).
// We use a relatively small number (0.1) because this accelerations accumulates over time, increasing the speed.
// Try changing this number to 2.0 and see what happens.
float gravity = 0.1;
void setup() {
size(1500,700, P3D);
}
//attempting to make a sphere in the location of each mouse press and make it fall down
void draw() {
background(0);
lights();
noStroke();
translate(x,y,z);
sphere(10);
// Add speed to location.
y = y + speed;
// Add gravity to speed.
speed = speed + gravity;
// If square reaches the bottom
// Reverse speed
if (y > height) {
// Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).
// This is known as a "dampening" effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).
speed = speed * -0.95;
}
}
What I am trying to do is be able to move my mouse on the window, click, and a sphere would appear where I clicked, and dropped down like the rest. For this, I would like advice on two things. How do I create multiple objects (or in this case, spheres) on the window? And how do I find away to make a sphere on the mouseX and mouseY coordinates, but have it still drop down? My biggest trouble with that is keeping the x and y values it has right now, and start it off with that instead.