Help a newbie with polymorphic particles

Hi there,

I am working on a university project with processing. This is a first time for me, so bear with me… We are creating a particle system (of sorts) that will eventually trigger sounds etc. The code I have attached below is stripped back to zoom in on the encountered problem and to ensure it doesn’t rely on external files.

The problem is that we have 2 classes of particles (named a larger blue ‘ball’ and smaller pink ‘blip’). Both collide with particles of the same type, but not with each other. We would like them to also collide with one another. in my research there are potentially 2 ways to do this:

  1. Create a parent class ‘particle’ of which particle types ‘ball’ and ‘blip’ extend this class
  2. Somehow recall the value of coordinates of one class of particle, and use them to define a collide function

I have tried both approaches cant seem to get either of the ground. Some pointers or code would be greatly appreciated!

Thanks a lot!

int numBalls = 4;

Ball[] balls = new Ball[numBalls];

int numBlips = 15;

Blip[] blips = new Blip[numBlips];

float spring = 1.6;

float gravity = 0.001;

float friction = -0.8;

void setup() {

frameRate(80);

size(displayWidth, displayHeight, OPENGL, P2D);

for (int i = 0; i < numBalls; i++)

balls[i] = new Ball(random(300, 600), random(450, 650), (80), i, balls);

for (int a = 0; a < numBlips; a++)

blips[a] = new Blip(random(300, 600), random(450, 650), (10), a, blips);

smooth();

}

int index = 0;

void draw() {

background(0, 10);

for (Ball ball : balls) {

ball.collide();

ball.move();

ball.display();

}

for (Blip blip : blips) {

blip.collide();

blip.move();

blip.display();

}

}

class Ball {

float x, y;

float diameter;

float vx = 0;

float vy = 0;

int id;

Ball[] others;

Ball(float xin, float yin, float din, int idin, Ball[] oin) {

x = xin;

y = yin;

diameter = din;

id = idin;

others = oin;

}

void collide() {

for (int i = id + 1; i < numBalls; i++) {

float dx = others[i].x - x;

float dy = others[i].y - y;

float distance = sqrt(dx*dx + dy*dy);

float minDist = others[i].diameter/2 + diameter/2;

if (distance < minDist) {

float angle = atan2(dy, dx);

float targetX = x + cos(angle) * minDist;

float targetY = y + sin(angle) * minDist;

float ax = (targetX - others[i].x) * spring*1.1;

float ay = (targetY - others[i].y) * spring*1.1;

vx -= ax;

vy -= ay;

others[i].vx += ax;

others[i].vy += ay;

}

}

}

void move() {

vy += gravity;

x += vx;

y += vy;

if (x + diameter/2 > width) {

x = width - diameter/2;

vx *= friction;

//sound[index].trigger();

} else if (x - diameter/2 < 0) {

x = diameter/2;

vx *= friction;

}

if (y + diameter/2 > height) {

y = height - diameter/2;

vy *= friction;

;

} else if (y - diameter/2 < 0) {

y = diameter/2;

vy *= friction;

;

}

}

void display() {

float disc=40;

gradientdisc(

x,

y,

40,

disc,

color(0, 0, 255),

color(255, 255, 255)

);

noStroke();

}

void gradientdisc( float x, float y, float radiusX, float radiusY, int fromC, int toC )

{

noStroke();

beginShape(TRIANGLE_STRIP);

int halfC = lerpColor(fromC, toC, 0.4);

for (float theta=0; theta<TWO_PI; theta+=TWO_PI/36)

{

fill(halfC);

vertex(x, y);

if ( theta <= PI )

fill(lerpColor(fromC, toC, (theta%PI)/PI ));

else

fill(lerpColor(toC, fromC, (theta%PI)/PI ));

vertex(x+radiusX*cos(theta), y+radiusY*sin(theta));

}

endShape();

float disc2=50;

stroke(0, 10);

strokeWeight(1);

noFill();

ellipse(x, y, 100, disc2*2);

}

}

class Blip {

float x, y;

float diameter;

float vx = 0;

float vy = 0;

int id;

Blip[] others;

Blip(float xin, float yin, float din, int idin, Blip[] oin) {

x = xin;

y = yin;

diameter = din;

id = idin;

others = oin;

}

void collide() {

for (int i = id + 1; i < numBlips; i++) {

float dx = others[i].x - x;

float dy = others[i].y - y;

float distance = sqrt(dx*dx + dy*dy);

float minDist = others[i].diameter/2 + diameter/2;

if (distance < minDist) {

float angle = atan2(dy, dx);

float targetX = x + cos(angle) * minDist;

float targetY = y + sin(angle) * minDist;

float ax = (targetX - others[i].x) * spring * 1.2;

float ay = (targetY - others[i].y) * spring * 1.2;

vx -= ax;

vy -= ay;

others[i].vx += ax;

others[i].vy += ay;

}

}

}

void move() {

vy += gravity * 2;

x += vx;

y += vy;

if (x + diameter/2 > width) {

x = width - diameter/2;

vx *= friction;

} else if (x - diameter/2 < 0) {

x = diameter/2;

vx *= friction;

}

if (y + diameter/2 > height) {

y = height - diameter/2;

vy *= friction;

} else if (y - diameter/2 < 0) {

y = diameter/2;

vy *= friction;

}

}

void display() {

stroke(255, 0, 144);

strokeWeight(1);

noFill();

ellipse(x, y, diameter, diameter);

}

}

submitted by /u/rbj123
[link] [comments]

Help a newbie with polymorphic particles

Hi there,

I am working on a university project with processing. This is a first time for me, so bear with me… We are creating a particle system (of sorts) that will eventually trigger sounds etc. The code I have attached below is stripped back to zoom in on the encountered problem and to ensure it doesn’t rely on external files.

The problem is that we have 2 classes of particles (named a larger blue ‘ball’ and smaller pink ‘blip’). Both collide with particles of the same type, but not with each other. We would like them to also collide with one another. in my research there are potentially 2 ways to do this:

  1. Create a parent class ‘particle’ of which particle types ‘ball’ and ‘blip’ extend this class
  2. Somehow recall the value of coordinates of one class of particle, and use them to define a collide function

I have tried both approaches cant seem to get either of the ground. Some pointers or code would be greatly appreciated!

Thanks a lot!

int numBalls = 4;

Ball[] balls = new Ball[numBalls];

int numBlips = 15;

Blip[] blips = new Blip[numBlips];

float spring = 1.6;

float gravity = 0.001;

float friction = -0.8;

void setup() {

frameRate(80);

size(displayWidth, displayHeight, OPENGL, P2D);

for (int i = 0; i < numBalls; i++)

balls[i] = new Ball(random(300, 600), random(450, 650), (80), i, balls);

for (int a = 0; a < numBlips; a++)

blips[a] = new Blip(random(300, 600), random(450, 650), (10), a, blips);

smooth();

}

int index = 0;

void draw() {

background(0, 10);

for (Ball ball : balls) {

ball.collide();

ball.move();

ball.display();

}

for (Blip blip : blips) {

blip.collide();

blip.move();

blip.display();

}

}

class Ball {

float x, y;

float diameter;

float vx = 0;

float vy = 0;

int id;

Ball[] others;

Ball(float xin, float yin, float din, int idin, Ball[] oin) {

x = xin;

y = yin;

diameter = din;

id = idin;

others = oin;

}

void collide() {

for (int i = id + 1; i < numBalls; i++) {

float dx = others[i].x - x;

float dy = others[i].y - y;

float distance = sqrt(dx*dx + dy*dy);

float minDist = others[i].diameter/2 + diameter/2;

if (distance < minDist) {

float angle = atan2(dy, dx);

float targetX = x + cos(angle) * minDist;

float targetY = y + sin(angle) * minDist;

float ax = (targetX - others[i].x) * spring*1.1;

float ay = (targetY - others[i].y) * spring*1.1;

vx -= ax;

vy -= ay;

others[i].vx += ax;

others[i].vy += ay;

}

}

}

void move() {

vy += gravity;

x += vx;

y += vy;

if (x + diameter/2 > width) {

x = width - diameter/2;

vx *= friction;

//sound[index].trigger();

} else if (x - diameter/2 < 0) {

x = diameter/2;

vx *= friction;

}

if (y + diameter/2 > height) {

y = height - diameter/2;

vy *= friction;

;

} else if (y - diameter/2 < 0) {

y = diameter/2;

vy *= friction;

;

}

}

void display() {

float disc=40;

gradientdisc(

x,

y,

40,

disc,

color(0, 0, 255),

color(255, 255, 255)

);

noStroke();

}

void gradientdisc( float x, float y, float radiusX, float radiusY, int fromC, int toC )

{

noStroke();

beginShape(TRIANGLE_STRIP);

int halfC = lerpColor(fromC, toC, 0.4);

for (float theta=0; theta<TWO_PI; theta+=TWO_PI/36)

{

fill(halfC);

vertex(x, y);

if ( theta <= PI )

fill(lerpColor(fromC, toC, (theta%PI)/PI ));

else

fill(lerpColor(toC, fromC, (theta%PI)/PI ));

vertex(x+radiusX*cos(theta), y+radiusY*sin(theta));

}

endShape();

float disc2=50;

stroke(0, 10);

strokeWeight(1);

noFill();

ellipse(x, y, 100, disc2*2);

}

}

class Blip {

float x, y;

float diameter;

float vx = 0;

float vy = 0;

int id;

Blip[] others;

Blip(float xin, float yin, float din, int idin, Blip[] oin) {

x = xin;

y = yin;

diameter = din;

id = idin;

others = oin;

}

void collide() {

for (int i = id + 1; i < numBlips; i++) {

float dx = others[i].x - x;

float dy = others[i].y - y;

float distance = sqrt(dx*dx + dy*dy);

float minDist = others[i].diameter/2 + diameter/2;

if (distance < minDist) {

float angle = atan2(dy, dx);

float targetX = x + cos(angle) * minDist;

float targetY = y + sin(angle) * minDist;

float ax = (targetX - others[i].x) * spring * 1.2;

float ay = (targetY - others[i].y) * spring * 1.2;

vx -= ax;

vy -= ay;

others[i].vx += ax;

others[i].vy += ay;

}

}

}

void move() {

vy += gravity * 2;

x += vx;

y += vy;

if (x + diameter/2 > width) {

x = width - diameter/2;

vx *= friction;

} else if (x - diameter/2 < 0) {

x = diameter/2;

vx *= friction;

}

if (y + diameter/2 > height) {

y = height - diameter/2;

vy *= friction;

} else if (y - diameter/2 < 0) {

y = diameter/2;

vy *= friction;

}

}

void display() {

stroke(255, 0, 144);

strokeWeight(1);

noFill();

ellipse(x, y, diameter, diameter);

}

}

submitted by /u/rbj123
[link] [comments]

Does art have any relevance “in the Age of AI”?

Christie’s recently sold for $432,000 a rather amusing portrait created by AI. Last Summer, (human) participants deemed that the artworks created by a computer system were more communicative and inspiring than human-made ones. A few years ago, an artist convincingly automated the kind of texts written by art critics. I could multiply the attention-grabbing stories but i’m sure that you’ve also been following the debates around the impact that AI is having on art and on the specificity of human creativity. But does art have a voice when it comes to understanding and shaping AI?


Blinking Turing by Vuk Cosic


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020

A couple of weeks ago in was in Rijeka, Croatia, to participate to E-relevance of Culture in the Age of AI, a seminar that aimed to offer food for thought to the Council of Europe’s reflection on the role that culture can have on the field of artificial intelligence. The sun was shining, i was wearing my favourite jumpsuit and the company was smart: Felix Stalder (media and cultural theorist and professor for Digital Culture and Network Theory at the Zürich University of the Arts), Vladan Joler (artist, founder the SHARE Foundation and professor at the University of Novi Sad), Gerfried Stocker (artistic director at Ars Electronica), Matteo Pasquinelli (professor in Media Philosophy at the University of the Arts and Design, Karlsruhe), etc. Everything was orchestrated by Vuk Cosic, a “cosmopolitan retired artist” and a classic of net.art.

I didn’t take many notes during the festival as i was engrossed in the debates so instead of a proper report, i’m just going to freewheel my way through a few bits and bobs i learnt over these two days in Rijeka. And i’ll focus ONLY on the art parts because you can’t really trust me with anything else.

E-relevance of Culture in the Age of AI. The cheekiness of the title isn’t obvious until you read it out loud. It sounds like the “irrelevance of culture in the age of AI.” It’s true it is often difficult to explain the invaluable role that art and culture can play in the evolution of forces that are going to shape society in ways we might not always fully comprehend.


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020

And yet, even if it is not immediately obvious, art (and culture in general) does have a role in stimulating a culture of reflection and healthy skepticism, in shaping new models and narratives, in articulating all the social dimensions of a technology like AI, on seeping into discussions and eventually into reality.

Science-fiction is a powerful example of the role art can have on the perception and even the development of a technology. Much the public’s imagination of what AI looks like and the kind of interaction we have with it is still shaped by Stanley Kubrick’s film 2001: A Space Odyssey. That film is 50 year old which tells us a lot about the role that culture can play in the debate around AI. The clean lines of Alexa and the voice of Siri, for example, probably owe a lot to the haunting image of AI that the film created.

As for the smoothness of technological ‘personal assistants’, they mask the complexity of the power relationships that are built into these machines.

Vuk Cosic made that hidden complexity of relationships more tangible when he brought to the discussion a series of anecdotes about the way folk culture is mocking AI, revealing how small accidents uncover the hold the technology has over our lives. And how we can sabotage it, albeit in very modest ways.

Starting with stories of accidental orders i had never heard of. Such as the one in which Amazon’s Alexa started ordering people dollhouses automatically upon hearing a news presenter on tv declare: “I love the little girl, saying ‘Alexa ordered me a dollhouse’.”


The burger king ad debacle. Photo from phandroid

A few months later, Burger King perhaps thought it would be a genius idea to piggyback on the dollhouse episode and exploit it for a TV spot. “You’re watching a 15-second Burger King ad, which is unfortunately not enough time to explain all of the fresh ingredients in the Whopper sandwich. But I’ve got an idea,” the narrator said, standing behind the counter at the burger chain. “OK Google, what is the Whopper burger?”

The trick was supposed to prompt voice-activated smart speakers into describing its burgers, just like Alexa had been tricked by a voice on the television to buy dollhouse. The problem, however, is that Google gets its explanation of the Whopper from Wikipedia, an encyclopedia everyone is free to edit.

Within hours of the ad’s release, users had made humorous modifications to the Whopper Wikipedia page. Soon after, Google appeared to make changes that stopped the commercial from activating the devices.

An interesting issue worth mentioning here is that wikipedia is free and written collaboratively by volunteers. And yet, this unpaid, crowdsourced source of valuable information is plundered by multi-billion corporations to make even more money.

At that moment in the conversation, Felix Stalder asked me: “Do you know of !Mediengruppe Bitnik’s work with Alexa?” No, i didn’t. And yes, it’s a great project. We wouldn’t expect anything less from these guys.


!Mediengruppe Bitnik (music by Low Jack, graphics by Knoth & Renner), Alexiety, 2018

Together with musician Low Jack, !Mediengruppe Bitnik have created an EP music record titled ‘Alexiety’. The album is made to be streamed on the radio “for the enjoyment of smart homes everywhere.”

In ‘Alexiety’, a set of three songs attempts to capture the feelings we develop toward Intelligent Personal Assistants: the carefree love that embraces Alexa before data privacy and surveillance issues outweigh the benefits; the alienation and decoupling / uncoupling from the allure of remote control and instant gratification; the anxiety and discomfort around Alexa and other Intelligent Personal Assistants that is Alexiety.

The work explores the unbalanced power relationship between Intelligent Personal Assistants that are taking more and more control over our lives and us, poor flesh and bones creatures who know so little about their algorithms, rule-sets and even real machinic presence.

Hardcore Anal Hydrogen, Jean-Pierre,2018

Speaking of music, in his statement Gerfried Stocker presented us with many fascinating artistic works that use AI. The one that really struck me might not be the most thought-provoking nor the most valuable in terms of critique of the technology though. Click and see above.


Kate Crawford and Vladan Joler, Anatomy of an AI System, 2018


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020

The event was also the opportunity to see Anatomy of an AI System in all its printed majesty. The map, created by Vladan Joler and Kate Crawford, elegantly dissects the whole genesis, life and death of an individual networked device based on a centralised artificial intelligence system. Printed on a gigantic sticker, the work was covering one of the walls of the seminar room.


Sterling Crispin, N.A.N.O. , B.I.O. , I.N.F.O. , C.O.G.N.O., 2015

My own contribution to the discussions in Rijeka consisted in reminding the audience that technology is not made of just algorithms and big data. I briefly explained the cost that the sometimes invisible materiality of AI, its infrastructures and the devices we use, is having on the environment and on the lives of workers who often live far away from us. I’m sure you already follow this kind of discussion so i’ll spare you the details. Among the artistic projects i used to illustrate the issue, i’ll only mention Sterling Crispin’s N.A.N.O. , B.I.O. , I.N.F.O. , C.O.G.N.O. because of the way it illustrates the tension between the grand vision and promises of the Silicon Valley and the fragility of a world that is increasingly shaken by contingencies such as the depletion of natural resources (energy, minerals, etc.) and climate change.


Michael Mandiblerg, Postmodern Times, 2018

I also talked about Michael Mandiberg’s Postmodern Times. The artist commissioned freelancers on the crowdsourcing labor platform Fiverr.com to recreate small clips of Charlie Chaplin’s Modern Times. Mandiberg then assembled all the small clips made by the hidden human cogs in the powerful digital machine and recreated the famous 1936 comedy, drawing a bittersweet portrait of the digital factory and its ruthless reliance on precarity.


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020

In conclusion, i’m not afraid for artists. I trust them to unfold all the expressive forms of AI technology, to use, abuse, hack, sabotage AI just like they do with any new medium. And as for us, the public, i suspect we’ll start treasuring human fallibility just like we are amused by the glitches in the machines nowadays.

With that said, i AM worried about the shrinking space that is left to art and culture today. Europe needs to create an even more nurturing environment for artists through education, commissions, residency programs and by facilitating collaboration with research centers. If Europe doesn’t make them feel valued, some of these bright and critical minds who have been educated with public money in Europe might just move to Silicon Valley (or to any of its European outposts) and dedicate their creativity to the sole glory of the GAFAM.


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020


E-relevance of Culture in the Age of AI at RiHub in Rijeka. Photo credit: Tanja Kanazir / ECOC Rijeka 2020

The seminar took place at RiHub in Rijeka, Croatia. RiHub in case you were wondering is a “nursery for innovative and creative work”. I find the term utterly ridiculous but the space is welcoming and amazingly well designed.

Processing help

I have a few problems with my code.

- I need to connect page 1 and 3, so I keep track of my game score and lives.

-On page 1 I have a problem with keeping track of score and lives

Page 3

--------

class Game {

int score=0;

int lives=5;

int speed=1;

int ellipseXdir=5;

int ellipseYdir=1;

int ellipseX;

boolean lost=false;

void life() {

if (lives<=0) {

noLoop();

lost=true;

textSize(20);

text("GAME OVER - Click to Restart", width/3.5, height/2);

textSize(13);

}

if (lost==true) {

speed=1;

lives=5;

score=0;

ellipseX=width/2;

ellipseXdir=5;

ellipseYdir=1;

lost=false;

loop();

//Will reset the game with the same speed of the ball and same amount of lives

}

}

}

Page 2

-----------

class Ball {

int ellipseX;

int ellipseY;

int ellipseH;

int ellipseW;

int speed=1;

int lives=5;

int score=0;

int ellipseXdir=2;

int ellipseYdir=1;

//All my declarions

Ball (int temp_ellipseX, int temp_ellipseY, int temp_ellipseH, int temp_ellipseW) {

ellipseX=temp_ellipseX;

ellipseY=temp_ellipseY;

ellipseH=temp_ellipseH;

ellipseW=temp_ellipseW;

}

void display () {

ellipse(ellipseX, ellipseY, ellipseH, ellipseW);

}

void moveBall() {

ellipseX=ellipseX+ellipseXdir*speed;

//I had to use float for both ellipseXdir and ellipse X, because int and floats can't be used in same equation

ellipseY=ellipseY+ellipseYdir*speed;

//I added a new variable for the direction

}

void bounderiesBall() {

if (ellipseX>width-ellipseW/2|| ellipseX<20)

//"||" means OR, so if the condition is true the code will be executed.

//

{

ellipseXdir=-ellipseXdir;

}

if (ellipseY>height-ellipseH/2 || ellipseY<20)

{

ellipseYdir=-ellipseYdir;

}

//Now rather than adding one to ellipseX each time we add ellipseXdir (the same with ellipseY)

//When ellipseXdir is negative we are subtracting one from ellipseX.

}

boolean detectHit() {

if (dist(mouseX, mouseY, ellipseX, ellipseY)<=ellipseH)

//dist calculates the distance two point using four variables

//x and y of the mousePressd and ellipseX and ellipseY

//this will make me able to set parameters of how far my mouseclick from what I define ("<=") my if() will be executed

{

speed=speed+1;

return true;

//You will get more points the higher

} else {

return false;

// if the ellipse is not clicked you'll lose a life

}

}

}

Page 1

-------------

Game gameInstance = new Game();

Ball [] BallRow = new Ball[2];

int n_Ball=BallRow.length;

int lives=5;

int score=0;

void setup() {

size(600, 400);

textSize(32);

// The setup of the basic variables

int eX = 200;

int eY =300;

int eH =40;

int eW =40;

for (int i=0; i<n_Ball; i++) {

BallRow[i] = new Ball(eX, eY, eH, eW);

BallRow[0]= new Ball(100, 100, 20, 20);

}

}

void draw() {

background(255);

pushMatrix();

fill(#FF0000);

text(("Score="+gameInstance.score), 50, 20);

//To keep track of score

text("Lives="+gameInstance.lives, 50, 40);

//To keep track of lives

popMatrix();

for (int i=0; i<n_Ball; i++) {

BallRow[i].display();

BallRow[i].moveBall();

BallRow[i].bounderiesBall();

}

}

void mousePressed()

{

for (int i=0; i<n_Ball; i++)

if (!BallRow[i].detectHit())

gameInstance.lives--;

else

gameInstance.score += gameInstance.score+BallRow[i].speed;

}

submitted by /u/torPske123
[link] [comments]

back again with another question!

Hey guys, If anybody could help me that would be great!

Can anybody tell me how I map the data inside a 2D array to a 2D grid?? I have tried google but couldn't find anything that helped.

basically, I have one 2D array that both creates a game board grid and fills the 2D array with info. However I can't seem to figure out how I make the game board clickable.

So whenever I click a square on the board it should tell me what data is in the array at that point.

here's my code:

// creating the gameboard int kaartx; int kaarty; void tekenKaart() { for (kaartx = 0; kaartx < ZeeInhoud.length; kaartx++) { for (kaarty = 0; kaarty < ZeeInhoud.length; kaarty++) { fill(DONKERGROEN); rect(blokmarge + blokgrootte * kaartx, blokmarge + blokgrootte * kaarty, blokgrootte, blokgrootte); } } } // filling the 2D array with data void bepaalInhoud() { for (int i = 0; i < ZeeInhoud.length; i++) { for (int j = 0; j < ZeeInhoud.length; j++) { ZeeInhoud[i][j] = round(random(3)); if (ZeeInhoud[i][j] == 1) { schepen ++; } } } } 

thanks for any help. edit: okay so I have tried something to "find" the info within the 2d array.

void inhoudPlaatsen() { if (rastergrootte == 10) { for (int i = 0; i < ZeeInhoud.length; i++) { for (int j = 0; j < ZeeInhoud.length; j++) { if (mouseX > blokmarge + blokgrootte * i && mouseX < blokmarge + blokgrootte * i && mouseY > blokmarge + blokgrootte + blokgrootte * j && mouseY < blokmarge + blokgrootte + blokgrootte * j) { println(ZeeInhoud[i][j]); } } } } } 

however this only finds the data within the first block of the gameboard and ignores the rest.

submitted by /u/IsThisGlenn
[link] [comments]

I’m having trouble with making a shape change color

I'm trying to make a circle move along with the mouse and change color depending on it's position (vibrant colors as you move down, hue change from left to right), but the color is always greyscale and never changes. I tried singling out the error and it seems like there's either some hidden rule I don't know or there's a combination of errors.

void setup() {

size(640, 360);

}

void draw(){

background(127);

cvlToRgb(mouseX/width,mouseY/height,0.5);

trackCircle(mouseX,mouseY);

}

void cvlToRgb(float c,float v,float l){

fill(255*l*(1+v*2*cos(TAU*c)),

255*l*(1+v*2*cos(TAU*(c-1/3))),

255*l*(1+v*2*cos(TAU*(c+1/3))));

}

void trackCircle(float x,float y){

ellipse(x,y,100,100);

}

submitted by /u/vroomvro0om
[link] [comments]

Why is an arc object moving with a jitter?

Hello, I'm currently taking my first coding class so I know that my code is sloppy but I need some help with an assignment. I am supposed to make a Pacman move with the arrow key and I am able to do that but for some reason, he only moves after a second and then he is very jittery when moving. I have a ghost moving on the screen as well but he just follows Pacman and he does not have this jitter. The Pacman appears to move at the same speed as the ghost even though he increases by 4 with each loop of draw and the Ghost increases by 2. Can someone help me understand why this is happening? I have bolded the part that moves pacman. Sorry if its a bit jumbled, like i said im new to this.

/*************

** Causes Pacman to move with the

** arrow keys and eat the dots to increase his score and draws a ghost that follows the pacman

** without leaving the canvas.

*/

//variables for Pacman

float pacman1X = 35; //Pacman1's x position.

float pacman2X; //Pacman2's x position.

float pacman3X; //Pacman3's x position.

float pacmanY; //Pacman's y position.

float pacArcX = 0.6; //Sets the x arc of pacman.

float pacArcY = 5.6; //Sets the y arc of pacman.

float chomp = 0.05;

final float DOT_DETECT_NUM = 2;

final float PACMAN_SPEED = 4;

final float PACMAN_SIZE = 70; //Sets the size of pacman.

final float FORWARD_SWAP = 0.8; //Determines where Pacman 1 should swap with Pacman 2 when going forward.

final float BACKWARD_SWAP = 0.2; //Determines where Pacman 1 should swap with Pacman 3 when going backward

//variables for the Ghost

float ghostX = 600;//Ghost's X

float ghostY = 250;//Ghost's Y

float tri1X;//Used to make the Ghost's Skirt.

float tri1Y;

float tri2X;

float tri2Y;

float tri3X;

float tri3Y;

final float GHOST_SIZE = 70; //Controls size of ghost

final float GHOST_SPEED = 2; //Contols speed of ghost

//Variable for the game

boolean success = false;

boolean failure = false;

//variables and arrays for the dots

float dotsNum = 50;// Number of dots that are drawn

boolean[] dotIs = new boolean[(int)dotsNum];//Sets of the dots should be drawn

float[] dotsX = new float[(int)dotsNum];//Sets dot X

float[] dotsY = new float[(int)dotsNum];//Sets dot

final float DOT_SIZE = 10;// Controls the size of the dots

/*

* setup - Prepares environment size and color.

*/

void setup()

{

//set canvas size and color

size(1000, 500);

background(0);

rectMode(CENTER);

noStroke();

pacmanY = height / 2; // Pacman Y initialized here so that it can be based on the height of the canvas.

assignDotPositions();

}

/*

* draw - Move pacman based on arrow keys and have him eat dots

* and make the ghost chase pacman.

*/

void draw()

{

//move pacman if mouse is clicked

//check bounds and make pacman bounce off top/bottom

//and wrap right to left

background(0); //Covers old drawings with each loop of draw

game();

}

/*

* game - determine what state the game is in, being played, won, or lost.

*/

void game()

{

if (success)

{

goodGame();

} else if (failure)

{

gameOver();

} else

{

playGame();

}

}

/*

* playGame - Runs the game.

*/

void playGame()

{

initializeVariables();

drawDots();

drawPacman();

makePacmanChomp();

makePacmanWrap();

drawGhost();

moveGhost();

displayScore();

death();

}

/*

* initializeVariables - initializes/adjusts all variables that need to be reinitialized with each loop of draw

*/

void initializeVariables()

{

pacman2X = pacman1X - width;

pacman3X = pacman1X + width;

tri1X = ghostX - 35;

tri1Y = ghostY + 35;

tri2X = ghostX - 17;

tri2Y = ghostY + 35;

tri3X = ghostX - 35;

tri3Y = ghostY + 45;

}

/*

* DrawPacman - Draws three pacmen at the given x, y.

*/

void drawPacman()

{

fill(253, 255, 0);

//Three Pacmen used to allow a smoother looking warp from left to right.

arc(pacman1X, pacmanY, PACMAN_SIZE, PACMAN_SIZE, pacArcX, pacArcY);

arc(pacman2X, pacmanY, PACMAN_SIZE, PACMAN_SIZE, pacArcX, pacArcY);

arc(pacman3X, pacmanY, PACMAN_SIZE, PACMAN_SIZE, pacArcX, pacArcY);

}

/\*

* keyPressed - makes Pacman move based in the arrow keys and prevents him from leaving the top or bottom of the screen

*/

void keyPressed()

{

int k = keyCode;

if (k == LEFT)

{

pacman1X -= PACMAN_SPEED;

}

if (k == RIGHT)

{

pacman1X += PACMAN_SPEED;

}

if (k == UP)

{

pacmanY -= PACMAN_SPEED;

}

if ( k == DOWN)

{

pacmanY += PACMAN_SPEED;

}

//Stop pacman from leaving screen

if (pacmanY <= (PACMAN_SIZE / 2))

{

pacmanY += PACMAN_SPEED;

} else if (pacmanY >= (height - (PACMAN_SIZE / 2)))

{

pacmanY -= PACMAN_SPEED;

}

}

void makePacmanChomp()

{

pacArcX = pacArcX - chomp;

pacArcY = pacArcY + chomp;

if (pacArcX <= -0.1 && pacArcY <= 6.4)

{

chomp = chomp * -1;

} else if (pacArcX >= 0.6 && pacArcY <= 5.6)

{

chomp = chomp * -1;

}

}

/*

* makePacmanWrap - Swaps Pacman1's position with pacman 2 or pacman 3 in order to maintain continued scrolling.

*/

void makePacmanWrap()

{

if (pacman2X >= (width * FORWARD_SWAP)) //Causes Pacman1 to swap places with Pacman 2.

{

pacman1X = pacman2X - width;

}

if (pacman3X <= (width * BACKWARD_SWAP)) //Causes Pacman1 to swap places with Pacman 3.

{

pacman1X = pacman3X + width;

}

}

/*

* DrawGhost - Draws a ghost on the canvas

*/

void drawGhost()

{

fill(0, 0, 255);

//Make ghost body

ellipse(ghostX, ghostY, GHOST_SIZE, GHOST_SIZE);

rect(ghostX, ghostY + 20, GHOST_SIZE, GHOST_SIZE - 40);

//Make ghost eyes

fill(255);

ellipse(ghostX - 17, ghostY - 10, GHOST_SIZE * 0.3, GHOST_SIZE * 0.3);

ellipse(ghostX + 17, ghostY - 10, GHOST_SIZE * 0.3, GHOST_SIZE * 0.3);

fill(0, 0, 255);

ellipse(ghostX - 17, ghostY - 10, GHOST_SIZE * 0.15, GHOST_SIZE * 0.15);

ellipse(ghostX + 17, ghostY - 10, GHOST_SIZE * 0.15, GHOST_SIZE * 0.15);

//Make ghost skirt

triangle(tri1X, tri1Y, tri2X - 5, tri2Y, tri3X, tri3Y);

triangle(tri1X + 32, tri1Y, tri2X - 5, tri2Y, tri3X + 30, tri3Y);

triangle(tri1X + 70, tri1Y, tri2X + 35, tri2Y, tri3X + 70, tri3Y);

triangle(tri1X + 38, tri1Y, tri2X + 39, tri2Y, tri3X + 40, tri3Y);

}

/*

* MoveGhost - Makes the ghost slowly move in the direction of pacman without being ablt to leave the canvas.

*/

void moveGhost()

{

//Make ghost move to pacman 1

if ((pacman1X > 0) && (pacman1X < width))

{

if (ghostX > pacman1X)

{

ghostX -= GHOST_SPEED;

} else if (ghostX < pacman1X)

{

ghostX += GHOST_SPEED;

}

} else if ((pacman2X > 0) && (pacman2X < width)) //Make ghost move to pacman 2

{

if (ghostX > pacman2X)

{

ghostX -= GHOST_SPEED;

} else if (ghostX < pacman2X)

{

ghostX += GHOST_SPEED;

}

} else if ((pacman3X > 0) && (pacman3X < width)) //Make ghost move to pacman 3

{

if (ghostX > pacman3X)

{

ghostX -= GHOST_SPEED;

} else if (ghostX < pacman3X)

{

ghostX += GHOST_SPEED;

}

}

if (ghostY > pacmanY)

{

ghostY -= GHOST_SPEED;

} else if (ghostY < pacmanY)

{

ghostY += GHOST_SPEED;

}

//Stop ghost from leaving screen

if (ghostX <= (GHOST_SIZE / 2))

{

ghostX += GHOST_SPEED;

} else if (ghostX >= (width - (GHOST_SIZE / 2)))

{

ghostX -= GHOST_SPEED;

}

if (ghostY <= (GHOST_SIZE / 2))

{

ghostY += GHOST_SPEED;

} else if (ghostY >= (height - (GHOST_SIZE / 2)) - 10)

{

ghostY -= GHOST_SPEED;

}

}

/*

* AssignDotPositions - Assigns values to the arrays

*/

void assignDotPositions()

{

for (int i = 0; i < dotsNum; i++)

{

dotIs[i] = true;

dotsX[i] = random((DOT_SIZE * 0.8), width - (DOT_SIZE * 0.8));

dotsY[i] = random((DOT_SIZE * 0.8), height - (DOT_SIZE * 0.8));

}

}

/*

* DrawDots - Draws dots and takes them away when pacman eats them

*/

void drawDots()

{

fill(255);

for (int i = 0; i < dotsNum; i++)

{

if (dotIs[i]) //Determines if pacman has already eaten the dot, if he hasent, draw the dot

{

ellipse(dotsX[i], dotsY[i], DOT_SIZE, DOT_SIZE);

}

if ((dotsY[i] <= pacmanY + (PACMAN_SIZE / DOT_DETECT_NUM)) && (dotsY[i] >= pacmanY - (PACMAN_SIZE / DOT_DETECT_NUM)) && ((dotsX[i] <= pacman1X + (PACMAN_SIZE /DOT_DETECT_NUM)) && (dotsX[i] >= pacman1X - (PACMAN_SIZE / DOT_DETECT_NUM))))//Determines if Pacman1 has touched a dot

{

dotIs[i] = false;

} else if ((dotsY[i] <= pacmanY + (PACMAN_SIZE / DOT_DETECT_NUM)) && (dotsY[i] >= pacmanY - (PACMAN_SIZE / DOT_DETECT_NUM)) && ((dotsX[i] <= pacman2X + (PACMAN_SIZE / DOT_DETECT_NUM)) && (dotsX[i] >= pacman2X - (PACMAN_SIZE / DOT_DETECT_NUM))))//Determines if Pacman2 has touched a dot

{

dotIs[i] = false;

} else if ((dotsY[i] <= pacmanY + (PACMAN_SIZE / DOT_DETECT_NUM)) && (dotsY[i] >= pacmanY - (PACMAN_SIZE / DOT_DETECT_NUM)) && ((dotsX[i] <= pacman3X + (PACMAN_SIZE / DOT_DETECT_NUM)) && (dotsX[i] >= pacman3X - (PACMAN_SIZE / DOT_DETECT_NUM))))//Determines if Pacman3 has touched a dot

{

dotIs[i] = false;

}

}

}

/*

* DisplayScore - Display the score on screen

*/

void displayScore()

{

float score = dotsNum;

for (int i = 0; i < dotsNum; i++)

{

if (dotIs[i])

{

score--;

}

}

fill(255, 255, 0);

textSize(30);

text("Score: " + (int)score, width - 150, 30);

if (score == dotsNum)

{

success = true;

}

}

/*

* death - determines if pacman has been eaten

*/

void death()

{

if ((ghostY - (GHOST_SIZE/3) <= pacmanY + (PACMAN_SIZE / 2)) && (ghostY + (GHOST_SIZE/3) >= pacmanY - (PACMAN_SIZE / 2)) && ((ghostX - (GHOST_SIZE/3) <= pacman1X + (PACMAN_SIZE / 2)) && (ghostX + (GHOST_SIZE/3) >= pacman1X - (PACMAN_SIZE / 2))))//Determines if Pacman1 has touched a dot

{

failure = true;

} else if ((ghostY - (GHOST_SIZE/3) <= pacmanY + (PACMAN_SIZE / DOT_DETECT_NUM)) && (ghostY + (GHOST_SIZE/3) >= pacmanY - (PACMAN_SIZE / DOT_DETECT_NUM)) && ((ghostX - (GHOST_SIZE/3) <= pacman2X + (PACMAN_SIZE / DOT_DETECT_NUM)) && (ghostX + (GHOST_SIZE/3) >= pacman2X - (PACMAN_SIZE / DOT_DETECT_NUM))))//Determines if Pacman2 has touched a dot

{

failure = true;

} else if ((ghostY - (GHOST_SIZE/3) <= pacmanY + (PACMAN_SIZE / DOT_DETECT_NUM)) && (ghostY + (GHOST_SIZE/3) >= pacmanY - (PACMAN_SIZE / DOT_DETECT_NUM)) && ((ghostX - (GHOST_SIZE/3) <= pacman3X + (PACMAN_SIZE / DOT_DETECT_NUM)) && (ghostX + (GHOST_SIZE/3) >= pacman3X - (PACMAN_SIZE / DOT_DETECT_NUM))))//Determines if Pacman3 has touched a dot

{

failure = true;

}

}

/*

* gameOver - Displays game over screen

*/

void gameOver()

{

fill(255, 0, 0);

textSize(50);

textAlign(CENTER);

text("You have been eaten by", width/2, height/2 - 50);

text("Inky, the Devourer of Gods", width/2, height/2 + 50);

}

/*

* goodGame - Displays victory screen

*/

void goodGame()

{

fill(100, 230, 255);

textSize(50);

textAlign(CENTER);

text("Congratulations! You prevailed against", width/2, height/2 - 50);

text("Inky, the Devourer of Gods", width/2, height/2 + 50);

}

submitted by /u/BoahNoa
[link] [comments]

Trying to animate a dithering algorithm from coding train.

This is the video in question : https://www.youtube.com/watch?v=JUDYkxU6J0o

I have been studying Java for about a year and a half right now, I do know my way around the language, but as many of you might know, processing is something similar enough that you recognize it, but at the same time so unfamiliar.

I have been trying to get my draw() function to loop and go over a image i have placed. I can get it to work if i implement normal nested for loops, the problem with that is that I am trying to get it animated. And the nested for loops will just do all the calculations and just spew out the final image. And personally that is not satisfying enough lol.

So here is my code so far (after removing a lot of unecessary bits) :

 PImage kitten; void setup() { size(1024, 512); kitten = loadImage("kitten.jpg"); image(kitten, 0, 0); frameRate(30); kitten.loadPixels(); } void draw() { kitten.loadPixels(); int x = 0; int y = 0; int index = x + y * kitten.width; color pix = kitten.pixels[index]; x++; if (x == kitten.width) { x = 0; y++; } float r = red(pix); float g = green(pix); float b = blue(pix); r = round(r / 255) * 255; g = round(g / 255) * 255; b = round(b / 255) * 255; kitten.pixels[index] = color(r, g, b); kitten.updatePixels(); image(kitten, 512, 0); } 

Thanks for your help!

submitted by /u/That_Doctor
[link] [comments]