Corona SDK - 计算球位置与线旋转

我有一个简单的问题,但是解决起来非常困难和糟糕,需要你的帮助。 我只是想要我的 myCircle 在我的 myLine x、y 上,但需要进行旋转,这很糟糕,所以...

我的对象:

My "obj" is working well..

myLine = display.newRect( obj.x, obj.y , 150, 5 )
myCircle = display.newCircle( myLine.x, myLine.y, 8 )

我每进入一帧都会得到这个:

myLine.x = obj.x
myLine.y = obj.y
myLine.rotation = obj.rotation

myCircle.x = myLine.x
myCircle.y = myLine.y

它运行得很好...但是,我需要在运行时-enterFrame中,每当线的旋转发生变化时,myCircle都会朝着相同的方向移动。如果 myCircle 在 obj.x 和 obj.y 或 myLine.x 和 myLine.y 上,则它能很好地工作。但是我需要,myCirle 就像处于 myLine 的 1 端一样。 像这样,但更好:

if rotation == 0 then
   myCircle.x = myLine.x
   myCircle.y = myLine.y - 30
elseif rotation == 90 then
   myCircle.x = myLine.x + 30
   myCircle.y = myLine.y
end
..

等等,我想你已经得到了这个想法。

我需要的就是得到这个:

  *
 /
/

*
|
|

...

谢谢任何帮助。

点赞
用户2114015
用户2114015

我认为你尝试的是旋转一条线,但让圆圈停留在线的末端。如果是这样,那么你需要使用 math.sin() 和 math.cos()。

myCircle.x = myLine.x + 30 * math.cos(rotation) --30 would be the length of the line.
myCircle.y = myLine.y + 30 * math.sin(rotation)

这应该可以,但如果不行,尝试将 sin 用于 x,cos 用于 y。

2013-10-23 00:48:47