Detecting Collision Between Spatials

so i’m trying to be able to detect when a collision happens between my player controlled character and a spatial enemy. i have both spatials loaded and the physics set up, but i’m unsure how to tell the program when there is a collision between the two of them. i’ve been looking at the Hello Picking tutorial and trying to understand the basis off of that, but the spatial used in that tutorial didn’t have a name associated with it to output the collision



what i want to do with this is whenever the enemy is hit, it subtracts damage (in this case 1 for testing) from the EnemyHP variable and then checks after collision if EnemyHP <= 0 then removes the spatial from the rootNode



this is my simpleInitApp() function that sets up the nodes and bulletappstate

[java]@Override

public void simpleInitApp() {

bulletAppState = new BulletAppState();

bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);

stateManager.attach(bulletAppState);

shootables = new Node(“Shootables”);

rootNode.attachChild(shootables);

shootables.attachChild(addEnemy());

setupKeys();

createLight();

createSky();

createTerrain();

createCharacter();

setupChaseCamera();

setupAnimationController();

setupFilter();

setupEnemyAnimationController();

}[/java]



heres my onAction() that starts the collision detection, im mostly focused in doing this under the attack command

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

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

if (value) {

left = true;

} else {

left = false;

}

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

if (value) {

right = true;

} else {

right = false;

}

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

if (value) {

up = true;

} else {

up = false;

}

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

if (value) {

down = true;

} else {

down = false;

}

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

character.jump();

} else if (binding.equals(“Attack”) && !value) {

Attack();

CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

shootables.collideWith(ray, results);

for(int i = 0; i < results.size(); i++) {

String hit = results.getCollision(i).getGeometry().getName();



}

}

},[/java]



and my makeEnemy() (if it’s needed to change)

[java]protected Spatial addEnemy() {

enemy1 = new RigidBodyControl(1f);

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

//model.setLocalScale(0.5f);

enemyMod1.addControl(enemy1);

enemy1.setPhysicsLocation(new Vector3f(-120, 15, -15));

getPhysicsSpace().add(enemy1);

rootNode.attachChild(enemyMod1);

EnemyHP = 10;

return enemyMod1;

}[/java]



am i on the right track or am i using methods that are only meant for geometry shapes because it’s not that clearly explained in the tutorial

First - dont use RigidBodyControl for characters, use CharacterControl instead.



Second, read this:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners

well the RigidBodyControl is only for the non-moving character and also because when i used CharacterControl my player would walk right through him instead of the solidness that im getting now with RigidBodyControl

so i’ve added the physics listener to the program but i’m still lost as to what i’m supposed to do with it, im still getting collision between my characters, but no matter how many times i attack the enemy, he doesn’t die



[java]public void collision(PhysicsCollisionEvent e) {

if(e.getNodeA().getName().equals(“player”)) {

final Node node = (Node)e.getNodeA();

EnemyHP -= 1;

} else if(e.getNodeB().getName().equals(“player”)) {

final Node node = (Node)e.getNodeB();

EnemyHP -= 1;

}

}[/java]



this part of the code i’m still unsure as to how to do it

@jpilgram2010 said:
so i've added the physics listener to the program but i'm still lost as to what i'm supposed to do with it, im still getting collision between my characters, but no matter how many times i attack the enemy, he doesn't die

[java]public void collision(PhysicsCollisionEvent e) {
if(e.getNodeA().getName().equals("player")) {
final Node node = (Node)e.getNodeA();
EnemyHP -= 1;
} else if(e.getNodeB().getName().equals("player")) {
final Node node = (Node)e.getNodeB();
EnemyHP -= 1;
}
}[/java]

this part of the code i'm still unsure as to how to do it


Did you add the CollisionListener?
[java]BulletAppState.getPhysicsSpace().addCollisionListener(this);[/java]

Additionally i see no code which removes the Enemy.

I can’t get the collision with the CharacterControls working either. I have tried it with a CharacterControl and a RigidBody in kinematic mode.



I would be very happy if someone can a make small example/tutorial for this.

@jpilgram2010 said:
but no matter how many times i attack the enemy, he doesn't die


perhaps you just need to get better at your own game :D

I see your attack code is using ray casting, I may be stating the obvious here but this will never result in a physics collision... are you actually projecting a physics object at your enemy or just shooting a ray ?

It sounds to me like you're just really missing something, have a good look over TestWalkingChar as that covers most of what you're asking.


@Vortex said:
I would be very happy if someone can a make small example/tutorial for this.


dude look at the tests!
@thetoucher said:
dude look at the tests!


actually there is really no character on character examples in the tests you pretty much have to come up with your own magic for that kind of collision...........actually been thinking of using strictly ragdoll on ragdoll collision for characters.... u get the benefits of the characterControl and closer character on character collisions ..........I hope anyway ................haven't tested how or if ragdoll interact well with each other yet will also have to figure on a way for the character control to dynamically ghost through scene objects as it cannot scalerotate to fit the characters shape at any point in time...............

@mcbeth: I’m happy that you understand what I mean. I was thinking about some of your points too.