Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (732)
Games in Android Showcase (221)
games submitted by our members
Games in WIP (806)
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 2420 times)
0 Members and 1 Guest are viewing this topic.
Offline Styrisen

Junior Newbie

« Posted 2007-09-21 17: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. []

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 Devvie

« Reply #1 - Posted 2007-09-21 23:38:37 »

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 :
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 @
on /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Styrisen

Junior Newbie

« Reply #2 - Posted 2007-09-22 15: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 Devvie

« Reply #3 - Posted 2007-09-24 02: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 12: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:
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);
  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.


Offline Abuse

JGO Ninja

Medals: 59

falling into the abyss of reality

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

Ideally you should implement the collision detect using sweep tests.

The fundamental concept you need to understand, and apply is Separating Axis Theorem.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Archive (407 views)
2017-04-27 17:45:51

buddyBro (588 views)
2017-04-05 03:38:00

CopyableCougar4 (1046 views)
2017-03-24 15:39:42

theagentd (1079 views)
2017-03-24 15:32:08

Rule (1052 views)
2017-03-19 12:43:22

Rule (1035 views)
2017-03-19 12:42:17

Rule (1027 views)
2017-03-19 12:36:21

theagentd (1166 views)
2017-03-16 05:07:07

theagentd (1122 views)
2017-03-15 22:37:06

theagentd (835 views)
2017-03-15 22:32:18
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

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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!