In game development, linear walk trajectory calculation is used. The algorithm used is DDA line painting!
Because my previous game version was in C language! Therefore, the modified method has the pointer shape, as shown below:
/*** This method can move a vertex (x1, y1) to a vertex (x2, y2) by step speed */static bool autoMove (int * x1, int * y1, int x2, int y2, int speed );
The reference function of C ++ is defined as follows:
static bool autoMove( int& x1, int& y1, int x2, int y2, int speed );
It seems that there is no difference between pointer form and reference form, but it is much more convenient to use the reference form!
Next, let's look at the implementation of the method.
Pointer format in C Language
Bool Tool: autoMove (int * x1, int * y1, int x2, int y2, int speed) {int k; int x_old = * x1, y_old = * y1, x_new, y_new, step_time = 0; double x, y, deltx, delty, length; if (* x1 = x2 & * y1 = y2) {return TRUE ;} if (abs (x2-* x1)> = abs (y2-* y1) length = abs (x2-* x1); elselength = abs (y2-* y1 ); if (length <1) {return TRUE;} deltx = (x2-* x1)/length; delty = (y2-* y1)/length; x = * x1; y = * y1; k = 1; while (k <= length) {x = x + deltx; y = y + delty; x_new = (Int) x; y_new = (int) y; if (x_old! = X_new) | (y_old! = Y_new) {x_old = x_new; y_old = y_new; step_time ++;} if (step_time> = speed) {* x1 = x; * y1 = y; return FALSE ;} k = k + 1;} // change the starting coordinate * x1 = x; * y1 = y; return FALSE ;}
Let's look at the implementation of the C ++ reference method.
Bool Tool: autoMove (int & x1, int & y1, int x2, int y2, int speed) {int k; int x_old = x1, y_old = y1, x_new, y_new, step_time = 0; double x, y, deltx, delty, length; if (x1 = x2 & y1 = y2) {return TRUE;} if (abs (x2-x1)> = abs (y2-y1) length = abs (x2-x1); elselength = abs (y2-y1); if (length <1) {return TRUE;} deltx = (x2-x1)/length; delty = (y2-y1)/length; x = x1; y = y1; k = 1; while (k <= length) {x = x + deltx; y = y + delty; x_new = (int) x; y_new = (Int) y; if (x_old! = X_new) | (y_old! = Y_new) {x_old = x_new; y_old = y_new; step_time ++;} if (step_time> = speed) {x1 = x; y1 = y; return FALSE ;} k = k + 1;} // change the start coordinate x1 = x; y1 = y; return FALSE ;}
When using the pointer form, you need to add * before each variable, which affects writing code and reading code. The reference form seems more comfortable!
Let's take a look at the usage of the pointer format.
The following code is used to draw each frame of a hero:
If (isAutoMove) {int x = heroPos. x; int y = heroPos. y; if (x! = Map_destination_x | y! = Map_destination_y) {bool isAutoMoveOk = Tool: autoMove (& x, & y, map_destination_x, map_destination_y, speed); setActID (act_move); setMoveToPos (x, y ); if (isAutoMoveOk) // reach the destination {setAutoMove (false); setActID (act_wait) ;}} else {setAutoMove (false); setActID (act_wait );} // setDir (dir );}
Pass the coordinates of the hero's position in the form of an address to the automatic movement method to calculate the coordinates after the frame moves speed, and then change the coordinates of the hero to the coordinates after calculation, the above method setMoveToPos (x, y );
Next, let's look at the C ++ reference format
If (isAutoMove) {int x = heroPos. x; int y = heroPos. y; if (x! = Map_destination_x | y! = Map_destination_y) {bool isAutoMoveOk = Tool: autoMove (x, y, map_destination_x, map_destination_y, speed); setActID (act_move); setMoveToPos (x, y); if (isAutoMoveOk) // reach the destination {setAutoMove (false); setActID (act_wait) ;}} else {setAutoMove (false); setActID (act_wait );} // setDir (dir );}
As shown above, there is almost no need to change the pointer form to the reference form, just change & x, & y to x, y.
Some may see that, since it is a modified hero's position coordinate, why should we set a coordinate? (SetMoveToPos method)
Of course, it can be removed, because the pointer and reference all modify the input function autoMove () parameter!
I have other operations here!
Okay. To sum up, you have no choice but to use pointers in the C language! C ++ can select references. Do your best to use references! Great benefits! No need to detail tables!
This article is from the "keycode window" blog, please be sure to keep this source http://kome2000.blog.51cto.com/969562/1287080