1 Reply Latest reply: Sep 11, 2010 7:14 AM by 800025 RSS

    Unchecked call to collidesWith(T) as a member of raw type Collidable

    611651
      I created a Collidable interface much like the Comparable interface.
      Then BruteForceCollision uses the interface so I can collide with any class in the future how nice.

      The problem is that i'm getting warnings:

      Unchecked call to collidesWith(T) as a member of raw type Collidable
      at entity.collidesWith(otherEntity)

      I don't understand why. I say T extends Collidable so it's at least Collidable
      /**
       * Defines objects that can collide with each other
       */
      public interface Collidable<T extends Collidable> {
        boolean collidesWith(T otherEntity);
      
        void collided(T otherEntity);
      
        boolean canCollide();
      }
      
      /**
       * An entity represents any element that appears in the game
       */
      public abstract class Entity implements Collidable<Entity> {
        public boolean collidesWith(Entity otherEntity) {
          return true;
        }
      
        public void collided(Entity otherEntity) {
        }
      
        public boolean canCollide() {
          return true;
        }
      }
      
      /**
       * Finds collisions between objects that implement collidable and notifies the two entities of the collision.
       */
      public class BruteForceCollision{
        List<Collidable> entities;
      
        public CollisionManager() {
          entities = new ArrayList<Collidable>();
        }
      
        public CollisionManager(Collidable... entities) {
          this();
          addEntities(entities);
        }
      
        public void addEntities(Collidable[] entities) {
          this.entities.addAll(Arrays.asList(entities));
        }
      
        /**
         * Compares each entity with every other entity.
         * If they collide with each other then both are notified
         *
         * @return if a collision occurred
         */
        public boolean searchForCollision() {
          for (Collidable entity : entities) {
            for (Collidable otherEntity : entities) {
              if (entity == otherEntity) continue;
      
              if (entity.canCollide() && entity.collidesWith(otherEntity)) {
                System.out.println(entity.getClass().getSimpleName() + " collided with " + otherEntity.getClass().getSimpleName());
                entity.collided(otherEntity);
                otherEntity.collided(entity);
                return true;
              }
            }
          }
          return false;
        }
      }
      I tried giving BruteForceCollision a type parameter Replacing all Collidable with T. But the warning remains.
      What's wrong?
        • 1. Re: Unchecked call to collidesWith(T) as a member of raw type Collidable
          800025
          Then BruteForceCollision uses the interface so I can collide with any class in the future how nice.
          Then why are you using generics and prescribing a particular type of Collidable? What is wrong with leaving out generics altogether and simply use Collidable as argument?

          In the following I put your classes in one source file for convenience:
          import java.util.*;
          
          public class Collision {
          
              public interface Collidable {
               boolean collidesWith(Collidable otherEntity);
          
               void collided(Collidable otherEntity);
          
               boolean canCollide();
              }
          
              public static abstract class Entity implements Collidable {
               public boolean collidesWith(Entity otherEntity) {
                   return true;
               }
          
               public void collided(Collidable otherEntity) {
               }
          
               public boolean canCollide() {
                   return true;
               }
              }
          
              public static class CollisionManager {
               List<Collidable> entities;
          
               public CollisionManager() {
                   entities = new ArrayList<Collidable>();
               }
          
               public CollisionManager(Collidable... entities) {
                   this();
                   addEntities(entities);
               }
          
               public void addEntities(Collidable[] entities) {
                   this.entities.addAll(Arrays.asList(entities));
               }
          
               /**
                * Compares each entity with every other entity. If they collide with
                * each other then both are notified
                * 
                * @return if a collision occurred
                */
               public boolean searchForCollision() {
                   for (Collidable entity : entities) {
                    for (Collidable otherEntity : entities) {
                        if (entity == otherEntity)
                         continue;
          
                        if (entity.canCollide() && entity.collidesWith(otherEntity)) {
                         System.out.println(entity.getClass().getSimpleName()
                              + " collided with "
                              + otherEntity.getClass().getSimpleName());
                         entity.collided(otherEntity);
                         otherEntity.collided(entity);
                         return true;
                        }
                    }
                   }
                   return false;
               }
              }
          }
          Piet