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

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  04 April 2006
Thanks Martin. That got me going in the right direction, along with this site:

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
 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 (
 		-- 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
 /* stop drawing the circle
 unregisterRedrawViewsCallback drawCircle
  04 April 2006
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
  04 April 2006
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
result = cosine


it does not work
any ideas???


Last edited by proteus2002 : 04 April 2006 at 09:55 AM.
  04 April 2006
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???
  04 April 2006
thanks a lot .

that is great topic . espacially the math website , thanks .
-- to be better every day --
google talk :
-- add me to ur IM buddy list as u wish.
  05 May 2006
I concur... this is really great, props to prettyPixel and everyone else that contributed!
Andy J.
  06 June 2006
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
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.
  08 August 2006
THought this might be the best topic to post it:

I have this camera data:

And this for the angle:

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?
  08 August 2006
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.
  08 August 2006
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.
  10 October 2006
I just wanna give you two link for resources about geometry (intersections , distance computations etc): (algorithms section)

Hope this helps
  12 December 2006
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?

  12 December 2006
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:
"There Really is No Secret"
Martin Brennand - mocha Product Manager - Imagineer Systems
  12 December 2006
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?


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.
  03 March 2007
L-system - definitve book

The Algorithmic Beauty of Plants can be downloaded here as PDF:

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 Duemlein
reply share thread

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
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:26 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.