SRL Archive - SRL Archives

CoordsToDegrees(x, y) & MakeCompassCoords(x, y)


PriSoner

2008-02-18 17:13:09

I could find nothing similar within SRL so I wrote these 2 new small but hopefully useful functions.
Do with them as you will…

function CoordsToDegrees(x, y: integer): integer;
Returns the direction of x & y coordinates in degrees relative to the player.

This result can be used with functions like MakeCompass to make an object appear above the player in the play area.

procedure MakeCompassCoords(x, y: integer);
Just a simple combination of CoordsToDegrees and MakeCompass to make an object appear above the player in the play area.

I had noticed that quite often when performing tasks Talking Randoms would occasionally appear behind the object I was working on. This would mean that UpText would not detect the random. I wrote these to ensure that my player was always on the visible side of my target object, thus enabling better detection of randoms.

Updated 21-Feb-2008 to make Degrees Relative to North.
{*******************************************************************************
function CoordsToDegrees(x, y: integer): integer;
By: PriSoner
Description: Returns the direction in degrees (0 to 359) to a set of coordinates
relative to your players position on the screen and North.
(degrees are in reverse to the convention to stay compatible with
MakeCompass etc i.e. 0 = N, 90 = W, 180 = S & 270 = E)
*******************************************************************************}
function CoordsToDegrees(x, y: integer): integer;
var d: double;
begin
x := x - 260;
y := y - 160;
d := ArcTan2(y, x) * (180 div Pi) + 90;
if d < 0 then d := d + 360;
result := round(abs(d - 360) + rs_GetCompassAngleDegrees);
if result >= 360 then result := result - 360;
end;


{*******************************************************************************
procedure MakeCompassCoords(x, y: integer);
By: PriSoner
Description: Moves Compass so that target x & y coordinates are directly north
of your player.
*******************************************************************************}
procedure MakeCompassCoords(x, y: integer);
begin
MakeCompass(IntToStr(CoordsToDegrees(x, y)));
end;

Negaal

2008-02-18 17:20:18

Really nice one(just because i don't know anything about trigonometry:p, I'll try to learn something about it tomorrow:)), so basically you can turn your mainscreen to exactly to look at object?

PriSoner

2008-02-18 17:25:22

yep.. you got it in one :)

I'm not a maths diva either, there were a lot of confusing moments especially when i realised that MakeCompass doesn't accept degrees in the conventional manner. That really threw me! :mad:

mastaraymond

2008-02-19 11:31:52

Has never been made as a seperate function :p.

Lots of functions do use this technique though

PriSoner

2008-02-19 11:47:59

I did look extensivley, but couldn't find anything that did this or uses this technique.

If there are lots of functions that use this technique then it shows it has value as a seperate function.

Edt:
After looking into it further I cannot find any other srl functions that use similar techniques. Can you point me to any that you know of that use this technique as i would be interested to see how they do it.

Wizzup?

2008-02-19 11:48:01

From: mastaraymond
Has never been made as a seperate function :p.

Lots of functions do use this technique though


I made a GetDegree as a function once, FilterColorsPie used to use it.
(RadialWalk is based off FilterColorsPie, you can find the code in the WizzyPlugin.dpr)
However, SCAR does not allow inline functions, so it was just added to FilterColorsPie's main code instead.

PriSoner

2008-02-21 10:05:04

You should have left the function in as it's quite useful to have..

I had a look at your functions after your post (oooh another extension to add to my search filter .dpr :))..

Very good but ended up confusing myself for a while.. I though i180pi was a shorthand way of saying something along the lines of round(180 / pi) or (180 div pi) etc took me a while of experimenting, getting errors and scratching my head to realise it was as simple as you having defined i180pi as a constant! :duh:

Anyhow.. I've made a small change to my function as it did not take into account the current degree that a player was facing. which meant that it worked great if the player was facing north but was unpredictable in other circumstances.

This is now fixed and the function will always return the correct value relative to north regardless of which way your player is currently facing.

Please report back if you have any problems.