Rotating

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
flaminarrow
Prole
Posts: 1
Joined: Mon May 09, 2011 12:55 am

Rotating

Post 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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Rotating

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Rotating

Post by Taehl »

That sounds kind of like a demo called Terus I made with Ensayia. Are you trying for something like this?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests