angle between 2 objects

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

angle between 2 objects

Postby pixartist » Mon Jan 18, 2010 12:09 am

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 ?
Image
pixartist
 
Posts: 65
Joined: Wed Sep 02, 2009 5:30 pm

Re: angle between 2 objects

Postby KarateBrot » Mon Jan 18, 2010 1:12 am

I didn't check the code but normally you can see it by looking if the difference of the angles is positive or negative.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: angle between 2 objects

Postby pixartist » Mon Jan 18, 2010 3:21 pm

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)
Image
pixartist
 
Posts: 65
Joined: Wed Sep 02, 2009 5:30 pm

Re: angle between 2 objects

Postby standardtoaster » Mon Jan 18, 2010 4:32 pm

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))))}
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: angle between 2 objects

Postby KarateBrot » Tue Jan 19, 2010 4:10 am

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: angle between 2 objects

Postby pixartist » Mon Feb 01, 2010 3:19 pm

toaster your function is bugged...
I'm getting really weird values (jumping from positive to negative for no apparent reason)
Image
pixartist
 
Posts: 65
Joined: Wed Sep 02, 2009 5:30 pm

Re: angle between 2 objects

Postby KarateBrot » Mon Feb 01, 2010 3:38 pm

Image
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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: angle between 2 objects

Postby RaRaMalum » Mon Feb 01, 2010 5:16 pm

I think my rangefinder does what you want, it goes from -180 to 180.

Rating: rated 6.8
Filesize: 11.79 kB
Comments: 5
Ratings: 5
download
Phundamentalists, come to the dark side.
We have much cooler toys.
User avatar
RaRaMalum
[Best newcomer 2010]
 
Posts: 280
Joined: Mon Aug 31, 2009 10:47 pm

Re: angle between 2 objects

Postby pixartist » Mon Feb 01, 2010 5:21 pm

KarateBrot wrote:Image
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...
Image
pixartist
 
Posts: 65
Joined: Wed Sep 02, 2009 5:30 pm

Re: angle between 2 objects

Postby standardtoaster » Mon Feb 01, 2010 6:07 pm

Then change the MaxRays to 1. ;)
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests