Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Just another collision thread (And some other questions)  (Read 1218 times)
0 Members and 1 Guest are viewing this topic.
Offline Styrisen

Junior Newbie





« Posted 2007-09-21 19:41:35 »

I want to start of by saying; sorry for making another collision thread. This is not only about collision though.
I've read through the forum and checked alot of other resources I just can't figure out how to do this and I need some help with my specific code. I'm new to Java and I still have some problems adapting others code to my own.

Anyways. I'm doing a modified bricks game, a little more advanced and a little diffrent. What i have is a ball (Ellipse2D.Double) and a unspecified level of blocks(Rectangle2D.Double) (diffrent kinds and diffrent amounts for each level).

(this part is not done yet, it only exists in my head Tongue)
What I thought of doing is a createLevel class that reads a specific levels layout from a csv or xml file, don't really know how to do that yet. The game panel should be tiled in a grid with tiles the same size as a block (all blocks have the same size). The level file would include a list of each tile and what kind of block (if any) should be present at that specific tile. The createLevel class then renders a level using the level file. I don't really know how to handle empty tiles. Should I place a special kind of invisible block with no collision or none at all?

First off, does that sound like a good idea for handeling diffrent levels?

createLevel would also store all blocks in an array (top left to bottom right tiles I guess). This would be for the collision since I need to check what kind of block the ball hits and in what tile it is.

Then, every time the ball moves I need to check if it hits something. I already made it collide with boundries of the game panel.
My "main question" is: how to do this?

I've tried 3 diffrent approaches. All with little or no luck.

   1. First I tried converting the ball and blocks to Areas and to a simple isEmpty() check on the ball to see if it was overlapping any block. This did how ever not even compile. I must've messed something up. Tried doing it exactly as I found it in another thread here on the forums. No success.
   2. Then I tried getting the x, y and dimensions for each block and checking if they overlapped by using Ellipse2D.intersects. This kinda worked. I got it to respond to colliding with the block (I only draw one just to test it). However the ball did get stuck on the block.. Couldn't get it to bounce of. This was my most successful try. I would think this is the most correct and exact way to do it. Am I right?
   3. Then I tried using the balls x and y coordinates to do a number of checks: first was the ball under the block (ball x > block x and ball x < block x + block width) and then check if the ball was aligned with any of the blocks y coordinates. I think that this failed due to my ball not moving one pixel at a time and therefore skipping the if-statement checking for y coordinates. Could probably be refined to work but would also probably be very ineffecient. So screw number 3.

Since I have 7 classes I'll post a link to a zip with all files in it. It's pretty well commented I think. Maybe not.. at least a little. [http://styrman.madtracker.net/Diamonds3.rar]

Any tips will be most welcome =)
Thanks for reading my long thread.. ^^

EDIT: Forgot to mention. in the collision method I need to be able to decide from what direction the bal hits the block. if the ball hits top or bottom, just change direction (bounce), if the ball hits sides keep going up/down but bounce the ball a little to the side, if ball hits the corner of a block send it back the same way it came.
Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-09-22 01:38:37 »

Hi!
What an ambitious project ! This be such an arcanoïd-like, wouldn't it ? Collisions are as simple as the coordinates are, the more variables for coordinates you have the more calculations you will have to make. Efficiency will be affected by number of loops you make on those calculations. But I can't make the code for you, by the way this is n't the most difficult task to achieve in a game, as for me I've chosen to brought up an easy 2D game where collisions are appearing eventually.
First of all, get the best of this and maybe other forums ! And as a suggestion  you should post the code in a more precise manner, to better focus on what you're talking about, that is, the collision alg. So here it is :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
import java.awt.*;
import java.awt.geom.*;

class Collision {
   // Variables
  public int bx;
   public int by;
   public int rx;
   public int ry;
   public int rw;
   public int rh;
   
   public int collide;      /*  This int can be: 0 = no collision
                                     1 = top collision
                                     2 = right collsion
                                     3 = bottom collision
                                     4 = left collision
                                                    */

   
   // Constructor
  public Collision() {
      /*
         We do not set variables here since then we would need to create a
         new Collision object each time we wanted to check for collision   */

   }
   
   public void setVariables(int bx, int by, int rx, int ry, int rw, int rh) {
      bx = bx;
      by = by;
      rx = rx;
      ry = ry;
      rw = rw;
      rh = rh;
   } // End of Collision()
 
   public int isColliding() {
      // Check to see if the ball is inside the blocks x span
     
      // Now, is the ball under or over the block?
     if(bx < rx && bx > (rx + rw)) {
         if(by == ry) { // Ball is over block
           collide = 1;
         }
         if(by == (ry + rh)) { // Ball is under block
           collide = 3;
         }
      }
      // Or is the ball on the left or right side of the block?
     else if(by > ry && by < (ry+ rh)) {
         if(bx == rx) { // Ball is on the left side of the block
           collide = 4;
         }
         if(bx == (rx + rw)) { // Ball is on the right side of the block
           collide = 2;
         }
      }
      else {   // No collision seems to be happening
        collide = 0;
      }
     
      return collide;
   } // End of isColliding()
} // End of Collision class
   

This can be quite enough, even though I didn't check the calculations, but we can discuss them afterwards. As a first check, I should say you missed the variables in the first public method, bx = bx etc. will toroughly fail !!! That is this.bx = bx and so on. Did you check that ?  Roll Eyes


::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Styrisen

Junior Newbie





« Reply #2 - Posted 2007-09-22 17:18:06 »

It will be pretty much like arcanoid. But no paddle and more puzzle =)

I'll check that out =)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fletchergames

Senior Member





« Reply #3 - Posted 2007-09-24 04:30:25 »

Store the velocity (along each axis), the old position, and the new position.  When there's a collision, move the ball back to the old position.  Then change the direction the ball is moving in.

The problem with this is that you need to know which axis the collision occurred on to know which direction to change.  I believe I checked for collisions after the x-coordinate changed and then collisions after the y-coordinate changed and finally after both coordinates are changed.  Change the direction for the axis along which the collision occurred.  The case when both coordinates are changed is required for the case when the collision only occurs after both coordinates are changed.

When I programmed Nanotron, I had to rewrite the collision code over and over because I kept having intermittent problems.  Finally, I got to work (with the ball getting stuck only once in a great while).  If the bricks in your game don't move, it shouldn't be too hard.  In Nanotron, the "bricks" were germs that moved around, making it a little more complicated.
Offline dishmoth
« Reply #4 - Posted 2007-09-24 14:58:14 »

Quote from: Styrisen
   1. First I tried converting the ball and blocks to Areas and to a simple isEmpty() check on the ball to see if it was overlapping any block. This did how ever not even compile. I must've messed something up. Tried doing it exactly as I found it in another thread here on the forums. No success.

For what it's worth the code would be something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
import java.awt.*;
import java.awt.geom.*;

[...]

// returns true if the two shapes overlap
public boolean checkForCollision(Shape s1, Shape s2) {
  Area a1 = new Area(s1);
  Area a2 = new Area(s2);
  a1.intersect(a2);
  return !( a1.isEmpty() );
}

The ball and the blocks are all types of "Shape" (Ellipse2D or Rectangle2D) so you can use them as arguments to this function.

But for what you're doing, this probably isn't necessary.  Unless the ball is quite large, you can approximate it quite adequately by a rectangle or just a single point.  As fletchergames says, you can treat movement and collision detection separately for the x direction and the y direction.  And if the end result looks a bit jumpy, then you can perform N updates by (dx/N, dy/N) instead of one update by (dx, dy), with N large enough so that the ball only moves by one or two pixels during each update.

Simon

Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #5 - Posted 2007-09-24 16:16:27 »

Ideally you should implement the collision detect using sweep tests.

http://www.gamasutra.com/features/19991018/Gomez_1.htm

The fundamental concept you need to understand, and apply is Separating Axis Theorem.

http://www.harveycartel.org/metanet/tutorials/tutorialA.html

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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 (37 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

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