Recently in the game development, I encountered a very annoying problem: the train of thought are right, and I want to run it is not a problem, I would like to base on the distance between two points, so that one point has to move to another point of the trend (in fact, I want to use the law of gravity to achieve this effect, according to the size of influence to , but I didn't). But the problem comes .....
This is my first code,
// The effect of mouse hole on mice, the greater the distance, the smaller the effect. cgfloat holemouse=[self gettwopointwithpointone:_mouse.center withpointtwo:_mousehole.center]; Get the distance cgfloat x=(*[self getsignwithsumone:_mousehole.center.x withsumtwo:_ Mouse.center.x]/holemouse) +_mouse. sudu.x; CGFloat y= (*[self getsignwithsumone:_mousehole.center.y withsumtwo:_mouse.center.y]/holemouse) + _mouse. SUDU.Y; _mouse. Sudu=cgpointmake (x, y); // Set Mouse Speed
Among them,[self getsignwithsumone:_mousehole. Center. X withsumtwo:_mouse. Center. X] 's function is to get two parameters minus the positive or negative. The internal implementation is this:
-(CGFloat) Getsignwithsumone: (cgfloat) sum1 withsumtwo: (cgfloat) sum2{ return (sum1-sum2 )/(ABS ((int) (sum1-sum2));}
(abs ((int) (sum1-sum2))) is obtained (SUM1-SUM2), because ABS operates on an int value, so there is a type cast.
In fact, these are the simplest, no problem, including the distance between two points (at least in the eyes of the students that these problems are simple), however, the problem is on this simple question.
[self gettwopointwithpointone:p Inger withpointtwo:_mouse. Center]; This sentence is to find the distance between two points: (obviously, the focus is coming)
< Span class= "S1" > first implementation
-(CGFloat) Gettwopointwithpointone: (cgpoint) point1 withpointtwo: (cgpoint) point2{ // The first coordinate cllocation *current=[[Cllocation alloc] initwithlatitude:point1.x longitude:point1.y]; // a second coordinate Cllocation *before=[[Cllocation alloc] initwithlatitude:point2.x longitude:point2.y]; // Calculate distance Cllocationdistance meters=[current Distancefromlocation:before]; return meters;}
The principle of this function is to convert two points to coordinates and then calculate distances based on coordinates. Logically there is no problem, and actually it doesn't matter how big it is.
However, when one of the point's horizontal ordinate is one of the infinite loop decimals, the function collapses. Figure it out for 0. (It has been proved that I want to cry here).
So, I looked for myself how to find the distance between two points (mathematical formula), yes, is to check, because I forgot (and want to cry.) ), read it and write this function yourself.
-(CGFloat) Gettwopointwithpointone: (cgpoint) point1 withpointtwo: (cgpoint) point2{ return Sqrtf ((point2.x-point1.x) * (point2.x-point1.x) + (POINT2.Y-POINT1.Y) * (point2.y-point1.y));}
sqrt () is the square root, and the return is F loat Type
It's simple and efficient.
In fact, said so much, I would like to spit Groove is: two points between the distance of the first method is to find the Internet, with a bit can I do not more to tube, and then let me find the bug for nearly a week because of the reason is such a simple question. If the beginning of my own writing will not encounter this kind of ghost dozen wall problems, but, I am lazy, with the network of such things, I have almost habitually in the network to find solutions, I did not think about it, very obvious in the trap.
With this blog post, remind yourself, learn not to rely too much on the network, encounter problems to think more about themselves.
iOS development oc-Code Ape's Bad Feelings