Skip to content

Jumping

Oğuz Eroğlu edited this page Aug 1, 2020 · 9 revisions

Manual jumping

jump API of steerables may be used in order to make a steerable jump through given JumpDescriptor. If a jump described by given JumpDescriptor is unachievable, the jump API returns false, it returns true otherwise.

jump API also expects toTakeoffBehavior. This represents the behavior immediately activated via the jump API to make a steerable go until the takeoffPosition of the JumpDescriptor. This may be any sort of behavior which consumes a target position (for instance SeekBehavior).

jump API takes the full control of the steerable. Once the the distance between the steerable and the takeoff point is less than the takeoff satisfaction radius defined by the JumpDescriptor, it then automatically switches to the jumpBehavior of the steerable. This behavior may be set via setJumpBehavior API of the steerable. setJumpCompletionListener API may be used to listen for jump completion. Users may then manually set another behavior to the steerable to keep them moving normally.

Note that Kompute jumps are performed on the axis Y.

// create a World
var world = new Kompute.World(1000, 1000, 1000, 10);
    
// set the gravity of the world
world.setGravity(-900);

// create a Steerable
var steerable = new Kompute.Steerable("steerable1", new Kompute.Vector3D(), new Kompute.Vector3D(10, 10, 10));

// set maxAcceleration, maxSpeed and jumpSpeed of the steerable
steerable.maxAcceleration = 50;
steerable.maxSpeed = 1000;
steerable.jumpSpeed = 500;

// insert steerable into the world
world.insertEntity(steerable);

// create a jump descriptor
var jumpDescriptor = new Kompute.JumpDescriptor({
  takeoffPosition: new Kompute.Vector3D(-70, 25, 25),
  landingPosition: new Kompute.Vector3D(60, 75, 25),
  takeoffPositionSatisfactionRadius: 5,
});

// set the jump behavior of the steerable
steerable.setJumpBehavior(new Kompute.JumpBehavior());

// create a behavior to go towards the takeoff point
var toTakeoffBehavior = new Kompute.SeekBehavior();

// perform jump
steerable.jump(toTakeoffBehavior, jumpDescriptor);

// listen for jump completion
steerable.setJumpCompletionListener(function(){
  console.log("Jump is completed.");
  // use steerable.setBehavior(..) here to continue steerable movement.
});

Canceling a Jump

Steerable#cancelJump API may be used in order to cancel an ongoing jump. Note that this method does nothing and returns false in case there's no ongoing jump. The linear acceleration and the velocity of the steerable are reset after jump cancelation.

// perform jump
steerable.jump(toRunupBehavior, jumpDescriptor);

// cancel jump
steerable.cancelJump();

Jumping via path following

See how to insert a jump descriptor into a Path.

See how to insert a jump descriptor into a Graph.

If a jump descriptor is inserted into a Path or a Graph, any behavior that consumes a Path or a Graph (PathFollowingBehavior, RandomWaypointBehavior and RandomPathBehavior) is capable of automatically triggering a jump when necessary. Users need to set a jump behavior to steerables in order to benefit from this functionality. Behaviors who trigger a jump, also bring back the original behavior of the steerable once the jump is completed. That's why users don't need to manually listen to jump completion and perform this themselves unlike manual jumping.

// create a Path
var path = new Kompute.Path();

// define waypoints
var vec1 = new Kompute.Vector3D(100, 0, 0);
var vec2 = new Kompute.Vector3D(150, 0, 0);
var vec3 = new Kompute.Vector3D(400, 0, 0);

// insert waypoints into the path
path.addWaypoint(vec1);
path.addWaypoint(vec2);
path.addWaypoint(vec3);

// create a jump descriptor
var jumpDescriptor = new Kompute.JumpDescriptor({
  takeoffPosition: vec2,
  landingPosition: vec3,
  takeoffPositionSatisfactionRadius: 5
});

// add jump descriptor to the path
path.addJumpDescriptor(jumpDescriptor);

// create an instance of PathFollowingBehavior
var pathFollowingBehavior = new Kompute.PathFollowingBehavior({ path: path, satisfactionRadius: 20 });

// create a World
var world = new Kompute.World(1000, 1000, 1000, 50);

// set the gravity of the world
world.setGravity(-900);

// create a steerable
var steerable = new Kompute.Steerable("steerable1", vec1, new Kompute.Vector3D(20, 20, 20));

// set max acceleration, max speed and jump speed of the steerable
steerable.maxAcceleration = 100;
steerable.maxSpeed = 700;
steerable.jumpSpeed = 450;

// set the behavior of the steerable
steerable.setBehavior(pathFollowingBehavior);

// set the jump behavior of the steerable
steerable.setJumpBehavior(new Kompute.JumpBehavior());

// insert the steerable into the world
world.insertEntity(steerable);

Clone this wiki locally