Orbital Rotation

Posted by wintermuteai1, Posted on September 30, 2011

1 vote

Showing how to set up an orbital rotation for your corona sdk
project. In this example the moon orbits the earth.
Download Source:
http://www.sims3tinkers.com/orbitalrotationcoronasdk.zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- 
-- Orbital Rotation - Moon around Earth
-- Demonstrates one method for orbiting objects
-- By Wintermuteai1 - http://www.sims3tinkers.com
-- Version: 1.0
-- 
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2011 ANSCA Inc. All Rights Reserved.
 
local physics = require("physics")
physics.start()
--system.activate( "multitouch" )
physics.setGravity(0, 0)
--physics.setDrawMode( "hybrid" )
display.setStatusBar( display.HiddenStatusBar )
 
 
 
 
 
 
 
local background = display.newImage( "space.png", true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
 
 
local planet1 = display.newImage( "earth.png" )
planet1.x = 160; planet1.y = 230
planet1.xScale = 0.7
planet1.yScale = 0.7
physics.addBody( planet1, {radius=40 } )
planet1.bodyType = "static"
 
local moon = display.newImage( "moon.png", 40, 40)
moon.x = planet1.x + 80
moon.y = planet1.y
moon.xScale = 0.3
moon.yScale = 0.3
moon.rotation = 100  
physics.addBody( moon, {radius=16 } )
moon.bodyType = "dynamic"
 
myJoint = physics.newJoint( "pivot", moon, planet1, planet1.x, planet1.y )
 
local rotateIt = function( event )    
        moon.rotation = moon.rotation + 1.5
end
 
Runtime:addEventListener( "enterFrame", rotateIt )


Replies

nicholas.windsor's picture
nicholas.windsor
User offline. Last seen 1 week 2 days ago. Offline
Joined: 6 Apr 2011

Works a treat, thanks.

Code is very easy to follow, even for a newbie.

Thanks for sharing.

Nick

nicholasclayg's picture
nicholasclayg
User offline. Last seen 13 hours 32 min ago. Offline
Joined: 16 May 2011

Dr Rafeal H. Did a youtube post on this some time back, basically doing what you are doing. Uses the same earth and moon as you as well.

Different ways to do different things.

http://www.youtube.com/watch?v=sKAWaLLG9vQ&feature=fvsr

wintermuteai1's picture
wintermuteai1
User offline. Last seen 3 weeks 1 day ago. Offline
Joined: 10 May 2011

@nicholasclayg:
Very interesting! I hadn't seen that before.
As you say different ways to accomplish rotation,
Corona is such a great tool!
cheers
-Winter

@nicholas.windsor: Glad you like it!

Nathan B's picture
Nathan B
User offline. Last seen 1 hour 5 min ago. Offline
Joined: 18 Feb 2011

Nice! If I wanted to stop the rotation of the moon later on, with the press of a button for example, what would be the command to make it stop? Thanks!

akamel's picture
akamel
User offline. Last seen 17 weeks 5 days ago. Offline
Joined: 10 Oct 2011

Hey Nathan,
.. you can simply do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local function stopRotation(event)
        Runtime:removeEventListener("enterFrame", rotateIt)
end     
 
 
stopButton = ui.newButton{
default = "buttonRed.png",
over = "buttonRedOver.png",
x = 160,
y = 450,
onPress = stopRotation,
text = "Stop",
emboss = true
}

and you should type this line in the begining of the main.lua file
local ui = require("ui")

cheers ;)

brian-nexus's picture
brian-nexus
User offline. Last seen 3 weeks 3 days ago. Offline
Joined: 23 Feb 2011

I am using a very similar setup to this and I've noticed when I place "moon.x = planet1.x + 57" or closer the rotation doesn't work.

I prefer to keep using pivot joints with .rotation. Any thoughts?