angle between 2 objects
10 posts • Page 1 of 1
angle between 2 objects
- Code: Select all
(e)=>{
Scene.my.v1 = e.normal; Scene.my.dif = (scene.my.v1(0) * scene.my.v2(0) + scene.my.v1(1) * scene.my.v2(1)) / ((scene.my.v1(0) ^ 2 + scene.my.v1(1) ^ 2) ^ 0.5 * (scene.my.v2(0) ^ 2 + scene.my.v2(1) ^ 2) ^ 0.5); }
I use this to get the angle between two objects, now how can is see if the acute angle is on the left or the right ?
- pixartist
- Posts: 65
- Joined: Wed Sep 02, 2009 5:30 pm
Re: angle between 2 objects
I didn't check the code but normally you can see it by looking if the difference of the angles is positive or negative.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: angle between 2 objects
KarateBrot wrote:I didn't check the code but normally you can see it by looking if the difference of the angles is positive or negative.
wrong, not when you do it like I did...
a*b
----------
|a|*|b|
what I needed was atan2(a,b)
- pixartist
- Posts: 65
- Joined: Wed Sep 02, 2009 5:30 pm
Re: angle between 2 objects
You can use my atan2.
- Code: Select all
Scene.my.atan2=(P1, P2)=>{2 * math.atan((P2(1)-P1(1)) / (((((P2(0)-P1(0))^2) + (P2(1)-P1(1))^2) ^ 0.5) + (P2(0)-P1(0))))}
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: angle between 2 objects
oh now i know what you mean. sorry i didn't read it properly. i thought you want to know something about the angle of an object relative to another object.
atan2 is okay but be aware of the lack in definition at Pi and -Pi. that's why i don't use atan2 for angles.
atan2 is okay but be aware of the lack in definition at Pi and -Pi. that's why i don't use atan2 for angles.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: angle between 2 objects
toaster your function is bugged...
I'm getting really weird values (jumping from positive to negative for no apparent reason)
I'm getting really weird values (jumping from positive to negative for no apparent reason)
- pixartist
- Posts: 65
- Joined: Wed Sep 02, 2009 5:30 pm
Re: angle between 2 objects
Atan2 goes from 0° to 180° and then it jumps to -180° and goes back to 0° in one full turn (in addition there's a lack in definition between 180° and -180°). Maybe your angles were all about 180° so you got negative angles for angles which were greater than 180°
Try another one with the inverse cosine:
- Code: Select all
scene.my.angle = (P1, P2) => { angle := math.acos((P2(0) - P1(0)) / ((P2(0) - P1(0))^2 + (P2(1) - P1(1))^2)^0.5); (P2(1) - P1(1)) < 0 ? {angle = 2 * math.pi - angle} : {}; angle }
It goes from 0° to 360° in one full turn.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: angle between 2 objects
I think my rangefinder does what you want, it goes from -180 to 180.
Phundamentalists, come to the dark side.
We have much cooler toys.
We have much cooler toys.
-
RaRaMalum - [Best newcomer 2010]
- Posts: 280
- Joined: Mon Aug 31, 2009 10:47 pm
Re: angle between 2 objects
KarateBrot wrote:
Atan2 goes from 0° to 180° and then it jumps to -180° and goes back to 0° in one full turn (in addition there's a lack in definition between 180° and -180°). Maybe your angles were all about 180° so you got negative angles for angles which were greater than 180°
Try another one with the inverse cosine:
- Code: Select all
scene.my.angle = (P1, P2) => { angle := math.acos((P2(0) - P1(0)) / ((P2(0) - P1(0))^2 + (P2(1) - P1(1))^2)^0.5); (P2(1) - P1(1)) < 0 ? {angle = 2 * math.pi - angle} : {}; angle }
It goes from 0° to 360° in one full turn.
i know that. the problem was, that e.pos from a laser collision event randomly switches between the first object the laser hits and the collisions of the reflected laser...
- pixartist
- Posts: 65
- Joined: Wed Sep 02, 2009 5:30 pm
Re: angle between 2 objects
Then change the MaxRays to 1.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
10 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests