I can't the physical box collisions

I’m doing a game based in Arkanoid but in 3D. The problem is that whan the ball colisions with some box disappear but its collision physics no(the ball continuos collisions in the place where the box disappeared). This is the code:

[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package balltingo;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Ray;
import com.jme3.scene.Node;
import com.jme3.scene.control.Control;
import com.sun.org.apache.bcel.internal.classfile.Code;

/**
*

  • @author dominuskernel
    */
    public class StageOne extends SimpleApplication{
    private Node boxNode;
    private Spatial scene, bar, ball, box[][];
    private BulletAppState bulletAppState;
    private RigidBodyControl landscape, bar_phy, ball_phy, box_phy;
    private CollisionShape barShape, boxShape;
    private boolean left = false, right = false, begin=false;

    public static void main(String[] args){
    StageOne app = new StageOne();
    app.start();
    }

    @Override
    public void simpleInitApp(){
    //the camera doesn’t move
    flyCam.setEnabled(false);
    //flyCam.setMoveSpeed(50);

     //call the class for collisions
     bulletAppState = new BulletAppState();
     stateManager.attach(bulletAppState);
     bulletAppState.getPhysicsSpace().enableDebug(assetManager);
     
     //set camera location
     cam.setLocation(new Vector3f(-10f, 28f, 0f));
     
     //the camera look at direction
     cam.lookAt(new Vector3f(0f, 16.5f, 0f), Vector3f.UNIT_Y);
     
     //Declare scene and models and set location for bar, ball and the boxes
     scene = assetManager.loadModel("Scenes/stage1/balltingo.j3o");
     scene.setLocalTranslation(0, 0, 1f);
     
     bar = assetManager.loadModel("Models/barra/barra.j3o");
     bar.setLocalScale(2f);
     bar.setLocalTranslation(-10f, 6.5f, 0f);
     
     
     ball = assetManager.loadModel("Models/bola/ball.j3o");
     ball.setLocalTranslation(0f, 8f, 0f);
     ball.setLocalScale(0.5f);
             
     //set the collision scene and barra model
     CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node)scene);
     landscape = new RigidBodyControl(sceneShape,0f);
     scene.addControl(landscape);
     bulletAppState.getPhysicsSpace().add(landscape);
     
     barShape = CollisionShapeFactory.createDynamicMeshShape((Node)bar);
     bar_phy = new RigidBodyControl(barShape,0f);
     bar_phy.setPhysicsLocation(new Vector3f(-10f, 6.5f, 0f));
     bar_phy.setKinematic(true);
     bar.addControl(bar_phy);
     bulletAppState.getPhysicsSpace().add(bar);
     
     ball_phy = new RigidBodyControl(1f);
     ball.addControl(ball_phy);
     bulletAppState.getPhysicsSpace().add(ball);
     bulletAppState.getPhysicsSpace().addCollisionObject(ball_phy);
     ball_phy.setCollisionGroup(0);
     
     //declare boxNode
     boxNode = new Node("Boxes");
     
     //attach stage
     rootNode.attachChild(scene);
     rootNode.attachChild(bar);
     rootNode.attachChild(ball);
     rootNode.attachChild(boxNode);
     
     //declare and attach every box
     box= new Spatial[3][9];
     
     float x = 0;
     float z = 0;
     
     for(int f=0;f<box.length;f++){
         for(int c=0;c<box[f].length; c++){
             box[f][c] = assetManager.loadModel("Models/box/box.j3o");
             box[f][c].setLocalScale(0.5f);
             box[f][c].setLocalTranslation(22f-x,6.5f,-8.2f+z);
             boxShape = CollisionShapeFactory.createBoxShape((Node)box[f][c]);
             box_phy = new RigidBodyControl(boxShape,0f);
             box[f][c].addControl(box_phy);
             bulletAppState.getPhysicsSpace().add(box[f][c]);
             bulletAppState.getPhysicsSpace().addCollisionObject(box_phy);
             z = z + 2f;
             boxNode.attachChild(box[f][c]);
         }
         z = 0f;
         x = x + 4f;
     }
     //call to setUpKeys method
     box_phy.setCollisionGroup(1);
     
     setUpKeys();
    

    }

    //set the controls key for barra
    public void setUpKeys(){
    inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping(“Shoot”, new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(actionListener,“Left”,“Right”,“Shoot”);
    }

    private ActionListener actionListener = new ActionListener() {
    public void onAction(String binding, boolean keyPressed, float tpf) {
    if(binding.equals(“Left”)){
    left = keyPressed;
    } else if(binding.equals(“Right”)){
    right = keyPressed;
    } else if(binding.equals(“Shoot”)){
    shootBall();
    begin = true;
    }
    }
    };

    //set the ball shoot
    public void shootBall(){
    ball_phy.setLinearVelocity(Vector3f.UNIT_X.mult(0.1f));
    }

    //when the ball collisions with some box. The box disappear.
    //This method is call in simpleUpdate
    public void collisionToBox(){
    CollisionResults results = new CollisionResults();
    boxNode.collideWith(ball.getWorldBound(), results);
    if(results.size()>0){
    CollisionResult closest = results.getClosestCollision();
    Spatial s = closest.getGeometry();
    if(s.getName().equals(“Box”)){
    s= s.getParent();
    System.out.println(s.getName());
    s.removeControl(box_phy);
    s.removeFromParent();
    }
    }
    }
    //set the actions
    @Override
    public void simpleUpdate(float tpf){
    if(left && bar.getLocalTranslation().z >= -8.5){
    bar.move(0,0,-20tpf);
    }
    if(right && bar.getLocalTranslation().z <= 8.5){
    bar.move(0,0,20
    tpf);
    }

     float velocity = ball_phy.getLinearVelocity().getX();
     if(begin==true){
         if(velocity &gt;=0){
             ball_phy.applyForce(new Vector3f(10f,0,0), Vector3f.ZERO);
             ball_phy.setLinearVelocity(ball_phy.getLinearVelocity().mult(new Vector3f(1f,0f,1f)));
         }else{
             ball_phy.applyForce(new Vector3f(-10f,0,0),Vector3f.ZERO);
             ball_phy.setLinearVelocity(ball_phy.getLinearVelocity().mult(new Vector3f(1f,0f,1f)));
         }
         collisionToBox();
     }
    

    }
    }
    [/java]

These images explain better what happen:

[java]
bulletAppState.getPhysicsSpace().add(box[f][c]);
bulletAppState.getPhysicsSpace().addCollisionObject(box_phy);
[/java]
isnt one enough ? what about once
[java]
bulletAppState.getPhysicsSpace().addAll(box);
[/java]

you must also remove object from physics space on collision

i think you also must add one rigidbody per box, not one rigidbody for all boxes

If I add one rigidbody per box. How I remove the rigidbody when the ball collisions with the box? Because I would have to use an array for that and would have to detect what element from that array belong to the box collided.

some inspiration
http://code.google.com/p/jmonkeyengine/source/browse/branches/jme3/src/test/jme3test/bullet/BombControl.java?r=6605

or you can do something like this to obtain rigid body assigned to the box
[java]
// Get physics control
RigidBodyControl rb = boxSpatial.getControl(RigidBodyControl.class);
// remove from physics space here
[/java]
in fact, you can have array of spatials (nodes, geometries) only… then you can retrieve any control through spatial.getControl(ClassName.class);

Finally I have resolved my problem. Now the ball bounces when collisions with the box and after the physics box disappear. This is the new code:

[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package balltingo;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.scene.Node;

/**
*

  • @author dominuskernel
    */
    public class StageOne extends SimpleApplication{
    private Node boxNode;
    private Spatial scene, bar, ball, box[][],boxDisappear;
    private BulletAppState bulletAppState;
    private RigidBodyControl landscape, bar_phy, ball_phy, box_phy[][],boxPhyDisappear;
    private CollisionShape barShape, boxShape;
    private boolean left = false, right = false, begin=false, collision=false;
    private Vector3f disappear;
    private float time=0;

    public static void main(String[] args){
    StageOne app = new StageOne();
    app.start();
    }

    @Override
    public void simpleInitApp(){
    //the camera doesn’t move
    flyCam.setEnabled(false);
    //flyCam.setMoveSpeed(50);

     //call the class for collisions
     bulletAppState = new BulletAppState();
     stateManager.attach(bulletAppState);
     bulletAppState.getPhysicsSpace().enableDebug(assetManager);
     
     //set camera location
     cam.setLocation(new Vector3f(-10f, 28f, 0f));
     
     //the camera look at direction
     cam.lookAt(new Vector3f(0f, 16.5f, 0f), Vector3f.UNIT_Y);
     
     //Declare scene and models and set location for bar, ball and the boxes
     scene = assetManager.loadModel("Scenes/stage1/balltingo.j3o");
     scene.setLocalTranslation(0, 0, 1f);
     
     bar = assetManager.loadModel("Models/barra/barra.j3o");
     bar.setLocalScale(2f);
     bar.setLocalTranslation(-10f, 6.5f, 0f);
     
     
     ball = assetManager.loadModel("Models/bola/ball.j3o");
     ball.setLocalTranslation(0f, 8f, 0f);
     ball.setLocalScale(0.5f);
             
     //set the collision scene and barra model
     CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node)scene);
     landscape = new RigidBodyControl(sceneShape,0f);
     scene.addControl(landscape);
     bulletAppState.getPhysicsSpace().add(landscape);
     
     barShape = CollisionShapeFactory.createDynamicMeshShape((Node)bar);
     bar_phy = new RigidBodyControl(barShape,0f);
     bar_phy.setPhysicsLocation(new Vector3f(-10f, 6.5f, 0f));
     bar_phy.setKinematic(true);
     bar.addControl(bar_phy);
     bulletAppState.getPhysicsSpace().add(bar);
     
     ball_phy = new RigidBodyControl(1f);
     ball.addControl(ball_phy);
     bulletAppState.getPhysicsSpace().add(ball);
     bulletAppState.getPhysicsSpace().addCollisionObject(ball_phy);
     ball_phy.setCollisionGroup(0);
     
     //declare boxNode
     boxNode = new Node("Boxes");
     
     //attach stage
     rootNode.attachChild(scene);
     rootNode.attachChild(bar);
     rootNode.attachChild(ball);
     rootNode.attachChild(boxNode);
     
     //declare and attach every box
     box= new Spatial[3][9];
     box_phy = new RigidBodyControl[3][9];
     
     float x = 0;
     float z = 0;
     
     for(int f=0;f&lt;box.length;f++){
         for(int c=0;c&lt;box[f].length; c++){
             box[f][c] = assetManager.loadModel("Models/box/box.j3o");
             box[f][c].setLocalScale(0.5f);
             box[f][c].setLocalTranslation(22f-x,6.5f,-8.2f+z);
             boxShape = CollisionShapeFactory.createBoxShape((Node)box[f][c]);
             box_phy[f][c] = new RigidBodyControl(boxShape,0f);
             box[f][c].addControl(box_phy[f][c]);
             bulletAppState.getPhysicsSpace().add(box[f][c]);
             z = z + 2f;
             boxNode.attachChild(box[f][c]);
         }
         z = 0f;
         x = x + 4f;
     }
     //call to setUpKeys method
     setUpKeys();
    

    }

    //set the controls key for barra
    public void setUpKeys(){
    inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping(“Shoot”, new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(actionListener,“Left”,“Right”,“Shoot”);
    }

    private ActionListener actionListener = new ActionListener() {
    public void onAction(String binding, boolean keyPressed, float tpf) {
    if(binding.equals(“Left”)){
    left = keyPressed;
    } else if(binding.equals(“Right”)){
    right = keyPressed;
    } else if(binding.equals(“Shoot”)){
    shootBall();
    begin = true;
    }
    }
    };

    //set the ball shoot
    public void shootBall(){
    ball_phy.setLinearVelocity(Vector3f.UNIT_X.mult(0.1f));
    }

    //when the ball collisions with some box. The box disappear.
    //This method is call from simpleUpdate
    public void collisionToBox(){
    CollisionResults results = new CollisionResults();
    boxNode.collideWith(ball.getWorldBound(), results);
    /If there is collision, this get the object and its physics when the ball
    * collisioned,the collision location, and the material dissappear but not
    * its physics
    /
    if(results.size()>0){
    collision = true;
    CollisionResult closest = results.getFarthestCollision();
    Spatial s = closest.getGeometry();
    disappear= s.getWorldTranslation();
    for(int f=0;f<box.length;f++){
    for(int c=0;c<box[f].length;c++){
    if(disappear.distance(box[f][c].getWorldTranslation())<0.5){
    boxDisappear = box[f][c];
    boxPhyDisappear = box_phy[f][c];
    boxNode.detachChild(boxDisappear);
    }
    }
    }
    }
    }

    //The physics of hidden box of last method disappear after 0.1 second
    void dissapearPhysics(){
    boxPhyDisappear.getPhysicsSpace().remove(boxDisappear);
    collision=false;
    time=0;
    }

    //set the actions
    @Override
    public void simpleUpdate(float tpf){
    if(left && bar.getLocalTranslation().z >= -8.5){
    bar.move(0,0,-20tpf);
    }
    if(right && bar.getLocalTranslation().z <= 8.5){
    bar.move(0,0,20
    tpf);
    }

     float velocity = ball_phy.getLinearVelocity().getX();
     if(begin==true){
         if(velocity &gt;=0){
             ball_phy.applyForce(new Vector3f(10f,0,0), Vector3f.ZERO);
             ball_phy.setLinearVelocity(ball_phy.getLinearVelocity().mult(new Vector3f(1f,0f,1f)));
         }else{
             ball_phy.applyForce(new Vector3f(-10f,0,0),Vector3f.ZERO);
             ball_phy.setLinearVelocity(ball_phy.getLinearVelocity().mult(new Vector3f(1f,0f,1f)));
         }
         collisionToBox();
     }
     if(collision==true){
         time = time + tpf;
         if(time&gt;=0.08){            
           dissapearPhysics();
         }
     }
    

    }
    }
    [/java]

1 Like