What are my x2 and y2 positions for distance if sharing the same single Object
in
Programming Questions
•
5 months ago
Hi all,
I'm not too sure how to word my question so I'll try keep it simple. Basically, I'm trying to get the distance between two ellipses that share the same class. A single ellipse is created in the 'display' method, so any instance of the class will share the same x and y coordinates. So at the moment I don't know what I should be plugging in for the x2 and y2 points of my distance formula because they are not defined. I could solve my problem by making another ellipse in the display method with different x,y variables but then I would have to implement my 'moveBall method etc to accommodate the new variables, which I don't want to do. So the way I have defined the class using a single ellipse I now don't know what I should be plugging in for the x2 and y2 coordinates of the distance formula located in the 'collision' method.
Hope that sort of make sense? I'm sure their is a simple logical solution I'm not seeing.
I'm not too sure how to word my question so I'll try keep it simple. Basically, I'm trying to get the distance between two ellipses that share the same class. A single ellipse is created in the 'display' method, so any instance of the class will share the same x and y coordinates. So at the moment I don't know what I should be plugging in for the x2 and y2 points of my distance formula because they are not defined. I could solve my problem by making another ellipse in the display method with different x,y variables but then I would have to implement my 'moveBall method etc to accommodate the new variables, which I don't want to do. So the way I have defined the class using a single ellipse I now don't know what I should be plugging in for the x2 and y2 coordinates of the distance formula located in the 'collision' method.
Hope that sort of make sense? I'm sure their is a simple logical solution I'm not seeing.
- color c = color(255, 0, 0);
color c2 = color(0, 255, 0);
Ball ballOne = new Ball(50, 250);
Ball ballTwo = new Ball(950, 250);
void setup() {
background(0);
size(1000, 500);
smooth();
ellipseMode(CENTER);
frameRate(60);
}
void draw()
{
background(0);
ballOne.display(c);
ballOne.moveBall();
ballTwo.display(c);
ballTwo.moveBall();
if (ballOne.collide(ballTwo))
{
fill(c2);
}
else
{
fill(c);
}
if (frameCount % 60 == 0)
{
println(frameRate);
}
}
class Ball {
int dia;
int x, y;
int speedX;
int speedY;
Ball(int x_, int y_)
{
x = x_;
y = y_;
dia = 100;
speedX = 6;
speedY = 1;
}
void display(color cAll)
{
noStroke();
fill(cAll);
ellipse(x, y, dia, dia);
}
void moveBall()
{
x = x + speedX;
if (x > width - dia/2)
{
speedX = -abs(speedX);
}
if (x < 0 + dia/2)
{
speedX = abs(speedX);
}
}
boolean collide(Ball b)
{
//How do i get the x2 and y2 coordinates for the second instance of Ball
float distance = sqrt (pow(x - x2, 2) + pow(y - y2, 2)); // < --------- ?!?!
println("This is distance: " + distance);
if (distance < ((b.dia/2) + dia/2))
{
return true;
}
return false;
}
}
1