How to check collision from two physic nodes

hello

i want to change the color of my cannon ball after it hit an other physical object, but i don’t know how do check if two physical nodes collide.

i have tried several things but nothing worked.

can someody show me an example or solve it for me please?^^

Really, I think there so absolutely no search field – beginning with the one in the manual when you press F1 and ending with all search fields on this site – that would not lead you to some answers to this question around here…

[java]private ActionListener actionListener = new ActionListener() {

@Override

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“Shoot”) && !keyPressed) {

Sphere sphere = new Sphere(32, 32, 0.4f, true, false);

sphere.setTextureMode(TextureMode.Projected);

Geometry ball_geo = new Geometry(“cannon ball”, sphere);

ball_geo.setMaterial(stone_mat);

rootNode.attachChild(ball_geo);

ball_geo.setLocalTranslation(cam.getLocation());

RigidBodyControl ball_phy = new RigidBodyControl(1f);

ball_geo.addControl(ball_phy);

bulletAppState.getPhysicsSpace().add(ball_phy);

ball_phy.setLinearVelocity(cam.getDirection().mult(50));



CollisionResults results = new CollisionResults();

shootables.collideWith(ball_geo, results);

System.out.println("-- Collisions? " + results.size() + “–”);

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

float dist = results.getCollision(i).getDistance();

Vector3f pt = results.getCollision(i).getContactPoint();

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

System.out.println("* Collision #" + i);

System.out.println(" You shot " + hit + " at " + pt + “, " + dist + " wu away.”);

}

}

}

};[/java]





this is my code, but i don’t know why this does not work :frowning:

i read the manuals and so on but i really don’t know how to manage it…

That is code that cannot be found via any search field xD You need to implement “PhysicsCollisionListener”. If you don’t know what implementing means, read up on java coding.

Have a look at TestWalkingChar.java, it has an example of a ball hitting another physics object and doing something (see around line 418). To understand how this part of the example works, read up on Physics Collision Listeners.

try this, :slight_smile:

[java]

public class HelloCollision extends SimpleApplication

implements ActionListener, PhysicsCollisionListener {



public function test_oto(){

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

GhostControl oto_ghost = new GhostControl(capsuleShape);

//load model

Node oto = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");

//set position



}



@Override

public void collision(PhysicsCollisionEvent event) {

if (event.getObjectA() instanceof GhostControl) {

System.out.println("A-Object- collision detected");

}

if (event.getObjectB() instanceof GhostControl) {

System.out.println("B-Object- collision detected");

}

}

}

[/java]