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
Related
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 am working on an animation of a simplified solar system and want to apply rotation to the Sun. It seems to be a simple task, but somehow it won't work. I try to write my program using object-oriented paradigm, so each planet, moon and the Sun is an object of the CelestialBody class.
I tried to put the rotate(angle) line in an following excerpt from the CelestialBody class, as follows:
if(Type == 4){
emissive(255, 255, 255);
pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
lightFalloff(0, 0, 0.00020); //light falls off right behind the surface of the sun
ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun
rotate(angle);
}
Sadly, it does nothing. How to fix this and make the sun rotate, for example along the x-axis?
Here's my code:
Main file:
import peasy.*; //<>//
PImage bg;
float angle = 0;
CelestialBody planet1;
CelestialBody planet2;
CelestialBody planet3;
CelestialBody planet4;
CelestialBody planet5;
CelestialBody sun;
PImage sunTexture;
PImage rocketTexture;
PShape rocket;
PeasyCam cam;
void setup() {
fullScreen(P3D);
bg = loadImage("background.jpg");
sunTexture = loadImage("sun.jpg");
cam = new PeasyCam(this, 1200);
rocket = loadShape("rocket.obj");
noStroke();
sun = new CelestialBody(70, 0, 0, 255, 200, 50, 4);//4 - type: sun
sun.planet.setTexture(sunTexture);
//noStroke();
//emissive(0, 0, 0);
planet1 = new CelestialBody(16, 150, 0.01, 150, 50, 255, 1);
planet1.spawnMoon(2);
planet2 = new CelestialBody(25, 300, 0.014, 50, 100, 255, 1);
planet2.spawnMoon(3);
planet3 = new CelestialBody(35, 450, -0.015, 255, 100, 40, 2);
planet3.spawnMoon(1);
planet4 = new CelestialBody(50, 650, 0.011, 50, 200, 255, 1);
planet4.spawnMoon(2);
planet5 = new CelestialBody(65, 900, -0.014, 0, 100, 0, 1);
planet5.planet = rocket;
planet5.planet.scale(0.9);
}
void makeOrbit(int a, int r){
for(int i=0; i<a; i++){
stroke(255, 10);
ellipse(height/2,width/2,r,r);
r-=180;
}
noStroke();
}
void draw() {
background(bg);
//lights();
noFill();
//translate(width/2, height/2);
stroke(#FFFFFF);
//ellipse(0,0,300,300);
//ellipse(0,0,600,600);
//ellipse(0,0,900,900);
//ellipse(0,0,planet4.distance,planet4.distance
// int z = 100;
//for (int i = 0; i<2; i++){
// z = -z;
// pointLight(255, 255, 255, -100, -100, z);
// pointLight(255, 255, 255, 100, -100, z);
// pointLight(255, 255, 255, 100, 100, z);
// pointLight(255, 255, 255, -100, 100, z);
//}
//emissive(255, 255, 255);
//pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
//lightFalloff(0, 0, 0.00025); //light falls off right behind the surface of the sun
//ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun
sun.show();
planet1.show();
planet1.orbit();
planet2.show();
planet2.orbit();
planet3.show();
planet3.orbit();
planet4.show();
planet4.orbit();
planet5.show();
planet5.orbit();
}
CelestialBody class:
class CelestialBody {
float radius;
float angle = random(TWO_PI);
float distance;
float orbitSpeed;
PShape planet;
CelestialBody[] moons;
int red;
int green;
int blue;
PVector v;
//PShape globe;
int Type;
CelestialBody(float _radius, float _distance, float _orbitSpeed, int _red, int _green, int _blue, int type) {
v = PVector.random3D();
radius = _radius;
distance = _distance;
v.mult(distance);
orbitSpeed = _orbitSpeed;
red = _red;
green = _green;
blue = _blue;
//stroke(red, green, blue);
fill(red, green, blue);
if(type == 1 || type == 4){
planet = createShape(SPHERE, _radius);
}
else if(type == 2){
planet = createShape(BOX, _radius);
}
Type = type;
}
void orbit() {
angle += orbitSpeed;
if (moons != null) {
for (int i = 0; i < moons.length; i++) {
moons[i].orbit();
}
}
}
void spawnMoon(int total){
moons = new CelestialBody[total];
for(int i = 0; i < moons.length; i++){
float r = radius / random(2,5);
float d = random((radius + r), (radius + r)*2);
float o = random(0.01, 0.05);
moons[i] = new CelestialBody(r, d, o, (int)random(0, 256), (int)random(0, 256), (int)random(0, 256), 1);
}
}
void show() {
pushMatrix();
PVector v2 = new PVector(1,0,1);
PVector p = v.cross(v2);
rotate(angle,p.x, p.y, p.z);
translate(v.x, v.y, v.z);
if(Type == 4){
emissive(255, 255, 255);
pointLight(255, 255, 255, 0, 0, 0); //for the normal behaviour of the sun light
lightFalloff(0, 0, 0.00020); //light falls off right behind the surface of the sun
ambientLight(255, 255, 255, 0, 0, 0); //ambientLight in the center of the sun
}
//shape(globe);
shape(planet);
if (moons != null) {
for (int i = 0; i < moons.length; i++) {
moons[i].show();
}
}
noStroke();
popMatrix();
}
}
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 am having trouble sending a sketch through Syphon to Madmapper.
The regular "send frames" sketch works, but when I try to incorporate the parameters to my sketch, the visualization doesn't show.
Please take a look at my code and let me know what am I doing wrong:
//Final Project
import codeanticode.syphon.*;
float rotation=0.1;
int num = 100, frms = 320;
float theta, time;
long rs;
PGraphics canvas;
SyphonServer server;
//long: Use this datatype when you need a number to have a greater magnitude than can be
//stored within an int.
void setup () {
size(800, 800, P2D);
canvas = createGraphics(800, 800, P2D);
loop();
server = new SyphonServer(this, "sublime");
};
void draw() {
background(0);
canvas.beginDraw();
canvas.smooth();
noStroke();
fill(255, 255, 255, 20);
rect(mouseX, mouseY, 50, 50);
time = (frameCount % frms)/float(frms);
paintQuads();
theta += 2/TWO_PI/frms;
}
void paintQuads() {
for (int i=0; i<num; i++) {
fill(0);
stroke(255);
float r = random(-.5, .5);
float sz = random(5, 15);
resetMatrix(); // Replaces the current matrix with the identity matrix.
// This effectively clears all transformation functions set before it.
//multiply the quads
//Translate
//Specifies an amount to displace objects within the display window.
//The x parameter specifies left/right translation, the y parameter specifies up/down
//translation, and the z parameter specifies translations toward/away from the screen.
//Using this function with the z parameter requires using P3D as a parameter in
//combination with size as shown in the above example.
translate(random(width), random(height));
rotate(r);
rotate(rotation);
//images
noStroke();
fill(255, 0, 0, 70);
quad(38, 31, 86, 20, 69, 63, 30, 76);
fill(255, 210, 0, 10);
quad(width-9, height-9, 86, 20, 69, 63, 30, 76);
fill(255, 0, 0, 30);
ellipse(36, 36, 16, 16);
fill(50, 46, 100, 20 );
quad(46, 20, 14, 14, 46, 20, 14, 14);
fill(50, 46, 100, 75);
quad(50, 0, 12, 12, 50, 0, 12, 12);
rotation=rotation+0.5;
}
canvas.endDraw();
image(canvas, 0, 0);
//send canvas to Syphon
server.sendImage(canvas);
}
Thank you!
-k
It seems you are not using the PGraphics instance properly: some calls use it, some draw into the main sketch, but not the canvas PGraphics which is what you send to Syphon.
One quick fix is to call server.sendScreen(); instead of server.sendImage();
This way what is see in Processing is what you see in MadMapper via Syphon:
Alternatively you can fix your PGraphics calls:
//Final Project
import codeanticode.syphon.*;
float rotation=0.1;
int num = 100, frms = 320;
float theta, time;
long rs;
PGraphics canvas;
SyphonServer server;
//long: Use this datatype when you need a number to have a greater magnitude than can be
//stored within an int.
void setup () {
size(800, 800, P2D);
canvas = createGraphics(800, 800, P2D);
loop();
server = new SyphonServer(this, "sublime");
};
void draw() {
background(0);
canvas.beginDraw();
canvas.smooth();
canvas.background(0);
noStroke();
fill(255, 255, 255, 20);
rect(mouseX, mouseY, 50, 50);
time = (frameCount % frms)/float(frms);
paintQuads(canvas);
theta += 2/TWO_PI/frms;
canvas.endDraw();
image(canvas,0,0);
server.sendImage(canvas);
}
void paintQuads(PGraphics g) {
for (int i=0; i<num; i++) {
g.fill(0);
g.stroke(255);
float r = random(-.5, .5);
float sz = random(5, 15);
g.resetMatrix(); // Replaces the current matrix with the identity matrix.
// This effectively clears all transformation functions set before it.
//multiply the quads
//Translate
//Specifies an amount to displace objects within the display window.
//The x parameter specifies left/right translation, the y parameter specifies up/down
//translation, and the z parameter specifies translations toward/away from the screen.
//Using this function with the z parameter requires using P3D as a parameter in
//combination with size as shown in the above example.
g.translate(random(width), random(height));
g.rotate(r);
g.rotate(rotation);
//images
g.noStroke();
g.fill(255, 0, 0, 70);
g.quad(38, 31, 86, 20, 69, 63, 30, 76);
g.fill(255, 210, 0, 10);
g.quad(width-9, height-9, 86, 20, 69, 63, 30, 76);
g.fill(255, 0, 0, 30);
g.ellipse(36, 36, 16, 16);
g.fill(50, 46, 100, 20 );
g.quad(46, 20, 14, 14, 46, 20, 14, 14);
g.fill(50, 46, 100, 75);
g.quad(50, 0, 12, 12, 50, 0, 12, 12);
rotation=rotation+0.5;
}
// image(canvas, 0, 0);
//send canvas to Syphon
// server.sendScreen();
}
Or if PGraphics is confusing to use for now, skip it altogether and send the screen:
//Final Project
import codeanticode.syphon.*;
float rotation=0.1;
int num = 100, frms = 320;
float theta, time;
long rs;
SyphonServer server;
//long: Use this datatype when you need a number to have a greater magnitude than can be
//stored within an int.
void setup () {
size(800, 800, P2D);
smooth();
server = new SyphonServer(this, "sublime");
};
void draw() {
background(0);
noStroke();
fill(255, 255, 255, 20);
rect(mouseX, mouseY, 50, 50);
time = (frameCount % frms)/float(frms);
paintQuads();
theta += 2/TWO_PI/frms;
server.sendScreen();
}
void paintQuads() {
for (int i=0; i<num; i++) {
fill(0);
stroke(255);
float r = random(-.5, .5);
float sz = random(5, 15);
resetMatrix(); // Replaces the current matrix with the identity matrix.
// This effectively clears all transformation functions set before it.
//multiply the quads
//Translate
//Specifies an amount to displace objects within the display window.
//The x parameter specifies left/right translation, the y parameter specifies up/down
//translation, and the z parameter specifies translations toward/away from the screen.
//Using this function with the z parameter requires using P3D as a parameter in
//combination with size as shown in the above example.
translate(random(width), random(height));
rotate(r);
rotate(rotation);
//images
noStroke();
fill(255, 0, 0, 70);
quad(38, 31, 86, 20, 69, 63, 30, 76);
fill(255, 210, 0, 10);
quad(width-9, height-9, 86, 20, 69, 63, 30, 76);
fill(255, 0, 0, 30);
ellipse(36, 36, 16, 16);
fill(50, 46, 100, 20 );
quad(46, 20, 14, 14, 46, 20, 14, 14);
fill(50, 46, 100, 75);
quad(50, 0, 12, 12, 50, 0, 12, 12);
rotation=rotation+0.5;
}
}
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.