Character is floating even with setGravity

Hello, the current issue I am having is that my player floats when moving off a ramp (a mountain). So i tried to do setGravity but I dont see any change. I am using a bettercharacter control.

Code for the player physics:
[java]
playerModel = assetManager.loadModel(“Models/Player/Mage/mage.j3o”);
playerModel.setMaterial(assetManager.loadMaterial(“Models/Player/Mage/mageMaterial.j3m”));
playerModel.setLocalScale(10, 10, 10);//for mage
playerModel.rotate(0, 3.2f, 0);

    player = new Player(playerModel, this.app.getInputManager(), this.app.getCamera(), this, assetManager, bulletAppState);
    player.setShadowMode(ShadowMode.Cast);
    player.setMage(true);
    player.getCharacterControl().warp(new Vector3f(300, 2f, 320f));//this is where the player spawns
    player.getCharacterControl().setGravity(new Vector3f(0f,-600f,0f));
    
    this.app.getRootNode().attachChild(player);
    this.bulletAppState.getPhysicsSpace().add(player.getCharacterControl());
    this.bulletAppState.getPhysicsSpace().add(player);
    this.bulletAppState.getPhysicsSpace().add(player.hitBox());

[/java]
How my player moves:
[java]
/**
* This allows the player to move.
*/
Vector3f camDir = cam.getDirection().clone();
camDir.y = 0;
Vector3f camLeft = cam.getLeft().clone();
camLeft.y = 0;
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());
    }
    characterControl.setWalkDirection(walkDirection.normalize().multLocal(movementSpeed));

[/java]

I want my player to have a gravity, because hes moving kinda fast sometimes he floats and if he hits ramps he goes flying. What factors are affecting my setGravity or am I doing it wrong?

This has been a known bug with the character controls with no clear solution…

Unfortunately you’re likely on your own to come up with a hacky fix such as casting a ray straight down all the time, and maybe mixing that with the isOnGround() method

The real prolbem here is, that a player has legs, wich are way more complex than a normal sphere, (eg no friction high friction based on movement, oriented friction when running downhill ecvt.) Basically you will need to adjust your player always.

What worked for me was to use a raycast and tape the player to the terrain, except if he presses jump.

Can you show how your character control is setup?

It may not be relevant in this case, but most people seem to setup short fat midget balloon people.

@pspeed
Sure, the code: [java]
characterControl = new BetterCharacterControl(1.5f, 5f, 3f);
[/java]

In debug mode:

Perhaps radius is not really radius… because that bounding box looks wrong then… but I read that as a character that is 3 meters wide, 5 meters tall, and yet weighs less than a small house cat.

I guess for a physics engine, these scales are not really arbitrary. And certainly if one scales up dimensions then they should scale up mass by a cube of the scaling. Double size is 8 times mass, etc…

I don’t know if it will help. I’ve never used BetterCharacterControl or Bullet… but from a physics engine I expect these things to normally matter.

1 Like
@pspeed said: Perhaps radius is not really radius... because that bounding box looks wrong then... but I read that as a character that is 3 meters wide, 5 meters tall, and yet weighs less than a small house cat.

I guess for a physics engine, these scales are not really arbitrary. And certainly if one scales up dimensions then they should scale up mass by a cube of the scaling. Double size is 8 times mass, etc…

I don’t know if it will help. I’ve never used BetterCharacterControl or Bullet… but from a physics engine I expect these things to normally matter.

Im going to play around with the values a bit more.

The third value in the BetterCharacterControl is mass according to the docs, I set it to a high a value and it does not affect my character at all. I also tried setting it to a negative value but i just fall throught the floor. Im going to have to look at all options but this floating thing is frustrating me.

What do you mean with “floating”? Is it jumping around? Or is it flying at constant distance from the ground?

@Riccardo said: What do you mean with "floating"? Is it jumping around? Or is it flying at constant distance from the ground?
It has the mass of a feather, if I accelerate off a ramp i will float in the air for a long time before falling back down.

First of all the gravity you set will be replaced with the gravity of the physics space when you add it. Set it afterwards or better, don’t set it at all. As pspeed said, you have a big but very light character, its five meters high but it only weighs 3 kg… So basically like a large balloon.

2 Likes
@normen said: First of all the gravity you set will be replaced with the gravity of the physics space when you add it. Set it afterwards or better, don't set it at all. As pspeed said, you have a big but very light character, its five meters high but it only weighs 3 kg.. So basically like a large balloon.
Setting the gravity after adding to the physics space worked, thank you normen!
@JacobAmaral said: Setting the gravity after adding to the physics space worked, thank you normen!

Why don’t you just raise the mass?

@normen said: Why don't you just raise the mass?
If I raise the mass, then I will have to raise all my npcs masses because colliding with them will have massive force. In addition, I tried the setGravity solution first and it worked.

@JacobAmaral

What exactly did you set the gravity to, to solve this?

My character will still go flying no matter what mass I set him to.

@BigBob said: @JacobAmaral

What exactly did you set the gravity to, to solve this?

My character will still go flying no matter what mass I set him to.

Well, did you set it before or after adding it to the physics space?

@pspeed

I’ve solved this my setting the gravity, but the mass is set in the constructor. So I’m unsure how I would set the mass after I add an unconstructed object to the physics space.

But yes it was setting the gravity to 0, -50, 0 AFTER setting it to the physics space solved it.

>.>

That was my biggest problem for so long, probably about 5 people in the IRC about to thank me for bringing this news there

@BigBob said: @pspeed

I’ve solved this my setting the gravity, but the mass is set in the constructor. So I’m unsure how I would set the mass after I add an unconstructed object to the physics space.

But yes it was setting the gravity to 0, -50, 0 AFTER setting it to the physics space solved it.

>.>

That was my biggest problem for so long, probably about 5 people in the IRC about to thank me for bringing this news there

Thank based normen!

Moral of the story: large bouncy balloons are bouncy.

ie: if you use realistic values for your physics from the beginning then you don’t have to set gravity to Jupiter levels.

@pspeed

My moral is completely the opposite… The mass has no effect, only gravity to jupiter-like levels

@BigBob said: @pspeed

I’ve solved this my setting the gravity, but the mass is set in the constructor. So I’m unsure how I would set the mass after I add an unconstructed object to the physics space.

But yes it was setting the gravity to 0, -50, 0 AFTER setting it to the physics space solved it.

>.>

That was my biggest problem for so long, probably about 5 people in the IRC about to thank me for bringing this news there

You don’t have to set the mass after you add it to the physics space, you can do it in the constructor.