Apply collision to a character

Hi guys i’m kinda new at this and i need help.

In the hello collision, they apply collision to a “CharacterControl player” which is an invisible character with a fly by camera…but me i’m doing a game with a real model “node ninja” (e.g. Oto) followed by a chase Camera but i don’t know how to define the collision shape to fit the model and how to apply collision detection to it…thank you for any help :smiley:

here’s the part that matter of my code:

Node ninja;

private BulletAppState bulletAppState;





public void simpleInitApp() {



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

ninja.setLocalScale(0.5f);

ninja.setLocalTranslation(0.0f, 2.7f, 0.0f);

chaseCam = new ChaseCamera(cam, ninja, inputManager);

chaseCam.setEnabled(true);



//from here i dont know what i’m doing anymore!!

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(ninja.getLocalScale().x,ninja.getLocalScale().y,1);

player = new CharacterControl(capsuleShape, 0.05f);



player.setPhysicsLocation(new Vector3f(ninja.getLocalTranslation().x,ninja.getLocalTranslation().y,ninja.getLocalTranslation().z));



rootNode.attachChild(sceneModel);

bulletAppState.getPhysicsSpace().add(landscape);

bulletAppState.getPhysicsSpace().add(player);

}

First you need to add a control to the ninja

ninja.addControl(player)

and something i always use, is, which is entirely optional:

bulletAppState.getPhysicsSpace().enableDebug(assetManager);

This will put a blue outline of all the collision shapes of any objects that have collision shapes applied.



For collision detection, you need to implement PhysicsCollisionListener. https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners

thank you very much for your help.I added the two commands that you gave, and it’s working but the problem now is that when i added the code,the character is not moving anymore.

Here’s the code:

public void simpleInitApp() {

/** Set up Physics /

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

//Model Management

initKeys();

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



ninja.setLocalScale(0.5f);

ninja.setLocalTranslation(0.0f, 2.7f, 0.0f);

Quaternion roll180 = new Quaternion();

roll180.fromAngleAxis(FastMath.PI
3/2, new Vector3f(0,1,0) );

ninja.setLocalRotation(roll180);



chaseCam = new ChaseCamera(cam, ninja, inputManager);

chaseCam.setEnabled(true);

chaseCam.setDefaultHorizontalRotation(0);



control = ninja.getControl(AnimControl.class);

control.addListener(this);

channel_walk = control.createChannel();

channel_walk.setAnim(“stand”);

channel_push=control.createChannel();







//Camera Management

flyCam.setEnabled(false);

chaseCam.setDefaultVerticalRotation(0.3f);

chaseCam.setDefaultDistance(10);

chaseCam.setTrailingEnabled(true);

chaseCam.setSmoothMotion(true);

//chaseCam.setChasingSensitivity(30f);

chaseCam.setTrailingSensitivity(5f);

chaseCam.setTrailingRotationInertia(0.1f);



//Light Management

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.0f, -2.5f, 0.0f));

rootNode.addLight(sun);



//Get scene from zip file + collision for scene

assetManager.registerLocator(“town.zip”, ZipLocator.class.getName());

sceneModel = assetManager.loadModel(“main.scene”);

sceneModel.setLocalScale(2f);

CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);

landscape = new RigidBodyControl(sceneShape, 0);

sceneModel.addControl(landscape);



//collision for the character When i’m adding this code, the caracter dont move anymore,but it keeps its animation

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

player = new CharacterControl(capsuleShape, 0.05f);

ninja.addControl(player);





rootNode.attachChild(sceneModel);

rootNode.attachChild(ninja);

bulletAppState.getPhysicsSpace().add(landscape);

bulletAppState.getPhysicsSpace().add(player);



bulletAppState.getPhysicsSpace().enableDebug(assetManager);





}

is the collision shape visible on the character and if it is, is it sunk in the ground at all?

yes the collision shape is visible, however i can’t seem to be able to trully see if it is sunk in the ground but it is real close to it.

try place your model higher up on the Y axis, so that you can see it falling. Otherwise im not sure what else the problem is.

already done that, it falls and stop moving, anw thanks a lot for your help.really appreciate it

ok i know maily whats the problem… when i made the collision shape smaller, the model sank to the ground until the collision shape hits the ground,so i think its a mass problem in the :



landscape = new RigidBodyControl(sceneShape, 0.05f);

player = new CharacterControl(capsuleShape,0.04f);



when i put the mass of the player greater or equal to the ground’s mass it sinks

when i put the mass of the player lower than the ground’s mass it flies.



Any idea ?? thank you once again

The value you give in the CharacterControl is no mass value…

ok i solved the sinking problem by defining the gravity to 0;

player.setGravity(0);



still no movement can be made…However when i tried to move it with

player.setWalkDirection(new Vector3f(1,0,0));

it worked just fine and it didnt collide with the walls…

then why using the initKeys() the model isn’t moving?

Still one problem to go

as you’ve seen before i’ve created a spacial ninja

and a CharacterControl player

everytime i add :

ninja.addControl(player);

the ninja.setLocalRotation(quaternion) doesn’t work anymore…

plz HELP!!

plz do TUTORIALZ!!!