1/* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 6package jme3test.bullet; 7 8/* 9 * Copyright (c) 2009-2010 jMonkeyEngine 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions are 14 * met: 15 * 16 * * Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * * Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40import com.jme3.app.SimpleApplication; 41import com.jme3.asset.TextureKey; 42import com.jme3.bullet.BulletAppState; 43import com.jme3.bullet.PhysicsSpace; 44import com.jme3.bullet.collision.shapes.SphereCollisionShape; 45import com.jme3.bullet.control.RigidBodyControl; 46import com.jme3.font.BitmapText; 47import com.jme3.input.MouseInput; 48import com.jme3.input.controls.ActionListener; 49import com.jme3.input.controls.MouseButtonTrigger; 50import com.jme3.material.Material; 51import com.jme3.math.Vector2f; 52import com.jme3.math.Vector3f; 53import com.jme3.renderer.queue.RenderQueue.ShadowMode; 54import com.jme3.scene.Geometry; 55import com.jme3.scene.shape.Box; 56import com.jme3.scene.shape.Sphere; 57import com.jme3.scene.shape.Sphere.TextureMode; 58import com.jme3.shadow.PssmShadowRenderer; 59import com.jme3.shadow.PssmShadowRenderer.CompareMode; 60import com.jme3.shadow.PssmShadowRenderer.FilterMode; 61import com.jme3.texture.Texture; 62import com.jme3.texture.Texture.WrapMode; 63 64/** 65 * 66 * @author double1984 (tower mod by atom) 67 */ 68public class TestBrickTower extends SimpleApplication { 69 70 int bricksPerLayer = 8; 71 int brickLayers = 30; 72 73 static float brickWidth = .75f, brickHeight = .25f, brickDepth = .25f; 74 float radius = 3f; 75 float angle = 0; 76 77 78 Material mat; 79 Material mat2; 80 Material mat3; 81 PssmShadowRenderer bsr; 82 private Sphere bullet; 83 private Box brick; 84 private SphereCollisionShape bulletCollisionShape; 85 86 private BulletAppState bulletAppState; 87 88 public static void main(String args[]) { 89 TestBrickTower f = new TestBrickTower(); 90 f.start(); 91 } 92 93 @Override 94 public void simpleInitApp() { 95 bulletAppState = new BulletAppState(); 96 bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL); 97 // bulletAppState.setEnabled(false); 98 stateManager.attach(bulletAppState); 99 bullet = new Sphere(32, 32, 0.4f, true, false); 100 bullet.setTextureMode(TextureMode.Projected); 101 bulletCollisionShape = new SphereCollisionShape(0.4f); 102 103 brick = new Box(Vector3f.ZERO, brickWidth, brickHeight, brickDepth); 104 brick.scaleTextureCoordinates(new Vector2f(1f, .5f)); 105 //bulletAppState.getPhysicsSpace().enableDebug(assetManager); 106 initMaterial(); 107 initTower(); 108 initFloor(); 109 initCrossHairs(); 110 this.cam.setLocation(new Vector3f(0, 25f, 8f)); 111 cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0)); 112 cam.setFrustumFar(80); 113 inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); 114 inputManager.addListener(actionListener, "shoot"); 115 rootNode.setShadowMode(ShadowMode.Off); 116 bsr = new PssmShadowRenderer(assetManager, 1024, 2); 117 bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal()); 118 bsr.setLambda(0.55f); 119 bsr.setShadowIntensity(0.6f); 120 bsr.setCompareMode(CompareMode.Hardware); 121 bsr.setFilterMode(FilterMode.PCF4); 122 viewPort.addProcessor(bsr); 123 } 124 125 private PhysicsSpace getPhysicsSpace() { 126 return bulletAppState.getPhysicsSpace(); 127 } 128 private ActionListener actionListener = new ActionListener() { 129 130 public void onAction(String name, boolean keyPressed, float tpf) { 131 if (name.equals("shoot") && !keyPressed) { 132 Geometry bulletg = new Geometry("bullet", bullet); 133 bulletg.setMaterial(mat2); 134 bulletg.setShadowMode(ShadowMode.CastAndReceive); 135 bulletg.setLocalTranslation(cam.getLocation()); 136 RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1); 137// RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1); 138 bulletNode.setLinearVelocity(cam.getDirection().mult(25)); 139 bulletg.addControl(bulletNode); 140 rootNode.attachChild(bulletg); 141 getPhysicsSpace().add(bulletNode); 142 } 143 } 144 }; 145 146 public void initTower() { 147 double tempX = 0; 148 double tempY = 0; 149 double tempZ = 0; 150 angle = 0f; 151 for (int i = 0; i < brickLayers; i++){ 152 // Increment rows 153 if(i!=0) 154 tempY+=brickHeight*2; 155 else 156 tempY=brickHeight; 157 // Alternate brick seams 158 angle = 360.0f / bricksPerLayer * i/2f; 159 for (int j = 0; j < bricksPerLayer; j++){ 160 tempZ = Math.cos(Math.toRadians(angle))*radius; 161 tempX = Math.sin(Math.toRadians(angle))*radius; 162 System.out.println("x="+((float)(tempX))+" y="+((float)(tempY))+" z="+(float)(tempZ)); 163 Vector3f vt = new Vector3f((float)(tempX), (float)(tempY), (float)(tempZ)); 164 // Add crenelation 165 if (i==brickLayers-1){ 166 if (j%2 == 0){ 167 addBrick(vt); 168 } 169 } 170 // Create main tower 171 else { 172 addBrick(vt); 173 } 174 angle += 360.0/bricksPerLayer; 175 } 176 } 177 178 } 179 180 public void initFloor() { 181 Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f); 182 floorBox.scaleTextureCoordinates(new Vector2f(3, 6)); 183 184 Geometry floor = new Geometry("floor", floorBox); 185 floor.setMaterial(mat3); 186 floor.setShadowMode(ShadowMode.Receive); 187 floor.setLocalTranslation(0, 0, 0); 188 floor.addControl(new RigidBodyControl(0)); 189 this.rootNode.attachChild(floor); 190 this.getPhysicsSpace().add(floor); 191 } 192 193 public void initMaterial() { 194 mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 195 TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg"); 196 key.setGenerateMips(true); 197 Texture tex = assetManager.loadTexture(key); 198 mat.setTexture("ColorMap", tex); 199 200 mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 201 TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG"); 202 key2.setGenerateMips(true); 203 Texture tex2 = assetManager.loadTexture(key2); 204 mat2.setTexture("ColorMap", tex2); 205 206 mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 207 TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg"); 208 key3.setGenerateMips(true); 209 Texture tex3 = assetManager.loadTexture(key3); 210 tex3.setWrap(WrapMode.Repeat); 211 mat3.setTexture("ColorMap", tex3); 212 } 213 214 public void addBrick(Vector3f ori) { 215 Geometry reBoxg = new Geometry("brick", brick); 216 reBoxg.setMaterial(mat); 217 reBoxg.setLocalTranslation(ori); 218 reBoxg.rotate(0f, (float)Math.toRadians(angle) , 0f ); 219 reBoxg.addControl(new RigidBodyControl(1.5f)); 220 reBoxg.setShadowMode(ShadowMode.CastAndReceive); 221 reBoxg.getControl(RigidBodyControl.class).setFriction(1.6f); 222 this.rootNode.attachChild(reBoxg); 223 this.getPhysicsSpace().add(reBoxg); 224 } 225 226 protected void initCrossHairs() { 227 guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); 228 BitmapText ch = new BitmapText(guiFont, false); 229 ch.setSize(guiFont.getCharSet().getRenderedSize() * 2); 230 ch.setText("+"); // crosshairs 231 ch.setLocalTranslation( // center 232 settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, 233 settings.getHeight() / 2 + ch.getLineHeight() / 2, 0); 234 guiNode.attachChild(ch); 235 } 236}