class Collision{ float angle; float zeroAngle; float distance; PVector activeObstical; Collision(){ angle = 180; zeroAngle = 0; distance = 300; activeObstical = new PVector(0,0); } void check(PVector car, PVector obstacle, PVector direction){ int obstacleSize = 10; // Radius of a obstacle PVector temp_vector = PVector.sub(obstacle,car); // Make temp vector to be able to calculate angle // Make the obstacle position on the edge of the obstacle float tempMag = temp_vector.mag(); temp_vector.normalize(); temp_vector.mult(tempMag - obstacleSize); // Angle between the current direction and the obstacle float tempAngle = degrees(PVector.angleBetween(temp_vector, direction)); // Angle between target direction and obstical // The distance from the current position to the obstacle float tempDistance = temp_vector.mag(); // Check if the previus obstacle has bean closer if(distance > tempDistance /*&& angle > tempAngle*/){ angle = tempAngle; distance = tempDistance; activeObstical.x = obstacle.x; activeObstical.y = obstacle.y; } } float adjustAngle(float angle, PVector car, PVector direction){ float returnAngle = 0; // Check adjAngle between the original rotation of the car PVector temp = PVector.sub(new PVector(width, car.y),car); // Make temp vector to be able to calculate angle PVector temp_vector = PVector.sub(activeObstical, car); // Make temp vector to be able to calculate angle zeroAngle = degrees(PVector.angleBetween(temp, direction)); float tempAngle = degrees(PVector.angleBetween(temp, temp_vector)); // Check if adjAngle should be positiv or negative if(activeObstical.y < car.y){ // If obstacle is over the zero vector if(zeroAngle > tempAngle){ if(angle > 0) returnAngle = angle * -1; }else{ returnAngle = abs(angle); } }else{ // If obstacle is under the zero vector if(zeroAngle <= tempAngle){ if(angle > 0) returnAngle = angle * -1; }else{ returnAngle = abs(angle); } } return returnAngle; } void clear(){ angle = 180; distance = 300; activeObstical.x = 0; activeObstical.y = 0; } }