Homing Missiles

I’m currently implementing homing missiles. I’m using the proportional pursuit algorithm and it’s working fine. It homes in on the player just as it should.

But, it currently doesn’t check to see if the player is visible from the missile. For that I need to do a raycast.
So far as I can tell, the way to do a raycast is to do:

app.systems.rigidbody.rayCastFirst(to, from, callback)

I’m wondering why this is a callback. Does it really run asynchronously? I’d much prefer it to be:

data = app.systems.rigidbody.rayCastFirst(to, from)

To try and squish the callback into the homing missile algorithm is quite inconvenient because I cannot pass the homing missile object (which I’ve encapsulated) into the callback. Because there can be multiple homing missiles, I cannot just store it globally either.
How can I implement a ‘line of sight’ raycast in Playcanvas?

Looking at the FPS character help, they make the function:

_checkGround: function () {
    var self = this;
    var pos = this.entity.getPosition();
    rayEnd.add2(pos, groundCheckRay);
    self.onGround = false;

    // Fire a ray straight down to just below the bottom of the rigid body,
    // if it hits something then the character is standing on something.
    app.systems.rigidbody.raycastFirst(pos, rayEnd, function (result) {
        self.onGround = true;

Which seems like the raycast does happen instantaneously. So again, why is this in a callback?

I think raycastFirst is asynchronous because it follows the Ammo/Bullet API underneath which contains a callback object. But you’re right, it appears that it is actually running synchronously so we could probably return the result object from the method.

I’ve created an issue: https://github.com/playcanvas/engine/issues/490