# Bullet, limit movement and object wise gravity?

Hi,

2 Questions came up when playing around with JME3 physics.

is there a official way in JME3 to limit movement and/or rotation of objects to certain axis… e.g. for a 2d side scroller game i would want my hero not to fall off the platforms into z direction if it hits something.

Can i exclude certain objects from gravity? Like a hovering spaceship. I tried PhysicsNode.setGravity(Vector3f.ZERO), but gravity still affects the object.

93interactive said:
Can i exclude certain objects from gravity? Like a hovering spaceship. I tried PhysicsNode.setGravity(Vector3f.ZERO), but gravity still affects the object.

Maybe with applyContinuousForce? You would simply apply a counterforce to the gravity in the up direction.

You are setting a local gravity value in addition to the global one I think. So you’d have to set the local gravity to y=+9.8 to counteract the global gravity.

About the axes thing, it should be possible via callbacks and checks but theres no “2d switch” in bullet, no.

Cheers,

Normen

thanx again.

applyContinuesForce works, setGravity on PhysicsNode seems to be ignored.

I found code in the bullet wiki, which allows limiting movement/rotation to certain axis:

``` btRigidBody::setLinearFactor(btVector3(1,0,1)); btRigidBody::setAngularFactor(btVector3(0,1,0)); ```

this seems not to be in JME3, any plans when this will happen? or is it there and i just did not find it?

Linear and angular damping should both be there. Also gravity should work, can you post your code?

as far as i have understood it, it has nothing to do with damping. LinearFactor and AngularFactor is just a general factor for the 3 axis that is added to every force, e.g.:

``` void applyCentralForce(const btVector3& force) { m_totalForce += force*m_linearFactor; } ```

for the gravity thing i did nothing special, i have a PhysicsNode with two Spatial’s attached as children. If i do nothing, it falls down. If i set a Gravity it still falls down:

``` body=assetManager.loadModel(“Models/body.j3o”); body.setMaterial(matHeli); CollisionShape sceneShape = CollisionShapeFactory.createDynamicMeshShape(body); bodyPhysics = new PhysicsNode(body, sceneShape, 1f); // this does not work: // bodyPhysics.setGravity(new Vector3f(0, 9.8f, 0)); // this does bodyPhysics.applyContinuousForce(true,new Vector3f(0,9.8f,0)); bodyPhysics.setCollisionGroup(PhysicsNode.COLLISION_GROUP_01); bodyPhysics.attachDebugShape(assetManager); bodyPhysics.updateGeometricState(); world.getRootNode().attachChild(bodyPhysics); world.getPhysicsSpace().add(bodyPhysics); Material matRotor = new Material( assetManager, “Common/MatDefs/Light/Lighting.j3md”); matRotor.setFloat(“m_Shininess”, 10f); matRotor.setBoolean(“m_UseMaterialColors”, true); matRotor.setColor(“m_Diffuse”, ColorRGBA.Gray); matRotor.setColor(“m_Ambient”, ColorRGBA.DarkGray); matRotor.setColor(“m_Specular”, ColorRGBA.Gray); sideRotor=assetManager.loadModel(“Models/siderotor.j3o”); sideRotor.setMaterial(matRotor); sideRotor.setLocalTranslation(0, 0.8f, -3.1f); bodyPhysics.attachChild(sideRotor); mainRotor=assetManager.loadModel(“Models/mainrotor.j3o”); mainRotor.setMaterial(matRotor); mainRotor.setLocalTranslation(0, 0, -0.1f); bodyPhysics.attachChild(mainRotor); ```

For the factor methods, I really did not put them in yet, sorry, thought they were in. Will add them tonight. About the gravity, try setting the gravity after you add the node to the physicsspace, I guess the physicsspace sets its own gravity to the object when you add it.

Cheers,

Normen

Looking at the jbullet code again I found why I did not add these functions. They are not in the jbullet version of bullet Theres only setAngularFactor(float) available in jbullet which I deemed useless due to the existence of the damping methods. You will have to wait until the physics is moved to native bullet, sorry.

Cheers,

Normen

ok, thanx, will give jbox2d a try then for now.