[SOLVED] Help with vector move macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Navee
Posts: 3
Joined: Mon Mar 26, 2018 6:58 pm

[SOLVED] Help with vector move macro

Post by Navee »

Hi everyone. I'm trying to make an object move toward the 0,0,0 point by a specific amount. For example, if my object is at 200,100,0 and I want to move it 50 millimeters directly toward the 0,0,0 point, how could I compute that? (I'm not using the z plane so really, I would do this on a 2 dimensional scale of x,y and leave z as 0). I can't figure out how to determine the end position for that. I know how to take two coordinates and determine the distance, but can't figure out how to reverse that to arrive at a coordinate a certain distance away. I know the proper commands for replacing in abject to a certain spot and redraw it. It's just the mathematical formula I need that I can't figure out. Any assistance would be appreciated.
Last edited by Navee on Thu Apr 26, 2018 4:03 pm, edited 1 time in total.
User avatar
wandererfan
Veteran
Posts: 6324
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Help with vector move macro

Post by wandererfan »

Navee wrote: Tue Apr 24, 2018 8:33 pm For example, if my object is at 200,100,0 and I want to move it 50 millimeters directly toward the 0,0,0 point, how could I compute that?
You need the unit vector that points from where you are to where you're going. In this case, it is easy (0,0,0) - (200,100,0) = (-200,-100,0), then normalize it. Once you have the vector that points in the right direction, multiply it by the distance you want to travel and add the result to your current location. Something like this (not actually tested!):

Code: Select all

current = FreeCAD.Vector(200,100,0)
target  = FreeCAD.Vector(0,0,0)
distance = 50
direction = target - current
direction.normalize()
movement = direction * distance
newposition = current + movement
Navee
Posts: 3
Joined: Mon Mar 26, 2018 6:58 pm

Re: Help with vector move macro

Post by Navee »

Thanks a lot. That worked for me. I was able to use that, combined with some arrays and loops to create the animation I was looking for.
Post Reply