Jump to content

pathRoute - movement problem


photo

Recommended Posts

This is me being stupid, not a unigine issue but I cannot for the life of me work out how to move a character along a path. Can someone take a look and see if I have done something sill here?

#include <core/unigine.h>



vec3 offset = vec3(0.0f, 0.0f, 1.0f);
Vec3 start_pos = Vec3(-0.0f, 0.0f,1.0f) + offset;
Vec3 target_pos = Vec3( 20.0f,-20.0f,1.0f) + offset;

Node agent;


int init() {
  PlayerSpectator camera = new PlayerSpectator();
  camera.setPosition(Vec3(2.0f,0.0f,1.5f));
  camera.setDirection(Vec3(-1.0f,0.0f,-0.5f));
  engine.game.setPlayer(camera);


  NavigationSector navigation = add_editor(new NavigationSector(vec3(1024.0f,1024.0f,9.0f)));
  navigation.setWorldTransform(translate(Vec3(0.0f,0.0f,5.0f) + offset));

  ObstacleBox box = add_editor(new ObstacleBox(vec3(10.0f,10.0f, 3.0f)));


  agent = add_editor(node_load("mup4b/meshes/agent.node"));

  engine.console.run("show_visualizer 1");


  return 1;
}

int shutdown() {
        return 1;
}

int update() {

  PathRoute path = new PathRoute(2.0f);

  path.create2D(start_pos, target_pos);

  path.renderVisualizer(vec4_one);

  if (path.isReady()) {
    vec3 current_pos = agent.getPosition();
    vec3 direction_to_face = path.getPoint(1) - path.getPoint(0);

    vec3 move = lerp(agent.getPosition(), path.getPoint(1), 2.0f * engine.game.getIFps());


    mat4 mat = setTo(current_pos * move,
                     direction_to_face,
                     vec3(0.0f, 0.0f, 1.0f));
    agent.setWorldTransform(mat * rotateX(-90) * rotateZ(180));
  } else {
    log.message("Path not ready\n");
  }


  return 1;
}

Node add_editor(Node node) {
  engine.editor.addNode(node);
  return node_remove(node);
}
void remove_editor(Node node) {
  engine.editor.removeNode(node);
}

The path appears correctly, and the character is facing in the right direction but it does not move along the path and I am stumped as to why. 

Link to comment

Hey Angus,

 

Try not to create new PathRoute instance every update call. ;)

 

 

Ok - I've updated the code so it now runs the below code in a thread, but it does not seem to make any difference. My code that runs in a thread is below. 

void do_movement() {

  sleep(5.0f);

  PathRoute path = new PathRoute(2.0f);

  path.create2D(start_pos, target_pos,1);


  while(1) {

    path.renderVisualizer(vec4_one);

    if (path.isReady()) {
      vec3 current_pos = agent.getPosition();
      vec3 direction_to_face = path.getPoint(1) - path.getPoint(0);

      vec3 move = lerp(agent.getPosition(), path.getPoint(1), 16.0f * engine.game.getIFps());



      mat4 mat = setTo(current_pos * move,
                       direction_to_face,
                       vec3(0.0f, 0.0f, 1.0f));
      agent.setWorldTransform(mat * rotateX(-90) * rotateZ(180));
      wait;
    } else {
      log.message("Path not ready\n");
      sleep(1.0f);
    }
  }

}

Link to comment

Hi Bob. I've attached a RAR of the data directory of the project. 

 

Thanks for looking at this. I'm pretty sure it's just me being dumb but for the life of me I can't figure out where.

 

mup4b.rar

Link to comment
  • 1 month later...

Hi Angus­,

 

We are deeply sorry for the late reply.

 

You are forgotten to check if route is reached (PathRoute::isReached), if false - call path.create2D again. And you need to call create2D from time to time to update it. (try every frame for start, then to improve performance reduce the number of calls).

 

Thanks!

Link to comment
×
×
  • Create New...