Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to intersect a oval and a rect ?  (Read 4724 times)
0 Members and 1 Guest are viewing this topic.
Offline thorep

Senior Newbie





« Posted 2014-02-15 12:42:18 »

How to intersect a oval and a rect ?

I am making a pong game , and have got it to work.
But to detect collision im using

private boolean collision() {
        return game.racquet.getBounds().intersects(getBounds());
    }


    public Rectangle getBounds() {
        return new Rectangle(x, y, DIAMETER, DIAMETER);
    }

As you can see by the getBounds() the ball is made into a rectangle.
This means that sometime it looks like the ball will miss the paddle but it actuly hits because its a rectangle.
Is there anyway around this?
Offline trollwarrior1
« Reply #1 - Posted 2014-02-15 13:04:24 »

If your paddles are not rotated, a square hitting a square is fine. Square hitting a square will never miss.




Its quite the opposite - the ball will sometimes intersect too soon then it should.
Offline thorep

Senior Newbie





« Reply #2 - Posted 2014-02-15 13:09:55 »

This will result in a collision


But i want that to be a gameover();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Opiop
« Reply #3 - Posted 2014-02-15 13:15:19 »

So do it.
Offline trollwarrior1
« Reply #4 - Posted 2014-02-15 13:18:17 »

This will result in a collision


But i want that to be a gameover();

I didn't quite get you o-o
Offline Roquen

JGO Kernel


Medals: 517



« Reply #5 - Posted 2014-02-15 13:24:17 »

http://www.java-gaming.org/topics/vectors-what-s-the-point/24307/msg/225743/view.html#msg225743
Offline Andre Lopes
« Reply #6 - Posted 2014-02-15 13:24:59 »

I sensed a Disturbance in the force.

http://stackoverflow.com/questions/622287/area-of-intersection-between-circle-and-rectangle
http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
http://www.java-gaming.org/index.php/topic,10265.
Offline yokiyoki

Junior Devvie


Exp: 1 month or less


"It is fun to learn new things"


« Reply #7 - Posted 2014-02-15 14:29:36 »

It is easy.

Just use if-else condition.

Actually I made a game like this before while I am learning how to make a game through the help of tutorial.
http://zetcode.com/tutorials/javagamestutorial/

I want to tell the ball if what part of the block it hitted. If it is the top, bottom, left or right side, so that I can command the ball if where it will bounce.

for example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
height = 10;
width = 50;
diameter = 20;

if (hitted) {
   if (oval.x + diameter == rectangle.x && oval.y + diameter < rectangle.y)
      left
   else if (oval.x == rectangle.x + width && oval.y + diameter < rectangle.y)
      right
}


With this you already know that the ball hitted the sides.
And you wanted it to be "game over";

Then you can write:

1  
2  
if (left || right)
   game over


It is easy, right?
But this is not a runnable code.
It is only an Algorithm.

I hope it will help you.
Offline Longarmx
« Reply #8 - Posted 2014-02-15 16:35:14 »

But i want that to be a gameover();
Then just call gameover();

1  
2  
3  
4  
if(collision())
{
     gameover();
}

Offline thorep

Senior Newbie





« Reply #9 - Posted 2014-02-15 16:59:00 »

But i want that to be a gameover();
Then just call gameover();

1  
2  
3  
4  
if(collision())
{
     gameover();
}

I cant call game over on collision , then the ball wont bounce
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #10 - Posted 2014-02-15 17:01:16 »

So the have a rectangle at the ends of the field behind the paddles. If the ball collides with those then call game over.

Offline DrZoidberg

JGO Coder


Medals: 21



« Reply #11 - Posted 2014-02-15 18:22:00 »

Here is a simply example. The ball bounces of the corners of the rectangle at the correct angle.
http://pastebin.com/ez8VWAj2
Offline Roquen

JGO Kernel


Medals: 517



« Reply #12 - Posted 2014-02-16 06:47:39 »


Come over to the dark side.  We do stuff the easy way.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (489 views)
2018-06-10 19:43:48

EgonOlsen (552 views)
2018-06-10 19:43:44

EgonOlsen (404 views)
2018-06-10 19:43:20

DesertCoockie (701 views)
2018-05-13 18:23:11

nelsongames (1013 views)
2018-04-24 18:15:36

nelsongames (979 views)
2018-04-24 18:14:32

ivj94 (1503 views)
2018-03-24 14:47:39

ivj94 (556 views)
2018-03-24 14:46:31

ivj94 (1244 views)
2018-03-24 14:43:53

Solater (559 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!