Followers 0

collision with fillOval

13 posts in this topic

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? Edited by burnt_casadilla
1

Share on other sites
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.
1

Share on other sites
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 Edited by burnt_casadilla
1

Share on other sites
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 [url=http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html#intersects(java.awt.Rectangle)]Rectangle.intersects()[/url].
1

Share on other sites
but the others are created by a random point so i dont have a set point to test the bounds againsst
[/quote]

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 Edited by BaneTrapper
1

Share on other sites
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?
1

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

[code]ArrayList<Ball> balls = new ArrayList<Ball>();
private Ball makeBall()
{
Random rand = new Random();
Ball newBall = new Ball();
newBall.x = rand.nextFloat() * FRUSTUM_WIDTH;
newBall.y = rand.nextFloat() * FRUSTUM_HEIGHT;
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,
}
[/code]

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
[code]for(int i = 0; i < balls.size(); i++)
{
float x = balls.get(i).getX();
float y = balls.get(i).getY();
//check collisions here
}[/code] Edited by David.M
1

Share on other sites
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
1

Share on other sites
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.

[code]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

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. Edited by David.M
1

Share on other sites
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?
1

Share on other sites
Yes. Loop through balls.size().

Here's a picture of what you're doing for collision detection.
[IMG]http://img4.imageshack.us/img4/4663/20120812163527.jpg[/IMG]
0

Share on other sites
i currently have this
[source lang="java"] public Rectangle getFixedBallBounds()
{
return new Rectangle (250, 250, 20, 20);
}

public Rectangle getMainBallBounds()
{
return new Rectangle(mainBall.xpos, mainBall.ypos, 10, 10);
}

public boolean checkCollision()
{
if(mainBallRectangle.intersects(fixedBallRectangle))
{
mainBall.xspeed = 0;
mainBall.yspeed = 0;
mainBall.xpos = 100;
mainBall.ypos = 100;
}
return true;
}[/source]
but it does nothing
0

Share on other sites
Rectangle constructs a rectangle whose upper-left is at the specified x and y coordinates and width and height extend from there. Make sure those are in the proper positions.

Any special reason you're using a boolean function that always returns true?
0

Share on other sites
Not really i thought that this method returned a boolean value for some reason
0

Create an account

Register a new account