PhysicsCharacterNode needed to move with a platform

ummm…I dont know. :frowning: How do I check it.



Here is the code.

[java]public class Main extends SimpleApplication implements AnimEventListener{



private AnimChannel animChannel;

private AnimControl animControl;



private BulletAppState bulletAppState;

private PhysicsNode pn_basicPlatform;

private PhysicsNode pn_movingPlatform;

private BitmapText reportGUIText;

private PhysicsCharacterNode pn_player;

private Vector3f walkDirection = new Vector3f();

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



Ray standingOnRay = new Ray();

CollisionResults standingOnRayResults = new CollisionResults();

private String standingOnObject = “”;



public static void main(String[] args) {

Main app = new Main();

app.setShowSettings(false);



AppSettings newAppSettings = new AppSettings(true);

newAppSettings.put(“Width”, 800);

newAppSettings.put(“Height”, 600);

newAppSettings.put(“Title”, “SST”);

newAppSettings.put(“VSync”, false);

newAppSettings.put(“Samples”, 4);

//newAppSettings.put(“Fullscreen”, true);



app.setSettings(newAppSettings);

app.start();

}



@Override

public void simpleInitApp() {

// TODO: DEBUG : Work around of detaching previous Key binding

flyCam.setEnabled(false);

// inputManager.deleteMapping(“KEY_RIGHT”);

// inputManager.addMapping(“KEY_RIGHT”, new MouseAxisTrigger(0, true));



initGUIText();

initState();

initViewPortLight();

initPlatform();

initPlayer();

initMovingPlatform();

}



public void initMovingPlatform()

{

Spatial geo_movingPlatform = assetManager.loadModel(“Scenes/moving_platfrom.j3o”);

//geo_movingPlatform.setLocalScale(0.6f);

geo_movingPlatform.setLocalTranslation(0,-1.7f,1);



CompoundCollisionShape cs_movingPlatform = CollisionShapeFactory.createMeshCompoundShape((Node)geo_movingPlatform);

pn_movingPlatform = new PhysicsNode(cs_movingPlatform, 0f);

pn_movingPlatform.attachChild(geo_movingPlatform);

pn_movingPlatform.attachDebugShape(assetManager);

//pn_movingPlatform.applyContinuousForce(true, new Vector3f(0,9.7f,0));

bulletAppState.getPhysicsSpace().add(pn_movingPlatform);

rootNode.attachChild(pn_movingPlatform);

}



public void initPlayer()

{

//TODO Checking the capsule size that encapsulates Player Model

Spatial model_player = assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);

model_player.setLocalScale(0.1f);

model_player.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.PI , new Vector3f(0,1,0)));

pn_player = new PhysicsCharacterNode(new CapsuleCollisionShape(.35f,.32f), 0.05f);

pn_player.attachChild(model_player);

pn_player.setGravity(17);

pn_player.setFallSpeed(17);

pn_player.setJumpSpeed(8);

pn_player.setLocalTranslation(0,35,8);



//DEBUG SHAPE

pn_player.attachDebugShape(assetManager);



animControl = model_player.getControl(AnimControl.class);

animControl.addListener(this);

animChannel = animControl.createChannel();

animChannel.setAnim(“stand”);



inputManager.addMapping(“CharLeft”, new KeyTrigger(KeyInput.KEY_LEFT));

inputManager.addMapping(“CharRight”, new KeyTrigger(KeyInput.KEY_RIGHT));

inputManager.addMapping(“CharForw”, new KeyTrigger(KeyInput.KEY_UP));

inputManager.addMapping(“CharBack”, new KeyTrigger(KeyInput.KEY_DOWN));

inputManager.addMapping(“CharJump”, new KeyTrigger(KeyInput.KEY_SPACE));

inputManager.addMapping(“CharShoot”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT) );



inputManager.addListener(actionListener, “CharLeft”);

inputManager.addListener(actionListener, “CharRight”);

inputManager.addListener(actionListener, “CharForw”);

inputManager.addListener(actionListener, “CharBack”);

inputManager.addListener(actionListener, “CharJump”);

inputManager.addListener(actionListener, “CharShoot”);



bulletAppState.getPhysicsSpace().add(pn_player);

rootNode.attachChild(pn_player);

}



public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {

if (!right & animName.equals(“Walk”)) //if walk animation comes to the end

// and still the Right button not pressed → User dont want to move any further.

// Then it starts blending the standing animation

{

channel.setAnim(“stand”, 0.5f);

// channel.setLoopMode(LoopMode.DontLoop);

// channel.setSpeed(1f);

}

}



public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {

// unused

}



private ActionListener actionListener = new ActionListener() {

public void onAction(String binding, boolean isPressed, float tpf)

{

if (binding.equals(“CharLeft”))

{

if (isPressed)

left = true;

else

left = false;

}



else if (binding.equals(“CharRight”))

{

if (isPressed)

{

right = true;

if (!animChannel.getAnimationName().equals(“Walk”)) {

animChannel.setAnim(“Walk”, 0.5f);

animChannel.setSpeed(0.75f);

//animChannel.setLoopMode(LoopMode.Loop);

}

}

else

right = false;

}



else if (binding.equals(“CharForw”))

{

if (isPressed)

up = true;

else

up = false;

}



else if (binding.equals(“CharBack”))

{

if (isPressed)

down = true ;

else

down = false;

}



else if(binding.equals(“CharJump”))

{

pn_player.jump();

}



else if(binding.equals(“CharShoot”))

{

//

}

}

};



public void initViewPortLight() {

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-3f, -5f, 5f));

sun.setColor(ColorRGBA.White);

rootNode.addLight(sun);



Spatial sky = SkyFactory.createSky(assetManager, “Textures/FullskiesBlueClear03.dds”, false);

sky.setLocalScale(1000);

rootNode.attachChild(sky);

}



public void initPlatform() {

Spatial basicPlatform = assetManager.loadModel(“Scenes/platform.j3o”);



CompoundCollisionShape cs_basicPlatform = CollisionShapeFactory.createMeshCompoundShape((Node)basicPlatform);

pn_basicPlatform = new PhysicsNode(cs_basicPlatform, 0f);

pn_basicPlatform.attachChild((Node)basicPlatform);

pn_basicPlatform.attachDebugShape(assetManager);



bulletAppState.getPhysicsSpace().add(pn_basicPlatform);

rootNode.attachChild(pn_basicPlatform);

}



private void initState() {

bulletAppState = new BulletAppState();

bulletAppState.setThreadingType(BulletAppState.ThreadingType.SEQUENTIAL);

stateManager.attach(bulletAppState);

}



protected void initGUIText() {

guiNode.detachAllChildren();

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



reportGUIText = new BitmapText(guiFont, false);

reportGUIText.setSize(guiFont.getCharSet().getRenderedSize());

reportGUIText.setLocalTranslation(100,75,0);

guiNode.attachChild(reportGUIText);

}



@Override

public void simpleUpdate(float tpf) {

//TODO These two are a quick-fix for PhysicsCharacterNode Jitter

rootNode.updateLogicalState(tpf);

rootNode.updateGeometricState();



updateStandingOnRay();



cam.setDirection(new Vector3f(-.7f,-.2f,-1f));

cam.setLocation(new Vector3f(pn_player.getLocalTranslation().getX()+3, pn_player.getLocalTranslation().getY()+1,pn_player.getLocalTranslation().getZ()+3f));



pn_player.setWalkDirection(Vector3f.ZERO);



//TODO : DEBUG

pn_movingPlatform.move(0, 0, 0.002f);



//if(animChannel.getAnimationName().equals(“stand”))pn_player.setWalkDirection(new Vector3f(0, 0, -0.005f));

if(animChannel.getAnimationName().equals(“Walk”)) pn_player.setWalkDirection(new Vector3f(0, 0, -0.02f));

}



@Override

public void simpleRender(RenderManager rm) {

}



private void updateStandingOnRay(){

standingOnRayResults = new CollisionResults();



//TODO : PlayerDEPENDENT

standingOnRay = new Ray(new Vector3f(pn_player.getLocalTranslation().getX(), pn_player.getLocalTranslation().getY()-.25f,pn_player.getLocalTranslation().getZ()), new Vector3f(0, -pn_player.getUpAxis(),0));

standingOnRay.setLimit(0.8f);



rootNode.collideWith(standingOnRay, standingOnRayResults);

if(standingOnRayResults.size() != 0){

standingOnObject = standingOnRayResults.getClosestCollision().getGeometry().getName();

reportGUIText.setText(standingOnObject);

if(standingOnObject.equals(“moving_platfrom-geom-1”) & pn_player.onGround()){

reportGUIText.setText(pn_player.getLocalTranslation() + " tt " + standingOnRayResults.getClosestCollision().getContactPoint());



pn_player.warp(pn_movingPlatform.getWorldTranslation().add(new Vector3f(0,1f,0)));

//pn_player.setLocalTranslation(pn_player.getLocalTranslation().addLocal(new Vector3f(0,0,1)));

}

}

}



}[/java]