RigidBodyControl Problem

I made a game, the code was similar to the Hello Physics Tutorial.

I loaded a box geometry with a mass of 1.

Geometry box=new Geometry(“box”, b);
box.setLocalTranslation(new Vector3f(0,10,0));
rootNode.attachChild(box);

RigidBodyControl rbc=new RigidBodyControl(1f);
box.addControl(rbc);
bulletAppState.getPhysicsSpace().add(rbc);

But when I tried to shoot at the box, nothing happens. But when I tried to shoot on its side, it moves…(it was collided with sphere)…The sphere serves as a bullet…
I add this code:
bulletAppState.enableDebug(assetManager);

When I ran it, I saw the box and the RIGIDBODYCONTROL was on its side.

The position of RigidBodyControl was not the same as the box.

My question is: Is there a way to position the RigidBodyControl the same as the position of the box?

Do you use the latest version of the SDK (RC2)? Or do you use some exotic setup like that unmaintained maven repro? Cause it sounds like you run an ancient version of jME (given that is really all code). If you have the latest version, please make a test case for this (that is a newly written, self-contained application class only containing code that causes the issue).

@blackSilver even though u didn’t post it, i suspect you may be using the wrong Box constructor. You need to use the one with a center at (0, 0, 0) and extends from each side. Try using:
[java]Box box = new Box (Vector3f.ZERO, 1, 1, 1); // replace for dimensions of your box but keep Vector3f.ZERO there[/java]

2 Likes

Im using JME3 RC2

And here’s my complete codes:

[java]public class Main extends SimpleApplication {

//Declarations
private Spatial gun;
private BulletAppState bulletAppState;
private RigidBodyControl rigidbodycontrol;
private RigidBodyControl guncontrol;
private RigidBodyControl bulletcontrol;
private RigidBodyControl boxcontrol;
private CharacterControl player;
private boolean forward=false,backward=false,right=false,left=false;
Vector3f walkdir=new Vector3f();

public static void main(String[] args) {
    //screen settings
    AppSettings settings=new AppSettings(true);
    settings.setRenderer(AppSettings.LWJGL_OPENGL2);
    settings.setFrameRate(60);
    settings.setFullscreen(true);
    //apply the settings
    Main app = new Main();
    app.setSettings(settings);
    app.start();
}

@Override
public void simpleInitApp() {
    physics();// set up the physical world
    gameControls(); //load the game controls
   viewPort.setBackgroundColor(ColorRGBA.White);
   flyCam.setMoveSpeed(80);
   initGun(); //initializing the gun object in the bottom right of the screen
   addLight(); //adding light to the scene
   addObject("Models/town/main.scene",new Vector3f(0,-5,0)); //adding town object to the scene  
   mainCharacter(); //add the main player in the game
   sight();//attach the sight
   brick(); //add a brick
}

@Override
public void simpleUpdate(float tpf) {
  //add the walk  command to the player 
    walkCommand();
 //move the gun with the player
    moveGun();
}

@Override
public void simpleRender(RenderManager rm) {
  
}

//Add light 
public void addLight(){
    DirectionalLight light=new DirectionalLight();
    light.setDirection(new Vector3f(0,0,0));
    light.setColor(ColorRGBA.White.mult(1.3f));
    
    AmbientLight al=new AmbientLight();
    al.setColor(ColorRGBA.White.mult(1.5f));
    
    rootNode.addLight(light);
    rootNode.addLight(al);
}

//Add object
public void addObject(String path,Vector3f location){
    
    Spatial object=assetManager.loadModel(path);//load the object model
    object.setLocalTranslation(location);
    object.scale(5);
    CollisionShape shape=CollisionShapeFactory.createMeshShape(object); //create a collision shape 
    rigidbodycontrol=new RigidBodyControl(shape,0);
    object.addControl(rigidbodycontrol);
    bulletAppState.getPhysicsSpace().add(rigidbodycontrol);
    rootNode.attachChild(object);
}

//initialize the gun object in the main scene
public void initGun(){
    gun=assetManager.loadModel("Models/famas/Bullpup.obj");
    gun.rotate(-15*FastMath.DEG_TO_RAD,180*FastMath.DEG_TO_RAD,0);
    rootNode.attachChild(gun);
}

//setting up the physical world
public void physics(){
    bulletAppState=new BulletAppState();
    stateManager.attach(bulletAppState);
}

//set up the main character
public void mainCharacter(){
    CapsuleCollisionShape shape=new CapsuleCollisionShape(1.5f,5.7f,1);
    player=new CharacterControl(shape,0.05f);
    player.setJumpSpeed(30);
    player.setFallSpeed(20);
    player.setGravity(30);
    player.setPhysicsLocation(new Vector3f(0,9,0));
    bulletAppState.getPhysicsSpace().add(player);
}

//game key controls
public void gameControls(){
    //key mappings
    inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("backward", new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    //key listeners
    inputManager.addListener(actionlistener, "forward");
    inputManager.addListener(actionlistener, "backward");
    inputManager.addListener(actionlistener, "right");
    inputManager.addListener(actionlistener, "left");
    inputManager.addListener(actionlistener, "jump");
    inputManager.addListener(actionlistener, "shoot");
}

private ActionListener actionlistener=new ActionListener(){

    public void onAction(String name, boolean isPressed, float tpf) {
       if(name.equals("forward")){
           forward=isPressed;
       }else if(name.equals("backward")){
           backward=isPressed;
       }else if(name.equals("right")){
           right=isPressed;
       }else if(name.equals("left")){
           left=isPressed;
       }else if(name.equals("jump")){
           player.jump();
       }else if(name.equals("shoot")){
           bullet();
       }
    }
    
};

//walk Command
public void walkCommand(){
    Vector3f cf=cam.getDirection().clone().multLocal(0.5f);
    Vector3f cl=cam.getLeft().clone().multLocal(0.5f);
    walkdir.set(0, 0, 0);
    if(forward){
        walkdir.addLocal(cf);
    }
    if(backward){
        walkdir.addLocal(cf.negate());
    }
     if(right){
        walkdir.addLocal(cl.negate());
    }
      if(left){
        walkdir.addLocal(cl);
    }
     player.setWalkDirection(walkdir);
     cam.setLocation(player.getPhysicsLocation().add(new Vector3f(0,5,0)));
}

// move the gun with the camera
public void moveGun(){
    gun.setLocalTranslation(cam.getLocation().add(cam.getLeft().negate()).add(cam.getDirection().mult(4)).add(new Vector3f(0,-5,0)));
    gun.setLocalRotation(cam.getRotation());
}


public void sight(){
    guiNode.detachAllChildren();
    guiFont=assetManager.loadFont("Interface/Fonts/Default.fnt");
    BitmapText sight=new BitmapText(guiFont,false);
    sight.setText("+");
    sight.setSize(20);
    sight.setLocalTranslation(settings.getWidth()/2-3f, settings.getHeight()/2-3f, 0);
    sight.setColor(ColorRGBA.Red);
    guiNode.attachChild(sight);
}

public void bullet(){
  Box box=new Box(cam.getDirection(),0.1f,0.1f,0.1f);
  Geometry geom=new Geometry("gun bullet",box);
  Material mat=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
  mat.setColor("Color", ColorRGBA.Black);
  geom.setMaterial(mat);
  geom.setLocalTranslation(cam.getLocation().add(cam.getDirection().mult(5)));
  rootNode.attachChild(geom);
  
  guncontrol=new RigidBodyControl(1f);
  geom.addControl(guncontrol);
  bulletAppState.getPhysicsSpace().add(guncontrol);
  guncontrol.setLinearVelocity(cam.getDirection().mult(550));
}

public void brick(){
  Box box=new Box(new Vector3f(0,20,0),10,10,10);
  Geometry geom=new Geometry("brick",box);
  Material mat=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
  mat.setColor("Color", ColorRGBA.Blue);
  geom.setMaterial(mat);
  geom.setLocalTranslation(cam.getLocation().add(cam.getDirection().mult(5)));
  rootNode.attachChild(geom);
  
  boxcontrol=new RigidBodyControl(50f);
  geom.addControl(boxcontrol);
  bulletAppState.getPhysicsSpace().add(boxcontrol);
}

}
[/java]

the brick method seems not working

Yep, as @wezrule suspected, your box mesh is off. The actual center of your box is at 0/0/0, only its mesh is at 0/20/0.

Ok i got… Thank you so much…