Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Matt B

Representing distance in Pixels

This topic is 5868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
i think this is right
psuedo code
distx = object position x - center x
disty = object position y - center y

dist=Squareroot((distx squared)+(disty squared))
// dist cant be equal to 0 because of future 0 division
Mult=(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#)

Share this post

Link to post
Share on other sites
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.18

Since you want the length to be 10, you multiply the X and Y by (10/11.18):

10 * (10/11.18) = 8.94
5 * (10/11.18) = 4.47

10 = 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) * Length
MovementY = -sin(Angle) * Length

The 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( );

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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);
x : Extended;
y : Extended;
x := StrToFloat(Edit1.Text);
y := StrToFloat(Edit1.Text);
X := Cos(X) * 30;
Y := (-Sin(y) * 30);
Label1.Caption := floatToStr(x);
Label2.Caption := FloatToStr(y);

Angle 0 -- X:30 Y:0 (yay! this one is correct)
Angle 90 -- X:-13 Y:-26
Angle 45 -- X:15 Y:-25
Angle 180 -- X:-17 Y:24

Any help would be greatly appreciated!

Share this post

Link to post
Share on other sites

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)

Share this post

Link to post
Share on other sites
Dont use sqrt its nicely slooooow.

If I need to measure distances instead of

traveledDist = sqrt(dx * dx + dy * dy) 

I compare

traveledDist = (dx * dx + dy * dy) 


requiredDist = (dist * dist) 

edit for spacing


[edited by - NightWraith on July 24, 2002 7:19:25 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!