001 package tutorial;
002 import javax.media.opengl.GL;
003 import javax.media.opengl.GLDrawable;
004
005 import de.rico.engine.effect.light.ogllight.PositionalLight;
006 import de.rico.engine.enums.BoundingVolume;
007 import de.rico.engine.game.AbstractBaseGame;
008 import de.rico.engine.geometry.collision.BoundingBox;
009 import de.rico.engine.geometry.collision.CollisionFinder;
010 import de.rico.engine.geometry.modelloader.ms3d.ModelMS3DAscii;
011
012 /**
013 * A class that shows how to check for collisions between 3d models.
014 * @author Frank Bruns
015 *
016 */
017 public class CheckCollisions extends AbstractBaseGame
018 {
019 private ModelMS3DAscii model1 = null;
020 private ModelMS3DAscii model2 = null;
021
022 private PositionalLight light = null;
023
024 /**
025 * Constructor for the new game.
026 * @param title title for the window bar
027 * @param maxFps maximum possible framerate
028 */
029 public CheckCollisions(String title, int maxFps)
030 {
031 super(title, maxFps);
032 }
033
034 /* (non-Javadoc)
035 * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
036 */
037 @Override
038 public void initResources(GLDrawable gld, GL gl, int width, int height)
039 {
040 this.model1 = new ModelMS3DAscii(BoundingVolume.BOX);
041 this.model1.load("./models/ms3d/recke/recke.txt", 0.1f);
042 this.model1.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);
043 // set the model to be sensitive for collisions
044 this.model1.setCollidable(true);
045 // visualize the bounding box of the model for demonstration
046 // purpose
047 this.model1.setShowBoundingVolume(true);
048 // translate the model a bit
049 this.model1.translateX(2f);
050 this.model1.translateY(3f);
051
052
053 this.model2 = new ModelMS3DAscii(BoundingVolume.BOX);
054 this.model2.load("./models/ms3d/recke/recke.txt", 0.1f);
055 this.model2.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);
056 this.model2.setCollidable(true);
057 this.model2.setShowBoundingVolume(true);
058 this.model2.translateX(-1f);
059
060 // create a simple white light to illuminate the scene
061 this.light = new PositionalLight(0);
062 this.light.setPosition(0f, 50f, 100f);
063 }
064
065 /* (non-Javadoc)
066 * @see de.rico.engine.game.AbstractBaseGame#update(long)
067 */
068 @Override
069 public void update(long elapsedTime)
070 {
071 // update the models
072 this.model1.update(elapsedTime);
073 this.model2.update(elapsedTime);
074
075 // check the bounding volumes for collision by using a static method
076 // of the CollisionFinder class. Only models with the same kind of bounding volume
077 // can be checked.
078 // The CollisionFinder class has even more methods on finding
079 // points of intersection between lines and bounding volumes,
080 // wich can be used for picking, for example.
081 boolean collision = CollisionFinder.collide(
082 (BoundingBox)this.model1.getBoundingVolume(),
083 (BoundingBox)this.model2.getBoundingVolume());
084
085 System.out.println("Do the models collide?: "+collision);
086 }
087
088 /* (non-Javadoc)
089 * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
090 */
091 @Override
092 public void draw(GL gl)
093 {
094 // bind the light for illumination of the scene
095 this.light.bind(gl);
096
097 // draw the models
098 this.model1.draw(gl);
099 this.model2.draw(gl);
100
101 // release the light again
102 this.light.release(gl);
103 }
104
105 /**
106 * The usual main method. It's the entry point to the application.
107 * @param args argument string
108 */
109 public static void main(String args[])
110 {
111 // create a new game
112 CheckCollisions game = new CheckCollisions("Test Game", 100);
113
114 // initialise the camera --> needs to be done!
115 game.initCamera(0f, 7.5f, 20f, 0f, 0f, 0.1f, 200f);
116
117 // show framerate --> this is optional
118 game.setDisplayFPS(true);
119
120 // finally start the game
121 game.start(game);
122 }
123 }
|