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.
Rotating
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Rotating
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:
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.
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
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.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Rotating
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Who is online
Users browsing this forum: Bing [Bot] and 5 guests