Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on September 05, 2007

Title: finding a point from 2 perpindicular vectors with a radius
Post by: Pixel_Outlaw on September 05, 2007
Ok I'm in need of some help with math.

Basically I'm trying to write a program for a tube racing game. The outside walls are a set radius away from a central spline of vector lines. I'm trying to calculate where the verticies should be to construct the tube.

There are two vectors that share a start point (x1,y1,z1). I need to find the coordinates along a plain perpindicular to the start of the first vector given a distance and angle away from it. The variable is the angle which would change the way the radius vector rotates.

Perhaps a this image will help:
I'm trying to find the value of (x,y,z) along the radius vector, given a radius and angle.

Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Jim on September 05, 2007
Tricky maths :)
OK, just for the moment, consider that the tube never points straight up and down, then you can use (0,1,0) as an up-vector.  You need an up-vector so that all your angles start from the same place, ie. there's a common zero point.  Then you can make the direction of the tube,
that's
dx = x2-x1
dy = y2-y1
dz = z2-z1
Take the unit vector of that:
U = unit(dx,dy,dz)
and take take the cross product with the up-vector, that gives
T = U x (0,1,0) = (-U.z, 0, U.x)
take the unit vector of that
T = unit(T)
Then the point (x,y,z) in your diagram is,
x = x2 + T.x * radius
y = y2 + T.y * radius
z = z2 + T.z * radius

Do you understand any of that, or do I need to go into more detail?  Once you've got that single point, then you need to rotate it round the U vector by 'angle' to get more points.

The maths is very similar to the maths in these threads used for making a look-at matrix or aligning an object to an axis.
http://dbfinteractive.com/index.php?topic=2050.0 (http://dbfinteractive.com/index.php?topic=2050.0)
http://dbfinteractive.com/index.php?topic=1874.0 (http://dbfinteractive.com/index.php?topic=1874.0)

Jim
...
<edit>Straight from Q5.01 in the comp.graphics.algorithms faq.
How to create a rotation matrix which rotates around a vector.

Our vector U above is our axis, so we can then make

x = sin(angle/2) * U.x
y = sin(angle/2) * U.y
z = sin(angle/2) * U.z
w = cos(angle/2)

Then the matrix M which rotates 'angle' degrees around U is
Code: [Select]
        M = {{1-2(yy+zz),  2(xy-wz),  2(xz+wy)},
             {  2(xy+wz),1-2(xx+zz),  2(yz-wx)},
             {  2(xz-wy),  2(yz+wx),1-2(xx+yy)}}.

If you want 10 segments, then angle is 36degrees (/180*pi for radians).  Rotate T 10 times by this matrix to generate 10 points on the circle, add on x2,y2,z2 to make each point.
</edit>
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Jim on September 06, 2007
Here's a quick sample of the maths in action (Freebasic source + EXE).
Jim
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Shockwave on September 06, 2007
A quick example you say?

I can't believe the amount of good stuff in that source code Jim, really nice, even if a lot of it is quite bewildering for me at first glance :)

I like your line draw routine too.
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: ninogenio on September 06, 2007
brilliant!
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Pixel_Outlaw on September 06, 2007
Thanks! I'm posting between work and college. I appreciate your reply and will review the code when I get a chance.
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Jim on September 07, 2007
Thanks Pixel_Outlaw, no problem :)

Quote
A quick example you say?
Very quick - I'm on holiday you know!  I used only DBF as my reference and coded it all from scratch :).

Quote
I like your line draw routine too.
Feel free to pinch it :)

Jim
Title: Re: finding a point from 2 perpindicular vectors with a radius
Post by: Shockwave on September 07, 2007
Hehe, thanks, have some Karma, I'll credit when I use it of course.