[Solved] Problem with physics - BetterCharacterControl : Character is flicking

Hi friends

I ran to an strange issue. Issue illustrated in this video :

Anyone had such issue ?
Any hint on what may cause this problem ? Any hint is much appreciated. :slight_smile:

  • I do not call warp() method on BetterCharacterControl in update loop.
  • Tested both with native bullet and jbullet.

I am putting relevant code here :

this is a control, and in update loop I am calling setViewDirection(direction);
which is on my GameCharacterControl.

 @Override
    protected void controlUpdate(float tpf) {
        this.tpf=tpf;
        viewDirectionUpdateInterval += tpf;
        if (viewDirectionUpdateInterval > 1) {
            viewDirectionUpdateInterval = 0;
            for (int i = 0; i < grid.getQuantity(); i++) {

                if (controls[i] != null) {
                    Vector3f gridTranslation = grid.getChild(i).getWorldTranslation();
                    Vector3f spatialTranslation = controls[i].getWorldTranslation();
                    gridTranslation.y = 0;
                    spatialTranslation.y = 0;
                    Vector3f viewDirection = gridTranslation.subtract(spatialTranslation);
                    controls[i].setViewDirection(new Vector3f(viewDirection));
                }

            }
        }

    }

GameCharcterControl class:

here is where i am setting up BetterCharacterControl:

 @Override
    public void setSpatial(Spatial spatial) {
    super.setSpatial(spatial);
    // Example:
    if (spatial != null){
      
        character=spatial;
    characterControl = new BetterCharacterControl(2f,3.5f,75);//TODO : Get measures as user data.
    characterControl.setJumpForce(new Vector3f(0, 300,0 ));//TODO : Get measures as user data.
        characterControl.setGravity(normalGravity);
    character.addControl(characterControl);
        
        characterAnimControl=new CharacterAnimControl();
        character.addControl(characterAnimControl);
        
    }

And setViewDirection() method on my GameCharacterControl :

 public void setViewDirection(Vector3f viewDirection){
           // this.viewDirection.set(viewDirection);
            viewDirection.y=0;
            viewDirection.normalizeLocal();
            //BetterCharacterControl
            characterControl.setViewDirection(viewDirection);
            
            Vector3f modelForwardDir = character.getWorldRotation().mult(Vector3f.UNIT_Z);
            Vector3f modelLeftDir = character.getWorldRotation().mult(Vector3f.UNIT_X);

            // WalkDirection is global!
            // You *can* make your character fly with this.
           
            walkDirection.set(0, 0, 0);
            if (left) {
                walkDirection.addLocal(modelLeftDir.mult(walkSpeed+tpf));
            } else if (right) {
                walkDirection.addLocal(modelLeftDir.negate().multLocal(walkSpeed+tpf));
            }
            if (forward) {
                walkDirection.addLocal(modelForwardDir.mult(walkSpeed+tpf));
            } else if (backward) {
                walkDirection.addLocal(modelForwardDir.negate().multLocal(walkSpeed+tpf));
            }
            //BetterCharacterControl
            characterControl.setWalkDirection(walkDirection);
        }

You are modifying the actual y coordinate of the child’s world translation. That seems like probably not what you want. You do the same for the control’s world translation but maybe that’s ok, I didn’t look.

I suspect it would be better to just zero out the y of viewDirection after subtracting.

Man You Are Awesome :grinning: You Saved Me Again
Thanks so much

Yes , the problem was because me zeroing spatialTranslation.y = 0;

I thought when calling getWorldTranslation(); it returns a clone :chimpanzee_facepalm:
but a look at source made it clear that it returns the actual reference.

:ok_hand:

Returning a clone would be wasteful if you just want to look at the values.

1 Like