Problem with simple planet orbitting script

btw i forgot to ask how will i make a part go in orbit with this?

1 Like

This was my script btw which had the speed of orbit problem
Orbit.rbxl (43.5 KB)

How can a function(or variable) that just returns the distance between 2 points speed it up?

i think he meant i could check the distance of the two part and somehow speed it up when its closer but the thing is i dont even know how to speed it up lol

Magnitude is a property from the vector3 class

Okay, but the property does not define the speed

I know that I’m just correcting you.

1 Like

I couldn’t understand, but magnitude doesn’t define the speed so it won’t work

1 Like

why are you fixing that problem with it if you have a solution to my problem just say it or just dont talk at all not being rude here

This is the stupidest answer you will get, but I think you want the object to slow down at the left, and move faster at the right

time it, or be synchronous and check if the object has reached a certain distance

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?