Okay so, what I want to do SOUNDS relatively simple, but I've come to understand it's not.
What I want to do is get an object to orbit another object, with pixel movement enabled. Sadly there isn't an orbit proc so I have no idea how to do this. Also consider that my math skills aren't the greatest.
To keep things simple, said orbit should be a constant, perfect circle.
Any ideas, even a starting point, would be amazing if anyone doesn't mind lending a hand.
EDIT: I say orbit because the primary object may or may not move, so I need it to understand where it is and where it should be.
ID:155026
![]() Oct 7 2011, 4:54 pm
|
|
You beat me to it, I was writing up a snippet as well. instead of doing spawn for() you can do a while loop and add a sleep(-1) before it so you do get an infinite loop error.
|
I attempted to convert it into this so I could use it with my naming scheme:
proc/orbit(var/atom/movable/a,atom/movable/b,distance,rotation) Problem is, I get a 'division by zero' error. Here's how I'm calling the proc: var/celestial/planet/C = new B(locate(x,O.y,world.maxz)) get_dist IS reporting accurately, but maybe I'm misunderstanding what distance is suppose to be set to? |
Oh, I forgot to tell you about that division by zero that you'll have to avoid.
If your planet and satelite have the same x value, there will be division by zero. You can handle that how you want, perhaps imposing a extremely small x distace (like 0.000001) if the x values are the same. |
Even increasing the x position of 'a' still returns a division by zero problem.
EDIT: This actually may be a problem on my end. Searching... |
The problem was having the same y axis, now my new problem is all my orbiting objects disappeared. lol
EDIT: Problem traced to set_location. px and py are coming in as 0. var/celestial/planet/C = new B(locate(x,O.y+1,world.maxz)); That's how I 'fixed' the zero issue. Refer to my second reply for the 'original' code. |
Proc together with demo:
proc/orbit(obj/O, x, y, speed, dist) Trivia: changing nx calculation to round(c * s * dist + x, 1) causes pixel to 'draw' number 8 |
Going off of this, how would one simulate elliptic (I believe this is what it's called? Orbit that's not a perfect circle, but more like an oval) orbit as well as orbital decay (an object will orbit the source so many times, and each time it will get a little bit closer to the source until it collides with it)?
|
Making elliptic movement is pretty easy. You have to make two different distance variables, one for X and one for Y. Make one larger or smaller than the other and you'll have an elliptical movement.
Orbital decay could be done by gradually decreasing the distance variables attached to the orbiting object and the object will gradually get closer and closer to the source, and finally just stop the orbit when the distance becomes zero. |
LordAndrew wrote:
Going off of this, how would one simulate elliptic (I believe this is what it's called? For elliptic movement along x or y axis, simple difference between distx and disty will be enough (which is pretty much what Oasiscircle said). var/nx = round(c * distx + x, 1) 'Rotated' orbits require some extra calculations: proc/orbit(obj/O, x, y, speed, distx, disty, elipseAngle) Picture shows two orbits (15 degree and 45 degree) created using code above. I used disty twice bigger than distx ![]() orbital decay (an object will orbit the source so many times, and each time it will get a little bit closer to the source until it collides with it)? As Oasiscircle said, you'll need to decrease distance. You could add distDecay variable, and subtract it from dist every iteration. |
I'm still having issues here. Oasis' code causes my orbiting objects to just vanish. Zaoshi's code doesn't factor in another object as the center (what if I make my sun or something orbit the galactic center guys?!). If it wasn't for the math I'd probably understand what I'm doing. x.X
|
Stevenw9 wrote:
Zaoshi's code doesn't factor in another object as the center (what if I make my sun or something orbit the galactic center guys?!) If you can't change code to use non-constant x and y values it's not really my fault. |
Oh. I'm sorry. It's hard for me to figure out what's actually going on when you start throwing sin, cos, and other funky things way outside my knowledge base.
EDIT: Got it working. |
For example you could replace x and y with object, which needs to be orbited, then use it's coordinates.
|
Yep yep, I ended up with
proc/orbit(atom/movable/a,atom/movable/b, speed, dist) and now it works perfectly. Thanks for the help guys! |
I take that back, for some reason all my objects are getting lumped into the bottom left hand side of the screen, except the orbits center (or b).
Here's the orbit proc: proc/orbit(atom/movable/a,atom/movable/b, speed, dist) Here's how I'm calling it: genplanets(var/atom/movable/O) |
Try
var/nx = round(c * dist + b.x * 32 + b.bound_x + b.bound_width / 2, 1) |
Didn't do any testing on that snippet, so you may need to change things if problems do arise. But otherwise that should be the basic way to make something orbit.
(Note: "rotation" should be a positive number for clockwise and negative for counter-clockwise and increasing the magnitude of the number increases the rate of rotation.)
Hope that helped! (: