# Geometrical calculations : points, lines, planes : intersections, distances, angles

 04 April 2006 JHaywood Know-it-All portfolio James Haywood USA Thanks Martin. That got me going in the right direction, along with this site: http://www.kirupa.com/developer/act...rigonometry.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 */`````` share quote
 04 April 2006 prettyPixel Know-it-All cg artist make a rotation by using a quaternion 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 ) )`````` share quote
 04 April 2006 proteus2002 Veteran   student 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 Last edited by proteus2002 : 04 April 2006 at 09:55 AM. share quote
 04 April 2006 proteus2002 Veteran   student 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??? share quote
 04 April 2006 hblan Frequenter   portfolio haibo lan Shanghai, China thanks a lot . that is great topic . espacially the math website , thanks . __________________ -- to be better every day -- google talk : haibo.lan@gmail.com msn: ehblan@hotmail.com -- add me to ur IM buddy list as u wish. share quote
 05 May 2006 ajohnson dRaster.com portfolio Andrew Johnson dRaster, Inc. Irvine, USA I concur... this is really great, props to prettyPixel and everyone else that contributed! __________________ Andy J. share quote
 06 June 2006 sleepite New Member sleepite Interactive designer Changeling United Kingdom Originally Posted by proteus2002: ...But is there a way not to use the matrix3 calculations? any ideas??? The code I use for doing a look-at type operation is: ``````fn PointAxisAt a which_axis b = ( local c = normalize (b - a) local angle = acos (dot which_axis c) local axis = normalize (cross which_axis c) local out = (angleaxis angle axis) as quat out ) a = \$Pyramid01.position b = \$Point01.position q = PointAxisAt a z_axis b r = q * (inverse \$Pyramid01.rotation) rotate \$Pyramid01 r`````` Last edited by sleepite : 06 June 2006 at 10:33 AM. share quote
 08 August 2006 dutch_delight Frequenter RR london, United Kingdom THought this might be the best topic to post it: I have this camera data: posz=-12.896441 posy=10.177296 posx=-248.952271 And this for the angle: anglez=1.000000 angley=0.000000 anglex=0.000000 this is how they calculated the angle: angle = camera_target - camera angle = normalize angle anglex = angle.x angley = angle.y anglez = angle.z how would i go about transforming the angle from this data? how can i create a target or free camera from this? share quote
 08 August 2006 arketip Frequenter   arketip Bruxelles, Belgium I prefer to rename "angle" in "direction" because the angle can remind the angle of the camera (fov) ``````thePos = [posx,posy,posz] theDirection = [directionx,directiony,directionz] theLength = 100.0 theTargetPos = thePos + ((normalize theDirection)*theLength) myCam = targetCamera pos:thePos target:(targetObject pos:theTargetPos)`````` Regrettably your data are not complete : the camera's FOV is missing. share quote
 08 August 2006 dutch_delight Frequenter RR london, United Kingdom Thanks for your reply but i already solved it. I did it slightly different though: camera_target_position = point3 (posx + directionx*10) (posy + directiony*10) (posz + directionz*10) Which returns the correct 'direction'. Oh and you dont need a FOV to create a camera, it uses default setting when you leave it out. share quote
 10 October 2006 xissburg Frequenter   portfolio xiss burg 2D/3D Artist, Programmer Santo Andre, Brazil I just wanna give you two link for resources about geometry (intersections , distance computations etc): http://www.softsurfer.com (algorithms section) http://www.geometrictools.com Hope this helps share quote
 12 December 2006 davestewart Repeat-customer   Dave Stewart Animator / scripter United Kingdom Great thread! Seeing basic concepts as code snippets is fantastic! I do think that the posting of questions (as opposed to answers) clutters the "repository" feel, though. For those asking (new and unrelated) questions wouldn't it be more helpful to start and finish a new thread, THEN post (a concise answer) here? share quote
 12 December 2006 erilaz Stealthy Tea Monkey   portfolio Martin Brennand VFX Artist Melbourne, Australia Originally Posted by davestewart: I do think that the posting of questions (as opposed to answers) clutters the "repository" feel, though. For those asking (new and unrelated) questions wouldn't it be more helpful to start and finish a new thread, THEN post (a concise answer) here? This is being transferred to the CGWiki bit by bit, so the information can be kept clean: http://wiki.cgsociety.org/index.php...ns_%283dsmax%29 __________________ "There Really is No Secret" Martin Brennand - mocha Product Manager - Imagineer Systems share quote
 12 December 2006 yuenlw New Member Yuen Cartago, Costa Rica point-segment distance Hy all, I am new at maxscript and I want to calculate the shortest distance between a vertex in a skin and a bone. Can someone help me? -edit- ok, i modified pretty pixel's code and got this: `````` fn pointSegmentDist pA pB pC = ( local vAB=pB-pA local vAC=pC-pA local vBC=pC-PB local c1=dot vAB vAC local c2=dot vAB vAB if c1 < 0 do return length vAC if c2 < c1 do return length vBC (length (cross vAB vAC))/(length vAB) )`````` not sure if it is 100% correct, i have other issues but will start a new thread for that. Last edited by yuenlw : 12 December 2006 at 09:55 PM. share quote
 03 March 2007 rdg random data generator   portfolio Georg Duemlein New Zealand L-system - definitve book The Algorithmic Beauty of Plants can be downloaded here as PDF: http://algorithmicbotany.org/papers/ This is the 'definitve book' about L-Systems (see also Challenge #16). Sometimes you can find a printed version in 'antique shops' - get it! I ordered mine some weeks ago, hope it arrives soon ... Georg __________________ Georg Duemlein share quote

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off CGSociety Society of Digital Artists www.cgsociety.org Powered by vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.