我有一个沿着向量(-0.7,-0.3)移动的精灵。我还有另一个点,我有它的坐标——我们称它们为 (xB|yB)。现在,很久以前我学会了计算从向量到点的垂直距离(此页面上的第一个公式http://en.wikipedia.org/wiki/Perpendicular_distance)。但是我试过了,如果我记录它,它会返回一个令人难以置信的高值,即 100% 错误。那么我做错了什么?看看我提供的图像。
incomingVector = (-0.7,-0.3) //这是精灵移动的向量
bh.position是我要计算距离的点
这是代码:
// first I am working out the c Value in the formula in the link given above
CGPoint pointFromVector = CGPointMake(bh.incomingVector.x*theSprite.position.x,bh.incomingVector.y*theSprite.position.y);
float result = pointFromVector.x + pointFromVector.y;
float result2 = (-1)*result;
//now I use the formula
float test = (bh.incomingVector.x * bh.position.x + bh.incomingVector.y * bh.position.y + result2)/sqrt(pow(bh.incomingVector.x, 2)+pow(bh.incomingVector.y, 2));
//the distance has to be positive, so I do the following
if(test < 0){
test *= (-1);
}