Changing translate() values/ Making Multiple Objects
in
Programming Questions
•
4 months ago
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.
Thanks.
1