Problems with collisions

I’m doing a game. Now I have a scene with a bar. This is the image:

The problem is that If I move the bar to left or right, it doesn’t collision with the wall, the bar continuous and no stop:

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.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Quaternion;
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;
import com.sun.java.swing.plaf.windows.WindowsTreeUI;

/**
*

  • @author dominuskernel
    */
    public class StageOne extends SimpleApplication{
    private Spatial scene;
    private Spatial barra;
    private BulletAppState bulletAppState;
    private RigidBodyControl landscape;
    private RigidBodyControl barra_phy;
    private boolean left = false, right = 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);
     
     //set camera location
     cam.setLocation(new Vector3f(-22f, 31f, 0f));
     
     //the camera look at direction
     cam.lookAt(new Vector3f(0f, 3f, 0f), Vector3f.UNIT_Y);
     
     //Declare scene and model and set location for barra
     scene = assetManager.loadModel("Scenes/stage1/balltingo.j3o");
     barra = assetManager.loadModel("Models/barra/barra.j3o");
     barra.setLocalTranslation(-22f, 6.5f, 0f);
     barra.setLocalScale(2f);
     
     //set the collision scene and barra model
     CollisionShape sceneShape = 
             CollisionShapeFactory.createMeshShape((Node)scene);
     landscape = new RigidBodyControl(sceneShape,0);
     scene.addControl(landscape);
     
     barra_phy = new RigidBodyControl(0.0f);
     barra.addControl(barra_phy);
     bulletAppState.getPhysicsSpace().add(barra);
     
     
     //attach scene
     rootNode.attachChild(scene);
     rootNode.attachChild(barra);
     
     //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.addListener(actionListener,“Left”,“Right”);
    }

    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;
    }
    }
    };

    //set the actions
    public void simpleUpdate(float tpf){
    if(left){
    barra.move(0,0,-20tpf);
    }
    if(right){
    barra.move(0,0,20
    tpf);
    }
    }
    }[/java]

You can’t move a spatial (barra.move(0,0,-20*tpf)) and at the same time have the physics engine simulate movement. You have to move the rigid body control. It is explained here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics#moving_a_physical_spatial

Either you use physics and then you have to use maths to calculate forces and impulses to move an object or you do not use the physics engine and then you can move a spatial but will have to find collisions yourself.