Hey Guys,
I need to find a way to create a shell, that will in essence keep the player in. I tried Creating a RigidBodyObject, then tried placing my camera Collision object inside it but it didn’t work, it just took me outside the mesh, Am I doing something wrong? Or is there another way to do this? My code shows my situation, it is attached below. (This is my Testing Main, please excuse me if it looks very weird)
[java]
import actor.Player;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.asset.BlenderKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsCharacter;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.blender.BlenderModelLoader;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;
import com.jme3.util.SkyFactory;
import com.jme3.util.TangentBinormalGenerator;
/** Sample 7 - how to load an OgreXML model and play an animation,
- using channels, a controller, and an AnimEventListener. /
public class HelloAnimation extends SimpleApplication implements ActionListener
{
Node Well = new Node("Well");
BulletAppState bullet;
private PhysicsCharacter player;
private Vector3f walkDirection = new Vector3f();
private boolean left=false,right=false,up=false,down=false;
public static void main(String[] args) {
HelloAnimation app = new HelloAnimation();
app.start();
}
@Override
public void simpleInitApp()
{
bullet = new BulletAppState();
stateManager.attach(bullet);
flyCam.setMoveSpeed(150f);
Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f);
Geometry cube = new Geometry("My Textured Box", boxshape1);
Material mat_stl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
mat_stl.setTexture("ColorMap", tex_ml);
cube.setMaterial(mat_stl);
rootNode.attachChild(cube);
/* A bumpy rock with a shiny light effect */
Sphere rock = new Sphere(32,32, 2f);
Geometry shiny_rock = new Geometry("Shiny rock", rock);
rock.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
TangentBinormalGenerator.generate(rock); // for lighting effect
Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat_lit.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
mat_lit.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
mat_lit.setBoolean("UseMaterialColors",true);
mat_lit.setColor("Specular",ColorRGBA.White);
mat_lit.setColor("Diffuse",ColorRGBA.White);
mat_lit.setFloat("Shininess", 5f); // [1,128]
shiny_rock.setMaterial(mat_lit);
shiny_rock.setLocalTranslation(0,2,-2); // Move it a bit
shiny_rock.rotate(1.6f, 0, 0); // Rotate it a bit
rootNode.attachChild(shiny_rock);
generateBackGround();
calcSunLocation((byte)0, (byte)1, (byte)0);
addPhyics();
rootNode.attachChild(Well);
setupKeys();
SphereCollisionShape camColl = new SphereCollisionShape(1f);
player = new PhysicsCharacter(camColl, .01f);
player.setPhysicsLocation(new Vector3f(1,1,1));
player.setGravity(0);
bullet.getPhysicsSpace().add(player);
bullet.getPhysicsSpace().enableDebug(assetManager);
}
private void setupKeys() {
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this,"Lefts");
inputManager.addListener(this,"Rights");
inputManager.addListener(this,"Ups");
inputManager.addListener(this,"Downs");
inputManager.addListener(this,"Space");
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Lefts")) {
if(value)
left=true;
else
left=false;
} else if (binding.equals("Rights")) {
if(value)
right=true;
else
right=false;
} else if (binding.equals("Ups")) {
if(value)
up=true;
else
up=false;
} else if (binding.equals("Downs")) {
if(value)
down=true;
else
down=false;
} else if (binding.equals("Space")) {
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());
}
public void generateBackGround()
{
Spatial sky;
Texture up = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_top3.jpg");
Texture down = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_bottom4.jpg");
Texture north = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_front5.jpg");
Texture south = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_back6.jpg");
Texture east = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_left2.jpg");
Texture west = assetManager.loadTexture("Textures/Spacescape/PurpleNebulaComplex/purpleNebulaComplex_right1.jpg");
sky = SkyFactory.createSky(assetManager, east, west, north, south, up, down);
sky.setLocalTranslation(0, 0, 0);
Well.attachChild(sky);
}
public void calcSunLocation(byte x, byte y, byte z)
{
DirectionalLight Sun = new DirectionalLight();
Sun.setDirection(new Vector3f(x,y,z));
Sun.setColor(ColorRGBA.Pink);
rootNode.addLight(Sun);
}
public void addPhyics()
{
SphereCollisionShape outerGravityWellWall = new SphereCollisionShape(150f);
Well.addControl(new RigidBodyControl(outerGravityWellWall, 0.0f));
bullet.getPhysicsSpace().add(Well.getControl(RigidBodyControl.class));
}
}
[/java]