collision detection


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: collision detection

  1. #1
    Join Date
    Oct 2004
    Posts
    14

    collision detection

    I had an idea for a project i could do, writing a 2d game framework.
    I want to do collision detection and have a collision listener CollisionListener.
    I only have a vague idea of how listeners work, and I have no experiance writing one, I've just used them.

    Can someone explain the internal workings of listeners or give me a link to a page that does?

    -Jonathan

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Listeners are objects that sign on to another objects listener-list.
    That object checks its listeners-list when events occur and informs the listenersof the event using a defined interface. Here is a simple example, note: this must be coded in three separate java-files...

    Code:
    /**
     * the interface used by the listener
     */
    public interface BroadcastUser {
      public void changeWasMade (Object change);
    }
    
    /**
     * The class implementing the listener interface
     */
    public class AnyClass implements BroadcastUser {
      protected Broadcaster bc=new Broadcaster();
      public AnyClass () {
        bc.addBroadCastUser(this);
      }
      public void changeWasMade (Object change) {
        System.out.println("Change reported:"+change.toString());
      }
    
    }
    /**
     * The class that reports to the listeners
     */
    public class Broadcaster {
      ArrayList broadCastListeners=new ArrayList();
      public Broadcaster() {}
      // this class only recognizes its listeners as BroadcastUsers
      public void addBroadCastUser(BroadcastUser bu) {
        this.broadCastListeners.add(bu);
      }
      // in this method the change (or whatevver) is reported
      // to all the registered listeners
      public void aChangeMethod(Object aChange) {
        for (int i=0; i<boadCastListeners; i++) {
          BroadcastUser bu=(BroadcastUser)broadCastListeners.get(i);
          bu.changeWasMade(aChange);
        }
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Oct 2004
    Posts
    14
    Thank you.
    I still have to write the collision detection and I was wondering if anyone has any suggestions about that.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Hint: you have to have a way of telling that two objects occupy the same area on the driveway at the same time, so you must have a driveway/area, and the location of each driver in that area at any time.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center