Need help with creating barriers/walls with boolean

please format code with </> button * homework policy * asking questions

float xpos;
float ypos;
float xposN;
float yposN;
float playersize;

PVector position;
float direction;
PVector velocity;
float jumpSpeed;
float walkSpeed;

void setup(){
size(640, 360);
//fullScreen();
xpos = 50;
ypos = 280;
playersize=20;
}

void draw(){
background(0); // Clear the screen with a black background

playerDraw(); //function for the player: a cube

xposN=xpos;//!!!make sure the player can move aside the wall!!!
yposN=ypos;

playerControl(); // up, down, left, right (keypressed)

playerRange();//move but stop at the floor or outside the canvas

floorDraw();//function for the ground and floors

//How do you create a wall? - #3 by colin

}

void playerDraw() {
stroke(255, 153, 0);//orange color stroke of the cube
fill(255, 153, 0); //orange color of the cube
rect(xpos, ypos, 20, 20);//position and size of the cube
}

void floorDraw() {
stroke(0, 153, 255); // blue color of the ground line
line(0, 310, 640, 310); // Creat a line as ground
//line(200, 250, 250, 250); // Creat a line as a floor
}

void playerControl()
{ if(keyPressed){
if (keyCode == LEFT || key == ‘a’) {xposN = xpos-2;}
if (keyCode == RIGHT || key == ‘d’) {xposN = xpos+2;}
if (keyCode == UP || key == ‘w’) {yposN = ypos-2;}
if (keyCode == DOWN || key == ‘s’) {yposN = ypos+2;}
}}

void playerRange() {
if(!floor(50,50,10,50) && !floor(100,50,10,50)){
xpos=constrain(xposN,0,width-playersize); //player size
ypos=constrain(yposN,0,height-playersize-50); //50 is the ground height
}}

boolean floor(float x1, float y1, float w1, float h1){
stroke(0, 153, 255);
fill(0, 153, 255);
rect(x1, y1, w1, h1);
return (xposN+playersize>x1 && xposN<x1+w1 && yposN+playersize>y1 && yposN<y1+h1);
}

//the reason to use boolean: The operator ! is undefined for the argument type(s) void
//the reason to use return: the method must return a result of type boolean
/boolean floor2(float x2, float y2, float w2, float h2){
stroke(0, 153, 255);
fill(0, 153, 255);
rect(x2, y2, w2, h2);
return (xposN+playersize>x2 && xposN<x2+w2 && yposN+playersize>y2 && yposN<y2+h2);
}
/

The rect on the right will just disappear if the cube hit the left one. I hope I could stop it from disappearing even the cube hit the left one, i.e, I have 2 barriers/walls.

I am a new learner and I got the current code from another forum so I am not sure the reason behind it also even I work on it for many hours.

Appreciate your kind help!

Regard,

Cherie

Hi! Welcome to the forum!

Before we begin, I have 2 comments - first please use </> button to format your code. Second, if you copied something from another forum, please give us a link - it’s totally ok to reuse and remix, but you must always give a credit (it helps us understand the context, too) :slight_smile:

1 Like

This is where I got the original code. Thanks for the reminder.

Thanks for your reminder. The link is attached.
For the </> button, May I know where should I add it to format it? Thanks!

Hello,

I usually share this link for new users:
https://discourse.processing.org/faq#format-your-code

Let me know if it is helpful.

:)

Hi thanks but I still can’t get the idea that " press the </> code button ".
What the </> code button mean? :sob:

No worries :slight_smile: You can find it here when you edit a message:

1 Like

you can go back and edit your initial post by pressing the “pencil button”

select the code in your post with the mouse and press the "</> button" in the small command bar

It’s a little hard to spot where the error is

reason is this:

if(!floor(50,50,10,50) && !floor(100,50,10,50)){

The if has TWO conditions.

BUT: when the first condition is met (or NOT met), the 2nd isn’t executed (to save time when running the Sketch) (which means, the 2nd floor() isn’t called so the rect gets not drawn )

Here I avoid this problem:


float xpos;
float ypos;
float xposN;
float yposN;
float playersize;

PVector position;
float direction;
PVector velocity;
float jumpSpeed;
float walkSpeed;

void setup() {
  size(640, 360);
  //fullScreen();
  xpos = 50;
  ypos = 280;
  playersize=20;
}

void draw() {
  background(0); // Clear the screen with a black background

  playerDraw(); //function for the player: a cube

  xposN=xpos;//!!!make sure the player can move aside the wall!!!
  yposN=ypos;

  playerControl(); // up, down, left, right (keypressed)

  playerRange();//move but stop at the floor or outside the canvas

  floorDraw();//function for the ground and floors

  //https://discourse.processing.org/t/how-do-you-create-a-wall/873/3
}

void playerDraw() {
  stroke(255, 153, 0);//orange color stroke of the cube
  fill(255, 153, 0); //orange color of the cube
  rect(xpos, ypos, 20, 20);//position and size of the cube
}

void floorDraw() {
  stroke(0, 153, 255); // blue color of the ground line
  line(0, 310, 640, 310); // Creat a line as ground
  //line(200, 250, 250, 250); // Creat a line as a floor
}

void playerControl()
{ 
  if (keyPressed) {
    if (keyCode == LEFT || key == 'a') {
      xposN = xpos-2;
    }
    if (keyCode == RIGHT || key == 'd') {
      xposN = xpos+2;
    }
    if (keyCode == UP || key == 'w') {
      yposN = ypos-2;
    }
    if (keyCode == DOWN || key == 's') {
      yposN = ypos+2;
    }
  }
}

void playerRange() {
  // two lines get executed no matter what ! 
  boolean a=floor(50, 50, 10, 50);
  boolean b=floor(100, 50, 10, 50);
  if (!a && !b) {
    xpos=constrain(xposN, 0, width-playersize); //player size
    ypos=constrain(yposN, 0, height-playersize-50); //50 is the ground height
  }
}

boolean floor(float x1, float y1, float w1, float h1) {
  stroke(0, 153, 255);
  fill(0, 153, 255);
  rect(x1, y1, w1, h1);
  return (xposN+playersize>x1 && xposN<x1+w1 && yposN+playersize>y1 && yposN<y1+h1);
}

//the reason to use boolean: The operator ! is undefined for the argument type(s) void
//the reason to use return: the method must return a result of type boolean
/*boolean floor2(float x2, float y2, float w2, float h2) {
 stroke(0, 153, 255);
 fill(0, 153, 255);
 rect(x2, y2, w2, h2);
 return (xposN+playersize>x2 && xposN<x2+w2 && yposN+playersize>y2 && yposN<y2+h2);
 }*/

Thanks for your help!

1 Like