Finding an Angle

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

Finding an Angle

Postby MrGlinzz » Mon Jul 01, 2013 10:04 pm

I have a box that I just spawned with textureMatrix data : [scene.my.target(0), scene.my.target(1), e.pos(0), e.pos(1), ... ]

I need to find the angle between the target [scene.my.target] and the current position, then put that in the angle box [which is in radians]

My current script for the angle is
Code: Select all
angle := {
                math.atan((texturematrix(4) - texturematrix(2)) / (texturematrix(3) - texturematrix(1)))
            };


Simply put, I cant get the right formula for the angle. I know this has been put everywhere, but most posts refer to a hinge changing speed to match the angle... which as far as I can tell is completely different to what I need :*(

Thanks!
MrGlinzz
 
Posts: 13
Joined: Tue Mar 19, 2013 11:51 pm

Re: Finding an Angle

Postby Ivan » Tue Jul 02, 2013 11:13 pm

There are two things that will be useful to you.
The atan function returns values between -pi/2 and pi/2, so it will return the same value for, for example, 3/4 pi and - 1/4 pi. In order to always get the real angle between -pi and pi, you should use the atan2 function that takes two arguments (y, x).
The other thing is that there is an easyer way to add/subtract arrays ([pos2(0) - pos1(0), pos2(1) - pos1(1)] = pos2 - pos1)

Here is how it would go:
Code: Select all
  X  targetPos
   \
    \
    \     
     \   
      \ 
       \     
       \ angle
        \__ _ _ _ _ _ _ _ _ _ _
         X  currentPos
         
posDiff := targetPos - currentPos
angle = math.atan2(posDiff(1), posDiff(0))


I am not sure what you are trying to make and why you are using textureMatrix to store values. If the angle is supposed to change dynamically, then the code should probably look like this:
Code: Select all
...
angle := {
   angdiff :=  scene.my.target - pos;
   math.atan(angdiff(1), angdiff(0))
}
...

If it's not supposed to change after spawned, try this:
Code: Select all
angdiff :=  scene.my.target - e.pos;
scene.addbox({
...
angle := math.atan(angdiff(1), angdiff(0))
...
})
User avatar
Ivan
 
Posts: 203
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia

Re: Finding an Angle

Postby MrGlinzz » Wed Jul 03, 2013 4:16 pm

Ah thank you so much! :)

Just to check, where your two codes are, did you mean to put atan2? It didnt work without that?

Thanks again! I can finally carry on with my scene :)
MrGlinzz
 
Posts: 13
Joined: Tue Mar 19, 2013 11:51 pm

Re: Finding an Angle

Postby Ivan » Mon Jul 08, 2013 1:49 pm

I'm glad I could help. :)
User avatar
Ivan
 
Posts: 203
Joined: Tue Oct 06, 2009 2:01 pm
Location: Croatia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests