嗨,我对编码很陌生,我正在使用Lua和solar2d,试图通过另一个对象2的协调器来转换object1,如果没有击中object2,object1将以相同的速度继续沿着相同的路径前进。
我可以很容易地过渡到服从的对象,但我不知道如何超越这一点。transition.to( object1,{ x=object2.x,y=object2.y,time=3000,})
我觉得我将不得不添加一个不完整的,但不确定是什么。
任何帮助都将不胜感激。
发布于 2022-10-22 18:51:22
你必须计算你所旅行的直线方程 (y =m*x+ b)。
公式:
M= (y2 - y1) / (x2 - x1)
B= y1 -m* x1
所以在你的情况下:
m = (object2.y - object1.y) / (object2.x - object1.x)
b = object1.y - m * object1.x现在,如果object1不按object2,则需要保留路径(行)的等式。
当转换结束时,您需要检查object2是否还在(object1击中了它) (object1一直在移动),因此需要包含一个onComplete侦听器来检查它。
至于速度,您必须决定是否需要恒定的速度,然后必须计算每个转换的时间,或者无论object2是接近还是远离object1,都要使用3秒。我想你可能想要第一种选择,所以如果物体离得很近,它不会走得太慢,如果物体离得太远,它就不会太慢。在这种情况下,您必须设置一个恒速s,这是您想要的。
公式:
速度=距离/时间
时间=距离/速度
两个点之间的距离:
D= squareRoot ( x2 - x1)^2 + (y2 - y1)^2 )
总之,应该是这样的:
s = 10 --Constant speed
m = (object2.y - object1.y) / (object2.x - object1.x)
b = object1.y - m * object1.x
direction = 1 --assume it's traveling to the right
if(object2.x < object1.x)then
direction = -1 --it's traveling to the left
end
local function checkCollision( obj )
if(obj.x == object2.x and obj.y == object2.y)then
-- Object1 hit Object2
else
-- Object2 is not here anymore, continue until it goes offscreen
-- following the line equation
x3 = -10 -- if it's traveling to the left
if(direction == 1)then
--it's traveling to the right
x3 = display.contentWidth + 10
end
y3 = m * x3 + b
d2 = math.sqrt( (x3 - obj.x)^2 + (y3 - obj.y)^2 )
t2 = d2 / s
transition.to( obj, {x=x3, y=y3, time=t2} )
end
end
d1 = math.sqrt( (object2.x - object1.x)^2 + (object2.y - object1.y)^2 )
t1 = d1 / s
transition.to( object1, { x=object2.x, y=object2.y, time=t1, onComplete=checkCollision} )你应该尝试不同的数值速度s,直到你得到想要的运动。
https://stackoverflow.com/questions/73488495
复制相似问题