Matt B 122 Report post Posted June 7, 2002 Newbie Alert!! 2D grid system (x,y) How do you compensate for diagonals pixels travelling further than they should? For example a point starting at (0,0) add 10 to the x axis and you get (10,0) or 10 pixels from origin. Move in any direction other than 90,180,270 or 360 degrees and you travel further than you should help 0 Share this post Link to post Share on other sites

christ-heffer 122 Report post Posted June 7, 2002 i think this is rightpsuedo codedistx = object position x - center xdisty = object position y - center ydist=Squareroot((distx squared)+(disty squared))// dist cant be equal to 0 because of future 0 divisionMult=(distance you want it to be away from center) / (distance it already is)x=centerx + (Mult*distx)y=centery + (Mult*disty)newx=centerx + (Multiplier#*Dx#)newy=centery + (Multiplier#*Dy#) 0 Share this post Link to post Share on other sites

CGameProgrammer 640 Report post Posted June 8, 2002 Chris Heffer is right, but I would do it differently. The most important equation, which he used, is the hypotenuse equation:C = sqrt( A*A + B*B )A is the movement along the X direction, B the movement along the Y direction. C is the length of that vector. If you want to move something in a direction like 10 units right and 5 units up (but only 10 units in length), then you do this:Length = sqrt( 10*10 + 5*5 ) = sqrt( 125 ) ~= 11.18Since you want the length to be 10, you multiply the X and Y by (10/11.18):10 * (10/11.18) = 8.945 * (10/11.18) = 4.4710 = sqrt( 8.94*8.94 + 4.97*4.97 ) (more or less, there are rounding errors)Anyway, I don''t like that way, I prefer knowing the angle you want to move and calculating it that way.MovementX = cos(Angle) * LengthMovementY = -sin(Angle) * LengthThe reason you make the sin negative is just that the Y component gets smaller as you go up on computer displays, which is opposite of what you''d do in mathematical space. ~CGameProgrammer( ); 0 Share this post Link to post Share on other sites

Kylotan 10105 Report post Posted June 8, 2002 Turn-based games that use action points or movement points usually just make the diagonals cost 40% more, which is approximately correct. [ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ] 0 Share this post Link to post Share on other sites

Guest Anonymous Poster Report post Posted July 24, 2002 I tried a few examples using the COS and SIN function, and it did not come out with the expected results. Could someone tell me what I may have been doing wrong?Code ( in Delphi):------------------procedure TForm1.Button1Click(Sender: TObject);var x : Extended; y : Extended;begin x := StrToFloat(Edit1.Text); y := StrToFloat(Edit1.Text); X := Cos(X) * 30; Y := (-Sin(y) * 30); Label1.Caption := floatToStr(x); Label2.Caption := FloatToStr(y);end;Results:--------Angle 0 -- X:30 Y:0 (yay! this one is correct)Angle 90 -- X:-13 Y:-26Angle 45 -- X:15 Y:-25Angle 180 -- X:-17 Y:24Any help would be greatly appreciated! 0 Share this post Link to post Share on other sites

granat 122 Report post Posted July 24, 2002 Remember that an angle can be specified using both DEGREE''s and RADIANS. Make sure you supply the cos/sin function with the type it expects.RADIAN = DEGREE * (pi/180.0) (I think) 0 Share this post Link to post Share on other sites

NightWraith 122 Report post Posted July 24, 2002 Dont use sqrt its nicely slooooow.If I need to measure distances instead oftraveledDist = sqrt(dx * dx + dy * dy) I comparetraveledDist = (dx * dx + dy * dy) torequiredDist = (dist * dist) edit for spacingNightWraith[edited by - NightWraith on July 24, 2002 7:19:25 AM] 0 Share this post Link to post Share on other sites