I can not throw the ball in front of the camera,
I do not know much’m still learning now,
I researched a lot but do not know how to solve.
This is my code
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;
/**
* Example 9 - How to make walls and floors solid. This collision code uses
* Physics and a custom Action Listener.
*
* @author normen, with edits by Zathras
*/
public class Main extends SimpleApplication
implements ActionListener {
private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
/**
* 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;
ChaseCamera chasecamera = null;
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(Sphere.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));
}
public void simpleInitApp() {
/**
* Set up Physics
*/
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
// We re-use the flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
setUpKeys();
setUpLight();
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);
// tex2.setWrap(WrapMode.Repeat);
stone_mat.setTexture("ColorMap", tex2);
// We load the scene from the zip file and adjust its size.
assetManager.registerLocator("town.zip", ZipLocator.class);
sceneModel = assetManager.loadModel("main.scene");
sceneModel.setLocalScale(2f);
// We set up collision detection for the scene by creating a
// compound collision shape and a static RigidBodyControl with mass zero.
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
// We set up collision detection for the player by creating
// a capsule collision shape and a CharacterControl.
// The CharacterControl offers extra settings for
// size, stepheight, jumping, falling, and gravity.
// We also put the player in its starting position.
CapsuleCollisionShape capsule = new CapsuleCollisionShape(3f, 4f);
player = new CharacterControl(capsule, 0.01f);
Node model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
//model.setLocalScale(0.5f);
model.addControl(player);
player.setPhysicsLocation(new Vector3f(0, 10, 0));
rootNode.attachChild(model);
bulletAppState.getPhysicsSpace().add(player);
// flyCam.setEnabled(true);
// chasecamera = new ChaseCamera(cam, model, inputManager);
// chasecamera.setInvertVerticalAxis(true);
// chasecamera.setDragToRotate(false);
// chasecamera.setRotationSpeed(20f);
//
// We attach the scene and the player to the rootNode and the physics space,
// to make them appear in the game world.
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}
/**
* We over-write some navigational key mappings here, so we can add
* physics-controlled walking and jumping:
*/
private void setUpKeys() {
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
}
/**
* These are our custom actions triggered by key presses. We do not walk
* yet, we just keep track of the direction the user pressed.
*/
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
left = value;
} else if (binding.equals("Right")) {
right = value;
} else if (binding.equals("Up")) {
up = value;
} else if (binding.equals("Down")) {
down = value;
} else if (binding.equals("Jump")) {
makeCannonBall();
}
}
/**
* This is the main event loop--walking happens here. We check in which
* direction the player is walking by interpreting the camera direction
* forward (camDir) and to the side (camLeft). The setWalkDirection()
* command is what lets a physics-controlled player walk. We also make sure
* here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.5f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.5f);
camDir.y = 0;
camLeft.y = 0;
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection.multLocal(2));
cam.setLocation(player.getPhysicsLocation());
player.setViewDirection(camDir.mult(2));
}
public void makeCannonBall() {
/**
* Create a cannon ball geometry and attach to scene graph.
*/
Vector3f v = cam.getLocation() ;
Spatial ball_geo = new Geometry("cannon ball", sphere);
ball_geo.setMaterial(stone_mat);
rootNode.attachChild(ball_geo);
/**
* Position the cannon ball
*/
ball_geo.setLocalTranslation(v);
/**
* Make the ball physcial with a mass > 0.0f
*/
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.
*/
ball_phy.setLinearVelocity(cam.getDirection().mult(2f));
}
}