Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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 2123 times)
0 Members and 1 Guest are viewing this topic.
Offline iams3b

Innocent Bystander

« Posted 2011-02-06 19: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:
 public void checkCollisions() {
       Rectangle r3 = car.getBounds();
       collide = false;
       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
             collide = true;
       if (!collide) {
    public Rectangle getBounds() {
       return new Rectangle(x, y, width, height);
(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:
       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: (keyboard input)
        if (key == KeyEvent.VK_LEFT) {
            if (accel != 0) {
               rotation -= 6;
        } (car's loop)
       if (accel != 0 && !collision) {
          rotation -= 90;
          x += Math.cos(Math.toRadians(rotation))*accel;
          y += Math.sin(Math.toRadians(rotation))*accel;
          rotation += 90;
       } (in paint loop)
        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

(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 !

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: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #1 - Posted 2011-02-07 02: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 02:37:39 »

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

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

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (24 views)
2015-08-04 11:03:17

SHC (39 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (45 views)
2015-07-27 16:38:00

Riven (25 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (41 views)
2015-07-25 00:14:37

BurntPizza (53 views)
2015-07-24 22:06:39

BurntPizza (33 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!