如下图所示,我正在创建一个程序,用于制作由两个铰接部件组成的卡车的2D动画。
卡车拉着拖车。
拖车根据卡车上的对接轴移动。
然后,当卡车转动时,拖车应该逐渐与卡车的新角度对齐,就像在现实生活中一样。
我想知道是否有任何公式或算法可以轻松地进行此计算。
我已经看过反向运动学方程式,但我认为只有2个部分它不会那么复杂。
有人能帮助我吗?
答案 0 :(得分:3)
让A
为前轴下方的中点,B
为中轴下方的中点,C
为后轴下方的中点。为简单起见,假设挂钩位于B
点。这些都是时间t
的所有功能,例如A(t) = (a_x(t), a_y(t)
。
诀窍是这个。 B
直接向A
移动,A
的速度在该方向上。dB/dt = (dA/dt).(A-B)/||A-B||
或者在符号中,dC/dt = (dB/dt).(B-C)/||B-C||
类似地,.
其中A
是点积。
这变成了6个变量中的非线性一阶系统。这可以通过常规技术解决,例如https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods。
更新:添加了代码
这是一个Python实现。您可以将https://rosettacode.org/wiki/Runge-Kutta_method替换为您喜欢的语言和您喜欢的线性代数库。甚至手动滚动。
在我的示例中,我从(1, 1)
B
开始,(2, 1)
C
和(2, 2)
A
开始。然后以0.01
为步长将#! /usr/bin/env python
import numpy
# Runga Kutta method.
def RK4(f):
return lambda t, y, dt: (
lambda dy1: (
lambda dy2: (
lambda dy3: (
lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6
)( dt * f( t + dt , y + dy3 ) )
)( dt * f( t + dt/2, y + dy2/2 ) )
)( dt * f( t + dt/2, y + dy1/2 ) )
)( dt * f( t , y ) )
# da is a function giving velocity of a at a time t.
# The other three are the positions of the three points.
def calculate_dy (da, A0, B0, C0):
l_ab = float(numpy.linalg.norm(A0 - B0))
l_bc = float(numpy.linalg.norm(B0 - C0))
# t is time, y = [A, B, C]
def update (t, y):
(A, B, C) = y
dA = da(t)
ab_unit = (A - B) / float(numpy.linalg.norm(A-B))
# The first term is the force. The second is a correction to
# cause roundoff errors in length to be selfcorrecting.
dB = (dA.dot(ab_unit) + float(numpy.linalg.norm(A-B))/l_ab - l_ab) * ab_unit
bc_unit = (B - C) / float(numpy.linalg.norm(B-C))
# The first term is the force. The second is a correction to
# cause roundoff errors in length to be selfcorrecting.
dC = (dB.dot(bc_unit) + float(numpy.linalg.norm(B-C))/l_bc - l_bc) * bc_unit
return numpy.array([dA, dB, dC])
return RK4(update)
A0 = numpy.array([1.0, 1.0])
B0 = numpy.array([2.0, 1.0])
C0 = numpy.array([2.0, 2.0])
dy = calculate_dy(lambda t: numpy.array([-1.0, -1.0]), A0, B0, C0)
t, y, dt = 0., numpy.array([A0, B0, C0]), .02
while t <= 1.01:
print( (t, y) )
t, y = t + dt, y + dy( t, y, dt )
拉到原点。这可以改变为你想要的任何东西。
A = [[5,-5,5],[7,7,7],[-6,-6,6]]
B = [[2,2,-1],[-1,2,4],[0,-4,3]]
index = [1,2]
def Matrix_Interchange(A,B,index):
print("Value of A Inputed",A)
Temp = A
for i in index:
Temp[i-1][:]=B[i-1][:]
print("Value of A in For Loop",A)
return(Temp)
Matrix_Interchange(A,B,index)
print(A)
答案 1 :(得分:0)
根据我看到的答案,我意识到解决方案并不是很简单,必须通过反向运动学算法来解决。
This site是一个例子,它只是一个开始,虽然它仍然没有解决所有问题,因为C点是固定的,而在卡车的情况下它应该移动。
答案 2 :(得分:0)
基于这个Analytic Two-Bone IK in 2D article,我做了一个fully functional model in Geogebra,其中核由两个简单的数学方程组成。