Sign in to follow this  
Followers 0
ChristianFrantz

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='burnt_casadilla' timestamp='1344744411' post='4968613']
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 this post


Link to post
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 this post


Link to post
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>();
//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));
}
[/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 this post


Link to post
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 this post


Link to post
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.

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

[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
ballRadius = balls.get(0).getRadius(); //gets the first ball's radius[/code]

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0