[SOLVED] Object not affected by gravity when there is no movement

When you hold X the object is pulled up by gravity when you let go gravity pulls it down, but if I wait for the cube to stop moving (completely) and hold X it’s not affected by it, it just sits there like I never even held X but I did. Is this a bug?



Here is the code: [java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.app.SimpleApplication;

import com.jme3.bounding.BoundingBox;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.PhysicsSpace;

import com.jme3.bullet.collision.PhysicsCollisionObject;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.font.BitmapText;

import com.jme3.input.ChaseCamera;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.DirectionalLight;

import com.jme3.light.PointLight;

import com.jme3.material.Material;

import com.jme3.math.*;

import com.jme3.renderer.Camera;

import com.jme3.renderer.queue.RenderQueue.ShadowMode;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial;

import com.jme3.texture.Texture;

import com.jme3.texture.Texture.WrapMode;

import com.jme3.util.SkyFactory;

import java.util.ArrayList;

import java.util.List;

import com.jme3.asset.plugins.ZipLocator;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.collision.shapes.CompoundCollisionShape;

import com.jme3.bullet.collision.shapes.SphereCollisionShape;

import com.jme3.bullet.control.CharacterControl;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.AmbientLight;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;

import com.jme3.scene.shape.Sphere;



/**

  • Example 9 - How to make walls and floors solid.
  • This collision code uses Physics and a custom Action Listener.
  • @author normen, with edits by Zathras

    /

    public class Main extends SimpleApplication

    implements ActionListener {



    private Spatial sceneModel;

    private BulletAppState bulletAppState;

    private RigidBodyControl landscape;

    private BoxCollisionShape boxShape = new BoxCollisionShape(new Vector3f(04.25f,4.25f,4.25f));

    private Node obstacle;

    private CapsuleCollisionShape obstacleCollisionShape = new CapsuleCollisionShape(3.5f, 3.5f);

    private SphereCollisionShape boxCollisionShape;

    private CharacterControl player;

    private Vector3f walkDirection = new Vector3f();

    private boolean left = false, right = false, up = false, down = false;



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    protected Geometry box_geo;



    public void simpleInitApp() {

    /
    * Set up Physics /

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);

    //bulletAppState.getPhysicsSpace().enableDebug(assetManager);



    // We re-use the flyby camera for rotation, while positioning is handled by physics

    viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

    flyCam.setMoveSpeed(100);

    setUpKeys();

    setUpLight();



    // We load the scene from the zip file and adjust its size.

    assetManager.registerLocator("town.zip", ZipLocator.class);

    sceneModel = assetManager.loadModel("main.scene");

    sceneModel.setLocalScale(2f);

    sceneModel.setLocalTranslation(0,-7,0);



    // We set up collision detection for the scene by creating a

    // compound collision shape and a static RigidBodyControl with mass zero.

    CollisionShape sceneShape =

    CollisionShapeFactory.createMeshShape((Node) sceneModel);

    landscape = new RigidBodyControl(sceneShape, 0);

    sceneModel.addControl(landscape);



    Box box = new Box( new Vector3f(0,0,0),4.25f, 4.25f, 4.25f);

    Material mat1 = new Material(assetManager,

    "Common/MatDefs/Misc/Unshaded.j3md");

    Geometry boxGeometry = new Geometry("Box", box);

    boxGeometry.setMaterial(mat1);

    mat1.setColor("Color", ColorRGBA.Gray);



    obstacle = new Node("obstacle");

    obstacle.rotate(3, 0, 2);

    obstacle.move(0,5,0);

    RigidBodyControl bodyControl = new RigidBodyControl(boxShape, 1);

    obstacle.addControl(bodyControl);

    bulletAppState.getPhysicsSpace().add(obstacle);

    rootNode.attachChild(obstacle);

    obstacle.attachChild(boxGeometry);



    Box b = new Box(1,1,1);

    box_geo = new Geometry("Box", b);

    box_geo.setMaterial(mat1);

    box_geo.addControl(new RigidBodyControl( 1.0f ));

    rootNode.attachChild(box_geo);

    bulletAppState.getPhysicsSpace().add(box_geo);







    // We set up collision detection for the player by creating

    // a capsule collision shape and a CharacterControl.

    // The CharacterControl offers extra settings for

    // size, stepheight, jumping, falling, and gravity.

    // We also put the player in its starting position.

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

    player = new CharacterControl(capsuleShape, 0.05f);

    player.setJumpSpeed(20);

    player.setFallSpeed(30);

    player.setGravity(30);

    player.setPhysicsLocation(new Vector3f(0, 10, 40));



    // We attach the scene and the player to the rootNode and the physics space,

    // to make them appear in the game world.

    rootNode.attachChild(sceneModel);

    bulletAppState.getPhysicsSpace().add(landscape);

    bulletAppState.getPhysicsSpace().add(player);

    }



    private void setUpLight() {

    // We add light so we see the scene

    AmbientLight al = new AmbientLight();

    al.setColor(ColorRGBA.White.mult(1.3f));

    rootNode.addLight(al);



    DirectionalLight dl = new DirectionalLight();

    dl.setColor(ColorRGBA.White);

    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());

    rootNode.addLight(dl);

    }



    /
    * We over-write some navigational key mappings here, so we can
  • add physics-controlled walking and jumping: /

    private void setUpKeys() {

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));

    inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));

    inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));

    inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));

    inputManager.addMapping("Gravity", new KeyTrigger(KeyInput.KEY_X));

    inputManager.addListener(this, "Gravity");

    inputManager.addListener(this, "Left");

    inputManager.addListener(this, "Right");

    inputManager.addListener(this, "Up");

    inputManager.addListener(this, "Down");

    inputManager.addListener(this, "Jump");

    }



    /
    * These are our custom actions triggered by key presses.
  • We do not walk yet, we just keep track of the direction the user pressed. */

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

    if (binding.equals("Left")) {

    left = value;

    } else if (binding.equals("Right")) {

    right = value;

    } else if (binding.equals("Up")) {

    up = value;

    } else if (binding.equals("Down")) {

    down = value;

    } else if (binding.equals("Jump")) {

    player.jump();

    } else if (binding.equals("Gravity")) {

    if (value) {

    box_geo.getControl(RigidBodyControl.class).setGravity(new Vector3f(0,10,0));

    } else {

    box_geo.getControl(RigidBodyControl.class).setGravity(new Vector3f(0,-10,0));

    }

    }

    }





    /**
  • This is the main event loop–walking happens here.
  • We check in which direction the player is walking by interpreting
  • the camera direction forward (camDir) and to the side (camLeft).
  • The setWalkDirection() command is what lets a physics-controlled player walk.
  • We also make sure here that the camera moves with player.

    */



    @Override

    public void simpleUpdate(float tpf) {

    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);

    Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);

    walkDirection.set(0, 0, 0);

    if (left) { walkDirection.addLocal(camLeft); }

    if (right) { walkDirection.addLocal(camLeft.negate()); }

    if (up) { walkDirection.addLocal(camDir); }

    if (down) { walkDirection.addLocal(camDir.negate()); }

    player.setWalkDirection(walkDirection);

    cam.setLocation(player.getPhysicsLocation());



    box_geo.getControl(RigidBodyControl.class).setFriction(4.0f);

    }





    }[/java]

I have found a loop hole to get past this, just apply a central force with no force like so : [java]box_geo.getControl(RigidBodyControl.class).applyCentralForce(new Vector3f(0,0,0));[/java]

Maybe the box is put to sleep when it isn’t moving and setting gravity might not wake it up.



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics



also you can google for “sleep threshold” and you’ll probably find the relevant API.

You were right, it was. All you have to do is use the activate() function http://hub.jmonkeyengine.org/groups/physics/forum/topic/re-activating-sleeping-objects/

Great that you got it working. I couldn’t give better links since I was surfing through the phone :slight_smile: The advanced physics tutorial has some more information about setSleepThreshold and those methods.



If you don’t mind perhaps you could edit the topic and prefix with “[Solved]”? It helps a bit when scanning the forum.


If you don't mind perhaps you could edit the topic and prefix with "[Solved]"? It helps a bit when scanning the forum.

I would if I knew how...I can't find how to mark it as solved, I've looked everywhere! :(

1 Like

My fault, I was looking for a “Mark as solved” button :smiley: