Spine的BoundingBoxAttachment碰撞检测

本文介绍了在CocosCreator2.3.4中,如何在游戏代码中操作PhysicsPolygonCollider,以及如何在没有原生`computeWorldVertices`接口时,自定义复写引擎方法来处理骨骼动画挂件的顶点位置计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引擎版本 —— cocos creator 2.3.4

游戏代码:

 //优先初始化的时候,获取到cc.PhysicsPolygonCollider
  this._poly = this.dragonFooAni.node.getComponent(cc.PhysicsPolygonCollider);
  
 //下面的修改顶点位置的方法可以在update里面去执行
 //获取骨骼动画上的挂件         str1:动画的父节点 str2:动画
 let attachment = this.skAni.getAttachment('pengzhuang2', 'pengzhuang3');
 //获取骨骼
 let slot = this.skAni.findSlot('pengzhuang2');
 //获取顶点的位置
 let arr = [];
 attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, arr, 0, 2)
 //console.log("多边形挂件:", attachment, "多边形顶点:", arr.toString());
 for (let i = 0; i < 4; i++) {
    this._poly.points[i].x = arr[i * 2];
    this._poly.points[i].y = arr[i * 2 + 1];
}
this._poly.apply();

在原生环境下没有computeWorldVertices接口,我们需要复写引擎中的computeWorldVertices方法

_computeWorldVertices = function (slot, start, count, worldVertices, offset, stride) {
        count = offset + (count >> 1) * stride;
        let deformArray = slot.deform;
        let vertices;
        if (deformArray.length > 0)
            vertices = deformArray;

        if (!vertices)
            return;

        let bone = slot.bone;
        let x = bone.worldX;
        let y = bone.worldY;
        let a = bone.a, b = bone.b, c = bone.c, d = bone.d;
        for (let v_1 = start, w = offset; w < count; v_1 += 2, w += stride) {
            let vx = vertices[v_1], vy = vertices[v_1 + 1];
            worldVertices[w] = vx * a + vy * b + x;
            worldVertices[w + 1] = vx * c + vy * d + y;
        }
        return;
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值