• ### Popular Now

• 11
• 14
• 12
• 10
• 11

#### Archived

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

# Representing distance in Pixels

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

## 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 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 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 on other sites
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:-26
Angle 45 -- X:15 Y:-25
Angle 180 -- X:-17 Y:24

Any help would be greatly appreciated!

##### 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.

(I think)

##### 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)

to

requiredDist = (dist * dist)

edit for spacing

NightWraith

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