Hello.
I’ve seen this question asked a million times on the thread so I feel bad to ask it myself but I’m just stuck on this issue with JME3.
I’m able to get no gravity with a CharacterController but those controllers cannot be rotated or have forces applied (if I’m wrong please supply a simple code example or a link to one showing how to do so).
Code to create the body.
[java]
mShape = new BoxCollisionShape(new Vector3f(1, 1, 1));
mController = new RigidBodyControl(mShape, 0.05f);
mController.setGravity( Vector3f.Zero );
[/java]
Code to update the body.
[java]
mController.applyCentralForce( new Vector3f(0,9.8f0.5f,0) );
[/java]
The idea was to give it a constant (MA) to oppose the (M*-A) of the physicSpace’s gravity (other posts recommended this).
This does not work. It only makes the body float a bit before flying off screen.
Is there a “copy-paste” example that anyone can give me of a RigidBodyControl free-floating without gravity?
I really do appreciate you taking the time to read this post.
As already mentioned I don’t use physics so this is just a random idea…does impulse already include mass? You might be including it twice by multiplying it in yourself.
First of all your masses don’t match up. Gravity in bullet is also 9.81.
I would use apply impulse with gravity * mass * timestep in the physiscs tick listener
You need to account for tpf in your forces I expect.
zarch said:
You need to account for tpf in your forces I expect.
Actually I don't think so (disclaimer: still learning this stuff). For each tick bullet will sum the forces on the body and the force isn't dependent on time (impulse and acceleration are, but not the force). The forces are cleared after each tick.
Impulse = force * time so it should be the same thing as wezrule says (and ofc you must use the right mass as he says).
Still haven't been able to balance force against gravity though so don't take what I say as "the truth", something tips the balance somewhere and I haven't been able to figure out what.
Well I saw an almost identical question a few months ago and tpf was the solution.
I don’t know if the setup was the same as this though…
Yeah I remember it too, I think it was the exact same thing and wezrule gave the solution using applyImpulse and that should work fine. So just do what wezrule says
@viperld you need to set gravity=0 to the physics entirely to achve the effect you want.
Like this:
[java]
bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0, 0, 0));
[/java]
or try this:
[java]
mController.setGravity( new Vector3f(0, 0, 0) );[/java]
for more information see my spaceship prototype:
http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fspaceship
First of all, the amount of responses has blown me away. Thank you all for actually caring about my issues.
That being said I’ve gone back and attempted to implement the solution as wezrule described. Here is what I have:
[java]
public class NoGravityRigidBody extends RigidBodyControl implements PhysicsTickListener{
protected Vector3f mVectorHelper;
public NoGravityRigidBody(CollisionShape shape, float mass){
super(shape,mass);
mVectorHelper=new Vector3f();
}
public void prePhysicsTick(PhysicsSpace space, float tpf) {
mVectorHelper.x=0;
mVectorHelper.y= 9.81f*getMass()*tpf;
mVectorHelper.z=0;
applyImpulse( mVectorHelper, getPhysicsLocation() );
}
public void physicsTick(PhysicsSpace space, float tpf) {
}
}
[/java]
And I register the ticker like so:
[java]
mController = new NoGravityRigidBody(mShape, 0.05f);
Globals.global_bulletAppState.getPhysicsSpace().addTickListener(mController);
[/java]
This has not removed gravity from the rigid body (it still flies away). Did I miss a step here?
@miffth.
I attempted to remove gravity by taking your advice and either setting the physicsSpace gravity to 0, or the rigidbody gravity to 0, or both to 0.
This did not resolve the issue for me.
[java]applyImpulse( mVectorHelper, getPhysicsLocation() );[/java]
should be
[java]applyImpulse( mVectorHelper, Vector3f.ZERO );[/java]
@wezrule said:
[java]applyImpulse( mVectorHelper, getPhysicsLocation() );[/java]
should be
[java]applyImpulse( mVectorHelper, Vector3f.ZERO );[/java]
Yea this didn't make a difference either. All of this advice is so sound but doesn't give me zero-gravity.
@mifth
I'm thinking that I need to think outside of the box. I'm going to see what your space project is doing that mine currently isn't.
Wow so I was able to get a stable zero gravity by looking through @mifth’s project code.
What did it for me was removing all functionality relating to the applyImpulse(gravitymasstpf, some position).
Next I set the world gravity to zero and the player mass to 1. With any other mass things went off balance and it sank or floated away.
So, in summary, for zero gravity make sure your world gravity is zero and your controller mass is 1.
Thanks to everyone who responded.
Also use these settings:
[java]
myRigidbodyControl.setDamping(0.9f, 0.99f);
myRigidbodyControl.setFriction(0.9f);
[/java]
Cool that space project was helpful for you. Btw, @wezrule helped me with math there.
@viperld said:
Yea this didn't make a difference either. All of this advice is so sound but doesn't give me zero-gravity.
You must do something else wrong then because I tried it myself and it works fine and the same solution I posted here works for him as well. http://hub.jmonkeyengine.org/groups/physics/forum/topic/bullet-and-forces-the-force-seems-not-to-be-with-me/
For anybody looking for a hovering solution, you simply have to disable gravity on the node you want to hover using this and BEWARE the node has to be added to the PhysicsSpace BEFORE you set the gravity, else it’s completely useless because the PhysicsSpace sets the global gravity to the node when it is added!
[java]
bulletAppState.getPhysicsSpace().add(this.node);
node.getControl(RigidBodyControl.class).setGravity(Vector3f.ZERO);
[/java]
This simple code works for hovering anything with a mass.
…the javadoc says this btw.
Oops, my bad, but I’m sure many people start searching using Google and this page is what came first in my search, that’s why I think it’s important to contribute with the solutions and useful information on here too