Problem with simple planet orbitting script

i dont want it slow down and speed up at certain sides i want to make it that it speeds up when the earth is closer to sun and slows down when its farther pls read the post properly

magnitudeā€¦ as the other guy said

Just create a corresponding part for each body, and update itā€™s Position every time the simulation updates.

it doesnt work at all i just flinged out idk why i also reduced the velocity and every time i fling

Have you tried setting G to a lower value?

yes i did and i even tried the gravitational constant and it didnt work or i could be setting the part position wrong

@Creeperman16487 Iā€™m glad you are enjoying the module! For this I would recommend putting a couple invisible parts and when the orbiting part touches the invisible part you can speed up or slow down the TimeToOrbit function in the module.

Also Iā€™m updating the module a ton to have regular orbit, eccentricity elliptical and other orbits.

damn bro,thats pretty cool your adding those in the module

Thanks man. Iā€™m doing my best since Iā€™m not the best scripter.

var canvas = document.getElementById('c');
var c = canvas.getContext('2d');

canvas.width = 600;
canvas.height = 450;

var cw = canvas.width;
var ch = canvas.height;

c.clearRect(0, 0, cw, ch);

if (l!=undefined) {
    clearInterval(l);
};

const f = 0.008;

const g = 0.0000006508;
var ibg = true;
const pg = 0.001;
var ig = false;

function clear() {
    c.clearRect(0, 0, cw, ch);
};

function dis(x1, y1, x2, y2) {
    return Math.sqrt( Math.abs(Math.pow(( x1 - x2 ), 2) + Math.pow(( y1 - y2 ), 2)) );
};

function ang(r) {
    return r * 180;
};

class Circle {
    constructor(x, y, r, b, dt, vx, vy, ac, m, d, s, an) {
        this.x = x; // changeable
        this.y = y; // changeable
        this.r = r; // changeable
        this.b = b; // changeable
        this.dt = dt; // changeable
        this.ac = 0.001;
        this.d = this.r*2;
        this.an = Math.floor(Math.random(0, 1)*360);
        this.vx = Math.cos(this.an);
        this.vy = Math.sin(this.an);
        this.s = Math.PI * (this.r**2);
        this.m = this.s
    } dr() {
        c.beginPath();
        c.ellipse(this.x, this.y, this.r, this.r, 0, 0, Math.PI*2);
        c.fill();
    } mo() {
        this.dt += this.ac;
        
        this.x += this.vx * this.dt;
        this.y += this.vy * this.dt;

        this.a = Math.atan2(this.vy, this.vx);
    } wc() {
        if (this.x + this.r > cw) {
            this.vx *= -1*this.b;
            this.x -= 5;
        };

        if (this.x - this.r < 0) {
            this.vx *= -1*this.b;
            this.x += 5;
        };

        if (this.y + this.r > ch) {
            this.vy *= -1*this.b;
            this.y -= 5;
        };

        if (this.y - this.r < 0) {
            this.vy *= -1*this.b;
            this.y += 5;
        };
    } bc() {
        for (let n of c1) {
            for (let e of c1) {
                if (n !== e) {
                    if (dis(n.x, n.y, e.x, e.y) < n.r + e.r) {
                        e.vx = n.m/e.m * (Math.cos(Math.atan2(e.y-n.y, e.x-n.x)));
                        e.vy = n.m/e.m * (Math.sin(Math.atan2(e.y-n.y, e.x-n.x)));
                        e.dt *= n.dt/e.dt * Math.sqrt(e.m)/Math.sqrt(n.m) * 0.999;
                    };
                };
            };
        };
    };
};

var c1 = [];

var cb = new Circle(cw/2, 100, 25, .9, 5);
var cc = new Circle(cw/2, 30, 10, .9, 5);

c1.push(cb);
c1.push(cc);

cc.vx = 1;
cc.vy = 0;

function up() {
    clear();

    c1[0].x = cw/2;
    c1[0].y = ch/2;

    for (let c of c1) {
        c.dr();
        c.mo();
        c.bc();
        c.wc();
    };

    // gravity

    if (ibg) {
        for (let o1 of c1) {
            for (let o2 of c1) {
                if (o1 !== o2) {
                    if (o1.x > o2.x) {
                        o1.vx -= (o1.m*o2.m/dis(o1.x, o1.y, o2.x, o2.y)^2) * g * o2.m/o1.m
                    } else {
                        o1.vx += (o1.m*o2.m/dis(o1.x, o1.y, o2.x, o2.y)^2) * g * o2.m/o1.m;
                    };
    
                    if (o1.y > o2.y) {
                        o1.vy -= (o1.m*o2.m/dis(o1.x, o1.y, o2.x, o2.y)^2) * g * o2.m/o1.m;
                    } else {
                        o1.vy += (o1.m*o2.m/dis(o1.x, o1.y, o2.x, o2.y)^2) * g * o2.m/o1.m;
                    };
                };
            };
        };
    };

    if (ig) {
        for (o of c1) {
            if (o.vy + pg*o.m < 17) {
                o.vy += pg*o.b;
            };
        };
    };
    
};

document.addEventListener("keydown", function(e) {
    if (e.keyCode == 32) {
        if (ig) {
            ig = false;
        } else {
            ig = true;
        }
    };
})

var l = setInterval(up, 15);

/*
<html><head></head><body><canvas id="c"  style="border: solid black 3px;"></canvas></body></html>
*/

This is a small program I made a few weeks ago simulating gravity. Itā€™s in Javascript.
(Paste the HTML commented at bottom of program)

I may be a little late, but Iā€™ve always had strong interest in simulating gravity through programming. Iā€™ve always wanted to make a game (or program) that does this for you where you can add masses, increase gravity and fiddle with the code.

Iā€™ve already done this here, but it tends to be very unstable, and the math isnā€™t very correct in some cases. When scaled correctly, it may look like normal gravity, but when the objects get bigger, the code basically breaks and pieces go flying across everywhere.

This time, Iā€™m going to make a gravity simulation in Roblox Lua in Roblox Studio. But I want the correct math for the equation. Can someone help me understand how to simulate gravity in Roblox Lua?

What Iā€™m thinking is a game where there is no gravity, but there are predefined objects that have gravity and velocity. You could even simulate a huge sphere that is, Earth! And have the moon orbit it.

I just donā€™t understand how to make a stable gravity simulation. Is it even possible to make said simulation?

It is possible with kepler orbital equation or use the newton gravity equation

I tried Newtonā€™s law of universal gravitation in this javascript program, it works fine, but I want it to be realistic and stable.

Things orbiting in real life donā€™t just orbit once and then fly out of control. They stably orbit for a considerably long amount of time. I donā€™t want to just MANUALLY make the orbit. I want actual simulated gravity.

One way Iā€™ve considered is to have an angle which represents itā€™s move direction. Of course itā€™s impossible to ā€œaddā€ angles of direction to a part so we have to break it down using the trigonometric functions math.sin and math.cos.

So we could say something like this:

local sphere = Instance.new("Part", game.Workspace)

sphere.Name = "Earth"
-- blah blah boring stuff here

local radian = math.pi / 180 -- 1 degree in radians
local angle = math.random(0, 360) * radians -- random angle in radians

local vx = math.cos(angle)
local vy = math.sin(angle)

local speed = 2

function move()
  sphere.Position.X += vx * s
  sphere.Position.Y += vy * s
end

Hopefully my math is correct.

Since the distance moved when applying vy and vx to the position of the sphere is always one which can be further explained here

All we have to do is multiply that value by the speed variable, and we get movement in speed units in the direction of angle radians. Velocity!

Now, all we have to do is calculate the positions of other objects around and using the Distance formula and move the object respectively. to calculate the angle and the speed of the current object (assuming we know the speed of the object every frame), you can use math.atan2 on the x and the y and itā€™ll return the angle in radians of the two positions. To get the speed, (which is little bit more complicated) we could use an equation like this:

local otherangle = math.atan2(othery, otherx)

local otherspeedx = x / math.cos(otherangle) 
local otherspeedy = y / math.sin(otherangle) 

local speed

if otherspeedx ~= otherspeedy then
  error("Something errored!")
else 
  speed = otherspeedx -- it doesn't matter which one we choose, 
                      --since if the calculations are right, the 
                      --object should be moving at the same 
                       --speed (*relative to the angle*) on the x-axis 
                       --and y-axis.
end

It may be a little hard to understand, but basically how this works is:

  1. It uses the built-in function math.atan2() to find the angle given two coordinate values (y and x)

  2. It calculates the speed of the object by dividing the original x and/or y coordinates by the math.cos and math.sin of the angle. Since we previously mutliplied the vx and vy velocity values by the speed, this should be the speed of the object.

Or, instead of doing all of these calculations, the script could just tell the other script what the speed and angle was.

Conclusion:

I just want another way to implement stable gravity in Roblox.

1 Like