[Closed] Geometrical calculations : points, lines, planes : intersections, distances, angles
Yes, if the results are parrallel or co-incident the rounding error tells the function from telling which!
J¬
Hey PP, just wondering if you’re done adding to this thread. I want to put together a word doc containing all these functions, but wanted to wait until you were fiinished.
I think that the essential formulae are there but anyone can add some more. The possibilities are illimited.
Here is an example: this script discovers if a line intersects a sphere.
It returns true or false.
fn lineSphereIntersection sphereCenter sphereRadius linePoint lineVector = (
local nLineVector= normalize lineVector
local projPoint=linePoint+((dot (sphereCenter-linePoint) nLineVector)*nLineVector)
local dist=distance sphereCenter projPoint
if dist>sphereRadius then false else true
)
I use the projection of a point on a plane which passes by the sphere center. If the distance between this point and the center is lower or equal to the sphere radius then the line intersects the sphere. It’s based on the function ‘Point-Plane Projection’ .
Another way of going farther is to write the same functions but on objects like triangular faces or segments. For example find the position of a line that intersects a mesh face (not a plane). The case is more restricted.
I’ve got a math question, and this seems like a good place to ask. I tried digging through Mathworld, but it’s way over my head.
I’d like to be able to find a point on an arc defined by the end point of an object rotated around it’s pivot, along a specific axis. For example, let’s say there’s a bone object with a end bone as it’s child. If I rotate the parent bone around it’s Y axis 10 degrees, the end bone is now at a new position. What’s the formula for finding that position?
This page at Mathworld describes what I’m talking about, except in 2D: http://mathworld.wolfram.com/Arc.html
That should be a straight trig calculation where (skeleton code):
r = $bone01.length
theta = (rotation of your bone)
phi = 90 - theta
y = sin(theta) * r
x = cos(theta) * r
z = sin(theta) * r * cos(phi)
I’m a little hazy on the Z phi stuff, so somebody correct me if i’m wrong!
Thanks Martin. That got me going in the right direction, along with this site: http://www.kirupa.com/developer/actionscript/trigonometry.htm
Here’s what I came up with to illustrate what I wanted to do:
global polylinePosArr = #()
fn drawCircle = (
gw.setColor #line green
gw.setTransform(Matrix3 1)
gw.Polyline polylinePosArr true
gw.enlargeUpdateRect #whole
gw.updateScreen()
)
fn drawObjectCirle obj radius step:36 axis:1 = (
polylinePosArr = #()
-- get the object's position
local p = obj.pos
for i = 0 to (360 - step) by step do (
-- make a point helper
-- define the current angle around the circle
local angle = i
-- calculate the X and Y coordinates of the point on the circle using the current angle
local x = cos(angle) * radius
local y = sin(angle) * radius
-- 1 = X axis
-- 2 = Y axis
-- 3 = Z axis
global p3 = case axis of (
1:[0,x,y]
2:[x,0,y]
3:[x,y,0]
)
-- using the coordinate system of the object, define the coordinates of the current point
newP = ( (matrix3 [1,0,0] [0,1,0] [0,0,1] p3) * (obj.transform) ).translationPart
append polylinePosArr newP
)
)
-- create a box
b = box length:1 width:1 height:10 pos:(random [-10,-10,-10] [10,10,10])
b.rotation = quat (random -1 1) (random -1 1) (random -1 1) 1
-- get the length of the box, which will be used as the radius for the circle calculations
l = b.height
-- be careful with the "steps" parameter, too low (around 10) and Max will crash
drawObjectCirle b l step:18 axis:2
-- draw the circle
registerRedrawViewsCallback drawCircle
redrawViews()
/* stop drawing the circle
unregisterRedrawViewsCallback drawCircle
*/
The quaternion is easy to use to rotate a position in 3D space. You know:
1- the axis of rotation (it must be normalized)
2- the center of the rotation
3- the angle
4- the position of the point to be moved
rotAxis = [0,0,2]
rotCenter = [0,2,0]
rotAngle = 90
thePoint = [1,2,3]
q = quat rotAngle (normalize rotAxis)
pointAfterRotation = (((thePoint - rotCenter ) * q) + rotCenter)
--> [0,1,3]
The direction of the rotation can be modified by changing the direction of the axis.
if you use rotAxis = -[0,0,2], the rotation will be reversed.
Here is a variation of your function:
fn drawObjectCirle obj radius step:36 axis:1 = (
polylinePosArr = #()
local rotCenter = obj.pos
local rotAxis = obj.dir
local p
case axis of (
1: p=[radius,0,0]*obj.transform
2: p=[0,radius,0]*obj.transform
3: p=[0,0,radius]*obj.transform
)
q = quat step rotAxis
for i = 0 to (360 - step) by step do (
p = ((p-rotCenter)*q) + rotCenter
append polylinePosArr p
)
)
hi all
i am trying to make a sort of “one axis lookat” scriptcontroller.
object_a is flying around in 3d space and object_b is located at position [0,0,0]
object_b has a script_contoller at the z-rotation so that it looks at object_a.
this is what i have so far as scriptcontroller for object_b z-rotation:
dependson $object_a
v1t = at time currenttime $object_a.position
a = normalize [v1t.x,v1t.y,0]-- set z to 0 because it is not needed
upv = [0,1,0] --defining a 2d "up vector"
cosine = a.x * upv.x + a.y * upv.y
if cosine > 1 then cosine1 = 1
if cosine < -1 then cosine1 = -1
if (a.x * upv.y - a.y * upv.x) < 0 then
(
result = -cosine
)
else
(
result = cosine
)
result
it does not work
any ideas???
thanks
this seems to work:
dependson $object_a
v1t = at time currenttime $object_a.position
a = normalize [v1t.x,v1t.y,0]
ndir = normalize a
upvector = [0,0,1]
rightvector = normalize (cross upvector ndir )
thematrix = matrix3 ndir rightvector upvector ndir
pitch = ((thematrix.rotation as eulerangles).z +180)*pi/180
But is there a way not to use the matrix3 calculations?
any ideas???
I concur… this is really great, props to prettyPixel and everyone else that contributed!