⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bulletterrainlistener.java

📁 Java 3D API, 一套完整的3d引擎
💻 JAVA
字号:
import com.threed.jpct.*;

/**
 * A CollisionListener that will be notified when a bullet hits the ground. This
 * listener will be added to the ground, not to every bullet.
 */
public class BulletTerrainListener implements CollisionListener {

   private ProjectileManager bulMan=null;

   public BulletTerrainListener(ProjectileManager bulMan) {
      this.bulMan=bulMan;
   }

   public boolean requiresPolygonIDs() {
      // We really need them...
      return true;
   }

   public void collision(CollisionEvent e) {
      /**
       * Make sure that something collided with the ground and that this
       * was an actual entity (and not the camera or whatever).
       */
      if (e.getType()==CollisionEvent.TYPE_TARGET && e.getSource()!=null) {
         Object obj=e.getSource();
         /**
          * Make sure, that the source of the collision was really
          * a bullet.
          */
         if (obj instanceof Bullet) {
            Bullet bullet=(Bullet) obj;
            bullet.disable();
            Object3D ground=e.getObject();

            /**
             * Here, we have to calculate the position and orientation of the decal the
             * bullet leaves at the ground. This requires a call to calcMinDistance() and
             * to make sure that we are not triggering the CollisionListener again in that case,
             * we temporally disable collision events on the ground.
             */
            ground.disableCollisionListeners();
            SimpleVector za=bullet.getZAxis();
            SimpleVector tc=bullet.getTransformedCenter();
            float d=ground.calcMinDistance(tc, za, bullet.getSpeed()*10);
            if (d!=Object3D.COLLISION_NONE) {
               za.scalarMul(d*0.6f);
               tc.add(za);
               int[] ids=e.getPolygonIDs();
               if (ids!=null&&ids.length>0) {
                  int id=ids[0];
                  SimpleVector n=e.getObject().getPolygonManager().getTransformedNormal(id);
                  bulMan.createDecal(tc, n);
               }
            }
            /**
             * And on again...
             */
            ground.enableCollisionListeners();
         }
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -