SRL Archive - General

Compass Math


Freddy1990

2008-01-15 10:53:51

Well, I'm going to kick the compass and minimap angle functions out of SCAR soon because I feel that it's more convenient for the scripters to make them themselves so they can also be fast and easily updated. And I'm confident that they will do a great job for it, so I'm releasing the math algorithm for my compass function. (Not saying it's better than what people are currently using, but it might offer some insight…

dX1 := mx - tx;
dY1 := my - ty;
dX2 := mx - pp.x;
dY2 := my - pp.y;
tmp := ((dX1 * dX2) + (dY1 * dY2)) / (Sqrt(Sqr(dX1) + Sqr(dY1)) * Sqrt(Sqr(dX2) + Sqr(dY2)));
Result := Abs(ArcCos(tmp) - Pi);
if pp.x > mx then Result := 2 * Pi - Result;

mx and my are the coordinates of the center point of the compass.
tx and ty are the coordinates of the middle, top point of the compass. (The highest point on the compass, at the same x coordinate as the center)
pp is the point at which the South indicator is located.

As some might've noticed, my algorithm is based on the law of cosines.

(Note that this is for calculating the radians)
(You might also have noticed that "dX1 := mx - tx;" will always be 0, but I just made it that way in case I'd ever need to change it)

if you leave out dX1 you'd get:
dY1 := my - ty;
dX2 := mx - pp.x;
dY2 := my - pp.y;
tmp := (dY1 * dY2) / (dY1 * Sqrt(Sqr(dX2) + Sqr(dY2)));
Result := Abs(ArcCos(tmp) - Pi);
if pp.x > mx then Result := 2 * Pi - Result;

Wizzup?

2008-01-15 10:55:10

Always been wondering how you did. Nice, using cosine laws. :)

TheSantaMan

2008-01-15 21:10:12

lol, don't even know what that means. Just guess i'm not smart enough :p

mickaliscious

2008-01-15 21:27:42

From: jesus45
lol, don't even know what that means. Just guess i'm not smart enough :p


I understand it, but hopefully SRL will just take over for all the rs_GetCompassAngle and other functions using this math… It will save me a few headaches.


…So we would have to input pp since that changes? Thats the only problem I'm having with trying to fix the MakeCompass() error due to the rs_GetCompassAngle begin 'broken'.

mastaraymond

2008-01-16 03:05:07

Advanced.

mickaliscious

2008-01-16 07:33:19

…I figured out a way for it to work but its not 100% accurate. Use a dtm of the N and reflect its coords across the centerpoint to find the South indicator location.

Freddy1990

2008-01-16 10:04:27

From: mickaliscious
…I figured out a way for it to work but its not 100% accurate. Use a dtm of the N and reflect its coords across the centerpoint to find the South indicator location.


I did a similar thing when I first developed the function, but it caused it to mess up in certain situation, it used findcolor rather than a dtm though. In the manual I thank ruler and geno for parts of the compass function, that's actually the part to get that point, it's uses a piece of code converted from ibot, since ibot seemed to use skipboxes which I had written myself once and later implemented in SCAR, so it was easy to modify to use my own skipboxfunctions. But there's many ways to do it really…

WT-Fakawi

2008-01-16 15:15:31

Good of you to skip this from SCAR, and thanks for sharing. You are right, it should not be there in the first place. SCAR is so much more versatile…

R0b0t1

2008-01-16 17:07:44

…


My head almost blew up. Back to wikipedia.

Kasi

2008-01-16 17:57:22

wow this is the fist time iv seen freddy post

NiCbaZ

2008-01-16 19:15:37

that post was a pure spam post that post had nothing to do with the topic,


Freddy THis is very interesting, will it be in the next scar.?

WT-Fakawi

2008-01-16 19:53:50

From: nicbaz
that post was a pure spam post that post had nothing to do with the topic,


Freddy THis is very interesting, will it be in the next scar.?



"Quote of the Day"

Town

2008-01-16 20:08:07

From: WT-Fakawi
"Quote of the Day"


Epic.

R0b0t1

2008-01-16 21:28:13

Fakawi: Epic Win.

Nicbaz: Epic Fail.

R0b0t1: Head blown up by super maths, he now goes to wikipedia to make sense of the situation.

mickaliscious

2008-01-16 21:47:46

From: R0b0t1
Fakawi: Epic Win.

Nicbaz: Epic Fail.

R0b0t1: Head blown up by super maths, he now goes to wikipedia to make sense of the situation.


Its just trigonometry dude. Theres no super maths there.

gamer 5

2008-01-16 22:10:52

It will take a while to get used to :D

MakeCompass(" ") is like so easy!:p

R0b0t1

2008-01-16 22:42:33

if (Trigonometry.Length >= 10) { cout "Dubya Tee Eff Mate?"; };

Markus

2008-01-18 05:34:37

From: WT-Fakawi
"Quote of the Day"


Amen

[/offtopic]

So, can I use it like this? Get north indicator, use skipboxes for west and east, then find south and calculate.

TheGuyWhoGotOn

2008-01-20 12:30:50

Well I'm trying this now I feel like I'm doing something wrong…


function GetCompassDegrees: extended;
var dx1, dx2, dy1, dy2: integer;
tmp: extended;
pp: TPoint;
begin
pp.x := 557;
pp.y := 24;
dX1 := 543 - 543;
dY1 := 24 - 8;
dX2 := 543 - pp.x;
dY2 := 24 - pp.y;
tmp := ((dX1 * dX2) + (dY1 * dY2)) / (Sqrt(Sqr(dX1) + Sqr(dY1)) * Sqrt(Sqr(dX2) + Sqr(dY2)));
Result := Abs(ArcCos(tmp) - Pi);
if pp.x > 544 then Result := 2 * Pi - Result;
end;

Now that results in 4.71238898038469 so is there something I have to do after? I was thinking it would be more around 270 degrees.

What am I doing wrong?

Edit: Ok after I tried this:


function GetCompassRadians: extended;
var dx2, dy1, dy2: integer;
tmp: extended;
pp: TPoint;
begin
pp.x := 557;
pp.y := 24;
dY1 := 24 - 8;
dX2 := 543 - pp.x;
dY2 := 24 - pp.y;
tmp := (dY1 * dY2) / (dY1 * Sqrt(Sqr(dX2) + Sqr(dY2)));
Result := Abs(ArcCos(tmp) - Pi);
if pp.x > 543 then Result := 2 * Pi - Result;
end;

However guess what? I got 4.71238898038469…

Wizzup?

2008-01-20 14:31:36

Doh. You use nothing but constants. ;)

Freddy1990

2008-01-20 14:46:11

TGWGO, you need to convert it to degrees

function GetCompassDegrees: Extended;
begin
Result := Degrees(GetCompassRadians);
end;

It was originally designed to work with degrees, but I changed it to radians


From: mickaliscious
Its just trigonometry dude. Theres no super maths there.

I have to agree with that

TheGuyWhoGotOn

2008-01-20 14:54:24

From: Freddy1990
TGWGO, you need to convert it to degrees

function GetCompassDegrees: Extended;
begin
Result := Degrees(GetCompassRadians);
end;

It was originally designed to work with degrees, but I changed it to radians



I have to agree with that


Thanks so I'm doing nothing wrong? Awesome…

@Wizzup?: I think only the TPoints need to be changed so I didn't make them constants.

FTW works now…Now to make it find the south indicator…Not 100% sure of how to go about this…

SKy Scripter

2008-01-20 15:03:31

I just did


{-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function GetCompassAngleDegrees: extended;
By: SKy Scripter
Description: Gets The Mini Map Degrees Ranging From 0 - 360
Note: I Do Know That In ArcTan2, I Have The X Where The Y Should be.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
function GetCompassAngleDegrees: extended;
var
mx, my: Integer;
P: TPointArray;
begin
FindColorsTolerance(P, 1911089, 520, 5, 560, 45, 0);
TPAAverage(P, mx, my);
if (Mx <> 0) then
Result := FixD( -Degrees(ArcTan2(543 - mx, 24 - my)) );
end;

Toterache

2008-01-21 09:09:42

Urgh, my head is spinning, but i think a few more hours and I'll be able to understand this thing ( trigonometry is something that would have been taught next year, but i switched back from the USA to Germany, and here all we are learning are quadratic equations :mad: {not sure how to spell it in english}, so trigonometry is still an unkown area for me {I'm only a 9th grader atm}). But still, thanks a lot, will be a nice challenge. :D

Freddy1990

2008-01-21 13:21:05

From: SKy Scripter
I just did


{-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function GetCompassAngleDegrees: extended;
By: SKy Scripter
Description: Gets The Mini Map Degrees Ranging From 0 - 360
Note: I Do Know That In ArcTan2, I Have The X Where The Y Should be.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
function GetCompassAngleDegrees: extended;
var
mx, my: Integer;
P: TPointArray;
begin
FindColorsTolerance(P, 1911089, 520, 5, 560, 45, 0);
TPAAverage(P, mx, my);
if (Mx <> 0) then
Result := FixD( -Degrees(ArcTan2(543 - mx, 24 - my)) );
end;


that's another way to do it

Wizzup?

2008-01-21 14:02:18

From: Freddy1990
that's another way to do it


Or just the one in SRL. ;)

n3ss3s

2008-01-21 14:15:18

Hehe, I can still filter that math, but I once happened to ask my grandfather - "What does hyperbolic inversed cotangent mean?" when I looked at SCAR manual…

…it was a once in a life time experience I will never forget xD

Toterache

2008-01-22 10:04:36

Cool, i think i understood it (a bit) :p
This is as far as i got, do i need to add anything to it?

function GetCompassDegrees: extended;
var
dx2, dy1, dy2: integer;
begin
dY1 := 24 - 7;
dX2 := 543 - pp.x;
dY2 := 24 - pp.y;
tmp := ((dX1 * dX2) + (dY1 * dY2)) / (Sqrt(Sqr(dX1) + Sqr(dY1)) * Sqrt(Sqr(dX2) + Sqr(dY2)));
Result := Abs(ArcCos(tmp) - Pi);
if pp.x > 543 then Result := 2 * Pi - Result;
end;