tags:

views:

41

answers:

1

Hello,

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)
+1  A: 

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:

float degrees=0.0
float rads = degrees * (Math.PI / 180.0);
float x = 20.0 * Math.cos(rads);
float y = 20.0 * Math.sin(rads);
degrees = degrees + 1
println x
println y

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.

Bill K
Hmm, I still must be missing something, I changed 20 to 20.0 and 180 to 180.0, however I still see no movement when I run it. By unit test to you mean try to have it output numbers instead of telling the object where to go? I am not certain I know what a unit test is. I also left out the part where i set degrees = 100, but i assumed that was just where it started in the circle. Could you please look and see if I could change anything of if i just left something out. Thanks, James.
James Dunay
@James well the easiest way to make a unit test is just to add a main() method to every class that tests that class. Just pass in numbers and see if they equal what you expect. I'm afraid I don't know exactly what is going on, "local rads" isn't a type I'm familiar with, it needs to be equivalent to float/double, not int/long. I'll do a quick check in Java and translate a few things and post my results.
Bill K
@James One problem, did you know that you said "local rads" and later you use "rad". Dynamic languages often have one HUGE flaw in that they create variables on the fly silently if you misspell them. Otherwise your algorithm is sound, I got valid results.
Bill K
You nailed it!! thank you so much. I do have to check into unit testing, I'm a self taught flash developer attempting to cross over to iphone and android, so I usually miss important things unless someone informs me of them. But what i was writing is in Corona SDK, which runs off of the Luna language. Thanks again.
James Dunay