Share My Creation Sun, Earth and moon revolution orbit


For learning purposes, I wrote a code that rotates an object in circle in pure way WITHOUT USING ANY 3RD PARTY LIB OR FUNCTION ;).
Important code is:
Rotate an child object around a parent object:
Sub rotate( parent As ImageView, child As ImageView, latency As Int, r As Int) As ResumableSub
    
    Dim parent_vertical_center As Double
    Dim parent_horizontal_center As Double

    '360 degree = 6.28319 radians
    '1 degree = 0.0174533 radians   
    Dim angle As Double =6.28319
    
    Do While True
        If angle < 0.0174533  Then angle=6.28319
                
        parent_vertical_center   = parent.Top  + ( (parent .Height - child.Height) / 2 )
        parent_horizontal_center = parent.Left + ( (parent.Width   - child.Width ) / 2 )
        
        Dim x As Int = parent_horizontal_center + (r * Sin(angle) )
        Dim y As Int = parent_vertical_center   + (r * Cos(angle) )  'https://stackoverflow.com/a/16802483
        
        child.Left = x
        child.Top  = y
        
        angle = angle - 0.01
        Sleep(latency)
    Loop
    
End Sub

and I called it like:
B4X:
    rotate(sun, earth, 35, sun.Width+10)   'rotate earth around sun with 35 latency and distance from sun to earh is sun width + 10
    rotate(earth, moon, 1, earth.Width-10) 'rotate moon around earth with 1 latency and distance from moon to earh is earth width - 10

Hope you like it :p.
 

Attachments

  • orbit.zip
    22.5 KB · Views: 140

kimstudio

Active Member
Licensed User
Longtime User
A small suggestion that could deal with value wrap around better:

B4X:
Dim Pi2 as double = 2 * cPi
Dim angle As Double = Pi2
'...
If angle < 0.0  Then angle=Pi2 + angle  'If angle < 0.0174533  Then angle=6.28319
'...
 

AbdurRahman

Member
A small suggestion that could deal with value wrap around better:

B4X:
Dim Pi2 as double = 2 * cPi
Dim angle As Double = Pi2
'...
If angle < 0.0  Then angle=Pi2 + angle  'If angle < 0.0174533  Then angle=6.28319
'...
Thanks for your interest and contribution.
Yeah now it would be more systematic way.
I tried to edit post, but unfortunately unable to find EDIT option. Not sure why.

Screenshot 2022-09-11 at 16-44-58 Sun Earth and moon revolution orbit.png
 

AnandGupta

Expert
Licensed User
Longtime User
I mean that naturally the moon shows the earth the same face and well in the app it shows several faces to the earth
@AbdurRahman ,

I think @Johan Hormaza is just mentioning what is the fact, but
For learning purposes, I wrote a code that rotates an object in circle in pure way WITHOUT USING ANY 3RD PARTY LIB OR FUNCTION ;).
you have made the project for "learning purpose" , so do not take it as criticism of your hard work.
Hope to see more contributions from you in future.
 

AbdurRahman

Member
I mean that naturally the moon shows the earth the same face and well in the app it shows several faces to the earth
Sorry still didn't understood.
Do you mean should the moon face shouldn't move according to earth ?

My IQ level is bad 😌.

you have made the project for "learning purpose" , so do not take it as criticism of your hard work.
Hope to see more contributions from you in future.
I'm open minded and criticism not really an issue for me. 😊
 

Johan Schoeman

Expert
Licensed User
Longtime User
We always see the same face of the moon. That is why the opposite side of the moon is called the dark side of the moon. So, as what the eath rotates around it's own axis the moon rotates around it's own axis too but is such a way that we only ever see the same side of the moon facing us.
 

AbdurRahman

Member
We always see the same face of the moon. That is why the opposite side of the moon is called the dark side of the moon. So, as what the eath rotates around it's own axis the moon rotates around it's own axis too but is such a way that we only ever see the same side of the moon facing us.
Oh my goodness, now I understood.
Yes that what I missed. and its in my future plan to rotate them on their on axis as well.
Thanks so much Johan. 🤗
 

Johan Schoeman

Expert
Licensed User
Longtime User
Oh my goodness, now I understood.
Yes that what I missed. and its in my future plan to rotate them on their on axis as well.
Thanks so much Johan. 🤗
Look at this sample


Assume the Jupiter image is the sun - the earth orbits the sun and also rotate around its own axis at the same time. The moon orbits the earth and rotates around its own axis too. They are both rotating around their own axis, both orbiting the sun but at the same time the moon is also orbiting the earth. But the moon orbits around the earth and rotates around its own axis in such a way that we are basically seeing the same side / face of the moon all the time (a little bit this way and a little bit that way but basically the same face all the time as observed from Earth).
 

AbdurRahman

Member
UPDATE 2:
1. Improved rotation accuracy (Thanks @kimstudio)
2. Made all objects to rotate its own axis as well (Thanks @Johan Hormaza and @Johan Schoeman )


Important Codes:
Important Variables:
Sub Globals
    Private pi As Float = 22 / 7
    Private angle_max  As Float =  2 * pi           ' 360 degree = 2pi
    Private angle_min  As Float = (2 * pi) / 360    ' 001 degree = 2pi / 360
End Sub

Rotate an child object around parent object with specified latency and radius:
Sub rotate( parent As ImageView, child As ImageView, latency As Int, r As Int, parent_img As Bitmap, child_img As Bitmap, rotateParent As Boolean) As ResumableSub
    Dim parent_vertical_center As Float
    Dim parent_horizontal_center As Float
    Dim angle As Float = angle_max
    Do While True
        If angle < angle_min Then angle = angle_max    'Reset angle
       
        parent_vertical_center    = parent.Top + ((parent .Height - child.Height)/2) ' lenght from top to center of parent vertically
        parent_horizontal_center  = parent.Left + ((parent.Width - child.Width)/2) ' lenght from left to center of parent horizontally
       
        Dim x As Float = parent_horizontal_center + (r * Sin(angle))
        Dim y As Float = parent_vertical_center   + (r * Cos(angle))  'https://stackoverflow.com/a/16802483
        child.Left = x
        child.Top = y
       
        If rotateParent Then RotateView(parent, parent_img,angle) 'Because earth is used two times. To prevent flickering and rotating times, used If rotateParent ...
        RotateView(child, child_img, angle) 'Rotate around its own axis
       
        angle = angle - 0.03 'increase to increase rotation speed
        Sleep(latency)
    Loop
   
End Sub

https://www.b4x.com/android/forum/threads/how-to-rotate-an-imageview.21480/post-124236 :
Sub RotateView(v As View, bmp As Bitmap, radians As Float)
    Dim clockwise_angle As Float = angle_max -( radians * (180/pi))
    Dim anti_clockwise_angle As Float = radians * (180/pi)
    Dim c As Canvas
    Dim r As Rect
    c.Initialize(v)
    r.Initialize(0,0,v.Width,v.Height)
    c.DrawRect(r,Colors.transparent,True,0)
    c.DrawBitmapRotated(bmp,Null,r,clockwise_angle)
    v.Invalidate
End Sub
 

Attachments

  • orbit.zip
    23.2 KB · Views: 95
Top