Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

How to Find Distance and Angle Between Two Helper Points

How to Find Distance and Angle Between Two Helper Points

ttazinazzo
Participant Participant
942 Views
10 Replies
Message 1 of 11

How to Find Distance and Angle Between Two Helper Points

ttazinazzo
Participant
Participant

I'm a newbie to maxscript/3DS Max so this might be an easy question but I'm struggling to get this to work.

 

I have an arm hinge that is connected to a base and can swing clockwise/counter-clockwise about the hinge point.

There is another small base to the left of the arm, where I placed a helper point Point013 - this point is fixed and cannot move.

An additional helper point named Point012 was placed on a connection hole in the arm and is constrained to that location of the arm - Point012 represents the connection point of an actuator that can extend vertically and rotate in the 2D plane and will follow and remained centered to the arm hole as the arm rotates.

 

Visual is below:

 

Actuator.PNG

In my code, I need to calculate both the distance between Point012 and Point013, as well as the angle that their connection line forms with the horizontal plane.

Ideally this would then be used in a trackview for the distance calculation and another trackview for the angle calculation so that both can be programmed in an animation.

 

I have the (x,y,z) coordinates for both points and geometrically calculated the initial distance between the points at their initial position shown above, as well as the initial angle.

 

How can I write code that iteratively calculates the distance and angle between those points that I can use in a trackview to program the animation?

I'm trying to use the distance function at least for the initial calculation but when I do distance Point012 Point013 I get an error stating:

 

-- No ""distance"" function for undefined
-- MAXScript callstack:
-- thread data: threadID:2236
-- ------------------------------------------------------
-- [stack level: 0]
-- In top-level

0 Likes
Accepted solutions (1)
943 Views
10 Replies
Replies (10)
Message 2 of 11

Swordslayer
Advisor
Advisor
0 Likes
Message 3 of 11

istan
Advisor
Advisor

Is the "hinge object" also fixed? If yes, you do not even need a special controller - just parent/child everything, setup all pivots and DOFs and animate it. Otherwise I proably did not understand your question.

0 Likes
Message 4 of 11

leeminardi
Mentor
Mentor

The following code snippet should do what you want.

(
p12 = $Point012
p13 = $Point013
TheArm = $Arm
d = distance p12.position p13.position
format "\nDistance p12 to p13 = %" d
v = normalize(p12.position - p13.position)	
ArmRotation = acos (dot v [1,0,0])
format "\nRotation Angle = %\n" ArmRotation
)
lee.minardi
0 Likes
Message 5 of 11

denisT.MaxDoctor
Advisor
Advisor

your whole idea is not very clear to me either, but take a look at this possible solution:

 

 

delete objects


with redraw off
(
	s = point name:#source pos:[-100,-50,0] wirecolor:green
	t = point name:#target pos:[-70,20,0] wirecolor:yellow
	h = point name:#hinge  pos:[10,10,0] wirecolor:red

	in h 
	(
		d = exposetm name:#data useParent:off transform:h.transform exposeNode:t localReferenceNode:s
		c = d.transform.controller = transform_script()
		c.addnode #parent h
		c.addobject #self d.baseobject
		c.setexpression "format \"distance:% angle:%\\n\" self.distance self.angle; matrix3 1" 
	)
)

 

 

transform_script is added to the snippet just to show that this whole “construction” is alive and reacts to all involved transformations.

This solution is very typical for Maya users, but not a classic for MAX. I agree with Swordslayer that using scripted controllers is more natural for MAX and this particular case.

0 Likes
Message 6 of 11

leeminardi
Mentor
Mentor

From the other responses you got from highly knowledgeable users I may be misinterpreting your question.  In the event that I am not I should add that in my post I included the statement: 

TheArm = $Arm

which is never referenced. This line can be deleted.

I assume that Point 012 is linked to the arm.  If the code were in an expression or script controller you would need to include Point012 times the transformation matrix of Arm to get its world coordinates.  Point012's position in a max script is in world coordinates already so it is not necessary to reference arm.

lee.minardi
0 Likes
Message 7 of 11

ttazinazzo
Participant
Participant

Thanks, I'll take a look at this video.

0 Likes
Message 8 of 11

ttazinazzo
Participant
Participant

The arm is attached to the triangular base on the ground and is fixed at the hinge point.

So it can rotate about that hinge point.

0 Likes
Message 9 of 11

ttazinazzo
Participant
Participant

Hey hey, your example code is perfect - exactly what I was looking for.

I believe for the purposes of this project I will have to use a script controller - so the only difference to writing the script controller would be getting the world coordinates of the helper point?

 

I also noticed just typing in the expressions in the listener that it wouldn't automatically catch the new positions if the arm was rotated. As in, setting d = distance p12.position p13.position would result in the value of 96.4993 and then once the arm is rotated just typing the variable d in the listener and hitting enter still results in 96.4993. But when I retyped d = distance p12.position p13.position in the listener at the new arm position then it resulted in the corrected value of 124.875. If these lines of script were to be used in a script controller, would these calculations be updated instantaneously if an animation were to be created?

0 Likes
Message 10 of 11

leeminardi
Mentor
Mentor
Accepted solution

To get the distance and angle to dynamically update the script needs to be re-executed.  I'm not sure the best way to do that.  I'm sure @denisT.MaxDoctor could help with that.

 

If you want to use a Script Controller rather than a Maxscript you could do something like the following:

I've created a dummy object with the following script controller.

leeminardi_0-1656344332626.png

Rotating the arm dynamically outputs the distance and the angle to the Listener.   I assume you want to use these values to control something else but it is not clear to me what or how you want to do this.

 

 

lee.minardi
0 Likes
Message 11 of 11

ttazinazzo
Participant
Participant

Thank you so much for the help, I really appreciate it! 🙂

0 Likes