### Author Topic: Rotation  (Read 3937 times)

0 Members and 1 Guest are viewing this topic.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Rotation
« Reply #30 on: May 20, 2011, 07:22:11 PM »
Get comfortable with what it's doing first, and convert it into code.  Worry about optimizing it later.  :>

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #31 on: May 20, 2011, 08:00:09 PM »
Yeah, arrgh... re-coding the stuff now

But Annikk, you'r also confusing me with one thing: You use pythagoras three times, but I just really need to use it once or?(I'm so stupid asking xD) And you don't really need to use sqrt, cause you actually use ^2 to just get it up again... I'll see though

EDIT 1:

Aww

Code: [Select]
`function FindAngle(x1,y1,x2,y2,x3,y3) local AO = ((x1-x2)^2)+((y1-y2)^2) local AB = ((x2-x3)^2)+((y2-y3)^2) local BO = ((x3-x1)^2)+((y3-y1)^2) local CosCalc = math.cos((AO+BO-AB)/(2*math.sqrt(AO*BO))) print(CosCalc) end`
Gives 0.850058381662452 not -0.55472 D:
« Last Edit: May 20, 2011, 08:31:00 PM by Aino »

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Rotation
« Reply #32 on: May 21, 2011, 03:33:55 AM »
Are the coords of the asteroids the same as in my diagram?

Also, you should use the sqrts.  :>

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #33 on: May 21, 2011, 09:40:15 AM »
There is a way to do this that doesn't need all three sqrts. It does need two, so it's slightly cheaper on processor time and also doesn't need the cosine rule.

I'll write up how to do it in a moment.

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #34 on: May 21, 2011, 10:10:40 AM »
Hokay.... A2 maths revision. I'll set out an example for you in another post, for now I'll just give you the theory.

If you assume there are three 'roids (A, B, O) in the same setup as annikk's example with O being the centre one, that is to rotate, then define the coordinates of the 'roids to be Ax, Ay, Bx, By, Ox, Oy.

Then you take the distance between the x any y coordinates of A and B from O, so

Ox - Ax = A'x
Oy - Ay= A'y
Ox - Bx = B'x
Oy - By = B'y

Multiply the corresponding results and add the results to get the scalar/dot product of the two lines (yes, we are treating them as vectors for this, it is allowable):

(A'x * B'x) + (A'y + B'y) = A.B

Now, calculate the lengths of the two lines OA and OB:

Length OA = math.sqrt(A'x^2 + A'y^2)
Length OB = math.sqrt(B'x^2 + B'y^2)

Then, divide A.B by the two lengths of the lines, multiplied together.

A.B / (Length OA * Length OB) = cosangle

This gives you the cosine of the smallest angle between the two lines in radians so convert to degrees.

Finally,

angle = math.deg(math.acos(cosangle))

YAY!
« Last Edit: May 21, 2011, 10:45:36 AM by Pilchard123 »

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #35 on: May 21, 2011, 10:18:29 AM »
EDIT: Asteroid O does not have to be at (0,0). I just did that to make it easer to do.

Example time!

O = (0,0)
A = (-3, 4)
B = (12, 5)

Ox - Ax = A'x = 0 - -3 = 3
Oy - Ay = A'y = 0 - 4 = -4
Ox - Bx = B'x = 0 - 7 = -12
Oy - By = B'y = 0 - 5 = -5

(A'x * B'x) + (A'y * B'y) = A.B = (3 * -12) + (-4 * -5) = -16

Length OA = math.sqrt(A'x^2 + A'y^2) = math.sqrt(9 + 16) = 5
Length OB = math.sqrt(B'x^2 + B'y^2) = math.sqrt(144 + 25) = 13

A.B / (Length OA * Length OB) = cosangle = -16/(5 * 13) = -0.24615384615384617

angle = math.deg(math.acos(cosangle)) = 104.250033 degrees
« Last Edit: May 21, 2011, 11:28:08 AM by Pilchard123 »

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #36 on: May 21, 2011, 11:00:00 AM »
Wow o.O

So much code just to get the angles right? Well, I'll do it

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #37 on: May 21, 2011, 11:19:22 AM »
It works! But Pilchard, does this give you the direct angle or just an angle that you must subtract with the original one, like: OldAngle - NewAngle ?

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #38 on: May 21, 2011, 11:25:13 AM »
The smallest angle between the two lines, so you might have to mess with it to find out which way to turn and stuff.

So it gives you this:

But not the direction of the turn. There's probably a way to do that, too. I'll have a think.

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #39 on: May 21, 2011, 11:46:20 AM »
More stuff then...

Assuming A is the 'roid pointed at, and B is the one to turn to:

If Ax > Bx AND Ay > Oy AND By > Oy TURN counterclockwise.

For EVERY one of those comparisons that returns FALSE, change the direction of the turn.

Code: (MIGHT NEED TWEAKING) [Select]
`-- Assuming a clockwise turn is a positive turn, thenfunction TurnDirection(Ax, Ay, Bx, By, Ox, Oy) TurnDirectionMultiplier = -1 if Ax > Bx then   break else   TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end if Ay > Oy then   break else   TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end if By > Oy then   break else   TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end`

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #40 on: May 21, 2011, 11:56:51 AM »
So something like this:

Code: [Select]
`function FindAngle(Ox,Oy,Ax,Ay,Bx,By) local AOx = Ox - Ax local AOy = Oy - Ay local BOx = Ox - Bx local BOy = Oy - By local AB = (AOx*BOx)+(AOy*BOy) local LenghtOA = math.sqrt(AOx^2+AOy^2) local LenghtOB = math.sqrt(BOx^2+BOy^2) local CosAngle = AB / (LenghtOA*LenghtOB) TurnDirectionMultiplier = -1 if Ax > Bx then break else TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end if Ay > Oy then break else TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end if By > Oy then break else TurnDirectionMultiplier = TurnDirectionMultiplier * -1 end return(math.deg(math.acos(CosAngle))*TurnDirectionMultiplier) end`

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: Rotation
« Reply #41 on: May 21, 2011, 12:39:57 PM »
Yeah, looks about right. The maths at least should work. All you need to do is put that in your turning function and you're set.

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #42 on: May 21, 2011, 12:56:16 PM »
Though, you don't need the break

Cuz it can't be placed before an else it says and that it stops the whole process...

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Rotation
« Reply #43 on: May 21, 2011, 01:02:30 PM »
You could just test the result.  If it's more than 180 degrees, subtract 360 degrees from it.  If it's less than -180 degrees, add 360 degrees to it.  That will always give you the smaller angle.

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: Rotation
« Reply #44 on: May 21, 2011, 01:44:34 PM »
Sure, I'll make it now...