I really hate to do this, but i feel like the best way for everyone to fully understand my issue is to see the whole program.
[java]package NanoBasicSP;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
public class NanoBase extends SimpleApplication implements ActionListener
{
private BulletAppState bulletAppState;
private RigidBodyControl cube_phy;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private RigidBodyControl floor_phy;
private static final Box floor;
private static final float cubeLength = 1.0f;
private static final float cubeHeight = 1.0f;
private static final float cubeWidth = 1.0f;
public int x = 0;
public int y = 0;
public int z = 0;
public static void main(String[] args)
{
NanoBase app = new NanoBase();
app.start();
}
static
{
floor = new Box(Vector3f.ZERO, 30, 0.1f, 30);
}
//@Override
public void simpleInitApp()
{
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
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(0,10,0));
bulletAppState.getPhysicsSpace().add(player);
initKeys();
setUpLight();
initFloor();
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = 0; k < 10; k++)
{
makeCube();
x = x + 2;
}
x = 0;
y = y + 2;
}
y = 0;
z = z + 2;
}
}
public void makeCube()
{
Box cubeTerrain = new Box(new Vector3f(x,y,z), cubeLength, cubeHeight, cubeWidth);
Geometry geom = new Geometry("Box", cubeTerrain);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.randomColor());
geom.setMaterial(mat);
rootNode.attachChild(geom);
cube_phy = new RigidBodyControl(0);
geom.addControl(cube_phy);
bulletAppState.getPhysicsSpace().add(cube_phy);
}
private void setUpLight()
{
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);
}
public void initFloor()
{
Geometry floor_geo = new Geometry("Floor", floor);
Material floor_mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
floor_mat.setColor("Color", ColorRGBA.Green);
floor_geo.setMaterial(floor_mat);
floor_geo.setLocalTranslation(5, -1.1f, 5);
this.rootNode.attachChild(floor_geo);
floor_phy = new RigidBodyControl(0.0f);
floor_geo.addControl(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy);
}
private void initKeys()
{
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
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.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
}
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"))
{
player.jump();
}
}
@Override
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());
}
}
[/java]
The makeCube() method works fine as far as making them goes, but it doesnt apply the physics. I want it to work the same way my initFloor() code works (it does work) so that i can walk across my cubes. Im wondering it the for look that makes them all is throwing off the physics? Im really not sure.
If you do
[java]bulletAppState.getPhysicsSpace().enableDebug(assetManager);[/java]
you will see why. It is because you are offsetting the box mesh (not recommended):
[java]
Box cubeTerrain = new Box(new Vector3f(x,y,z), cubeLength, cubeHeight, cubeWidth);[/java]
instead do
[java]
Box cubeTerrain = new Box(Vector3f.ZERO, cubeLength, cubeHeight, cubeWidth);
cubeTerrain.setLocalTranslation(new Vector3f(x,y,z));
[/java]
Im getting a “cannot find symbol” error when i try to do what you suggested.
I then went and just typed in cubeTerrain. to bring up the little menu of options i can call on it and .setLocalTranslion() is not among them.
sorry i meant
[java] Geometry geom = new Geometry("Box", cubeTerrain);
geom.setLocalTranslation(new Vector3f(x,y,z));[/java]