p5js, need help using get() to detect the color of a pixel as a RGBA array to see if the mouse is hovering over it - p5.js

Currently learning and working on a project, for this particular case I'm trying to simulate a collision detection by obtaining the color of a pixel with get().
In the console log, I can see the color array of the pixel the mouse is hovering on corresponds to my color in the colors array but then I can't seem to compare them. Seems like I'm unable to see through some basic principle, I've been trying for some hours to find a solution to no avail.
function setup() {
createCanvas(400, 400);
colors = [[255, 165, 0, 255], [61, 145, 64, 255], [204, 0, 0, 255], [31, 117, 254, 255],
[160, 32, 240, 255], [0, 128, 128, 255], [244, 0, 161, 255]];
}
function draw() {
background(220);
for(var i = 0; i < colors.length; i++){
fill(colors[i]);
ellipse(120, 40 + 45 * i, 40);
let c = get(mouseX, mouseY);
// console.log(c);
if(c == colors[i]){
console.log(colors[i]);
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

You can use the function I added at the bottom.
function setup() {
createCanvas(400, 400);
colors = [[255, 165, 0, 255], [61, 145, 64, 255], [204, 0, 0, 255], [31, 117, 254, 255],
[160, 32, 240, 255], [0, 128, 128, 255], [244, 0, 161, 255]];
}
function draw() {
background(220);
for(i = 0; i < colors.length; i++){
fill(colors[i]);
ellipse(120, 40 + 45 * i, 40);
}
for(i = 0; i < colors.length; i++){
let c = get(mouseX, mouseY);
if(arrayEquals(c,colors[i])){
console.log(colors[i]);
}
}
}
function arrayEquals(a, b) { //define a function with arguments a,b
return Array.isArray(a) && //true if 'a' is an array, '&&' checks if all arguments are true to return true otherswise false
Array.isArray(b) && //true if 'b' is an array
a.length === b.length && //true if 'a' is the same length as 'b'
a.every((val, index) => val === b[index]); //true if 'a' value is the same as the 'b' value at the same position for all positions
}

Related

make a translated object collide with something [duplicate]

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

Particle system within a drawing in p5.js

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

Forcing a sphere to spin

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();
}
}

Unable to move a shape in processing

I am attempting to create a movable entity who can move left and right on a map. I have a map that sets the initial location of the entity but once the 'a' or 'd' keys are pushed, the character moves only slightly then resets to its initial location once the key is released. I have a Boolean variable "playerIsSpawned" to ensure that the character's location is only set in that location once, however this didn't seem to fix anything. What is causing this and how can I fix it?
var start_map = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 9, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1]
];
var playerX;
var playerY;
function drawPlayer() {
fill(0);
rect(playerX, playerY, 50, 50);
}
function drawMap(map) {
// The x and y do not represent the x and y axis
// Keep in mind a 2d array is an array of an array
noStroke();
var playerIsSpawned = false;
for (var x = 0; x < map.length; x++) {
for (var y = 0; y < map.length; y++) {
// Background
if (map[y][x] == 0) {
fill(184, 236, 255);
rect((10 + 50*x), (10 + 50*y), 50, 50);
}
// Ground
else if (map[y][x] == 1) {
fill(51, 153, 51);
rect((10 + 50*x), (10 + 50*y), 50, 50);
}
// Player
else if (map[y][x] == 9) {
if (playerIsSpawned == false) {
playerX = (10 + 50*x);
playerY = (10 + 50*y);
playerIsSpawned = true;
}
fill(184, 236, 255);
rect((10 + 50*x), (10 + 50*y), 50, 50);
}
}
}
drawPlayer();
function keyPressed() {
if (key == "d") {
playerX += 5;
}
else if (key == "a") {
playerX -= 5;
}
}
keyPressed();
}
function setup() {
background(0);
createCanvas(800, 800);
}
function draw() {
drawMap(start_map);
}
You declared playerIsSpawned inside drawMap. That'll get reset to false every time through there.
Also, consider defining the keyPressed() at the top level (same as draw and setup, and getting rid of that call to keyPressed() inside your draw loop.

Using Syphon Send Frames for MADMAPPER

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;
}
}

Resources