cocos ----------点在旋转矩形内实现方法2----

来源:互联网 发布:宋江 知乎 编辑:程序博客网 时间:2024/06/06 16:53


原理:

旋转前(a,b) 旋转后(a',b') 旋转角度为X

a'=acos(t+X)=a*costcosX-a*sintsinX=acosX-bsinX   

b'=bsin(t+X)=b*costsinX+b*sintcosX=asinX+bcosX 

 p'(acosX - bsinX,bsinX + bsinX) :


------------------------代码

    local rectW = self.pic_BeEequal:getSize().width

    local rectH = self.pic_BeEequal:getSize().height

精灵默认锚点是0.5       所以中点就是这个X

    local rectCentPosX = self.pic_BeEequal:getPos().x 

    local rectCentPosY = self.pic_BeEequal:getPos().y 


    local nTempX ,nTempY =0,0

    local deg = (self.pic_BeEequal:getRotation()) * math.pi / 180

-- 还原鼠标点 ,先平移到中点。

    nTempX = rectCentPosX + (touches[1]:getLocation().x - rectCentPosX) * math.cos(deg) - (touches[1]:getLocation().y - rectCentPosY) * math.sin(deg) 
    nTempY = rectCentPosY + (touches[1]:getLocation().x - rectCentPosX) * math.sin(deg) + (touches[1]:getLocation().y - rectCentPosY)* math.cos(deg)


    -- rect
    print("nTempX",nTempX)
    print("nTempY",nTempY)
    dump(self.rect) -- 旋转之前的矩形
    if cc.rectContainsPoint(self.rect,cc.p(nTempX,nTempY)) then
        print("............<<<<<<<<<<<<<<")
    end
    
0 0
原创粉丝点击