如何镜像CGPoint

时间:2013-03-16 09:53:32

标签: c++ cocos2d-x

我不想将屏幕中心周围的触摸点旋转180度(镜像它)。我试过了ccpRotateByAngle(screenCenter, touchPoint, 180),但它给了我错误的数据。

1 个答案:

答案 0 :(得分:4)

拯救的三角学:

// get your center in some way, here I'm using fixed coordinates 'cuz I'm lazy
CGPoint screenCenter = ccp(240, 160);

// this creates the vector between center and touch location
CGPoint touchPointToCenter = ccpSub(screenCenter, touchPoint);

// just add the vector to screen center and you mirrored it around center
CCPoint mirror = ccpAdd(screenCenter, touchPointToCenter);

例如,假设你的接触点是200,200:

touchPointToCenter: {240, 160} - {200, 200} = {40, -40}
mirror: {240, 160} + {40, -40} = {280, 120}

镜像点是280,120。

注意:我使用的功能来自cocos2d-iphone。我假设它们也存在于cocos2d-x中。它们的名称可能不同,我不确定。您也可以像我在示例中那样“手动”运行计算。