I am attempting to orbit an object around a center point, much like the sun and our planets. However I am having no luck at all. Any help would be really great. Thanks. I’m writing in luna and I dont think there is a category for that here, but this probably is just general code. But if its in the wrong place, my apologies.
local function animate(event)
local rads = degrees * (Math.PI / 180);
ballOne.x = 20 * Math.cos(rad);
ballOne.y = 20 * Math.sin(rad);
degrees = degrees + 1
end
Runtime:addEventListener("enterFrame", animate)
You might try 20.0 and 180.0 to force them to floating point, otherwise your calculations might end up being integer which won’t work well, otherwise everything seems fine.
Also I suggest a unit test or two for this. If you pass in 0, 90, 180 and 260 you should get even numbers out (+/- 20,0 or 0,20). That will help you tweak it and let you believe in the results.
— edit —
Wrote a groovy version of your program because I don’t recognize a lot of your code. Found a problem with rad vs rads (mentioned in the comments). Here’s a groovy dump of your algorithm:
This works. What I meant by unit test is that you can set up something that calls your method with “0.0” degrees and ensure that the output has x of 20.0 and y of 0.0, then call it with 90 and you should get 0,20 (Actually you may get a slight floating point error, just test to see that x is less than .001 or something)
If you are using a google-approved framework to develop these apps I’m 100% sure there will be some “Official” way to do unit testing, I highly recommend you look into it, especially with a dynamic language unit tests can be absolutely critical.
One of the most important parts of unit tests is that you can run them all with a single touch inside your development environment. This ensures that no matter what code you add, everything that used to work still works. It can give you confidence to go and refactor existing code without worrying about breaking something you didn’t even know interacted.
Most people who work in dynamic languages consider unit tests an absolute requirement, and even in statically typed languages they are very important as your project grows beyond a trivial size.