collision with fillOval

Started by
12 comments, last by ChristianFrantz 11 years, 8 months ago
I have 4 ovals being drawn to the screen and i need a way to check if a movable ball collides with the 4 that were drawn on the screen.
They 4 are drawn at random points on the screen but itd be near impossible to hit the exact center of the oval.
How would i detect a collision with the edges of the oval?

say i have the main ball moving right with starting coordinates 100, 100 and it comes into contact with a ball at 200, 100. Do i use a formula to measure this?

If you see a post from me, you can safely assume its C# and XNA :)

Advertisement
If you have a small enough circle or accuracy isn't an issue you can assign bounding rectangles. Otherwise I would check the bounds against circle.x + radius, circle.x - radius, circle.y + radius, and circle.y - radius.
i changed the balls to rectangles to make it a little easier

the main rectangle has the coords 100, 100 and is 10, 10 high and wide, but the others are created by a random point so i dont have a set point to test the bounds againsst

If you see a post from me, you can safely assume its C# and XNA :)

You don't need a set point to test against. You should be keeping track of the x and y position of the object so you just need to check if one side is inside another. If the left side of one rectangle is inside another rectangle its (position - width) will be less than the other rectangle's right edge and greater than its left. This can easily be applied to the other sides.

Rectangle to Rectangle you can use Rectangle.intersects().

but the others are created by a random point so i dont have a set point to test the bounds againsst


hope you understand there is no such thing as random.
If its drawn at some point, that point had to exist, you just need to grab it at that time.

EDIT:: oh java... well i got low knowledge of java. But it should be still same
ok so i have random balls created every 3 seconds at random points x and y. I know these are stored in the array list, but how would i call the points from the list and then set the boundaries within the public Rectangle getBounds() method?

If you see a post from me, you can safely assume its C# and XNA :)

If you want to make a ball at some random position I would do something like the below.

ArrayList<Ball> balls = new ArrayList<Ball>();
//Option 1: return a ball to be added like balls.add(makeBall());
private Ball makeBall()
{
Random rand = new Random();
Ball newBall = new Ball();
newBall.x = rand.nextFloat() * FRUSTUM_WIDTH;
newBall.y = rand.nextFloat() * FRUSTUM_HEIGHT;
newBall.radius = rand.nextFloat() * MAX_RADIUS;
return newBall;
}

//Option 2: add the ball to the list inside the method
private void newBall()
{
Random rand = new Random();
balls.add(new Ball(rand.nextFloat() * FRUSTUM_WIDTH, rand.nextFloat() * FRUSTUM_HEIGHT,
rand.nextFloat() * MAX_RADIUS));
}


This would have a ball class which stores an x and y position and a radius. The list will store all the balls and be looped through to update and render the balls. Option 1 has a blank constructor and the fields (x, y, radius) must be initialized manually. Option 2 has a constructor public Ball(float xPosition, float yPosition, float radius).

Edit: Then you would call this
for(int i = 0; i < balls.size(); i++)
{
float x = balls.get(i).getX();
float y = balls.get(i).getY();
//check collisions here
}
this is what i have for creating balls

[source lang="java"]class Ball
{
int x;
int y;
int width;
int height;

public Ball(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball

public void paint(Graphics g)
{
g.setColor(color[getRandomColor()]);
g.fillOval(x, y, width, height);
} //end paint

} //ball class[/source]

[source lang="java"] public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);

for (Ball ball : BALLS)
{
ball.paint(g);
}

updateGame();
}[/source]
and this works fine for what i want. but im wondering how to grab these points from the arraylist and the use those points in the getBounds() method for collision against another ball

If you see a post from me, you can safely assume its C# and XNA :)

Any special reason for having both a width and height? If it's a circle those are the same so you can have just one field, radius, to cover width and height.

To your Ball class you can add getX(), getY(), and getRadius() methods.

ArrayList<Ball> balls = new ArrayList<Ball>();

ballPositionX = balls.get(0).getX(); // gets the first ball's x position
ballPositionY = balls.get(0).getY(); //gets the first ball's y position
ballRadius = balls.get(0).getRadius(); //gets the first ball's radius


Then, circle collision detection is easy. Take two balls. Grab the x and y positions (centers) of both of them. If those positions are within (<) ball1.radius + ball2.radius then they are colliding.
well its really an oval lol. so id have to put that in a for loop in order to get each coordinates of each ball getting created every 3 seconds?

If you see a post from me, you can safely assume its C# and XNA :)

This topic is closed to new replies.

Advertisement