user2679738
user2679738

Reputation: 13

Corona SDK - Calculate position of ball with line rotation

I got simple question, but very difficult and bad solution, I need your help. I simply want myCircle in myLine x,y, but with change of rotation, It's bad, so..

My objects :

My "obj" is working well..

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

I got for every enterFrame this :

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

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

And It's working fine.. but, I need to do, myCircle for runtime - enterFrame, with every change of line rotation will be moving in same direction. It's working fine, if myCircle is in obj.x and obj.y.. or myLine.x and myLine.y. But I need, that myCirle would be like on the 1 end of the myLine.. Something like this, but better :

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
..

And so on, I think you get the idea.

All i need is to get this :

  *
 /
/

*
|
|

..

Thx for any help.

Upvotes: 0

Views: 177

Answers (1)

Keaton mcguirk
Keaton mcguirk

Reputation: 21

what I think you are trying to do is rotate a line, but have the circle stay at the end of the line. If that is what you are trying to do, then math.sin() and math.cos() are what you need.

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

that should work, but if it doesn't, try using cos for y and sin for x instead.

Upvotes: 0

Related Questions