removing rectangle with mousepressed() processing - processing

At the moment my object changes when i click anywhere in the window.
But i just wanna change the object when i click on the object.
I have no clue how to do this, can someone help me with this?
int a = 300,b = 200,c = 200,d = 100;
void setup()
{
size(600, 400);
background(230);
}
void draw(){
if (mousePressed == true) {
background(230);
ellipseMode(CENTER);
ellipse(300,200,200,100);
a = 0;
b = 0;
c = 0;
d = 0;
}
stroke(0);
fill(#032EFF);
rectMode(CENTER);
rect(a,b,c,d);
}

I found some helpful Forum posts and got it :)
Here is the working code:
float a = 200;
float b = 150;
float c = 200;
float d = 100;
void setup()
{
size(600, 400);
background(230);
}
void draw(){
stroke(0);
fill(#032EFF);
rect(a,b,c,d);
if (mousePressed) {
if(mouseX>a && mouseX <a+c && mouseY>b && mouseY <b+d){
println("es hat funktioniert");
background(230);
ellipseMode(CENTER);
ellipse(300,200,200,100);
a = 0;
b = 0;
c = 0;
d = 0;
}
}
}

Related

Can someone help me fix my DDA collision algorithm in processing?

I have been trying to make a DDA algorithm for a raycaster for a while now. For some reason I'm having a ton of trouble. The way my code works is that it puts a player into the map that spins at a constant rate. the green circle should be landing wherever the player is looking on the nearest wall. It seems to almost work when the line is more vertical than horizontal, but it still is a bit off. the moment it passes the y=x or y=-x line it goes crazy, seeming to shoot off into infinity. I suspect it has something to do with slope, but frankly I'm not really sure.
I have been following this video closely, but to no avail. I hope someone can spot the error in my code so I can continue with this project.
Here's a condensed version of my code:
int[][] map = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
float fov;
Player p;
void setup() {
size(800, 600);
background(0);
stroke(255);
fov = PI/2;
p = new Player(12.5, 12.5, -PI/4);
}
void draw() {
background(0);
p.map_();
p.ddaTest4();
p.dir += PI/512;
p.dir %= TWO_PI;
}
class Player {
float dir;
PVector pos = new PVector();
Player (float x, float y, float dir) {
this.dir = dir;
this.pos.x = x;
this.pos.y = y;
}
// this draws the map array as rectangles
void map_() {
noStroke();
float sf = height/map.length;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[j][i] != 0) {
stroke(0);
fill(255);
rect(i*sf, j*sf, height/sf, height/sf);
} else {
stroke(255);
fill(0);
rect(i*sf, j*sf, height/sf, height/sf);
}
}
}
}
void ddaTest4() {
float sf = height/map.length;
PVector rayDir = PVector.fromAngle(dir);
PVector stepScale = new PVector(sqrt(((rayDir.y/rayDir.x) * (rayDir.y/rayDir.x)) + 1), sqrt(((rayDir.x/rayDir.y) * (rayDir.x/rayDir.y)) + 1));
PVector mapPos = new PVector((int)pos.x, (int)pos.y);
PVector rayLength = new PVector();
PVector step = new PVector(1, 1);
if (rayDir.x < 0) {
step.x = -1;
rayLength.x = (pos.x - mapPos.x) * stepScale.x;
} else {
rayLength.x = ((mapPos.x + 1) - mapPos.x) * stepScale.x;
}
if (rayDir.y < 0) {
step.y = -1;
rayLength.x = (pos.y - mapPos.y) * stepScale.y;
} else {
rayLength.x = ((mapPos.y + 1) - mapPos.y) * stepScale.y;
}
boolean hit = false;
float distance = 0;
while(!hit) {
if (rayLength.x < rayLength.y) {
mapPos.x += step.x;
distance = rayLength.x;
rayLength.x += stepScale.x;
} else {
mapPos.y += step.y;
distance = rayLength.y;
rayLength.y += stepScale.y;
}
if (map[(int)mapPos.y][(int)mapPos.x] != 0) {
hit = true;
}
}
PVector hitPoint = PVector.add(pos, PVector.mult(rayDir, distance));
fill(0, 255, 0);
stroke(0, 255, 0);
line(pos.x*sf, pos.y*sf, hitPoint.x*sf, hitPoint.y*sf);
ellipse(hitPoint.x*sf, hitPoint.y*sf, 5, 5);
}
}
PS: sorry if I have poorly phrased my question, I'm not really sure how else to put it.

different speed of three sphere

I am a new coder on processing, that's why please be gentle.I made a simple code for you. Normally my code is more longer and complex.However, I wrote a simple code for you.
//main class
ArrayList<Clouds> clouds = new ArrayList();
void setup() {
size(1200, 800, P3D);
for (int i = 0; i < 3; i++)
{
Clouds C = new Clouds();
clouds.add(C);
}
}
void draw() {
background(0);
for (int i = 0; i < clouds.size(); i++)
{
clouds.get(i).drawClouds();
}
}
//Clouds class
class Clouds
{
float xC, yC, zC, speedC;
public Clouds()
{
xC = 20;
yC = 40;
zC = 0;
noStroke();
speedC = 1;
}
public void drawClouds()
{
translate(xC,yC);
pushMatrix();
makingClouds(100, 100, 100);
popMatrix();
if (xC > width - 780) {
xC = -660;
}
xC += speedC;
}
public void makingClouds(float xF, float yF, float zF ) {
translate(xF, yF, zF);
pushMatrix();
lights();
scale(1, 1, 1);
sphere(20);
popMatrix();
}
}
I hope, I'm not doing wrong with writing two class in here but I've worked on it two whole days and made me sick. So my question is: Like you see, there are three sphere and they have same speed but when I run the program, they go to end with different speeds. How they have same speed? If you help me, you will be my hero! Thank you.
translate() do not just set a translation, it defines a translation matrix and multiplies the new translation matrix to the current matrix.
You have to construct the clouds at different positions:
void setup() {
// [...]
for (int i = 0; i < 3; i++) {
Clouds C = new Clouds(20, i*40);
clouds.add(C);
}
}
class Clouds {
float xC, yC, zC, speedC;
public Clouds(float x, float y) {
xC = x;
yC = y;
zC = 0;
// [...]
}
And to move translate in the pushMatrix() / popMatrix() block:
class Clouds {
// [...]
public void drawClouds() {
pushMatrix();
translate(xC,yC);
// [...]
popMatrix();
// [...]
}
public void makingClouds(float xF, float yF, float zF ) {
pushMatrix();
translate(xF, yF, zF);
// [...]
popMatrix();
}
Example code:
//main class
ArrayList<Clouds> clouds = new ArrayList();
void setup() {
size(1200, 800, P3D);
for (int i = 0; i < 3; i++) {
Clouds C = new Clouds(20, i*40);
clouds.add(C);
}
}
void draw() {
background(0);
for (int i = 0; i < clouds.size(); i++) {
clouds.get(i).drawClouds();
}
}
//Clouds class
class Clouds {
float xC, yC, zC, speedC;
public Clouds(float x, float y) {
xC = x;
yC = y;
zC = 0;
speedC = 1;
}
public void drawClouds() {
noStroke();
pushMatrix();
translate(xC,yC);
makingClouds(100, 100, 100);
popMatrix();
if (xC > width - 780) {
xC = -660;
}
xC += speedC;
}
public void makingClouds(float xF, float yF, float zF ) {
pushMatrix();
translate(xF, yF, zF);
lights();
scale(1, 1, 1);
sphere(20);
popMatrix();
}
}

Array index out of bounds exception: 100

//----------------------------------------------\\
float x = 300;
float y = 300;
float direction = 0;
float increment = 1;
float speed = 5;
boolean toggle = true; // - For spaceship reversal
float wormX = random(0, 600); // - For wormHole v
float wormY = random(0, 600);
float wormGrowth = 0;
boolean growthSwitch = true; // - for wormHole ^
float[] starXpos = new float[100]; //starsRandom
float[] starYpos = new float[100]; //starsRandom
float d = dist(x, y, wormX, wormY);
int score = 0;
//----------------------------------------------\\
//----------------------------------------------\\ Setup
void setup (){
size (600, 600);
starsP1();
}
//----------------------------------------------\\ Draw
void draw (){
background (0);
spaceShip();
starsP2();
wormHole ();
score();
warpInitial();
blackHoleAt(100, 40);
blackHoleAt(400, 500);
}
//----------------------------------------------\\
//----------------------------------------------\\ starsRandom
void starsP1(){
int i = 0;
while (i < 100){
starXpos[i] = random(0, width);
starYpos[i] = random(0, height);
i = i + 1;
}
}
void starsP2(){
stroke(255);
strokeWeight(5);
int i = 0;
while (i < 100){
point(starXpos[i], starYpos[i]);
i = i + 1;
}
if (key == 'w'){
starYpos[i] = starYpos[i] + 1;
}
}
I'm trying to create a form of parallax for the stars in my code. When the user presses w,a,s,d the array of stars should correspond to the direction. I don't understand how this should work as I keep getting this error.
Try formatting your code to better see what's going on:
void starsP2(){
stroke(255);
strokeWeight(5);
int i = 0;
while (i < 100){
point(starXpos[i], starYpos[i]);
i = i + 1;
}
if (key == 'w'){
starYpos[i] = starYpos[i] + 1;
}
}
Your while loop executes until i == 100. Then after the while loop exits, you use that i variable again. Since i is 100, and your starYpos array only has up to index 99, you get an error.
The fix is to either move that if statement to inside the while loop, or to refactor your code so i doesn't go outside the bounds of the array.

Moving a shape and having the other one follow it

I am trying to draw a number of shapes in processing which react to each others movements. So when I move one of them, it is connected to say 1 or 2 of the other shapes in the sketch. However I don't want it to be a static movement, I want it to look quite natural.
The only code I have at the moment is basic drawing of a shape which moves when you drag it, but have no idea how to link shapes :( Help!!
float bx;
float by;
int boxSize = 75;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
void setup()
{
size(640, 360);
bx = width/2.0;
by = height/2.0;
rectMode(RADIUS);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > bx-boxSize && mouseX < bx+boxSize &&
mouseY > by-boxSize && mouseY < by+boxSize) {
overBox = true;
}
// Draw the box
rect(bx, by, boxSize, boxSize);
}
void mousePressed() {
if(overBox) {
locked = true;
} else {
locked = false;
}
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
void mouseDragged() {
if(locked) {
bx = mouseX-xOffset;
by = mouseY-yOffset;
}
}
void mouseReleased() {
locked = false;
}
I am thinking I need to use PShape, but am unsure.
First of all, I recommend you to use a class for the shapes, that way you can link them easily.
I make some changes in your code in order to get some kind of following behaviour.
class Box{
float bx;
float by;
int size = 75;
Box(float bx, float by){
this.bx = bx;
this.by = by;
}
void drawBox(){
rect(bx, by, size, size);
}
void moveBox(float x, float y){
bx = bx + x;
by = by + y;
}
}
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
Box box, secondBox, thirdBox;
void setup()
{
size(640, 360);
box = new Box(width/2.0, height/2.0);
secondBox = new Box(100, 100);
thirdBox = new Box(500, 300);
rectMode(RADIUS);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > box.bx-box.size && mouseX < box.bx+box.size &&
mouseY > box.by-box.size && mouseY < box.by+box.size) {
overBox = true;
box.drawBox();
secondBox.drawBox();
thirdBox.drawBox();
}
}
void mousePressed() {
if(overBox) {
locked = true;
} else {
locked = false;
}
xOffset = mouseX-box.bx;
yOffset = mouseY-box.by;
}
void mouseDragged() {
if(locked) {
box.bx = mouseX-xOffset;
box.by = mouseY-yOffset;
deltaMovemente(box, secondBox);
deltaMovemente(box, thirdBox);
}
}
void mouseReleased() {
locked = false;
}
void deltaMovemente(Box follow, Box box){
float dx = (follow.bx - box.bx)/50;
float dy = (follow.by - box.by)/50;
box.moveBox(dx, dy);
}
Hope this can be useful.
Regards.

Moving image with the cursor

So, I'm working on a processing project that lets me use an image as a cursor but I've been having problems with the cursor image because it has been constantly blinking. I read that if the cursor image is too big it has a tendency to constantly blink. However, I was wondering if there was any way that I could keep the size of my image, while still maintaining it as a cursor. OR,
I was wondering if there was a code that lets me press the image and drag it around the screen. :/
Here's the code that I've been using.
// Declaring a variable of type PImage
PImage img;
PImage img2;
void setup() {
size(815,514);
// Make a new instance of a PImage by loading an image file
img = loadImage("preamble.jpg");
img2 = loadImage("blackgun.png");
}
void draw() {
background(0);
// Draw the image to the screen at coordinate (0,0)
image(img,0,0);
//using the image as the cursor
if (mouseX < 50) {
cursor(img2);
} else {
cursor(img2);
}
}
In the reference they say: "it is recommended to make the size 16x16 or 32x32 pixels" about the image to be used as cursor. You can do this by calling resize:
img2 = loadImage("blackgun.png");
img2.resize(32,32);
Also there is no point in the lines:
if (mouseX < 50) {
cursor(img2);
} else {
cursor(img2);
}
As either way you end up with the same img2 as cursor image.
YOu can just use:
image(img, mouseX, mouseY);
but the cursor will be over the image.
That's a simple and poor drag...
I have here an old example of a little better drag and drop, it is using rects() intead of images but the idea is the same and you can easily adapt it to use images:
DragMe[] drags = new DragMe[40];
void setup() {
size(400, 400);
for (int i = 0; i < drags.length; i++) {
drags[i] = new DragMe();
}
}
void draw() {
background(255);
for (int i = 0; i < drags.length; i++) {
drags[i].display();
drags[i].update();
}
}
void mousePressed() {
for (int i = 0; i < drags.length; i++) {
if (!drags[i].isOver())
drags[i].dontMove = true;
drags[i].offset_x = mouseX - drags[i].pos_x;
drags[i].offset_y = mouseY - drags[i].pos_y;
}
}
void mouseReleased() {
for (int i = 0; i < drags.length; i++) {
drags[i].locked = false;
drags[i].dontMove = false;
}
}
class DragMe {
float pos_x, pos_y, SIZE = 20;
float prev_x, prev_y;
boolean locked;
boolean dontMove;
color c = color (0, 170, 170);
float offset_x, offset_y;
DragMe() {
pos_x = random(width-SIZE);
pos_y = random(height-SIZE);
}
void update() {
if (isOver() && !locked && !dontMove || locked && !dontMove )
c = color (170);
else
c = color (0, 170, 170);
if (isClicked()) {
locked = true;
}
if (locked && !dontMove) {
pos_x = mouseX - offset_x;
pos_y = mouseY - offset_y;
}
}
void display() {
fill(c);
rect(pos_x, pos_y, SIZE, SIZE);
}
boolean isOver() {
float right_x = pos_x + SIZE;
float bottom_y = pos_y + SIZE;
return mouseX >= pos_x && mouseX <= right_x &&
mouseY >= pos_y && mouseY <= bottom_y;
}
boolean isClicked() {
return isOver() && mousePressed && !dontMove;
}
}

Resources