Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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  
  Difficult collision detection  (Read 1739 times)
0 Members and 1 Guest are viewing this topic.
Offline iams3b

Innocent Bystander





« Posted 2011-02-06 20:47:15 »

Hi! A little introduction, my name is Seb and I just started doing Java this past week. I have experience with C++ and Flash/ Actionscript which helps me a lot, so I jumped straight into game developing with Java Smiley


So a little background, my friend tried giving me a challenge: make a car that you can drive from top view, and have it get more and more damaged as you hit the walls. So I took it as a good way to learn by trial and error how to do it !

This is what I have so far:



It works great so far ! Except: for the collision detecting on the walls. If you hit the wall in certain ways, sometimes it will float off through the wall into non-existence, or get stuck. But I realize thats because on a rotating car, I just made a rectangle around it and checked if it x.intercepts(y);

Ideally what I would love to do, is to make a front collision bumper on it and a back one for when you're driving forwards or backwards, but I have no idea how to place it at the right spot as the car is turning. This is where I need help Smiley


Here's my coding for the collision detection:
Board.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
 public void checkCollisions() {
       Rectangle r3 = car.getBounds();
       collide = false;
       
       //WALLS
      for (int i = 0; i < walls.size(); i++) {
          Wall b = (Wall)walls.get(i);  //walls objects in an ArrayList
         Rectangle r2 = b.getBounds();  //return rectangle from the wall obj
         if (r3.intersects(r2)) {  //ctest
            car.collision();
             collide = true;
          }
       }
       if (!collide) {
          car.resetCollision();
       }
}


Car.java
[code=java]
    public Rectangle getBounds() {
       return new Rectangle(x, y, width, height);
    }
[/code]
(walls.class has the same function)


car.collision() just sets 'collision' to be true in car so when its checked in the car's loop it does this:
car.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
       if (collision) {
          rotation -= 90;  //face the rotation towards the sprite
         if (accel >0) { accel = accel/accel; }
          if (accel < 0) { accel = -accel/accel; }

               //bounce car back away from the wall
         x += Math.cos(Math.toRadians(rotation))*-(accel*2);
          y += Math.sin(Math.toRadians(rotation))*-(accel*2);
          rotation += 90;
       }




now, as far as the car control goes, I have:
car.java (keyboard input)
1  
2  
3  
4  
5  
6  
        if (key == KeyEvent.VK_LEFT) {
            if (accel != 0) {
               rotation -= 6;
              makeTire();
               }
        }


car.java (car's loop)
1  
2  
3  
4  
5  
6  
       if (accel != 0 && !collision) {
          rotation -= 90;
          x += Math.cos(Math.toRadians(rotation))*accel;
          y += Math.sin(Math.toRadians(rotation))*accel;
          rotation += 90;
       }


Board.java (in paint loop)
1  
2  
3  
4  
        g2d.rotate(Math.toRadians(car.getRotation()), car.getX() + (car.getWidth()/2), car.getY() + (car.getHeight()/2)); //rotate the from its origin
       g2d.drawImage(car.getImage(), car.getX(), car.getY(), this); //draw the car at its position
       
        g2d.setTransform(at);




(I tried to limit the code i paste to just whats necessary to get the point across.. if you'd like to see more just request)


So any help or suggestions/concepts on how to go about getting better collision would be helpful !



EDIT:
So I took an extra step and drew the bounding box used for collision



I think I just need to rotate the rectangle object to the same rotation the car is.. can you do that? All I'm finding by searching is how to draw the rectangle rotated onto the screen
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-02-07 03:35:40 »

You can use the java.awt.Polygon class to create a box with the specified points.
Then you can use it's intersects() methods Cheesy

Note that you have to give it parallel arrays, one for x points and one for y points.

Offline philfrei
« Reply #2 - Posted 2011-02-07 03:37:39 »

The link below is to a very similar question, I suspect.

http://www.java-gaming.org/topics/rotating-a-rectangle-object/23694/view.html

I'm not at all sure what is most efficient, but I think you need to have a way of deriving the four corners of your car, which can be done with trig. Then you can test the four corner Points via a contains() on the wall structures?

I'm not sure you can assume there is no need to check rear collisions when moving forward. Consider this scenario: driving very close to the wall, in parallel, and turning away from it. Will the rear end rotate into the wall? I guess that depends on how you program the turns, if they occur as if only the front wheels change direction, or if both the front and back wheels change (as if rotating via a central point).

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!