const
PatternX := 160;
PatternY := 80;
procedure GetCoord;
begin
mX := MouseX div PatternX;
mY := (MouseY div PatternY) * 2;
DeltaMouseX := mX * PatternX - (mY AND 1) * PatternY;
DeltaMouseY := mY * (PatternY div 2);
end;
[edited by - ra001 on March 14, 2003 4:32:37 AM]
screen coord to ISO map
Hi.
I find some examples how to get screen (mouse) coordinates to isometric map tile coordinate, but always i get not so "fine" result ((
Maybe some get sample in Delphi (optional Powerdraw) how I can calculate that coordinates.
My today code is
Here is how I got it to work.. spend quit some time geting it to work.. But I finally got something that works (for me anyway)
m2b = Mouse To Board (Board = cell)
b2m = Board To Mouse
m2b = Mouse To Board (Board = cell)
b2m = Board To Mouse
Procedure m2b(mx,my:Integer; Out FeltX,FeltY:Integer);var xx,yy:Integer; offset:Single;Begin //Find ca. feltet (kantet) FeltX:=mx div CellWidth; FeltY:=(my div CellHeight)*2; xx:=FeltX * CellWidth + HalfCellWidth; yy:=FeltY * (HalfCellHeight) + HalfCellHeight; offset:=(HalfCellHeight) / (HalfCellWidth); //Find det præcise felt If mx > xx then begin if my > yy then begin if my > yy+Round(offset * (mx-xx-HalfCellWidth)*-1) then Inc(FeltY) end else begin if my < yy-Round(offset * (mx-xx-HalfCellWidth)*-1) then Dec(FeltY) end; end else begin if my > yy then begin if my > yy-Round(offset * (xx-mx-HalfCellWidth)) then begin Inc(FeltY);dec(FeltX);end; end else begin if my < yy+Round(offset * (xx-mx-HalfCellWidth)) then begin Dec(FeltY);dec(FeltX);end; end; end;end;Procedure b2m(FeltX,FeltY:Integer; Out wX,wY:Integer);Begin wX:=(FeltX * CellWidth) + ((FeltY mod 2) * (HalfCellWidth)); wY:=(FeltY * (HalfCellHeight)); wX:=wX+HalfCellWidth; wY:=wY+HalfCellHeight;end;
This is the way I did it:
function TDXCGIsoMap.PointToCell(X,Y : Integer): TPoint;
Var
BX,BY,CX,CY,DX,DY,AD, BD : Integer;
begin
X := X + MapLeft+32;
Y := Y + MapTop;
Y := Y - 192;
BX := X div 128;
BY := (Y div 64)*2;
DX := ABS(((BX * 128) + 64) - X);
DY := ABS((((BY div 2) * 64) + 32) - Y);
AD := DX + 2 * DY;
X := X - 64;
Y := Y + 32;
CX := X div 128;
CY := (Y div 64) * 2 - 1;
DX := ABS(((CX * 128) + 64) - X);
DY := ABS((((CY div 2) * 64) + 96) - Y);
BD := DX + 2 * DY;
If AD < BD then
Begin
Result := Point(BX,BY);
End
Else
Begin
Result := Point(CX,CY);
End;
end;
A couple of pointers. My min top point of the map is at 192, my min left is 32. Isometric squares are 128x64.
Why those exact figures work I dont know but they do
The theory is as follows.
1. For any point there are two possible squares - find these two squares
2. For each square check if the point is in a corner - if it is then this is not the square selected.
3. Remember to incluse the offset of the odd numbered rows
Hope it helps.
function TDXCGIsoMap.PointToCell(X,Y : Integer): TPoint;
Var
BX,BY,CX,CY,DX,DY,AD, BD : Integer;
begin
X := X + MapLeft+32;
Y := Y + MapTop;
Y := Y - 192;
BX := X div 128;
BY := (Y div 64)*2;
DX := ABS(((BX * 128) + 64) - X);
DY := ABS((((BY div 2) * 64) + 32) - Y);
AD := DX + 2 * DY;
X := X - 64;
Y := Y + 32;
CX := X div 128;
CY := (Y div 64) * 2 - 1;
DX := ABS(((CX * 128) + 64) - X);
DY := ABS((((CY div 2) * 64) + 96) - Y);
BD := DX + 2 * DY;
If AD < BD then
Begin
Result := Point(BX,BY);
End
Else
Begin
Result := Point(CX,CY);
End;
end;
A couple of pointers. My min top point of the map is at 192, my min left is 32. Isometric squares are 128x64.
Why those exact figures work I dont know but they do
The theory is as follows.
1. For any point there are two possible squares - find these two squares
2. For each square check if the point is in a corner - if it is then this is not the square selected.
3. Remember to incluse the offset of the odd numbered rows
Hope it helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement