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 Professional Discussion / Using a vector for orbit

Author
Message
Giroware
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: object position x
Posted: 20th Aug 2010 01:23
Hi guys. Im having some trouble with vectors.
I am trying to make a small sphere orbit around a bigger sphere at a distance of 10 (like a planet orbiting a sun) but I havnt got a clue how to use the vector commands properly and the manual is not very helpful here.
Could someone please help.
some code examples would be nice
thanks

It's a dark business....A dark business indeed
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 20th Aug 2010 11:51
Perhaps you don't need to use vectors exactly.

What I mean is, your really more concerned with velocity which is not necesserily related to vector movement, vectors would help, but they aren't vital.

Personally, I would send the smaller planet off, on a trajectory and let gravity affect it. So, when your moving your planet, you move it according to its X Y and Z speed, buy starting at the required distance, and shooting the orbiting planet kinda parallel to the the sun. Once you've moved the planet on it's trajectory, check the distance to the sun, and work out the 'vector' between the sun and the planet, from the planet's perspective.

So, if the sun is at 0,0,0 - always a good idea in space games to do it like that, makes it all much more straightforward as you don't have to compare positions quite as much.

PX#=1000 : PY#=0 : PZ#=0

So that's 1000 units to the right of the sun.

PXS#=0 : PYS#=0 : PZS#=1.0

And moving on the Z axis, 1 unit per loop.

Inc PX#,PXS# : Inc PX#,PXS# : Inc PX#,PXS#

Then work out that distance to the sun, with SQRT.

Now, planets are never pushed away from the sun, they are falling towards it constantly, but are moving too fast, so they orbit instead. You can do the same. Work out the required distance to orbit on, in this case 1000 units, also the start X position. Now, if the distance is >1000 you know that the planet is being thrown out of orbit, so needs adjusted, so work out the vector to the sun, and add that to the velocities.

Maybe something like:
if Dist#>1000
GX#=PX#*-0.001
GY#=PY#*-0.001
GZ#=PZ#*-0.001
Inc PXS#,GX#
Inc PYS#,GY#
Inc PZS#,GZ#
endif

So as the planet moves away from the sun, it's velocity is affected by the gravitational pull, as long as it's moving away - if the planet is closer to the sun, then it should stay on it's current trajectory and pick up the orbit when it moves on.

Health, Ammo, and bacon and eggs!
Giroware
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: object position x
Posted: 20th Aug 2010 18:00
Thanks that answers my question in a Mr Spok kinda way That is really useful for modeling an accurate solar system. I suppose u can modify it do give eccentric orbits and such by fluctuating its gravitational pull or velocity.
Very Good
Kirk out

It's a dark business....A dark business indeed
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 21st Aug 2010 10:44 Edited at: 21st Aug 2010 10:57
Here's a quick and dirty way to have planets orbiting a sun...



Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Aug 2010 11:28 Edited at: 21st Aug 2010 11:40
VanB's method is great if you need an accurate planetary simulation. However, if you don't need accuracy and just need one object to orbit another, then I would suggest using a spherical co-ordinate system.

Instead of using cartesian co-ordinates (x, y & z) that DBPro uses, you supply a lattitude, longtitude and radius. It is then a simple case of increasing one or both of lattitude and longtitude and you would get an orbital function.

Of course DBP needs its co-ordinates to be cartesian so you would need to have a spherical to cartesian co-ordinate conversion function.

I have a Sun, Planet and Moon demo somewhere, give me a minute ....

Here you go:




Login to post a reply

Server time is: 2026-07-25 12:05:23
Your offset time is: 2026-07-25 12:05:23