need help with angle calculations!!

About advanced scenes, and the Thyme scripting language used in Algodoo.

need help with angle calculations!!

Postby jessebax129 » Sat Apr 03, 2010 9:33 pm

hello i need some halp here:

i'm working on an angle reader for a ''box''

i know the calculation for it:


angle1 = math.acos((hit1(0) + ((hit1(1)^2 + hit1(0) ^ 2)^0.5)

but when i let it calculate it, it does nothing (it freezes on the last number that the variable was)

plz don't begin crying about the variables than they have to be: scene.my.angle1 because i WILL change it later




EDIT!!:
i saw a scene where they did the same i want to do and it worked with them... so i tried out myself and the solution whas: ''NaN'' (so angle1 = NaN) out of the calculation came ''3'' so math.acos(3) = NaN
:shock: :shock:
jessebax129
 
Posts: 34
Joined: Tue Jan 05, 2010 6:32 pm

Re: need help with angle calculations!!

Postby KarateBrot » Sun Apr 04, 2010 9:12 am

You made a maths mistake. It's correct if it's like this:

angle1 = math.acos((hit1(0) / ((hit1(1)^2 + hit1(0) ^ 2)^0.5)

And furthermore that's only half of the whole code because with this you can only get angles from 0° to 180°. If you also make a case differentiation you can get all 360°.

angle1 = math.acos((hit1(0) / ((hit1(1)^2 + hit1(0) ^ 2)^0.5); hit1(1) < 0 ? {angle1 = 2*math.pi - angle1} : { }

That's it

- - - - -

Btw. it's no surprise to get NaN from acos(3) because it's only defined in a range from -1 to 1. If you look at the heigth of the cosine wave you will see that -1 and 1 are the most extreme values for this function.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: need help with angle calculations!!

Postby Mr_Stabby » Mon Apr 05, 2010 8:50 am

either way ive found using straight e.normal(0/1) makes for a shorter/faster code for whatever your trying to pinpoint because all converting to angles does is like having 2 translators with u all the time, one translating what you said into latin and the other translating that back to what you said. they are easy to use too once you get used to them, work like this

Code: Select all
   -1
-1  +  1
    1
horizontal e.normal(0)
vertical e.normal(1)
Mr_Stabby
 
Posts: 155
Joined: Wed Dec 16, 2009 12:16 am

Re: need help with angle calculations!!

Postby KarateBrot » Tue Apr 06, 2010 2:54 am

but if he really wants to have angles the normal vector in its regular form is useless. Of course you can use e.normal to extract the angle from it and the code gets simplified to

angle1 = math.acos(e.normal(0)); e.normal(1) < 0 ? {angle1 = 2*math.pi - angle1} : { }

yeah but otherwise you're right. depends on what you want to do

PS. LOL your avatar xD
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 6 guests