Physics collision when setPhysicsLocation

if i use set walking direction collisions work normally
but if use setPhysicsLocation he set location ignore physics collisions

how to check for collision?

This is intended functionality. The setPhysicsLocation() method is supposed to instantly teleport the physics body to a location. This is used in cases such as the end of a round when you need to teleport the players back to their spawn point to be ready for the next round.

If you want to check for collision then you need to use setWalkDirection() to navigate your character to the desired location.

1 Like

how to move character to position if i know position
my test

@Override
  public void simpleUpdate(float tpf) {

    if(moveTo != null){
        
        
        //set loock at mark
        characterControl.setViewDirection(mark.getLocalTranslation());
        //get player position
        Vector3f playerPosition = characterControl.getPhysicsLocation();
        float distance = playerPosition.distance(moveTo);
        
        if(distance > 2f){
            walkDirection.addLocal(moveTo);
            //move character
            interp += (moveSpeed/distance) * tpf;
            //characterControl.setPhysicsLocation(new Vector3f().interpolateLocal(playerPosition, moveTo, interp));
        }else{
            interp =  0.0f;
        }
        
    }```

try this

@Override
  public void simpleUpdate(float tpf) {

    if(moveTo != null){
        
        
        //set loock at mark
        characterControl.setViewDirection(mark.getLocalTranslation());
        //get player position
        Vector3f playerPosition = characterControl.getPhysicsLocation();

        Vector3f direction = playerPosition.subtract(moveTo).normalize();

        characterControl.setWalkDirection(direction);

    }
  }

but character not move

Read this carefully

And This

1 Like

I read and didn’t find anything new

i getting direction

Vector3f direction = playerPosition.subtract(moveTo).normalize();

and setup direction

  characterControl.setWalkDirection(direction);

what is wrong?

I would start by adding printlns() in your code like this to check that your code is working as you expect it to. The code you’re using to move the characterControl doesn’t appear appear to be wrong as far as I can tell, but if moveTo is null then it would explain why your character isn’t moving.

1 Like

loc: (5.070182, 1.0, -2.5265324)
direction: (-5.070182, 1.0, 2.5265324)

try like in example

private Vector3f  walkDirection = new Vector3f(0,0,0);

  @Override
  public void simpleUpdate(float tpf) {
    walkDirection.set(0, 0, 0);
    System.out.println("loc: " + moveTo);
    walkDirection.set(0, 0, 0);
    if(moveTo != null){
        
        //set loock at mark
        characterControl.setViewDirection(mark.getLocalTranslation());
        //get player position
        Vector3f playerPosition = characterControl.getPhysicsLocation();

        Vector3f direction = playerPosition.subtract(moveTo).normalize();
        System.out.println("direction: " + direction);
        walkDirection.addLocal(direction);
        characterControl.setWalkDirection(walkDirection);
        
    }
  }
}

try this

  @Override
  public void simpleUpdate(float tpf) {
    characterControl.setWalkDirection(new Vector3f(10,10,10));
}

set walking direction method not working

Commet off all inside simpleUpdate and use only

characterControl.setWalkDirection(Vector3f.UNIT_Z.mult(0.01f));

try not work

full listing

package tests;

import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.ChaseCamera;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.Ray;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
/**
 * This is the Main Class of your Game. It should boot up your game and do initial initialisation
 * Move your Logic into AppStates or Controls or other java classes
 */
public class Tests extends SimpleApplication   {
private Spatial player;
 private Node shootables;
  private Geometry mark;
  CharacterControl characterControl;
    public static void main(String[] args) {
        Tests app = new Tests();
        app.setShowSettings(false); //Settings dialog not supported on mac
        app.start();
    }
    
  private void initMark() {
    Sphere sphere = new Sphere(30, 30, 0.2f);
    mark = new Geometry("BOOM!", sphere);
    Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mark_mat.setColor("Color", ColorRGBA.Red);
    mark.setMaterial(mark_mat);
  }

  private void initKeys() {
    inputManager.addMapping("Shoot",
      new KeyTrigger(KeyInput.KEY_SPACE), // trigger 1: spacebar
      new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); // trigger 2: left-button click
    inputManager.addListener(actionListener, "Shoot");
  }
      
  final private ActionListener actionListener = new ActionListener() {
    @Override
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Shoot") && !keyPressed) {
        Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

        Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

        direction.subtractLocal(origin).normalizeLocal();

        CollisionResults results = new CollisionResults();

        Ray ray = new Ray( cam.getLocation() , direction );



        shootables.collideWith( ray, results );

        if( results.size() > 0 ){

          CollisionResult closest = results.getClosestCollision();
          // Let's interact - we mark the hit with a red dot.
          Vector3f contactPoint = closest.getContactPoint();
          mark.setLocalTranslation(contactPoint);
          rootNode.attachChild(mark);
          Vector3f  correction = new Vector3f(contactPoint.x,contactPoint.y+1,contactPoint.z);
          
          
          moveTo = correction;
            
        }
      }
    }
  };
  
@Override
  public void simpleInitApp() {
    shootables = new Node("Shootables");
    rootNode.attachChild(shootables);
    
    setUpLight();
    initKeys();
    initMark();
    
    //init physics
    BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    

    //ini player
    player = (Spatial) assetManager.loadModel("Models/char.glb");
    rootNode.attachChild(player);
    
//init player physics

    SphereCollisionShape sphereShape = new SphereCollisionShape(1.2f);
    characterControl = new CharacterControl( sphereShape , 1.2f );
    characterControl.setApplyPhysicsLocal(true);
    player.addControl(characterControl);
    characterControl.setPhysicsLocation(new Vector3f(0,2f,0));
    rootNode.attachChild(player);
    
    //init flor
    Spatial    flor = assetManager.loadModel("Models/flor.glb");
    shootables.attachChild(flor);
    //init flor physics
    CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(flor);
    RigidBodyControl landscape = new RigidBodyControl(sceneShape, 0);
    flor.addControl(landscape);
    bulletAppState.getPhysicsSpace().add(landscape);
    

        
        
    // Disable the default first-person cam!
    flyCam.setEnabled(false);

    // Enable a chase cam
    ChaseCamera chaseCam = new ChaseCamera(cam, player, inputManager);

    //Uncomment this to invert the camera's vertical rotation Axis 
    //chaseCam.setInvertVerticalAxis(true);

    //Uncomment this to invert the camera's horizontal rotation Axis
    //chaseCam.setInvertHorizontalAxis(true);

    //Comment this to disable smooth camera motion
    chaseCam.setSmoothMotion(true);

  }

  private void setUpLight() {

        
        // We add light so we see the scene
        AmbientLight al = new AmbientLight();
        al.setColor(ColorRGBA.White.mult(1.3f));
        rootNode.addLight(al);

        DirectionalLight dl = new DirectionalLight();
        dl.setColor(ColorRGBA.White);
        dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
        rootNode.addLight(dl);
        
        DirectionalLight dl2 = new DirectionalLight();
        dl2.setColor(ColorRGBA.White);
       dl2.setDirection(new Vector3f(100,3,26).normalizeLocal());
       rootNode.addLight(dl2);
       
       
    }
  private Vector3f moveTo = null;
  private float interp = 0.0f;
  private float moveSpeed = 0.05f;
  
  private Vector3f  walkDirection = new Vector3f(0,0,0);

  @Override
  public void simpleUpdate(float tpf) {
    characterControl.setWalkDirection(Vector3f.UNIT_Z.mult(0.01f));    /**  
    walkDirection.set(0, 0, 0);
    System.out.println("loc: " + moveTo);
    walkDirection.set(0, 0, 0);
    if(moveTo != null){
        
        //set loock at mark
        characterControl.setViewDirection(mark.getLocalTranslation());
        //get player position
        Vector3f playerPosition = characterControl.getPhysicsLocation();

        Vector3f direction = playerPosition.subtract(moveTo).normalize();
        System.out.println("direction: " + direction);
        walkDirection.addLocal(direction);
        
    }

   **/
  }
}

Add to you simpleInitApp

spatial.addControl(characterControl);
bulletAppState.getPhysicsSpace().add(characterControl);

You forgot to add

bulletAppState.getPhysicsSpace().add(characterControl);
1 Like

thanks didn’t follow