Tracking a moving shape
in
Programming Questions
•
2 years ago
Hi there,
I have written a bi of code that basically makes a ball bouns between 1/10 and 9/10 of the width and height of the screen. The reason why it's 1/10 and 9/10 is because I want 4 paddles that will 'track' the ball so it looks like it is bouncing off the paddles. However when I tried coding the relative x and y points of the lines I was using for the paddles they follow their own paths and are out of sinc with the ball.
Is there a way of getting the 'center point' of the circle as it moves along the x and y axis. I'm after something like this
line(width/10, "x value of the center of the circle - shapesize/2", width/10 "x value of the center of the circle + shapesize/2");
This is my code thus far:
- float shapesize, xPos, yPos, xSpeed, ySpeed, xDirection, yDirection;
- void setup()
{
size(640, 640);
frameRate(30);
smooth();
strokeWeight(5);
xPos = width/2;
yPos = height/2;
xSpeed = 5;
ySpeed = 4;
xDirection = 1;
yDirection = 1;
if (width >= height)
{
shapesize = height/10;
}
else
{
shapesize = width/10;
}
} - void draw ()
{
background (80, 50, 80);
xPos = xPos + (xSpeed * xDirection);
yPos = yPos + (ySpeed * yDirection);
if (xPos > (width/10)*9-shapesize || xPos < width/10)
{
xDirection = xDirection * -1;
}
if (yPos > (height/10)*9-shapesize || yPos < height/10)
{
yDirection = yDirection * -1;
}
stroke(0, 0, 0);
ellipse(xPos+shapesize/2, yPos+shapesize/2, shapesize, shapesize);
stroke (255, 0, 0);
line(width/10, (yPos+shapesize/2)-shapesize/2, width/10, (yPos+shapesize/2)+shapesize/2);
stroke (0, 255, 0);
line((width/10)*9, (yPos+shapesize/2)-shapesize/2, (width/10)*9, (yPos+shapesize/2)+shapesize/2);
stroke (0, 0, 255);
line((yPos+shapesize/2)-shapesize/2, height/10, (yPos+shapesize/2)+shapesize/2, height/10);
stroke (255, 255, 0);
line((yPos+shapesize/2)-shapesize/2, (height/10)*9, (yPos+shapesize/2)+shapesize/2, (height/10)*9);
}
1