Skip to content

.solveLateralMoving() Playcanvas

Alex edited this page Sep 15, 2020 · 5 revisions

Ballistics.solveLateralMoving(from, target, speed, velocity, height)

  • from (pc.Vec3) position from which the projectile is fired
  • target (pc.Vec3) position of the target that projectile is trying to hit
  • speed (number) projectile speed
  • velocity (pc.Vec3) target velocity
  • height (number) the maximum height the projectile can reach

Solve the firing arc with a fixed lateral speed at a moving target.

Returns a firing solution or null if none found for the given inputs

{
    velocity: pc.Vec3(),
    hit: pc.Vec3()
    force: pc.Vec3(),
    gravity: pc.Vec3()
}
  • velocity (pc.Vec3) fire impulse you should apply to the rigidbody
  • hit (pc.Vec3) position in world space, where the projectile will hit the moving target
  • force (pc.Vec3) a constant force that needs to be applied on projectile. Don't use it, if you change gravity.
  • gravity (pc.Vec3) a gravity force needed for the projectile to hit the target. Don't use it, if you use force.

Since you can control the maximum height of the trajectory, this enables a visually pleasing arc. However, this control comes at a cost - we must compensate with additional force on the projectile. The library will provide all the needed values. You have 2 options:

  • Apply a constant force on the projectile by using the force property of the solution. For example:
this.entity.rigidbody.applyForce(solution.force);
  • Another option is to change the world gravity to the gravity solution:
this.app.systems.rigidbody.gravity.y = -solution.gravity;

Both options will make your projectile hit the target. The choice which one to use will simply depend on the design of your application.