Finding an Angle
4 posts • Page 1 of 1
Finding an Angle
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
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!
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
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:
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:
If it's not supposed to change after spawned, try this:
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))
...
})
-

Ivan - Posts: 203
- Joined: Tue Oct 06, 2009 2:01 pm
- Location: Croatia
Re: Finding an Angle
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
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
I'm glad I could help. 
-

Ivan - Posts: 203
- Joined: Tue Oct 06, 2009 2:01 pm
- Location: Croatia
4 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 5 guests



