Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Small Problem with planets orbiting sun in 2D DBC

Author
Message
Hangar18
18
Years of Service
User Offline
Joined: 13th Mar 2007
Location:
Posted: 6th Apr 2007 04:27
The code below works 99% of the time but because I've broken down the equations down into 4 quadrants, sometimes a planet gets stuck at either 90, 180, 270 or 360 degrees. I've tried and tried to fix it but no luck.

Rem orbit planet PX and PY are the x,y coords of planet "i"
Rem H is the hypotenuse to the sun which is at 0,0
Rem A2# and step(i) is my unsuccesful (crude) attempt to force
Rem the planet into the next quadrant
Rem objects 300-306 are the planets

PX# = val(system$(i,5))
PY# = val(system$(i,6))
H# = sqrt(abs(PX#)^2+abs(PY#)^2)
A1#=+0.35/H#*50*val(system$(i,8))
A2#=A1#*1

if step(i) <> 4 then step(i) = 0

Rem 0-90 range
if step(i) = 4 or PX#>=0 and PY#>=0
A# = asin(PX#/H#)
A# = A#+A1#
PX# = H#*sin(A#)
PY# = sqrt(H#^2-abs(PX#)^2)
if abs(PY#)<A2# then step(i) = 1
endif


Rem 90-180 range
if step(i) = 1 or PX#>=0 and PY#<=0
A# = asin(abs(PY#)/H#)
A# = A#+A1#
PY# = H#*sin(A#)*-1
PX# = sqrt(H#^2-abs(PY#)^2)
if abs(PX#)<A2# then step(i) = 2
endif

Rem 180-270 range

if step(i) = 2 or PX#<=0 and PY#<=0
A# = asin(abs(PX#)/H#)
A# = A#+A1#
PX# = H#*sin(A#)*-1
PY# = sqrt(H#^2-abs(PX#)^2)*-1
if abs(PY#)<A2# then step(i) = 3
endif

Rem 270-360 range
if step(i) = 3 or PX#<=0 and PY#>=0
A# = asin(abs(PY#)/H#)
A# = A#+A1#
PY# = H#*sin(A#)
PX# = sqrt(H#^2-abs(PY#)^2)*-1
if abs(PX#)<A2# then step(i) = 4
endif

system$(i,5)=str$(PX#)
system$(i,6)=str$(PY#)

Position object 300+i-1,PX#,PY#,20

Next i
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 6th Apr 2007 13:55
Hello,

Is there a reason you are breaking it into 4 quadrants?

You may be making things more difficult for yourself. Your approach seems sound but the method may need adjusting.

It's easier for me if I think in terms of an elypse or circle. I'll say the Hypotenuse is really a representation of a radius - the distance from the sun to any particular body or planet. For my example, I don't need to care about quadrant - I can pick that up later depending on the position of a planet. So far so good?

Ok, a point on a circle (orbit) can be represented in 2d cartesian coordinates with

x=radius*cos(angle to x axis)+center x offset
y=radius*sin(angle to x axis)+center y offset

So, just like you were calculating C (hypotenuse c^2=a^2+b^2), we can calculate the radius from 0,0 to whatever the center of a planet is (let's say 100,100)

let's call x=x , y=y, theta=angle to x axis, r=radius

r=sqrt((100-0)^2+(100-0)^2)=~141

Now all we need is a loop to change the angle and we can calculate the x and y position of the planet whose origin is 100,100

do
theta=wrapvalue(theta+1)
x=r*cos(theta)+0
y=r*sin(theta)+0

rem do something with x and y
loop

That's all you need to calculate the orbit. If you want an eliptical orbit, then you need a second radius to use in the y calculation.

For the quadrants, you can query theta at any given time to see what it's angle is. If it's between 0 and 90, it's in quadrant 1 etc.

Enjoy your day.
Hangar18
18
Years of Service
User Offline
Joined: 13th Mar 2007
Location:
Posted: 7th Apr 2007 11:50
Thanks! I have a hazy memory when it comes to trig (and which sectors cos, sin etc are pos/neg).

Login to post a reply

Server time is: 2025-05-28 20:52:11
Your offset time is: 2025-05-28 20:52:11