I am creating a sound application where it uses filters such as reverb and a dynamic compressor. I tried to implement a slider to change the values of the output, release, etc.
// Dynamic Compressor
// Method 1
dynamicCompressor.amp(sliderDCOutput.value());
dynamicCompressor.set(sliderDCAttack.value(), sliderDCKnee.value(), sliderDCRatio.value(), sliderDCThreshold.value(), sliderDCRelease.value());
dynamicCompressor.drywet(sliderDCDryWet.value());
// Method 2
dynamicCompressor.drywet(sliderDCDryWet.value());
dynamicCompressor.amp(sliderDCOutput.value());
dynamicCompressor.attack(sliderDCAttack.value());
dynamicCompressor.knee(sliderDCKnee.value());
dynamicCompressor.ratio(sliderDCRatio.value());
dynamicCompressor.release(sliderDCRelease.value());
dynamicCompressor.threshold(sliderDCThreshold.value());
I've tried using both methods, but it still doesnt change anything. I tried using the 2 methods for all the filters I'm trying to chain together. I'm not sure if it's even the slider problem or something else altogether.
Here is the whole code just in case:
let mySoundFile = "/Sounds/320952__kevcio__amen-break-f-180-bpm-16-bars.wav";
let LPfilter, wDistortion, dynamicCompressor, reverb, fft;
let playButton, stopButton, sliderVolume, startButton, skipButton, loopButton, recordButton;
let mic, recorder, soundFile, state = 0;
let sliderReverbDuration, sliderReverbDryWet, sliderReverbOutput, sliderReverbDecay, reverbReverseButton, reverseReverb = false;
let sliderLPDryWet, sliderLPOutput, sliderLPCutoffFreq, sliderLPResonance;
let sliderDCDryWet, sliderDCOutput, sliderDCAttack, sliderDCKnee, sliderDCRelease, sliderDCRatio, sliderDCThreshold;
let sliderWDDistortion, sliderWDOversample, sliderWDDryWet, sliderWDOutput;
function preload(){
soundFormats("wav", "mp3");
mySound = loadSound(mySoundFile);
LPfilter = new p5.LowPass();
wDistortion = new p5.Distortion();
dynamicCompressor = new p5.Compressor();
reverb = new p5.Reverb();
fft = new p5.FFT();
mySound.disconnect();
}
function setup() {
createCanvas(1200, 1000);
background(180);
// Set up audio chain
mySound.connect(LPfilter);
LPfilter.chain(wDistortion, dynamicCompressor, reverb);
// Set up other controls and buttons
gui_configuration();
}
function draw() {
mySound.setVolume(sliderVolume.value());
// Low-Pass Filter
LPfilter.amp(sliderLPOutput.value());
LPfilter.set(sliderLPCutoffFreq.value(), sliderLPResonance.value());
LPfilter.drywet(sliderLPDryWet.value());
// Waveshaper Distortion
wDistortion.amp();
if (sliderWDOversample.value() == 0){
wDistortion.set(sliderWDDistortion.value(), "none");
}
else{
wDistortion.set(sliderWDDistortion.value(), `${sliderWDOversample.value()}x`);
}
wDistortion.drywet(sliderWDDryWet.value());
// Dynamic Compressor
dynamicCompressor.amp(sliderDCOutput.value());
dynamicCompressor.set(sliderDCAttack.value(), sliderDCKnee.value(), sliderDCRatio.value(), sliderDCThreshold.value(), sliderDCRelease.value());
dynamicCompressor.drywet(sliderDCDryWet.value());
dynamicCompressor.drywet(sliderDCDryWet.value());
dynamicCompressor.amp(sliderDCOutput.value());
dynamicCompressor.attack(sliderDCAttack.value());
dynamicCompressor.knee(sliderDCKnee.value());
dynamicCompressor.ratio(sliderDCRatio.value());
dynamicCompressor.release(sliderDCRelease.value());
dynamicCompressor.threshold(sliderDCThreshold.value());
// Reverb
reverb.amp(sliderReverbOutput.value());
reverb.set(sliderReverbDuration.value(), sliderReverbDecay.value(), reverseReverb);
reverb.drywet(sliderReverbDryWet.value());
let a = 490
let b = 510
let spectrum = fft.analyze();
noStroke();
fill(0, 255, 0, 200);
for (let i = 0; i < spectrum.length; i++) {
let x = map(i, 0, spectrum.length, a, 800);
let h = -b + map(spectrum[i], 0, 255, b, 300);
rect(x, a, b / spectrum.length, h);
}
// let c = 690;
// let d = 510;
// let spectrum2 = fft2.analyze();
// noStroke()loopButton;
// fill(0, 255, 0, 200);
// for (let i = 0; i < spectrum2.length; i++){
// let x = map(i, 0, spectrum2.length, c, 800);
// let h = -b + map(spectrum2[i], 0, 255, d, 500);
// rect(x, c, d / spectrum2.length, h)
// }
}
function pauseSound(){
mySound.pause();
console.log(getAudioContext().state);
}
function playSound(){
mySound.play();
console.log(getAudioContext().state);
}
function stopSound(){
mySound.stop();
console.log(getAudioContext().state);
}
function jumpToStart(){
mySound.jump(0);
}
function jumpToEnd(){
let dur = mySound.duration();
mySound.jump(dur);
console.log(dur);
}
function loopSound(){
mySound.loop();
}
// Handle recording of audio
function recordSound(){
// Resume audio context if not running
if (getAudioContext().state !== "running"){
getAudioContext().resume();
}
// Check current state and perform appropriate action
switch (state) {
case 0: // Ready to record
// Check if mic is enabled
if (mic.enabled) {
// Display recording message
text("Recording", 505, 100);
// Start recording
recorder.record(soundFile);
// Increment state
state++;
}
break;
case 1: // Recording in progress
// Clear background
background(180);
// Display message
text("Recording has been downloaded", 435, 100);
// Stop recording
recorder.stop();
// Save recording to file
save(soundFile, "output.wav");
// Reset state
state = 0;
break;
default: // Invalid state
console.error("Invalid state");
}
}
function reverbReverse(){
reverseReverb != reverseReverb;
}
function gui_configuration(){
// Pause Button
pauseButton = createButton("Pause");
pauseButton.position(20, 20);
pauseButton.size(60, 60);
pauseButton.mousePressed(pauseSound);
// Play Button
playButton = createButton("Play");
playButton.position(100, 20);
playButton.size(60, 60);
playButton.mousePressed(playSound);
// Stop Button
stopButton = createButton("Stop");
stopButton.position(180,20);
stopButton.size(60, 60);
stopButton.mousePressed(stopSound);
// Master Volume Slider
push();
fill(180);
strokeWeight(2);
stroke(0);
rect(575, 100, 100, 200);
pop();
sliderVolume = createSlider(0, 2, 1, 0.01);
sliderVolume.style("transform: rotate(" + 270 + "deg);");
sliderVolume.position(560, 200);
text("Master Volume", 585, 130);
// Skip to Start Button
startButton = createButton("Skip to Start");
startButton.position(260, 20);
startButton.size(60, 60);
startButton.mousePressed(jumpToStart);
// Skip to End Button
skipButton = createButton("Skip to End");
skipButton.position(340, 20);
skipButton.size(60, 60);
skipButton.mousePressed(jumpToEnd);
// Loop Button
loopButton = createButton("Loop");
loopButton.position(420, 20);
loopButton.size(60, 60);
loopButton.mousePressed(loopSound);
// Record Button
mic = new p5.AudioIn();
mic.start();
recorder = new p5.SoundRecorder();
recorder.setInput(mic);
soundFile = new p5.SoundFile();
recordButton = createButton("Record");
recordButton.position(500, 20);
recordButton.size(60, 60);
recordButton.mousePressed(recordSound);
// Low-Pass Filter Section ////////////
push();
fill(180);
strokeWeight(2);
stroke(0);
rect(20, 100, 200, 420);
pop();
text("Low-Pass Filter", 80, 130);
// Low-Pass Filter Cutoff Frequency Slider
sliderLPCutoffFreq = createSlider(10, 22050, 11020);
sliderLPCutoffFreq.style("transform: rotate(" + 270 + "deg);");
sliderLPCutoffFreq.position(10, 230);
text("Cutoff Frequency", 30, 160);
// Low-Pass Filter Resonance Slider
sliderLPResonance = createSlider(0.001, 1000, 500);
sliderLPResonance.style("transform: rotate(" + 270 + "deg);");
sliderLPResonance.position(100, 230);
text("Resonance", 135, 160);
// Low-Pass Filter Dry/Wet Slider
sliderLPDryWet = createSlider(0, 1.0, 0.5, 0.01);
sliderLPDryWet.style("transform: rotate(" + 270 + "deg);");
sliderLPDryWet.position(10, 430);
text("Dry/Wet", 55, 360);
// Low-Pass Filter Output Level Slider
sliderLPOutput = createSlider(0, 1.0, 0.5, 0.01);
sliderLPOutput.style("transform: rotate(" + 270 + "deg);");
sliderLPOutput.position(100, 430);
text("Output Level", 130, 360);
///////////////////////////////////////
// Waveshaper Distortion //////////////
push();
fill(180);
strokeWeight(2);
stroke(0);
rect(250, 540, 200, 420);
pop();
text("Waveshaper Distortion", 290, 570);
// Waveshaper Distortion Amount Slider
sliderWDDistortion = createSlider(0, 1.0, 0.5, 0.01);
sliderWDDistortion.style("transform: rotate(" + 270 + "deg);");
sliderWDDistortion.position(240, 670);
text("Distortion\n Amount", 280, 590);
// Waveshaper Oversample Slider
sliderWDOversample = createSlider(0, 4, 2, 2);
sliderWDOversample.style("transform: rotate(" + 270 + "deg);");
sliderWDOversample.position(330, 670);
text("Oversample", 365, 600);
// Waveshaper Dry/Wet Slider
sliderWDDryWet = createSlider(0, 4, 2, 2);
sliderWDDryWet.style("transform: rotate(" + 270 + "deg);");
sliderWDDryWet.position(240, 860);
text("Dry/Wet", 285, 790);
// Waveshaper Output Slider
sliderWDOutput = createSlider(0, 4, 2, 2);
sliderWDOutput.style("transform: rotate(" + 270 + "deg);");
sliderWDOutput.position(330, 860);
text("Output Level", 360, 790);
///////////////////////////////////////
// Dynamic Compressor Section /////////
push();
fill(180);
strokeWeight(2);
stroke(0);
rect(250, 100, 300, 400);
pop();
text("Dynamic Compressor", 345, 130);
// Dynamic Compressor Attack Slider
sliderDCAttack = createSlider(0, 1, 0.5, 0.01);
sliderDCAttack.style("transform: rotate(" + 270 + "deg);");
sliderDCAttack.position(220, 230);
text("Attack", 270, 160);
// Dynamic Compressor Knee Slider
sliderDCKnee = createSlider(0, 40, 20);
sliderDCKnee.style("transform: rotate(" + 270 + "deg);");
sliderDCKnee.position(280, 230);
text("Knee", 330, 160);
// Dynamic Compressor Release Slider
sliderDCRelease = createSlider(0, 1.0, 0.5, 0.01);
sliderDCRelease.style("transform: rotate(" + 270 + "deg);");
sliderDCRelease.position(340, 230);
text("Release", 385, 160);
// Dynamic Compressor Ratio Slider
sliderDCRatio = createSlider(1, 20, 10);
sliderDCRatio.style("transform: rotate(" + 270 + "deg);");
sliderDCRatio.position(400, 230);
text("Ratio", 450, 160);
// Dynamic Compressor Threshold Slider
sliderDCThreshold = createSlider(-100, 0, -50);
sliderDCThreshold.style("transform: rotate(" + 270 + "deg);");
sliderDCThreshold.position(450, 230);
text("Threshold", 490, 160);
// Dynamic Compressor Dry/Wet Slider
sliderDCDryWet = createSlider(0, 1.0, 0.5, 0.01);
sliderDCDryWet.style("transform: rotate(" + 270 + "deg);");
sliderDCDryWet.position(280, 400);
text("Dry/Wet", 325, 330);
// Dynamic Compressor Output Level Slider
sliderDCOutput = createSlider(0, 1.0, 0.5, 0.01);
sliderDCOutput.style("transform: rotate(" + 270 + "deg);");
sliderDCOutput.position(400, 400);
text("Output Level", 430, 330);
///////////////////////////////////////
// Reverb Section /////////////////////
push();
fill(180);
strokeWeight(2);
stroke(0);
rect(20, 540, 200, 450);
pop();
text("Reverb", 105, 570);
// Reverb Duration Slider
sliderReverbDuration = createSlider(0, 10, 5);
sliderReverbDuration.style("transform: rotate(" + 270 + "deg);");
sliderReverbDuration.position(10, 670);
text("Reverb Duration", 35, 600);
// Reverb Decay Rate Slider
sliderReverbDecay = createSlider(0, 100, 50);
sliderReverbDecay.style("transform: rotate(" + 270 + "deg);");
sliderReverbDecay.position(100, 670);
text("Decay Rate", 135, 600);
// Reverb Reverse Button
reverbReverseButton = createButton("Reverse");
reverbReverseButton.position(45, 760);
reverbReverseButton.size(60,40);
reverbReverseButton.mousePressed(reverbReverse);
// Reverb Dry/Wet Slider
sliderReverbDryWet = createSlider(0, 1.0, 0.5, 0.01);
sliderReverbDryWet.style("transform: rotate(" + 270 + "deg);");
sliderReverbDryWet.position(10, 900);
text("Dry/Wet", 55, 830);
// Reverb Output Level Slider
sliderReverbOutput = createSlider(0, 1.0, 0.5, 0.01);
sliderReverbOutput.style("transform: rotate(" + 270 + "deg);");
sliderReverbOutput.position(100, 900);
text("Output Level", 130, 830);
}
Related
This question already has answers here:
Ball to Ball Collision - Detection and Handling
(15 answers)
Closed 2 years ago.
//Spielfeld Größe
int sizeX = 800;
int sizeY = 600;
//Robo Startposition
float roboX = sizeX/2;
float roboY = sizeY/2;
//Robo Speed
float speed = 0.03;
//Robo Acceleration
float accel = 0.05;
float x1;
float y1;
void setup() {
size(800, 600);
frameRate(60);
background(200);
}
void draw() {
background(200);
println("x " +x1);
println("y " +y1);
robo();
x1=screenX(roboX,roboX);
y1=screenY(roboY,roboY);
stopRobot();
object();
}
//Beschleunigung zurücksetzen
void mouseReleased() {
accel = 0.05;
}
void robo() {
rectMode(CENTER);
//RoboMovement
if(mousePressed){
float zielX = mouseX;
float moveX = zielX - roboX;
roboX += moveX * speed*accel;
float zielY = mouseY;
float moveY = zielY - roboY;
roboY += moveY * speed*accel;
//Beschleunigung am Anfang
if(accel< 1) {
accel += 0.05;
}
}
pushMatrix();
translate(roboX, roboY);
rotate(atan2(mouseY-roboY, mouseX-roboX));
rotate(radians(90));
//Hitbox
fill(0, 0);
stroke(160);
ellipse(0, 0, 78, 78);
noStroke();
//Track Connectors
fill(129, 29, 29);
rect(18, 12, 10, 8);
rect(18, -12, 10, 8);
rect(-18, 12, 10, 8);
rect(-18, -12, 10, 8);
//Robo Body
fill(63, 82, 21);
rect(0, 0, 13, 30);
ellipse(7, 15, 20, 20);
ellipse(-7, 15, 20, 20);
ellipse(7, -15, 20, 20);
ellipse(-7, -15, 20, 20);
rect(9, 0, 16, 23);
rect(0, 17, 20, 15);
rect(-9, 0, 16, 23);
rect(0, -17, 20, 15);
//Robo Body Details
fill(120);
ellipse(0, 0, 25, 25);
fill(60);
rect(0, -18, 6, 36);
rect(0, -36, 10, 5);
ellipse(0, 0, 20, 20);
fill(255, 0, 20);
ellipse(10, 18, 5, 5);
//Robo Tracks
//Right
fill(80);
rect(25, 0, 12, 34);
ellipse(24, 20, 10, 10);
ellipse(26, 20, 10, 10);
ellipse(24, -20, 10, 10);
ellipse(26, -20, 10, 10);
//Left
rect(-25, 0, 12, 34);
ellipse(-24, 20, 10, 10);
ellipse(-26, 20, 10, 10);
ellipse(-24, -20, 10, 10);
ellipse(-26, -20, 10, 10);
//Track Details
fill(140);
for (int i = -21; i< 22; i += 6) {
rect(25, i, 12, 2);
rect(-25, i, 12, 2);
}
popMatrix();
}
float [] [] bobject= new float [][] {{250, 250}, {500, 500}, {100, 100}};
void object() {
fill(255);
stroke(0);
circle(bobject[0][0], bobject[0][1], 20);
circle(bobject[1][0], bobject[1][1], 20);
circle(bobject[2][0], bobject[2][1], 20);
}
void stopRobot() {
if(dist(x1,y1,bobject[0][0],bobject[0][1])<=45){
speed=0;
if(mouseX<bobject[0][0]){
speed=0.03;}
if(mouseY>bobject[0][1]){
speed=0.03;}
}
if ( x1<=0+45) {
speed=0;
if (mouseX>=0) {
speed=0.03;
}
}
if ( x1>=width-45) {
speed=0;
if (mouseX<=width-45) {
speed=0.03;
}
}
if ( y1<=0+47) {
speed=0;
if (mouseY>=0+47) {
speed=0.03;
}
}
if ( y1>=height-45) {
speed=0;
if (mouseY<=height-45) {
speed=0.03;
}
}
}
This is the code, basically the idea is, that if the robot collides with the wall/objects it stops, independently from the position of the small circles.
Somehow i cant paste in the code so heres a link to the file
https://drive.google.com/drive/folders/1AvXQ2-stM-ubR38XSVG0zOaKvXHJTkC7?usp=sharing
If anybody could help me, that would be nice
The code was a Lil messy but I managed to make what you need, the first thing you need to do is to init the radius of the tank as a global variable so you can check your collision with it later. Secondly, its better to change your circles array by adding a 3rd value to store the radius, this way you can check the collision for multiple sized circles. Then I implemented a simple check collision method which checks for a given position (x,y) if the tank will collide with any of the circles. Lastly what you need to do is to store the tank position right before moving it, then check if the new position actually collides with any circle, if it does, you will need to stop the tank and put it back to the previous place. I hope my explanations made sense, here is the final code:
//Spielfeld Größe
int sizeX = 800;
int sizeY = 600;
//Robo Startposition
float roboX = sizeX/2;
float roboY = sizeY/2;
//Robo Speed
float speed = 0.03;
float robotRadius = 78;
//Robo Acceleration
float accel = 0.05;
float x1;
float y1;
void setup() {
size(800, 600);
frameRate(60);
background(200);
}
void draw() {
background(200);
println("x " +x1);
println("y " +y1);
robo();
x1=screenX(roboX,roboX);
y1=screenY(roboY,roboY);
object();
}
//Beschleunigung zurücksetzen
void mouseReleased() {
accel = 0.05;
}
void robo() {
rectMode(CENTER);
//RoboMovement
if(mousePressed){
//save robot position before moving it
float saveX = roboX;
float saveY = roboY;
float zielX = mouseX;
float moveX = zielX - roboX;
roboX += moveX * speed*accel;
float zielY = mouseY;
float moveY = zielY - roboY;
roboY += moveY * speed*accel;
//check if its colliding after moving it
if(checkCollision(roboX,roboY))
{
//in case the tank collide, put the robot baack and make acc = 0
accel = 0;
roboX = saveX;
roboY = saveY;
}
//Beschleunigung am Anfang
if(accel< 1) {
accel += 0.05;
}
}
pushMatrix();
translate(roboX, roboY);
rotate(atan2(mouseY-roboY, mouseX-roboX));
rotate(radians(90));
//Hitbox
fill(0, 0);
stroke(160);
ellipse(0, 0, robotRadius, robotRadius);
noStroke();
//Track Connectors
fill(129, 29, 29);
rect(18, 12, 10, 8);
rect(18, -12, 10, 8);
rect(-18, 12, 10, 8);
rect(-18, -12, 10, 8);
//Robo Body
fill(63, 82, 21);
rect(0, 0, 13, 30);
ellipse(7, 15, 20, 20);
ellipse(-7, 15, 20, 20);
ellipse(7, -15, 20, 20);
ellipse(-7, -15, 20, 20);
rect(9, 0, 16, 23);
rect(0, 17, 20, 15);
rect(-9, 0, 16, 23);
rect(0, -17, 20, 15);
//Robo Body Details
fill(120);
ellipse(0, 0, 25, 25);
fill(60);
rect(0, -18, 6, 36);
rect(0, -36, 10, 5);
ellipse(0, 0, 20, 20);
fill(255, 0, 20);
ellipse(10, 18, 5, 5);
//Robo Tracks
//Right
fill(80);
rect(25, 0, 12, 34);
ellipse(24, 20, 10, 10);
ellipse(26, 20, 10, 10);
ellipse(24, -20, 10, 10);
ellipse(26, -20, 10, 10);
//Left
rect(-25, 0, 12, 34);
ellipse(-24, 20, 10, 10);
ellipse(-26, 20, 10, 10);
ellipse(-24, -20, 10, 10);
ellipse(-26, -20, 10, 10);
//Track Details
fill(140);
for (int i = -21; i< 22; i += 6) {
rect(25, i, 12, 2);
rect(-25, i, 12, 2);
}
popMatrix();
}
//added a 3rd value to the circles array to store the radius
float [][] bobject= new float [][] {{250, 250, 20}, {500, 500, 20}, {100, 100, 20}};
void object() {
fill(255);
stroke(0);
for(int i=0; i<bobject.length; i++)
{
circle(bobject[i][0], bobject[i][1], bobject[i][2]);
}
}
//deleted stopRobot method
//created a method to check weather a specific place has a collision with a circle
boolean checkCollision(float x, float y)
{
//for every circle in the array, check weather the distance between it and the given tank position is lower than both radius sumed ( which means they collide )
for(int i=0; i<bobject.length; i++)
{
if(dist(x,y,bobject[i][0],bobject[i][1])< (robotRadius + bobject[i][2])/2)
return true;
}
return false;
}
feel free to ask in comment if you don't understand any part
I'm trying to create a particle system within a basic-shape drawing. The particles should emit from the top of the leftmost structure (commented "Heading") and flow downwards, similar to a fountain. I have a majority of the Particle class figured out, but error messages related to the variables arise when I run the sketch.
Applied some edits based on answers, but cannot get the particle system to display. Amended code listed below.
var particles = [ ];
function Particle() {
initialization()
this.x = 135;
this.y = 75;
this.vx = random(-1, 1);
this.vx = random(-5, -1);
this.alpha = 255;
update()
this.x += this.vx;
this.y += this.vy;
this.alpha -= 5;
show()
noStroke();
fill ('#1E740C');
ellipse (this.x, this.y, 8, 8);
}
function setup() {
createCanvas(600, 400);
//Particle Array
for(var i = 0; i < particles.length; i++) {
particles[i] = new Particle();
}
}
function draw() {
background('#87D9E8');
stroke(0);
strokeWeight(1);
//Loop array and alter each element
for(var i=0; i < particles.length; i++) {
particles[i].show();
particles[i].update();
}
//SLIME GEYSER
///Center Pipe
fill ('#1FA600');
rect(340, 75, 20, 290);
rect(340, 75, 130, 20);
////Body
fill ('#1FA600');
ellipse (305, 495, 450, 400);
///Heading (Spout)
//rect(100, 140, 80, 140, 15); //Top segment
//triangle (100, 150, 180, 150, 140, 100); //"Neck" segment
// rect(120, 75, 40, 40); //SPOUT TIP
// ellipse(140, 115, 100, 45); //Spout "bulb"
rect(120, 350, 40, 30); //Bottom pipe
rect(100, 290, 80, 60, 20); //Bottom segment
rect(100, 277, 80, 15); //Midway segment
//////Pressure Condenser Unit Grille
fill('#989E9B');
rect(390, 135, 140, 130);
line (410, 305, 410, 100);
line (430, 305, 430, 100);
line (450, 305, 450, 100);
line (470, 305, 470, 100);
line (490, 305, 490, 100);
line (510, 305, 510, 100);
////Pressure Condenser Unit
fill ('#1FA600');
arc(460, 265, 140, 140, 0, HALF_PI+HALF_PI);
arc(460, 135, 140, 140, PI, 0);
////Tube
noFill();
stroke('#1FA250');
strokeWeight(10);
beginShape();
vertex(110, 270);
quadraticVertex(10, 200, 110, 150);
endShape();
}
First of all you don't have to declare a global Particle variable, the class would do it.
You aren't calling the show function on the particles array so it won't display the particles.
Instead you can do something like this,
function Particle() {
this.x = 120;
this.y = 200;
this.vx = random(-1,1);
this.vy = random(-5,1);
this.alpha = 255;
this.show = function() {
noStroke();
fill("#1E740C");
ellipse(this.x, this.y, 8, 8);
}
this.update = function() {
this.x += this.vx;
this.y += this.vy;
//console.log("run")
}
}
var p = [];
function setup() {
createCanvas(345,400);
for(var i = 0; i < 10; i++) {
p[i] = new Particle();
}
}
function draw() {
background(0);
for(var i = 0; i < p.length; i++) {
p[i].show();
p[i].update()
}
}
That's probably the easiest way to create the particles system. If this answer wasn't helpful then checkout these links:
CodingTrain: https://thecodingtrain.com/CodingChallenges/078-simple-particle-system.html
Or check out p5's own example:
https://p5js.org/examples/simulate-multiple-particle-systems.html
I have a scene with a cube and a custom obj. I am attempting to apply a gradient as a texture to the cube via createGraphics, but every time I use createGraphics as a texture on the cube it flickers
The custom model does not have the same issue when using the createGraphics texture for it, it is exclusively a problem with the cube.
Here is my code:
var camy;
var camz;
var camoffset;
var horiZon;
var c1;
var c2;
function preload() {
fria = loadImage('nocity.png');
island = loadModel('untitled.obj');
}
function setup() {
canvas = createCanvas(windowWidth, windowHeight, WEBGL);
pixelDensity=1;
c1 = color(255, 255, 255);
c2 = color(0, 0, 0);
sunset = createGraphics(200, 200);
}
function windowResized() {
resizeCanvas(windowWidth,windowHeight);
}
function draw() {
background(0, 5, 100);
angleMode(DEGREES);
camoffset = 2500 - windowWidth;
horiZon = map(mouseY, 0, height, -35, -65);
camx = map(mouseX, 0, width, -500, 500);
camz = map(mouseY, height, 0, -1400 - camoffset, -2100 - camoffset);
camy = map(mouseY, height, 0, -1000 - camoffset, -400);
setGradient(0, 0, 200, 200, c1, c2);
perspective(PI / 3.0, width / height, 0.1, 10000);
camera(camx, camy, camz, 0, 0, 0, 0, 1, -0.25);
translate(-2.5, 6, 0);
rotateZ(180);
rotateY(180);
noStroke();
texture(fria);
model(island);
texture(sunset);
translate(0, 100, horiZon);
box(200, 200, 1);
}
function setGradient(x, y, w, h, c1, c2) {
noFill();
for (var i = y; i <= y + h; i++) {
var inter = map(i, y, y + h, 0, 1);
var c = lerpColor(c1, c2, inter);
sunset.stroke(c);
sunset.line(x, i, x + w, i);
}
}
I found a solution to this problem. When I removed the camera code, the flickering disappeared. So the solution I made was I instead used translation and rotation to emulate the camera motions that I wanted. It is quite hard to think of it this way, but you can figure out what translation you need to do with simple trig.
I am creating a simulator using P5.js. Within the simulator, I need a green box, however it does not seem to be appearing. The code is below:
var outputs = [];
function setup() {
createCanvas(600, 400, WEBGL);
background(200);
for (var i = 0; i < 1; i++) {
drop = new Water(width / 2, height / 2, 0, 1);
outputs[i] = drop;
}
}
function draw() {
push();
translate(200, 150, 0);
stroke(0, 100, 0);
fill(0, 255, 0);
box(150, 150, 150);
pop();
for (var i = 0; i < outputs.length; i++) {
outputs[i].update();
}
background(200);
}
Here is the water class:
function Water(x_, y_, z_, yVel_) {
this.r = random(0.25, 1);
this.xOff = random(-(this.r / 10), (this.r / 10));
this.zOff = random(-(this.r / 10), (this.r / 10));
this.x = x_ + this.xOff;
this.y = y_;
this.z = z_ + this.zOff;
this.yVel = yVel_;
this.pos = createVector(this.x, this.y, this.z);
this.show = function() {
push();
translate(this.pos.x, this.pos.y, this.pos.z);
noStroke();
fill(0, 0, 255);
sphere(this.r * 2);
pop();
}
this.update = function() {
this.vel = createVector(random(-(this.r / 10), (this.r / 10)),
this.yVel, random(-(this.r / 10),
(this.r / 10)));
this.pos.add(this.vel);
this.show();
}
}
This is a web based simulation, with another module which appears to be working fine.
Any help would be greatly appreciated. Thanks in advance!
Removing the parts that require the Water class, and moving the background function call to the top of draw, it seems to work just fine.
So, the problem is
Either that you forgot to put background on top
Or something's wrong with the Water class.
Here's your code with the mentioned problems fixed.
var outputs = [];
function setup() {
createCanvas(600, 400, WEBGL);
}
function draw() {
background(200);
push();
translate(200, 150, 0);
stroke(0, 100, 0);
fill(0, 255, 0);
box(150, 150, 150);
pop();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.min.js"></script>
Its not rendering because you're background is over your scene
function draw() {
background(200);
push();
translate(200, 150, 0);
stroke(0, 100, 0);
fill(0, 255, 0);
box(150, 150, 150);
pop();
for (var i = 0; i < outputs.length; i++) {
outputs[i].update();
}
}
What your doing is drawing the box and the drops and you cover it all up with your background
if you don't have a background you will see how p5.js renders animation
p5.js not moving it, its just looping through draw every frame and the background covers up the previous frame
I keep getting a "cannot run pushMatrix more than 32 times" in the middle of running this sketch. I'm aware that this can be an issue of having pushMatrix(); calls without a matching popMatrix(); but I don't see it in my code. Any help appreciated.
For the record; I'm aware this code is not very pretty and could be refactored extensively. I'm still new to programming and this is just an ongoing project I've been working on since the day I started coding.
Thanks again for any help.
float x, y, d, r, g, b, t;
float c = random(0, mouseY+75);
float easing = 0.03;
int diameter= 40;
int timer= -3000;
int direction = 1;
float speed = .7;
float angle = 0.0;
float offset;
float scalar = 60;
boolean button = false;
void setup() {
size(1000, 400);
smooth();
strokeWeight(5);
}
void draw() {
background(204);
int currentTime = millis();
textSize(16);
fill (125);
text ("Flying Circles", 20, 20);
if ((currentTime < 5000) && (mousePressed==false)){
r = noise(frameCount * 0.01) * 255; //clockwise spin
g = frameCount % 255;
b = 255 - noise(1 + frameCount * 0.025) * 255;
rectMode(CENTER);
background(204);
pushMatrix();
translate(mouseX, mouseY);
pushMatrix();
rotate(-angle);
ellipse(0,0, 150, 150);
ellipse(0,0, 125, 125);
rect(0,0, 80, 80);
popMatrix();
pushMatrix();
rotate(angle);
rect(0, 0, 80, 80);
rect(0, 0, 60, 60);
popMatrix();
pushMatrix();
rotate(-angle);
fill(r,g,b);
rect(-270, 0, 80, 80);
rect(270, 0, 80, 80);
rect(0, 0, 40, 40);
angle += 0.1;
popMatrix();
popMatrix();
}else{
drawCounterspin();
}
}
void keyPressed() {
button = !button;
}
void drawFighter(){
float targetX = mouseX;
float d = random(0, 150);
x += (targetX - x) * easing*1.5;
fill(mouseY, mouseY/2, mouseY/3);
ellipse(x-300, mouseY, 100, 100);
fill(d, d*1.2, d*1.5);
ellipse(x, mouseY, 80, 80);
fill(mouseY, mouseY/2, mouseY/3);
ellipse(x+300, mouseY, 100, 100);
line(x+40, mouseY,x+250, mouseY);
line(x-40, mouseY,x-250, mouseY);
fill(d, d, d);
ellipse(x, mouseY+100, 80, 80);
fill(d, d, d);
ellipse(x, mouseY+200, 60,60);
line(x+30,mouseY+30, width, mouseY*2);
line(x-30,mouseY+30, 0, mouseY*2);
}
void drawOilbounce() {
x = width/2;
y += speed * direction;
if ((y > height/2+diameter) || (y < 0)) {
direction = -direction;
scalar = -scalar;
}
float spinspeed = speed * .1;
float spinx = x+ cos(angle) * scalar;
float spiny = y+ sin(angle) * scalar;
angle += spinspeed;
scalar += spinspeed;
scalar = constrain(scalar, 30, 300);
fill(r,g,b);
ellipse(spinx, spiny, 30, 30);
fill(r*.95, g*.85, b*.75);
ellipse(x, y, diameter, diameter);
fill(r*.85, g*.75, b*.65);
ellipse(x, y*1.2, y*.02*diameter, diameter);
fill(r*.75, g*.65, b*.55);
ellipse(x, y*1.5, y*.05*diameter, diameter);
}
void drawCounterspin(){
pushMatrix();
rectMode(CENTER);
mouseX = constrain (mouseX, 250, width-250);
mouseY = constrain (mouseY, 48, height-48);
fill(mouseY, 80, 100);
if (mouseButton == LEFT) {
translate(mouseX, mouseY);
scale(sin(angle));
rotate(angle);
ellipse(0,0, 150, 150);
ellipse(0,0, 125, 125);
fill(mouseY, mouseX/2, mouseX);
rect(0, 0, 80, 80);
fill(mouseY/2, mouseX/3, mouseX);
rect(250, 0, 80, 80);
fill(mouseY, mouseX, mouseX/2);
rect(-250, 0, 80, 80);
angle += 0.05;
popMatrix();
}
}
Getting rid of the two calls (push and pop) inside the function drawCounterspin() seams to solve the problem without side effects. But I don't really know why... I have sen this behaviour before using push/popMatrix inside functions. If needed, isolate the call in draw, and not in the function, like:
pushMatrix();
drawCounterspin();
popMatrix();
Worked for me before.