Skip to content

.solveLateralStatic() Playcanvas

Alex edited this page Sep 15, 2020 · 7 revisions

Ballistics.solveLateralStatic(from, target, speed, 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
  • height (number) the maximum height the projectile can reach

Solve the firing arc with a fixed lateral speed. Vertical speed and gravity varies.

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

{
    velocity: pc.Vec3(),
    force: pc.Vec3(),
    gravity: pc.Vec3()
}
  • velocity (pc.Vec3) fire impulse you should apply to the rigidbody
  • 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.