-1

//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

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Leo
  • 67
  • 1
  • 10
  • You will improve your chances of getting help if you write your question in a way which makes it easier for people to help you. In this case, the most obvious improvements would be to create a [minimal, working example](https://stackoverflow.com/help/minimal-reproducible-example) and post it here in the question, formatted as code and not as a link. A future user with a similar problem may find a broken link and no answer otherwise, and we write those answer for you and also for the future. Tag me when you improve this question and I'll take a good look at it! – laancelot Jan 13 '21 at 21:28
  • 1
    A minimal example will be easy to produce and waaay easier to work with (and I don't know about the issue with pasting code but I have a hunch that a shorter code snippet than the whole project may work just fine! Otherwise tag me with the link of the example and I'll paste it into your question). – laancelot Jan 13 '21 at 21:32
  • 1
    @laanceloti added a code sniplet, dont know if that worked – Leo Jan 14 '21 at 07:33
  • @laancelot added some code – Leo Jan 14 '21 at 08:44

1 Answers1

1

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

  • I am glad I could help, sorry for the late comment after posting the answer. About the collision, when you are calculating if two objects are colliding, if your hitbox is a circle, its the easiest type since all you need to do is calculate the distance between them then check whether it's superior or not to the sum of their radius, you can check this link : https://happycoding.io/tutorials/processing/collision-detection scroll down to "Circle Circle Collision" In the code I wrote, I divided the sum of the radius by two since in processing it represents the circles heights, not the radius – YOUSFI Mohamed Walid Jan 17 '21 at 10:33
  • Thanks for the help m8, and sorry for my late answering. I do understand it now. – Leo Feb 01 '21 at 08:18