0
\$\begingroup\$

so I'm trying to create a MOBA style game, this involves point and click movement.

I'm at the point now where i'm messing about with movement:

https://playground.babylonjs.com/#6R8BFY#11

The problem I'm having is that if I click on the ground (so that the mesh moves towards it). If its not perfectly diagonal or straight, then the x axis or the z axis is reached first.

It kind of goes diagonally for a bit before going straight for the rest of the route.

The code is really simple, And my feeling is the movement logic needs improving. I'm essentially applying a force of 1 or -1 depending on where the mesh is currently until it reaches its point as seen below:

/**
* The movement function, 
*/
var applyImpulse = function(mesh, destination) {    
   var position = mesh.getAbsolutePosition();
   var impulseX = 0;
   var impulseZ = 0;
   if(position.x > destination.x)
   {
     impulseX = -2;
   }
   else
  {
    impulseX = 2;
  }

  if(position.z > destination.z)
  {
     impulseZ = -2;
  }
  else
  {
    impulseZ = 2;
  }
  mesh.lookAt(destination);                        
  mesh.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(impulseX,0, impulseZ));
}

Using physics means I am pushing it rather than setting, but I bet it would have the same problems even without applying force.

Is there a better way to do this. Like using the ray or something? It kinda sucks.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You just need the direction between the two, which can be calculated by subtracting the two Vector3's from each other: var difference = destination.subtract(position) Here is an updated PG: https://playground.babylonjs.com/#6R8BFY#14

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .