cocos creator 3D 3D怎么使用关节组件嘞?还是说没有丫,小白太难了

挺急的,有了解的大佬请指教一下,感谢,
我就是想要把一串节点依次连接起来,做成绳子,奈何人太笨,只有在这里求助了

1赞

我上次写的已经包含怎么使用关节了,
下面都是查了源代码1.1.0之后看到的。
要点1:获得物理世界
let cannonWorld = cc.PhysicsSystem.instance.physicsWorld._world
要点2:获得物理世界的节点
let node1:any = (XXXXXX.getComponent(RigidBodyComponent) as any)._body.impl
要点3:给两个物理世界的节点增加关节约束
let joint_st = new CANNON.DistanceConstraint(node1, node2, 1,1000)
要点4:把约束放入物理世界数组
cannonWorld.addConstraint(joint_st)

这个约束不需要的话,一定要注意内存回收:
let cannonWorld = cc.PhysicsSystem.instance.physicsWorld._world
let cts:Array = cannonWorld.constraints
while(cts.length>0){
cannonWorld.removeConstraint(cts[0])
// console.log(cts.length+"!!!")
}

就是官方没开放,就要多看源代码

好滴,谢谢
之前一直做的2D,物理组件用得也不多,现在转3D,害 太难了
感谢

好滴,主要是文档太难啃了 ,谢谢了

哦,如果你写ts的话,还漏了一点,找到你电脑上的CANNON.d.ts (cocos安装目录里面找,或者用everything那样的软件搜)。放到项目目录中temp\declarations\下面,你ts就可以访问CANNON里的东西了,js的话无所谓。而且所有接口都在CANNON.d.ts,看起来比较方便

好勒,感谢

我还想请问一下,我可以拖动这条绳子吗,我目前是拖动一头,就只有一个节点动了,其他节点没有一起动

当然是可以的,一切都是调参数,我这个线只加了距离约束,没有角度,效果是这样的

啊,我就是想要这个效果,那可能是 我哪里没有弄对

麻烦你帮忙看下,可以这么玩吗:sweat_smile:

第一节移动,我是直接设置坐标的,这样操作,在几个物理引擎中表现是不一样的,在CANNON中,会无视约束跟随鼠标,看上去像bug,所以我把第一个节点隐藏了,第二个节点之后都是正常的

下面是我调的参数

createRope(nodes:number,staticTar_:Node,plugTar_:Node){
    this.staticTar = staticTar_
    this.plugTar = plugTar_
    let lastNode:Node = null
    let cannonWorld = cc.PhysicsSystem.instance.physicsWorld._world
    console.log('>>>0>>',cc.PhysicsSystem.instance)
    console.log('>>>>>',cannonWorld)

    let node_ = this.node
    node_.getComponent(RigidBodyComponent).allowSleep = false
    node_.getComponent(RigidBodyComponent).useGravity = false
    node_.setPosition(this.plugTar.getPosition())
    let nodec = node_.getComponent(ColliderComponent)
    nodec.setGroup(PHY_GROUP.Group1)
    nodec.setMask(PHY_GROUP.Group0)
    ///每个节点坐标位移
    let sx:number = this.plugTar.getPosition().x
    let sz:number = this.plugTar.getPosition().z
    let sy:number = this.plugTar.getPosition().y
    let mx:number = (this.staticTar.getPosition().x - this.plugTar.getPosition().x)/nodes
    let mz:number = (this.staticTar.getPosition().z - this.plugTar.getPosition().z)/nodes

    for(let i = 0 ;i<nodes;i++){
        let nod:Node = cc.instantiate(node_);
        this.node.parent.addChild(nod)
        nod.getComponent(RigidBodyComponent).allowSleep = false
        nod.getComponent(RigidBodyComponent).mass = 0.3
        nod.setPosition(new Vec3(sx+mx,sy+1,sz+mz))
        let nodec2 = nod.getComponent(ColliderComponent)
        nodec2.setGroup(PHY_GROUP.Group1)
        nodec2.setMask(PHY_GROUP.Group0)
        //由于cannon.js的bug,第一节有问题,只能作为受力点,所以隐藏,第二节是插头
        let fdis:number = 0.8
        if(i==0){
            this.fllowNode = nod
            this.fllowNode.getComponent(RigidBodyComponent).mass = 100
            fdis = 1.2
            ///表现节点增加插头模型,,插头跟随点
        }
        if(i == nodes -1){
            // nod.getComponent(RigidBodyComponent).mass = 100
            this.tail = nod
            ////与静态物体链接
            let stat_node:any = (this.staticTar.getComponent(RigidBodyComponent) as any)._body.impl
            let tl_node:any = (nod.getComponent(RigidBodyComponent) as any)._body.impl
            let joint_st = new CANNON.DistanceConstraint(stat_node, tl_node, fdis,1000)
            cannonWorld.addConstraint(joint_st)
            // let agt_st = new CANNON.ConeTwistConstraint(stat_node, tl_node,{
            //     pivotA: stat_node.position,
            //     pivotB: tl_node.position,
            //     axisA: new CANNON.Vec3(30,30,30),
            //     axisB: new CANNON.Vec3(30,30,30)
            //     })
            // cannonWorld.addConstraint(agt_st)
        }   
        if(lastNode){
            let cn_lastNode:any = (lastNode.getComponent(RigidBodyComponent) as any)._body.impl
            let cn_node:any = (nod.getComponent(RigidBodyComponent) as any)._body.impl
            let joint = new CANNON.DistanceConstraint(cn_lastNode, cn_node, fdis,1000)
            cannonWorld.addConstraint(joint)
            // let agt = new CANNON.ConeTwistConstraint(cn_lastNode, cn_node,{
            //     pivotA: cn_lastNode.position,
            //     pivotB: cn_node.position,
            //     axisA: new CANNON.Vec3(30,30,30),
            //     axisB: new CANNON.Vec3(30,30,30)
            //     })
            // cannonWorld.addConstraint(agt)
        }else{
            this.head =  node_
            this.head.getComponent(RigidBodyComponent).mass = 1
            let cn_lastNode2:any = (node_.getComponent(RigidBodyComponent) as any)._body.impl
            let cn_node2:any = (nod.getComponent(RigidBodyComponent) as any)._body.impl
            let joint_ = new CANNON.DistanceConstraint(cn_lastNode2, cn_node2, 0,1000)  ///反正会隐藏,所以关节距离0
            cannonWorld.addConstraint(joint_)
            // let agt_ = new CANNON.ConeTwistConstraint(cn_lastNode2, cn_node2,{
            //     pivotA: cn_lastNode2.position,
            //     pivotB: cn_node2.position,
            //     axisA: new CANNON.Vec3(30,30,30),
            //     axisB: new CANNON.Vec3(30,30,30)
            //     })
            // cannonWorld.addConstraint(agt_)
        }
        lastNode = nod
    }
    node_.getComponent(ModelComponent).enabled = false
    return this.head
}
2赞

好滴,我研究研究,谢谢

实现了,谢谢大佬

大佬,请教你一下,我这边点击了一个3D物体,想要跟随鼠标移动,就是你那个插头的那个效果, 但是我这边位置有一点偏差,是我适配的问题吗

mark123

使用大量约束器,在ios微信小游戏里会很卡。慎重选择。

那有其他的方法吗,

用绳子关节这个怎么做出绳子的形状啊,现在是一个一个的小球组成的

节点连接 不应该是这样吗? 那应该是那样的?