Page 1 of 1

Rotating

Posted: Mon May 09, 2011 1:02 am
by flaminarrow
Hey. So I am trying to learn LUA as quick as I can(know only a couple other languages). and I know how to make a object move with the given key input, but I need to know how to move and object left or right while always facing center. So like in a circular motion. Snippets of code are greatly appreciated!

-Thanks.

Re: Rotating

Posted: Mon May 09, 2011 1:51 am
by BlackBulletIV
You'll need to find the angle between the object and the centre of the screen. You can do this using math.atan2; I always have a function called math.angle that I keep to do this:

Code: Select all

function math.angle(x1, y1, x2, y2)
  local a = math.atan2(y2 - y1, x2 - x1)
  return a < 0 and a + math.pi * 2 or a
end
The last line is to ensure the result is a positive angle (as atan can return a negative).

Once you've done that, you'll need to pass the two points in, and then rotate the graphic via the desired rotation. You'll probably want to have the object rotating around its centre too; I cover that in my article on draw origins.

Re: Rotating

Posted: Mon May 09, 2011 7:58 am
by Taehl
That sounds kind of like a demo called Terus I made with Ensayia. Are you trying for something like this?