Author Topic: Rotation  (Read 37585 times)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Rotation
« on: May 09, 2011, 01:01:37 AM »
Well, so I wonder: rotation...

How do you do it?? I've tried this:

Code: [Select]
local Rotation = (r/360)*(3.1415*2)

for i = ModelPartStart[id],ModelPartEnd[id] do
DrawX = xpos + (math.cos(Rotation)*ModelPartX[i])
DrawY = ypos + (math.sin(Rotation)*ModelPartY[i])

it's a snippet from my current proj. and I thought it would go just fine, but then: it just twists and do stuff I wouldn't expect... and the Y-axis ain't working properly at all. Here's two comparisons:

1:


2:

Well, why the f**k?

Sniped50

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 97
Re: Rotation
« Reply #1 on: May 09, 2011, 04:01:35 AM »
Did you want those spheres to rotate as if they were a single line? I'm not sure what exactly you were aiming for, but that's what I could gather since those are rather simple equations.

Also, what does that 'r' stand for? Is it something you called earlier in the code?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #2 on: May 09, 2011, 04:34:15 AM »
A straight line was what I was aiming for, and r stands for rotation or degree... But I think I might be able to solve this, cause in a new code I've made, it just moves around the center with the same shape, but I think I can solve this :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #3 on: May 09, 2011, 04:42:39 AM »
But still, don't stop helpin', cause if my new idea fails... I'll need to call for you again -.-

EDIT: And to simplify: I want the Yaw to rotate, nothing else...
« Last Edit: May 09, 2011, 04:53:34 AM by Aino »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #4 on: May 09, 2011, 09:56:25 PM »
YEEES! Figured it out, now to make moving sprites :)

I hope this tool will contribute to game making in Eufloria, that would be awesome IMO :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #5 on: May 10, 2011, 02:02:47 AM »
And now the whole thing turned crappy... It isn't drawing anything at all! Better take the rest of the day away from coding :)

And expect a new map to come soon, it's been some beta testing... But it seems like everyone(Bonobo(the others haven't responded yet :S)) had a hard time beating it... But still simple as hell(IMO) :D(this means it is beatable, but hard :D)

Bonobo

  • Achiever
  • Old Oak
  • ****
  • Thank You
  • -Given: 139
  • -Receive: 12
  • Posts: 670
  • Eufloria: Yes
Re: Rotation
« Reply #6 on: May 10, 2011, 03:22:13 AM »
So much for the outing of mine one as a l4m0r ;)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #7 on: May 10, 2011, 04:14:52 AM »
YEAH!!!!!!!!!!!!

seed YEAH!

sry for swearing, but this is a huge step for me(which are having plans becoming a programmer...) cause I just made that f**king thing work! Finally, time to make real 2D game, no more single dots :D

(it even supports animation :P)

EDIT: And lol, the code is approx. 160 lines long xD Im so happy now, but I won't leave the forum just now... gonna make some kind of 3D showof thing before I even imagine leaving this awesome forum :)
« Last Edit: May 10, 2011, 04:23:28 AM by Aino »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #8 on: May 12, 2011, 09:02:02 AM »
Need more help, which is about rotation...

Ok, so I need help about something I've been thinking about for ages, but now I need it kinda...

So the problem is to simply find the rotation or wahtever yuo call it between two entities, lets make a simple example:

Bob is lookng south, he sees a buddy and wants to talk to him, but noone talks without looking at the person they talk to? So how is bob gonna turn around and know when he sees his buddy(which angle is he supposed to look).

And those who can't seem to find out what I mean, I made a decently telling picture:



So now, help?

Widget

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 97
Re: Rotation
« Reply #9 on: May 12, 2011, 07:35:47 PM »
Couldn't you use the x/y position of the two points to calculate the measurements of a hypothetical right-angle triangle and work from there? A straight line between the two points would be your hypotenuse and you could use the measurements to calculate the relative angle.

There's probably a much easier way but that's all that springs to mind.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #10 on: May 12, 2011, 07:54:49 PM »
What did he say? I'm confused... what is all those crazy things? *googling*


EDIT: Are you talking abouth Pythagoras Theorem? If so, how do I use it to find angles?

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #11 on: May 13, 2011, 01:08:24 AM »
Trig.

Sine of angle = side opposite angle / hypotenuse
Cosine of angle = side next to angle / hypotenuse
Tangent of angle = side opposite angle / side next to angle

Use the inverse functions to get the angle.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #12 on: May 13, 2011, 01:28:59 AM »
Ok Pilchard, I know you love to confuse people... But this is my first time looking into this and please explain deeper and more understandable? Maybe you want to add some pictures too? :)

Edit: After some research, you refer to atan,asin and acos... which I am going to use on the exact thing you said now?
« Last Edit: May 13, 2011, 02:30:52 AM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #13 on: May 13, 2011, 09:46:43 PM »
Aino, the maths needed to calculate what you want is called Trigenometry.  This uses Sin, Cos (which are discussed in the Intermediate Guide) and also another one called Tan (which is short for "tangent") to calculate angles.

I'd recommend you find yourself a nice beginners guide to Trigenometry and work through that.  It will help you get up to speed on the subject.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #14 on: May 13, 2011, 10:16:57 PM »
>.<

Well, better get going...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #15 on: May 13, 2011, 10:54:08 PM »
So, over to some new... I found a way to do it, my uncle(he's been doing some coding too :D) told me..

Explaining it simple with pictures...


(Don't worry, it will contain Cos and Sin...)

It will be hard to make, but thats the theory... And annikk, the tutorials sucks :/ Why don't you explain(I 've learnt almost everything from you :P)?

EDIT: Or maybe a simplier version:

You have a line for the unit that search, the line will be as long as the distance between the two units(Pythagoras Theorem) and then when that lines end is close to the coords of the unit it search for, it will say that THIS is the angle... That makes it all much more simplier, I can even see the code in my head(literally D:)...

EDIT 2: It works, not totally propperly, because it isn't recognizing it in some areas... but it works! :D

Code: (Code for facing angle) [Select]
function FindFacingAngle(x1,y1,x2,y2)

local XDIFF = (x1-x2)^2
local YDIFF = (y1-y2)^2

local Distance = math.abs(math.sqrt(XDIFF+YDIFF))

for i = 1,360 do
local CurAngle = (i/360)*(math.pi*2)
if x1 + math.cos(CurAngle)*Distance >= x2-Distance/(math.pi*2) and x1 + math.cos(CurAngle)*Distance <= x2+Distance/(math.pi*2) and y1 + math.sin(CurAngle)*Distance >= y2-Distance/(math.pi*2) and y1 + math.sin(CurAngle)*Distance <= y2+Distance/(math.pi*2) then
return(i)
else
if i == 360 then

return(i)

end
end
end

end

Yeah, I know it is very resource consumpting, but you can remove that by making some if's and stuff to check if the angle has been changed(or simply if the unit is a target and the x/y coords are changed :P)
« Last Edit: May 13, 2011, 11:27:38 PM by Aino »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #16 on: May 13, 2011, 11:58:38 PM »
Maybe not so resource consumting as i thought?



That is 11x11 which is 121 models, and then 121x360 which is 43560(+sqrt(pythagoras)) calculations per DRAW! so, it's very silent and I guess that if you use the function correctly, you won't need 121 of them at the same time per draw :P
« Last Edit: May 14, 2011, 12:51:14 AM by Aino »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #17 on: May 14, 2011, 02:40:19 AM »
...on the asteroid.

It has the highlight that they get when you hover over them.

Also, the squarer end of all the models/sprites are popinting to the centre.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #18 on: May 14, 2011, 06:10:58 AM »
O.o

Well, you see that they point that way, thats' what I made... it rotates toward the mouse :D

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #19 on: May 14, 2011, 06:01:15 PM »
Need help, again...

This is about finding the shortest turn, but if you didn't understand what I meant:



So, any advice?

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #20 on: May 14, 2011, 09:13:22 PM »
Do you have a property for the rotation of the whatever-it-is-that-you-want-to-turn?

If so, how does it work, and where is 0 degrees?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #21 on: May 14, 2011, 09:31:12 PM »
0 Degree would be at the absolute right side of the sircle, and 90 would be straight DOWN and so on :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #22 on: May 20, 2011, 05:54:06 PM »
I'll try to help with this properly now.


First of all lets be clear about what the problem is.



Asteroid O is facing toward Asteroid A.  We wish to rotate it such that it is facing Asteroid B instead.

We already know the coordinates of Asteroids O, A, and B.

How to find the angle?



Let me know if this is indeed what you are trying to calculate Aino.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #23 on: May 20, 2011, 05:56:44 PM »
Yes Annikk, yes...

Thats indeed what I need help with, cause the current one is very inaccurate when it comes to accurate stuff :/
« Last Edit: May 20, 2011, 06:00:49 PM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #24 on: May 20, 2011, 07:04:51 PM »
You can calculate the angle using the following formula:


Tan Angle = (Am - Bm) / (1 + (Am * Bm))


Two steps will be required to turn this formula into something useful.

First, we need to find out what Am and Bm are.

Second, we need that "Tan" to be on the right-hand side, leaving just the "angle" on the left hand side.



"m" refers to the "slope" of the lines.

To calculate the slope of the line between points O and A, we use the following:

Am = (Ay - Oy) / (Ax - Ox)

We can fill those out already.  Ay = 200, Oy = 0, Ax = 300, Ox = 0.

So we can fill those values in:
Am = (200 - 0) / (300 - 0)
Am = 200 / 300
Am = 0.666666


I have to go out for a couple hours now.  Aino, can you use this example to calculate Bm.  I'll check it when I get back, and continue with the explanation.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #25 on: May 20, 2011, 07:17:55 PM »
Thanks for the stuff, finally :P

I'm currently "away" myself, can't do any coding... But as soon as I can I'll test it :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #26 on: May 20, 2011, 09:16:14 PM »
Slacker :P  We're not finished yet you know!

Anyway, here's Bm:


Bm = (By - Oy) / (Bx - Ox)

We can fill those out already.  By = -300, Oy = 0, Bx = 0, Ox = 0.

So we can fill those values in:
Bm = (-300 - 0) / (0 - 0)
Bm = -300 / 0
Bm = Infinity


w00t, divided by zero.


Bear with me, I'll find a better method to do this.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #27 on: May 20, 2011, 10:43:09 PM »
Ok here's my awesome guide.

































I made this with some help from this guide.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #28 on: May 21, 2011, 12:11:57 AM »
Beware the ambiguous case [thunder rumbles].

http://en.wikipedia.org/wiki/Law_of_cosines#Applications   Last paragraph of the Applications section, though I don't think it applies in this case.


Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #29 on: May 21, 2011, 02:48:34 AM »
aww, it includews a sqrt :/

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #30 on: May 21, 2011, 03:22:11 AM »
Get comfortable with what it's doing first, and convert it into code.  Worry about optimizing it later.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #31 on: May 21, 2011, 04:00:09 AM »
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 21, 2011, 04:31:00 AM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #32 on: May 21, 2011, 11: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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #33 on: May 21, 2011, 05:40:15 PM »
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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #34 on: May 21, 2011, 06:10:40 PM »
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, 06:45:36 PM by Pilchard123 »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #35 on: May 21, 2011, 06:18:29 PM »
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, 07:28:08 PM by Pilchard123 »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #36 on: May 21, 2011, 07:00:00 PM »
Wow o.O

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

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #37 on: May 21, 2011, 07:19:22 PM »
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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #38 on: May 21, 2011, 07:25:13 PM »
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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #39 on: May 21, 2011, 07:46:20 PM »
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, then
function 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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #40 on: May 21, 2011, 07:56:51 PM »
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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #41 on: May 21, 2011, 08: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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #42 on: May 21, 2011, 08:56:16 PM »
Though, you don't need the break :P

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
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #43 on: May 21, 2011, 09: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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #44 on: May 21, 2011, 09:44:34 PM »
Sure, I'll make it now...

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #45 on: May 21, 2011, 10:16:10 PM »
You always get the smallest angle. Just reverse the comparison signs and remove the break...else section. I don't know why I didn't say that before. Derp.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #46 on: July 26, 2011, 02:46:10 AM »
New question (yay!), which obviously is about rotation!

So the case is this:



The arrow shows the velocity, obviously that is ok and fine knowledge and simple to handle. But one/two more arrows, pointing theese ways:



Gives a challenge, or finding the values... Lets say you got the values you got there, then want to do this:



How the hell do you do that?

And please, no complicated stuff, I'm a bit tired of having to read the code 100 times to understand it(literally!). And if it is so complicated, please explain D:
(I also hope I explained myself very clearly this time, and hope for no question regarding my question about this little thing :))

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #47 on: July 26, 2011, 03:53:13 AM »
So you want to turn the velocity 5 degrees to the left, so it is moving in a different direction? Or do you mean to move the point, but keep the velocity the same?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #48 on: July 26, 2011, 04:05:35 AM »
The last one, keeping the velocity the same, but the current position is changed by thoose 5...

hitman271

  • Shoot
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
Re: Rotation
« Reply #49 on: July 27, 2011, 10:53:34 AM »
With vectors, you just add them together. So if one vector is { -100, -60 } and you want to move "5 distances" you would make "5 distances" a vector and just add those two vectors together. If this wasn't what you meant, give a more detailed question :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #50 on: July 27, 2011, 11:34:28 AM »
I think I see what you mean.

If the red arrow is "pushing" with a force of 10, and the blue arrow with a force of 5, then the total force exerted would be 5 in the direction of the red arrow.

However, it's not clear what you wish to calculate.  Do you need to rotate an object?  If so then "distances" could be degrees.  Or perhaps you want to work out what direction something is travelling in after the result of several forces acting on it?


If you can explain what your diagrams mean, that will help.  :>  You have written "Lets say you got the values you got there, then want to do this:".  Do what?  "How the hell do you do that?"  Do what?  :>  If you can't put into words what you're trying to do, you're unlikely to be able to actually do it!  And we're even less likely to be able to help.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #51 on: July 27, 2011, 07:19:59 PM »
Ok, think of a ball, just rolling one way. Suddenly some strange wind appears and the angle of velocity is NOT changed, as it usually would be. But the ball is rather being pushed with the wind but still rolling the same exact way. This I hope help you :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #52 on: July 27, 2011, 08:30:17 PM »
That's easy in that case.  It's just very simple vectors.


If I have a ball rolling west at x = -30
And some wind blows it south with a force of y = -10
Then the overall trajectory of the ball will be x = -30, y = -10

For diagonals, picture a ball rolling northwest at x = -15, y = -10
A wind blows the ball southwest by x = -5, y = 4
The overall trajectory of the ball will be x = -20, y = -6

See how that is working?  You just add both X's and both Y's to find the total X and total Y.  It really is that simple.


(click to show/hide)
« Last Edit: July 27, 2011, 08:34:12 PM by annikk.exe »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #53 on: July 27, 2011, 08:46:13 PM »
Hmm, re-readign your post, maybe you meant something else.


I will try to describe what I think you are trying to do.

You have a ball rolling in some arbitrary direction (EG North East).  You want to move the ball either left or right, such that you are "nudging" it along a line that is perpendicular to the direction of travel.  This must be done without affecting the direction the ball is travelling in.

So for example if the ball was travelling north.  The code should be capable of nudging the ball either west, or east.  After the nudge, the ball should still be travelling perfectly north.

Is that correct?  If that is the problem you're trying to solve, it may be helpful for you to note that where Line 1 is perpendicular to Line 2, m1 = 1 / -m2m is the "slope" variable from the slope/intercept equation of a line y = mx + c
Let me know if this is the problem and if so I shall explain further :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #54 on: July 27, 2011, 09:46:06 PM »
By first read atleast, it seems like that is what I want :)

And the wind I meantioned, was just an exampling. The real purpose has no wind, my plans with this is to make the dot in "Tough Collisions" to jump from one side to another when you travel too fast, so it looks like you shake like hell :P

BTW, like my new Avatar? :D

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #55 on: July 27, 2011, 09:54:27 PM »
heh :>


Ok so yeah.  Take a look at this page: http://www.wtamu.edu/academic/anns/mps/math/mathlab/col_algebra/col_alg_tut28_parpen.htm

Look at the bit about calculating perpendicular lines.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #56 on: July 27, 2011, 10:08:08 PM »
Annikk, I'm confused x.x

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #57 on: July 28, 2011, 01:48:34 AM »
This is all so much easier that you're all making it out to be. Really.

Will the jump ALWAYS be at right angles to the velocity, or will it be in an arbitrary direction?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #58 on: July 28, 2011, 04:05:42 AM »
In all directions :P

But I made it all VERY VERY much harder than it really is(or to say for the purpose)...

My purpose of it was to make a speedup effect for the dot you are in Tough Collisions, where the dot shrinks and shakes, both illusions ofcourse, as the dot is actually just flowing normally in-game.

How I did it? Math.Random is your friend! I used it to add/subract some numbers to move it... Simple :)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Rotation
« Reply #59 on: July 28, 2011, 05:08:52 AM »
So it's done now, then?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Rotation
« Reply #60 on: July 28, 2011, 05:13:14 AM »
Occam's Razor, etc.  :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #61 on: July 28, 2011, 05:15:03 AM »
Yes, it is done :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #62 on: July 31, 2011, 01:36:22 AM »


I just saw that Atan2 calculates angles between two points x.x

Just do math.atan2(x2-x1,y1-y2) and it should work, it works together with math.sin/cos since it returns negative to positive pi >.<

And as we know, be is the circle, you can use pi*2 in sin/cos to get a circle instead of using negative, but negative works just as well :)

I've been experiementing alot with trigonometry :o


Going to test it now, solving the most ancient programming question I've had in my mind since I started with math...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #63 on: July 31, 2011, 01:54:51 AM »
Results Test 1:

Weird that this code halflike works:

Code: [Select]
function LevelSetup()
Globals.G.Asteroids=0
Globals.G.EnemyFactionsMin=0
Globals.G.EnemyFactionsMax=0
Globals.Asteroids.SpawnCap=0
SetBackdropColour(0,0,0)

a = AddAsteroid(0,0)
a.Radius = 50
a.Owner = 1
a.TreeCap = 1
a:AddSeedlings(10)

posx = -100
posy = -100

end

function LevelLogic()

end

function GetMouseX()

return(GetCameraX()+(((GetMouseScreenX()-(GetScreenWidth()/2)))*GetCameraZoom()))

end

function GetMouseY()

return(GetCameraY()+(((GetMouseScreenY()-GetScreenHeight()/2))*GetCameraZoom()))

end

function LevelDraw()

local dx = posx-GetMouseY()
local dy = posy-GetMouseX()

local atan2 = math.atan2(dx,dy)
local dist = math.sqrt((dx^2)+(dy^2))

DrawLine(posx,posy,posx+math.cos(atan2-math.pi)*dist,posy+math.sin(atan2-math.pi)*dist,1,1,1,1,1,1,1,1,5)

end

Because of:

Code: [Select]
local dx = posx-GetMouseY()
local dy = posy-GetMouseX()

Where the values are switched, WTH?

Though it only work on perfectly similar positions, like (-100,-100) or (100,100) :P

Test 2 soon, I'm gonna get to the bottom of this seed!

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #64 on: July 31, 2011, 02:15:29 AM »
Attempt 2, succesful!

Code: [Select]
function LevelSetup()
Globals.G.Asteroids=0
Globals.G.EnemyFactionsMin=0
Globals.G.EnemyFactionsMax=0
Globals.Asteroids.SpawnCap=0
SetBackdropColour(0,0,0)

a = AddAsteroid(0,0)
a.Radius = 50
a.Owner = 1
a.TreeCap = 1
a:AddSeedlings(10)

posx = -100
posy = -100

end

function LevelLogic()

end

function GetMouseX()

return(GetCameraX()+(((GetMouseScreenX()-(GetScreenWidth()/2)))*GetCameraZoom()))

end

function GetMouseY()

return(GetCameraY()+(((GetMouseScreenY()-GetScreenHeight()/2))*GetCameraZoom()))

end

function LevelDraw()

local dx = posx-GetMouseX()
local dy = posy-GetMouseY()

local atan2 = math.atan2(dx,dy)
local atan22 = math.atan2(dy,dx)
local dist = math.sqrt((dx^2)+(dy^2))

DrawLine(posx,posy,posx+math.cos(atan2-(atan22/2))*dist,posy+math.sin(atan2-(atan22/2))*dist,0,1,0,1,1,1,1,1,5)
DrawLine(posx,posy,posx+math.cos(atan2-atan22)*dist,posy+math.sin(atan2-atan22)*dist,1,0,0,1,1,1,1,1,5)
DrawLine(posx,posy,posx+math.cos(atan2)*dist,posy+math.sin(atan2)*dist,1,1,1,1,0,1,0,1,5)
DrawLine(posx,posy,posx+math.cos(atan22-math.pi)*dist,posy+math.sin(atan22-math.pi)*dist,1,1,1,1,1,0,0,1,5)
DrawText(atan2,GetMouseX(),GetMouseY(),1,25)

end

!!

My life is complete!!

I love using memes :D

Edit:

Uploaded the complete file without extra arrows, it should work! :)
« Last Edit: July 31, 2011, 02:22:23 AM by Aino »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Rotation
« Reply #65 on: July 31, 2011, 03:03:09 AM »
I realize I've solved alot of problems, of my own(coding wise), with three lines of code o.o

The three can be compressed to one even :D

This is so awesome ;D