Problem with walkDirection: Spatial in air

Hello everyone. I have some problem while using walkDirection() method: spatial flies above the ground, how i can fix it? And one more question : how to rotate collision to viewDirection?

Here is a piece of code:

      camDir.set(cam.getDirection());
      camLeft.set(cam.getLeft());
      pos.set(0,0,0);
  
        if (forw){   pos.addLocal(camDir); }
        if (backw){  pos.addLocal(camDir.negate()); }

        if (left){  pos.addLocal(camLeft);}
        if (right){ pos.addLocal(camLeft.negate());}

        if (jump){if(this.onGround()){ jump(new Vector3f(0, 25, 0)); }}

               pos.multLocal(25f).multLocal(tpf).normalizeLocal();              
               setWalkDirection(pos);
           
                setViewDirection(camLeft.negate());

Extend BetterCharacterControl and override these.

    //Override default collisionshape due to .7 offset.
    @Override
    protected CollisionShape getShape() {
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        float radius = getFinalRadius();
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        float height = getFinalHeight();
        float cylinder_height = height - (2.0f * radius);
        CylinderCollisionShape cylinder = new CylinderCollisionShape(
                new Vector3f(radius, cylinder_height / 2f, radius)/*NB constructor want half extents*/, 1);
        SphereCollisionShape sphere = new SphereCollisionShape(getFinalRadius());
        CompoundCollisionShape compoundCollisionShape = new CompoundCollisionShape();
        compoundCollisionShape.addChildShape(sphere,
                new Vector3f(0,/*sphere half height*/ radius, 0)); // bottom sphere
        compoundCollisionShape.addChildShape(cylinder,
                new Vector3f(0,/*half sphere height*/ (radius) +/*cylinder half height*/ (cylinder_height / 2.f), 0)); // cylinder, on top of the bottom sphere
        compoundCollisionShape.addChildShape(sphere,
                new Vector3f(0,/*half sphere height*/ (radius) +/*cylinder height*/ (cylinder_height), 0)); // top sphere       
        return compoundCollisionShape;
    }
    
    //need to overide because we extended BetterCharacterControl
    @Override
    public PCControl cloneForSpatial(Spatial spatial) {
        try {
            PCControl control = (PCControl) super.clone();
            control.setSpatial(spatial); 
            return control;
        } catch (CloneNotSupportedException ex) {
            throw new RuntimeException("Clone Not Supported", ex);
        }
    }

    //need to override because we extended BetterCharacterControl
    @Override
    public PCControl jmeClone() {
        try {
            return (PCControl) super.clone();
        } catch (CloneNotSupportedException ex) {
            throw new RuntimeException("Clone Not Supported", ex);
        }
    }

Not sure what you are rotating here, the collisionshape or the spatial.

Rotating the spatial will rotate the collisionshape.

1 Like

Normalize will kill all of that, so it has to be after pos and before mult. That would mean you have a speed of 25m/s resulting.

Flying also sounds like you need a .setY(0f) in the Walk Direction.
Setting the ViewDirection on pos.normalize() (not local!) looks into the walk direction.

or he also might forget about “friction”.

or he might need set gravity properly for BetterCharacterControl.

there could be many reasons. about pos, i see he normalizeLocal it seems fine for me, well i agree its wrong having it at the end or why it is there, but should work anyway, just not related to having Spatial in air imo.