Raycast to shoot a ray from rotating object?

I would like to shoot a raycast out from an object that rotates.

From the documentation I raycast is like this:

cc.director.getPhysicsManager().rayCast (p1, p2, type);

p1 = this.node.position,
p2 = I need this point to be a distance from the rotating centre. 

This is what I have in a function which I run in start:

   const cannon = this;

   //get position of the object
    const cpos = cannon.barrelNode.convertToWorldSpaceAR(cc.v2(0, 0));

    //get the rotation
    const angle = cannon.barrelNode.rotation;
    
    //get the head of the cannon and add 1000 distance to it
    const h = cannon.barrelNode.height + 1000;

    //set the point for p2
    cpos.x += h * sind(angle);
    cpos.y += h * cosd(angle);

    var p1 = this.node.position;
    var p2 = cpos;

    var results = cc.director.getPhysicsManager().rayCast (p1, p2, cc.RayCastType.All);

Running this function in start, I don’t see anything drawn.

I had following the manual and added the following. What is missing in my code to get the ray to draw on screen ?

  //enable physics
            cc.director.getPhysicsManager().enabled = true;
            cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
            cc.PhysicsManager.DrawBits.e_pairBit |
            cc.PhysicsManager.DrawBits.e_centerOfMassBit |
            cc.PhysicsManager.DrawBits.e_jointBit |
            cc.PhysicsManager.DrawBits.e_shapeBit;

            //thiss stops debug drawing - 0 disable, 1 enable drawing
             cc.director.getPhysicsManager().debugDrawFlags = 1;
             
            
            //set gravity to zero
            cc.director.getPhysicsManager().gravity = cc.v2 ();

documentation reference: https://github.com/cocos-creator/creator-docs/blob/master/en/physics/physics/physics-manager.md

1 Like

Check these out https://github.com/2youyou2/physics-example

1 Like

Very good examples, but the examples doesn’t have this part:
cc.director.getPhysicsManager().enabled = true;
cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
cc.PhysicsManager.DrawBits.e_pairBit |
cc.PhysicsManager.DrawBits.e_centerOfMassBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit;

    //thiss stops debug drawing - 0 disable, 1 enable drawing
     cc.director.getPhysicsManager().debugDrawFlags = 1;

What I’m trying to do is understand what debugDrawFlags does. Any other examples showing how to use it?

Another question from the raycast example, why is the background blue?
I turned all of the nodes to inactive but I still see a blue background.

This script is from the example project. It works find in the sample project. I opened a new project and try using this but it doesn’t work. Nothing happens.
What must I do to make the script work? Is there some editor setting I need to do to make the script work in another project?

//Physics-bound.js
cc.Class({
extends: cc.Component,

properties: {
    size: cc.size(0, 0),
    mouseJoint: true
},

// use this for initialization
onLoad: function () {
    let width   = this.size.width || this.node.width;
    let height  = this.size.height || this.node.height;

    let node = new cc.Node();

    let body = node.addComponent(cc.RigidBody);
    body.type = cc.RigidBodyType.Static;

    if (this.mouseJoint) {
        // add mouse joint
        let joint = node.addComponent(cc.MouseJoint);
        joint.mouseRegion = this.node;    
    }
    
    this._addBound(node, 0, height/2, width, 20);
    this._addBound(node, 0, -height/2, width, 20);
    this._addBound(node, -width/2, 0, 20, height);
    this._addBound(node, width/2, 0, 20, height);

    node.parent = this.node;

},

_addBound (node, x, y, width, height) {
    let collider = node.addComponent(cc.PhysicsBoxCollider);
    collider.offset.x = x;
    collider.offset.y = y;
    collider.size.width = width;
    collider.size.height = height;
}

});

In the sample project, no error. Using this script in another project, I get this error:
CCPhysicsManager.js:219 Uncaught TypeError: Cannot read property ‘init’ of undefined
at CCClass.testPoint (CCPhysicsManager.js:219)
at cc_MouseJoint.onTouchBegan (CCMouseJoint.js:229)
at EventListeners.112.EventListeners.invoke (event-listeners.js:48)
at _doDispatchEvent (event-target.js:70)
at cc_Node.113.proto.dispatchEvent (event-target.js:403)
at TheClass._touchStartHandler [as onTouchBegan] (CCNode.js:159)
at _onTouchEventCallback (CCEventManager.js:493)
at Object._dispatchEventToListeners (CCEventManager.js:628)
at Object._dispatchTouchEvent (CCEventManager.js:550)
at Object.dispatchEvent (CCEventManager.js:1057)
57%20PM