View Full Version : Calculating distance between angled objects  my trigonometry is not correct.?
miauu 04 April 2012, 05:01 PM I want to get the distance, marked with green line. I want the Source object to be moved until it touches the Target object(without intersection)
I use two methods:
1. intersectRay:
http://img837.imageshack.us/img837/3806/48360522.png
DIST = intersectRay target (ray.p4.pos.x, p4.pos.y+1, p.pos.z] [1,0,0])
This works fine and calculate the proper distance, but it is slow, compared to method 2, which is:
2. I use trigonometry to find the distance (DIST)
http://img851.imageshack.us/img851/5562/41881713.png
angA = 90
angC = 12
distX = p4.pos.x  p3.pos.x
distZ = p4.pos.z  p3.pos.z
A = distZ*(sin angC) / (sin (angA+angC))
DIST = A + distX
The method 2 works much faster than method 1. On heavy scene the time for completing the task is:
method 1(intersectRay)  118.53 sec
method 2(trigonometry)  89.63 sec
When the angle of the Source and the Target object is different, the DIST is not calculated properly. When I run the code line by line everything works fine(even if the angles are different), but when the code is placed in the for loop, the method 2 works incorrectly.
Can someone help me with this?


denisT
04 April 2012, 05:23 PM
angC = 12
distX = p4.pos.x  p3.pos.x
distZ = p4.pos.z  p3.pos.z
A = distZ * (tan angC)
DIST = A + distX
but this method is very limited, it covers only some 2D cases.
miauu
04 April 2012, 05:37 PM
Thanks, Denis!
I need it only for 2D.
But, when the code is in for loop(while ... do also) the result is not what I want.
http://img96.imageshack.us/img96/7540/88373607.png
The gaps between books is the problem. intersectRay place books properly, but as I said is very slow, compared to the trigonometry.
denisT
04 April 2012, 06:02 PM
Thanks, Denis!
I need it only for 2D.
But, when the code is in for loop(while ... do also) the result is not what I want.
http://img96.imageshack.us/img96/7540/88373607.png
The gaps between books is the problem. intersectRay place books properly, but as I said is very slow, compared to the trigonometry.
shelvedbookplacer... :)
i think you have to use more accurate method based on bbox of a book. let me think a little...
3ak
04 April 2012, 06:17 PM
http://my.jetscreenshot.com/11355/m_20120421guud15kb.jpg (http://my.jetscreenshot.com/11355/20120421guud15kb)
I think you could make it without trig at all. If you have all the books tilted to the left:
if b1p3.z > b2p2.z then dist = (b2p2.z  b1p4.z) / (b1p3.z  b1p4.z) * (b1p4.x  b1p3.x) + (b2p2.x  b1p4.x)
I think "else" clause for book1 smaller or equal than book2 you can addd yourself=)
denisT
04 April 2012, 06:36 PM
http://my.jetscreenshot.com/11355/m_20120421guud15kb.jpg (http://my.jetscreenshot.com/11355/20120421guud15kb)
I think you could make it without trig at all. If you have all the books tilted to the left:
if b1p3.z > b2p2.z then dist = (b2p2.z  b1p4.z) / (b1p3.z  b1p4.z) * (b1p4.x  b1p3.x) + (b2p2.x  b1p4.x)
I think "else" clause for book1 smaller or equal than book2 you can addd yourself=)
that's absolutely correct formula...
but in real life (:)) books not shift, they heel on the side. so we have to rotate and not move.
miauu
04 April 2012, 07:07 PM
Thanks, 3ak (http://forums.cgsociety.org/member.php?u=20671)!
I will test this later.
Denis, the script rotate the books, then placed it at the proper position. :)
3ak
04 April 2012, 08:20 PM
that's absolutely correct formula...
but in real life (:)) books not shift, they heel on the side. so we have to rotate and not move.
Yes, but to find intersection between circle (rotation of the corner point) and line (side of the first book) we should solve quadratic equation imo. or...
http://my.jetscreenshot.com/11355/m_20120421euap19kb.jpg (http://my.jetscreenshot.com/11355/20120421euap19kb)
Let's say we have first book already with tilt. and second 90 degrees with floor. Then to find angle we need to rotate book around b2p1 :
p5 = ( (distance b1p1 b1p4) / ((b1p2.z  b1p1.z) / (distance b1p1 b1p2)) ) + b1p1
alpha = 90 + (acos ((b1p2.z  b1p1.z) / (distance b1p1 b1p2)))
beta = asin ( (b2p1.x  p5.x) * (sin alpha) / (distance b2p2 b2p1) )
ANGLE = 90  beta
miauu
04 April 2012, 08:53 PM
I begin to think that Max is doing something wrong
Here the result from 3ak formula:
http://img805.imageshack.us/img805/5593/77728845.png
The objects intersects. With my trigonometric formula there are gaps between objects.
Here is how the script works:
1. get book1
2. placed it at proper position
3. rotate the book to some angle(12 for example)
4. get the book2
5. placed it far behind the book1
6. rotate book2 to angle, bigger than the angle of the book1(14 for example)
7. calculate the distance.
8. move the book2 so it touches book1
As I said, I already have working version of the script, but intersectRay is slower than the trigonometry and 3ak formula. And I still wonder why with intersectRay the script works without intersections, while there are problems with trig and 3ak formula.
3ak
04 April 2012, 09:23 PM
The objects intersects. With my trigonometric formula there are gaps between objects.
Here is how the script works:
1. get book1
2. placed it at proper position
3. rotate the book to some angle(12 for example)
4. get the book2
5. placed it far behind the book1
6. rotate book2 to angle, bigger than the angle of the book1(14 for example)
7. calculate the distance.
8. move the book2 so it touches book1
As I said, I already have working version of the script, but intersectRay is slower than the trigonometry and 3ak formula. And I still wonder why with intersectRay the script works without intersections, while there are problems with trig and 3ak formula.
4. Find point coords where book2 will touch book1 as:
t = (b2p1.z  b1p4.z) / (b1p3.z  b1p4.z)
POINT (x,z) = [ t* (b1p4.x  b1p3.x) + b1p3.x, b2p2.z]
5. book2.pos = POINT  b2p2_local_coords
miauu
04 April 2012, 10:18 PM
Still have the same problem. I will do more tests tomorrow. Thank you.
miauu
04 April 2012, 01:05 PM
Strange thing happens. When I use the 3ak formula , executed line by line, the result is:
http://img801.imageshack.us/img801/4809/002dl.png
When I use the 3ak formula in the script the result is:
http://img821.imageshack.us/img821/300/001cv.png
When all objects have the same angle  there are no intersections, when the objects have different angles  there are intersections.
CGTalk Moderation
04 April 2012, 01:05 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.