[Solved] CannonBall Problems

Hello, i’m trying to make a simple fps game, but i’m having difficulty with the shoot method. My player don’t shoot the balls in the right direction. Someone can help me?



import com.jme3.app.SimpleApplication;

import com.jme3.asset.TextureKey;

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.font.BitmapText;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

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.Sphere;

import com.jme3.scene.shape.Sphere.TextureMode;

import com.jme3.terrain.heightmap.AbstractHeightMap;

import com.jme3.terrain.geomipmap.TerrainQuad;

import com.jme3.terrain.heightmap.ImageBasedHeightMap;

import com.jme3.texture.Texture;

import com.jme3.texture.Texture.WrapMode;

import com.jme3.util.SkyFactory;



public class Game extends SimpleApplication implements ActionListener {

private BulletAppState bulletAppState;

private RigidBodyControl landscape;

private CharacterControl player;

private Vector3f walkDirection = new Vector3f();

private boolean left = false, right = false, up = false, down = false;

private TerrainQuad terrain;

private Material mat_terrain;

private Spatial ninja;

private RigidBodyControl ball_phy;

private static final Sphere sphere;

private Node ninjas;

Material stone_mat;



static {

sphere = new Sphere(32, 32, 0.4f, true, false);

sphere.setTextureMode(TextureMode.Projected);

}



public void simpleInitApp() {

buildMap();

setUpKeys();

initMaterials();

loadPlayer();

loadEnemies();

loadAim();

}



private void buildMap() {

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

flyCam.setMoveSpeed(100);

ninjas = new Node(“ninjas”);

rootNode.attachChild(ninjas);



mat_terrain = new Material(assetManager,“Common/MatDefs/Terrain/Terrain.j3md”);



mat_terrain.setTexture(“Alpha”, assetManager.loadTexture(“Textures/Terrain/splat/alphamap.png”));



Texture grass = assetManager.loadTexture(“Textures/Terrain/splat/dirt.jpg”);//grass

grass.setWrap(WrapMode.Repeat);

mat_terrain.setTexture(“Tex1”, grass);

mat_terrain.setFloat(“Tex1Scale”, 64f);



Texture dirt = assetManager.loadTexture(“Textures/Terrain/splat/dirt.jpg”);

dirt.setWrap(WrapMode.Repeat);

mat_terrain.setTexture(“Tex2”, dirt);

mat_terrain.setFloat(“Tex2Scale”, 32f);



Texture rock = assetManager.loadTexture(“Textures/Terrain/splat/road.jpg”);

rock.setWrap(WrapMode.Repeat);

mat_terrain.setTexture(“Tex3”, rock);

mat_terrain.setFloat(“Tex3Scale”, 128f);



AbstractHeightMap heightmap = null;

Texture heightMapImage = assetManager.loadTexture(“Textures/Terrain/splat/mountains512.png”);

heightmap = new ImageBasedHeightMap(heightMapImage.getImage());

heightmap.load();



terrain = new TerrainQuad(“my terrain”, 65, 513, heightmap.getHeightMap());



terrain.setMaterial(mat_terrain);

terrain.setLocalTranslation(0, -100, 0);

terrain.setLocalScale(2f, 1f, 2f);

rootNode.attachChild(terrain);



CollisionShape terrainShape = CollisionShapeFactory.createMeshShape((Node) terrain);

landscape = new RigidBodyControl(terrainShape, 0);

terrain.addControl(landscape);

rootNode.attachChild(SkyFactory.createSky(assetManager, “Textures/Sky/Bright/BrightSky.dds”, false));

}



private void loadPlayer() {

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

player = new CharacterControl(capsuleShape, 0.05f);

player.setJumpSpeed(20);

player.setFallSpeed(30);

player.setGravity(30);

player.setPhysicsLocation(new Vector3f(-168.4592f, -30.497353f, -16.97171f));

bulletAppState.getPhysicsSpace().add(terrain);

bulletAppState.getPhysicsSpace().add(player);

}



private void loadEnemies() {

ninja = assetManager.loadModel(“Models/Ninja/Ninja.mesh.xml”);

ninja.setName(“ninja”);

ninja.scale(0.05f,0.05f,0.05f);

ninja.setLocalTranslation(new Vector3f(-221.46085f, -31.6f, -51.832806f));

DirectionalLight s = new DirectionalLight();

s.setDirection(new Vector3f(0.0f, 1.0f, 1f));

ninjas.addLight(s);

ninjas.attachChild(ninja);

CollisionShape ninjaShape = new CapsuleCollisionShape(1.5f, ninja.getWorldScale().y, 1);

RigidBodyControl corpoDoNinja = new RigidBodyControl(ninjaShape, ninja.getWorldScale().y);

ninja.addControl(corpoDoNinja);

bulletAppState.getPhysicsSpace().add(ninja);

}



private void loadAim() {

guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);

BitmapText ch = new BitmapText(guiFont, false);

ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);

ch.setText("+");

ch.setLocalTranslation(settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,

settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);

guiNode.attachChild(ch);

}



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.addListener(this, “Left”);

inputManager.addListener(this, “Right”);

inputManager.addListener(this, “Up”);

inputManager.addListener(this, “Down”);

inputManager.addListener(this, “Jump”);

inputManager.addListener(this, “Walk”);

inputManager.addListener(this, “Shoot”);

}



private void initMaterials() {

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);

}



public void onAction(String binding, boolean value, float tpf) {

if (binding.equals(“Left”)) {

if (value) { left = true; } else { left = false; }

} else if (binding.equals(“Right”)) {

if (value) { right = true; } else { right = false; }

} else if (binding.equals(“Up”)) {

if (value) { up = true; } else { up = false; }

} else if (binding.equals(“Down”)) {

if (value) { down = true; } else { down = false; }

} else if (binding.equals(“Jump”)) {

player.jump();

} else if (binding.equals(“Shoot”)) {

makeCannonBall();

}

}



public void simpleUpdate(float tpf) {

Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);

Vector3f 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());

}



public void makeCannonBall() {

Geometry ball_geo = new Geometry(“cannon ball”, sphere);

ball_geo.setMaterial(stone_mat);

rootNode.attachChild(ball_geo);

ball_geo.setLocalTranslation(cam.getLocation());

ball_phy = new RigidBodyControl(1f);

ball_geo.addControl(ball_phy);

bulletAppState.getPhysicsSpace().add(ball_phy);

ball_phy.setLinearVelocity(cam.getDirection().mult(25));

}

}

Are you sure the cannon balls aren’t being made, or their speed is just too high? Try decreasing it a bit.



Also, I think you are missing a collision shape in there, the RigidBodyControl has none. Try putting it in the RigidyBodyControl’s constructor.

The cannon balls, are colliding with the enemy ninja, and their is speed is not as high, i’ve tried with less speed. But the problem is when i click to shoot, the shoot appear in the other direction, not in the direction of the camera

Your player is colliding with the cannon balls, that’s what’s happening. Take a look at the CollisionGroups, and avoid the collision between the cannon balls and the player. Let the player collide only with the terrain, let’s say, it should be group 2, and add collision with group 1. The cannon balls should be group 3, and add collision to group 1. Everything thing else could be group 1.

Thanks, bro. You solved my problem :smiley: