Changing the direction of gravity

I’m not sure where to put code samples so I apologize in advance if this is the wrong forum.



I searched for an example of how to change the direction of gravity since I want my player running around a sphere but didn’t find any way to do that for a charactercontroller. Here is the solution I came up with and hopefully it will help others who are having the same questions.



The steps are

  • calculate the vector between the player and the center of the world

  • normalize the direction vector

  • multiply the normalized vector by my "fallspeed" parameter


Right now I'm calculating the gravity as velocity and not acceleration so the effect isn't "realistic" but its good enough to let me continue my proof of concept.

Also I'm letting the player thrust away from the planet which is simply negating the gravity vector and multiplying that by the thrust parameter.

[java] Vector3f gravityDirection= world.getLocalTranslation().subtract(player.getPhysicsLocation());
walkDirection.addLocal(gravityDirection.normalize().mult(fallSpeed));

if (thrust) {walkDirection.addLocal(gravityDirection.normalize().mult(thrustSpeed*-1));}
player.setWalkDirection(walkDirection);
[/java]

I'm sure this solution is totally obvious to the vets here but like I said I didn't see a solution when I searched.
1 Like

Hi,

I’m trying to achieve something really similar. I would like to be able to set the player’s gravity vector to something other than
[java]CharacterControl player;
//…
player.setUpAxis(0|1|2); // for x, y, z[/java]

I tried various things and the only thing that I could get working was to basically rotate everything else around the player by say 90 degrees, by doing this:
[java]Quaternion rot = new Quaternion();
Vector3f dir = new Vector3f(-9.81f,0f,0f);
rot.fromAngleAxis(FastMath.PI/2, dir);
gameLevel.rotate(rot);
landscape.setPhysicsRotation(rot);[/java]

player.setWalkDirection(Vector3f vector)
only seems to change the view of the player and “walk” him for a tiny bit.

Basically, I want to make a game based on gravity and it would be cool if someone could give me some hints. I did pre-order the “jMonkeyEngine Beginner’s Guide” book to support the project and because I want it on my shelf. :slight_smile:

Any ideas much appreciated.

I don’t use the physics, but I believe each physics object has a method to set the gravity for that object?

Thanks for the quick answer.

But that’s what I was looking for in CharacterControl. It doesn’t have the setGravity(Vector3f) method, which surprised me. However, it does all the walking etc so nicely. It’s really a shame that it seems I cannot use it.