Hello I’ve a problem, I tryed to do a simple test in wich my player (first person) can move and shoot sphere, I’ve used for help, TestBrickWall , but I I’m not able to shoot sphere from the center of screen, this is my code .
[java]
public class Shoot extends SimpleApplication implements ActionListener{
//mat
Material mat2;
//player
private PhysicsCharacter player;
//physics
private BulletAppState bulletAppState;
//cam
//Temporary vectors used on each frame.
//They here to avoid instanciating new vectors on each frame
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
//terrain
private TerrainQuad terrain;
private Material mat_terrain;
private RigidBodyControl landscape;
//bullet
private static Box brick;
private static SphereCollisionShape bulletCollisionShape;
private static Sphere bullet;
//direction walk
private boolean left = false, right = false, up = false, down = false;
private Vector3f walkDirection = new Vector3f();
public static void main(String[] args) {
Shoot app = new Shoot();
app.start();
}
private void initTerrain()
{
/** 1. Create terrain material and load four textures into it. */
mat_terrain = new Material(assetManager,"Common/MatDefs/Terrain/Terrain.j3md");
/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("Alpha", assetManager.loadTexture(
"Textures/Terrain/splat/alphamap.png"));
/** 1.2) Add GRASS texture into the red layer (Tex1). */
Texture grass = assetManager.loadTexture(
"Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex1", grass);
mat_terrain.setFloat("Tex1Scale", 64f);
/** 1.3) Add DIRT texture into the green layer (Tex2) */
Texture dirt = assetManager.loadTexture(
"Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex2", dirt);
mat_terrain.setFloat("Tex2Scale", 32f);
/** 1.4) Add ROAD texture into the blue layer (Tex3) */
Texture rock = assetManager.loadTexture(
"Textures/Terrain/splat/road.jpg");
rock.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex3", rock);
mat_terrain.setFloat("Tex3Scale", 128f);
/** 2. Create the height map */
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture(
"Textures/Terrain/splat/road_normal.png");
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
heightmap.load();
int patchSize = 65;
terrain = new TerrainQuad("my terrain", patchSize, 513, heightmap.getHeightMap());
/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(2f, 1f, 2f);
rootNode.attachChild(terrain);
/** 5. The LOD (level of detail) depends on were the camera is: */
TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
terrain.addControl(control);
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node) terrain);
landscape = new RigidBodyControl(sceneShape, 0);
terrain.addControl(landscape);
bulletAppState.getPhysicsSpace().add(terrain);
}
public void initMaterial(){
mat2 = 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);
mat2.setTexture("ColorMap", tex2);
}
public void initBULLETS(){
bullet = new Sphere(32, 32, 0.4f, true, false);
bullet.setTextureMode(TextureMode.Projected);
bulletCollisionShape = new SphereCollisionShape(0.4f);
}
private void initPlayer(){
setUpKeys();
this.cam.setFrustumFar(2000);
player = new PhysicsCharacter(new SphereCollisionShape(5),.01f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(60, 0, -60));
bulletAppState.getPhysicsSpace().add(player);
}
@Override
public void simpleInitApp(){
flyCam.setEnabled(true);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);
initPhysics();
initBULLETS();
initMaterial();
this.cam.setLocation(new Vector3f(0, 6f, 6f));
cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));
cam.setFrustumFar(15);
setUpKeys();
initTerrain();
initPlayer();
initCrossHairs();
}
public void initPhysics(){
bulletAppState = new BulletAppState();
//physics
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);
}
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.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("gc", new KeyTrigger(KeyInput.KEY_X));
InputListener actionListener = null;
inputManager.addListener(actionListener, "gc");
inputManager.addListener(this, "SHOOT");
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
}
public PhysicsSpace getPhysicsSpace(){
return bulletAppState.getPhysicsSpace();
}
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// unused
}
@Override
public void simpleUpdate(float tpf) {
camDir = cam.getDirection().clone().multLocal(0.6f);
camLeft = cam.getLeft().clone().multLocal(0.4f);
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);
cam.setLocation(player.getPhysicsLocation());
}
protected void initCrossHairs() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+"); // crosshairs
ch.setLocalTranslation( // center
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
@Override
public void simpleRender(RenderManager rm){
//TODO: add render code
}
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals("SHOOT") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(mat2);
bulletg.setShadowMode(ShadowMode.CastAndReceive);
Vector3f f= cam.getLocation();
bulletg.setLocalTranslation(f); //<<<<----------problem
SphereCollisionShape bulletCollisionShape = new SphereCollisionShape(0.4f);
RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
// RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
bulletNode.setLinearVelocity(cam.getDirection().mult(60));
System.out.println("location"+ cam.getLocation());
System.out.println("rotation"+ cam.getRotation());
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
}
if (name.equals("gc") && !isPressed) {
System.gc();
}
if (name.equals("Left")) {
left = isPressed;
} else if (name.equals("Right")) {
right= isPressed;
} else if (name.equals("Up")) {
up = isPressed;
} else if (name.equals("Down")) {
down = isPressed;
} else if (name.equals("Jump")) {
if (isPressed)
{ player.jump(); }
}
}
}
[/java]
I think that the problem is here, in onAction:
[java]
Vector3f f= cam.getLocation();
bulletg.setLocalTranslation(f);
[/java]
I try to change cam.getLocation()–>>> x,y,z, variable but, when I move the mouse the sphere doesn’t cum from center.
Anyone with a little of expirience than me, can help me :)?
thanks in advance