Jump to content

  • Log In with Google      Sign In   
  • Create Account

collision with fillOval


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 442

Like
1Likes
Like

Posted 11 August 2012 - 08:39 PM

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, 11 August 2012 - 08:54 PM.

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


Sponsor:

#2 David.M   Members   -  Reputation: 731

Like
1Likes
Like

Posted 11 August 2012 - 09:39 PM

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.

#3 burnt_casadilla   Members   -  Reputation: 442

Like
1Likes
Like

Posted 11 August 2012 - 10:06 PM

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, 11 August 2012 - 10:10 PM.

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


#4 David.M   Members   -  Reputation: 731

Like
1Likes
Like

Posted 12 August 2012 - 09:18 AM

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().

#5 BaneTrapper   Members   -  Reputation: 1207

Like
1Likes
Like

Posted 12 August 2012 - 10:55 AM

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

Edited by BaneTrapper, 12 August 2012 - 10:57 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#6 burnt_casadilla   Members   -  Reputation: 442

Like
1Likes
Like

Posted 12 August 2012 - 03:18 PM

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 :)


#7 David.M   Members   -  Reputation: 731

Like
1Likes
Like

Posted 12 August 2012 - 04:03 PM

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
}

Edited by David.M, 12 August 2012 - 04:08 PM.


#8 burnt_casadilla   Members   -  Reputation: 442

Like
1Likes
Like

Posted 12 August 2012 - 04:10 PM

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 :)


#9 David.M   Members   -  Reputation: 731

Like
1Likes
Like

Posted 12 August 2012 - 04:24 PM

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.

Edited by David.M, 12 August 2012 - 04:26 PM.


#10 burnt_casadilla   Members   -  Reputation: 442

Like
1Likes
Like

Posted 12 August 2012 - 04:33 PM

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 :)


#11 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 12 August 2012 - 04:41 PM

Yes. Loop through balls.size().

Here's a picture of what you're doing for collision detection.
Posted Image

#12 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 12 August 2012 - 08:28 PM

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

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


#13 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 12 August 2012 - 09:51 PM

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?

#14 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 12 August 2012 - 09:53 PM

Not really i thought that this method returned a boolean value for some reason

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS