JME基础教程代码分析13物理碰撞
package com.hello; import com.jme3.app.SimpleApplication; import com.jme3.asset.TextureKey; import com.jme3.bullet.BulletAppState; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.font.BitmapText; import com.jme3.input.MouseInput; import com.jme3.input.controls.ActionListener; import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.material.Material; import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; import com.jme3.renderer.queue.RenderQueue.ShadowMode; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Sphere; import com.jme3.scene.shape.Sphere.TextureMode; import com.jme3.shadow.BasicShadowRenderer; import com.jme3.texture.Texture; import com.jme3.texture.Texture.WrapMode;
/** * Example 12 - how to give objects physical properties so they bounce and fall. * @author base code by double1984, updated by zathras */ public class HelloPhysics extends SimpleApplication {
public static void main(String args[]) { HelloPhysics app = new HelloPhysics(); app.start(); }
/** Prepare the Physics Application State (jBullet) */ private BulletAppState bulletAppState;
/** Activate custom rendering of shadows */ //激活自定义呈现的阴影,基本阴影渲染对象 BasicShadowRenderer bsr;
/** Prepare Materials */ Material wall_mat; Material stone_mat; Material floor_mat;
/** Prepare geometries and physical nodes for bricks and cannon balls. */ //三个刚体对象 private RigidBodyControl brick_phy; private static final Box box; private RigidBodyControl ball_phy; private static final Sphere sphere; private RigidBodyControl floor_phy; private static final Box floor;
/** dimensions used for bricks and wall */ private static final float brickLength = 0.48f; private static final float brickWidth = 0.24f; private static final float brickHeight = 0.12f;
static { //初始化三个几何体 /** Initialize the cannon ball geometry */ sphere = new Sphere(32, 32, 0.4f, true, false); sphere.setTextureMode(TextureMode.Projected); /** Initialize the brick geometry */ box = new Box(Vector3f.ZERO, brickLength, brickHeight, brickWidth); box.scaleTextureCoordinates(new Vector2f(1f, .5f)); /** Initialize the floor geometry */ floor = new Box(Vector3f.ZERO, 10f, 0.1f, 5f); floor.scaleTextureCoordinates(new Vector2f(3, 6)); }
@Override public void simpleInitApp() { /** Set up Physics Game */ //又是一个弹道物理应用状态 bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); /** Configure cam to look at scene */ //相机位置 cam.setLocation(new Vector3f(0, 6f, 6f)); cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0)); //锥角距离 cam.setFrustumFar(15); /** Add InputManager action: Left click triggers shooting. */ inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); inputManager.addListener(actionListener, "shoot"); /** Initialize the scene, materials, and physics space */ initMaterials(); initWall(); initFloor(); initCrossHairs(); initShadows(); }
/** * Every time the shoot action is triggered, a new cannon ball is produced. * The ball is set up to fly from the camera position in the camera direction. */ private ActionListener actionListener = new ActionListener() { public void onAction(String name, boolean keyPressed, float tpf) { if (name.equals("shoot") && !keyPressed) { makeCannonBall(); } } };
/** Initialize the materials used in this scene. */ public void initMaterials() { wall_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg"); key.setGenerateMips(true); Texture tex = assetManager.loadTexture(key); wall_mat.setTexture("ColorMap", tex);
stone_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG"); key2.setGenerateMips(true); Texture tex2 = assetManager.loadTexture(key2); stone_mat.setTexture("ColorMap", tex2);
floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.png"); key3.setGenerateMips(true); Texture tex3 = assetManager.loadTexture(key3); tex3.setWrap(WrapMode.Repeat); floor_mat.setTexture("ColorMap", tex3); }
/** Make a solid floor and add it to the scene. */ public void initFloor() { Geometry floor_geo = new Geometry("Floor", floor); floor_geo.setMaterial(floor_mat); floor_geo.setShadowMode(ShadowMode.Receive); floor_geo.setLocalTranslation(0, -0.1f, 0); this.rootNode.attachChild(floor_geo); /* Make the floor physical with mass 0.0f! */ floor_phy = new RigidBodyControl(0.0f); floor_geo.addControl(floor_phy); bulletAppState.getPhysicsSpace().add(floor_phy); }
/** This loop builds a wall out of individual bricks. */ public void initWall() { float startpt = brickLength / 4; float height = 0; for (int j = 0; j < 15; j++) { for (int i = 0; i < 4; i++) { Vector3f vt = new Vector3f(i * brickLength * 2 + startpt, brickHeight + height, 0); makeBrick(vt); } startpt = -startpt; height += 2 * brickHeight; } }
/** Activate shadow casting and light direction */ //初始阴影的方法 private void initShadows() { bsr = new BasicShadowRenderer(assetManager, 256); bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal()); //可视范围加载处理器 viewPort.addProcessor(bsr); // Default mode is Off -- Every node declares own shadow mode! rootNode.setShadowMode(ShadowMode.Off); }
/** This method creates one individual physical brick. */ public void makeBrick(Vector3f loc) { /** Create a brick geometry and attach to scene graph. */ Geometry brick_geo = new Geometry("brick", box); brick_geo.setMaterial(wall_mat); rootNode.attachChild(brick_geo); /** Position the brick geometry and activate shadows */ brick_geo.setLocalTranslation(loc); brick_geo.setShadowMode(ShadowMode.CastAndReceive); /** Make brick physical with a mass > 0.0f. */ brick_phy = new RigidBodyControl(2f); /** Add physical brick to physics space. */ brick_geo.addControl(brick_phy); bulletAppState.getPhysicsSpace().add(brick_phy); }
/** This method creates one individual physical cannon ball. * By defaul, the ball is accelerated and flies * from the camera position in the camera direction.*/ public void makeCannonBall() { /** Create a cannon ball geometry and attach to scene graph. */ Geometry ball_geo = new Geometry("cannon ball", sphere); ball_geo.setMaterial(stone_mat); rootNode.attachChild(ball_geo); /** Position the cannon ball and activate shadows */ //制作了一个可抛出的球体,位置放在相机位置 ball_geo.setLocalTranslation(cam.getLocation()); //阴影模式是建造和接受 ball_geo.setShadowMode(ShadowMode.CastAndReceive); /** Make the ball physcial with a mass > 0.0f */ //给球体加个质量1 ball_phy = new RigidBodyControl(1f); /** Add physical ball to physics space. */ ball_geo.addControl(ball_phy); //加到物理空间 bulletAppState.getPhysicsSpace().add(ball_phy); /** Accelerate the physcial ball to shoot it. */ //从相机方向产生的矢量线速度,矢量乘数25 ball_phy.setLinearVelocity(cam.getDirection().mult(25)); }
/** A plus sign used as crosshairs to help the player with aiming.*/ //在gui节点画个加号作为瞄准器 protected void initCrossHairs() { guiNode.detachAllChildren(); guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); BitmapText ch = new BitmapText(guiFont, false); ch.setSize(guiFont.getCharSet().getRenderedSize() * 2); ch.setText("+"); // fake crosshairs :) ch.setLocalTranslation( // center settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, settings.getHeight() / 2 + ch.getLineHeight() / 2, 0); guiNode.attachChild(ch); } } |
posted on 2011-08-12 11:33 苏桓(osbert) 阅读(760) 评论(0) 编辑 收藏 举报