[SOLVED] Help with vector move macro
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[SOLVED] Help with vector move macro
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.
- wandererfan
- Veteran
- Posts: 6324
- Joined: Tue Nov 06, 2012 5:42 pm
- Contact:
Re: Help with vector move macro
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
Re: Help with vector move macro
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.