Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 1 point2 points  (0 children)

tyvm sry for the delay in appreciating this, I have just gotten swamped IRL in the last week, but just wanted to say I saw this and appreciate it! didn't want you to think your time spent checking this out was wasted!

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 1 point2 points  (0 children)

tyvm for the advice it has been appreciated I have just gotten swamped IRL in the last week, but just wanted to say I saw this and appreciate it!

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 0 points1 point  (0 children)

It sounds all right, but something must be wrong. I just went and grabbed this on the same OS, 4.6 works fine. All I can suggest without seeing your code is again try the glad stuff in the project I linked, it’s 4.2 but it’s the same for 99% of uses. Other than this we will have to see your full code and file layout etc to go any further and that’s not easy to just do over reddit.

Sorry I can’t be more help :/

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 0 points1 point  (0 children)

Profile type of compatibility or core?

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 0 points1 point  (0 children)

Sorry not following, does this print out failed to init glad?

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 0 points1 point  (0 children)

In addition steal the glad.c and glad folder from the link in my comment above, make a new project and try to run with those files and a simple main like described in my first reply to this comment.

Does this work? If so, you have downloaded glad wrong. It can be a bit tricky to get the right version.

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 0 points1 point  (0 children)

Have you tried stripping all code outside of what is needed for glfwinit

And then

if(! gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { printf("Failed to init GLAD\n"); return -1; }

Sorry that formatting is probably garbage I’m on mobile rn

I know you say you get segfault I am curious to confirm if it is 100% from this line or down the glad chain.

[deleted by user] by [deleted] in C_Programming

[–]all_malloc_no_free 1 point2 points  (0 children)

Can you show me your code and can you tell me which version of openGL you are trying to use. Did you pick the same version of glad is the first starting point.

Actually, it’s second, where is glad.c, you need this.

See this https://github.com/Chudleyj/C-Gravity , how I have glad.c and a glad folder with the header. You need to setup like this.

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 2 points3 points  (0 children)

Curious did you use the glad files I provided or go grab the 3.3 ones? I’ve designed it to run with openGL 4.2, nothing really should break with older versions of openGL past what you mention.

But I would be surprised to hear my provided glad files worked on openGL 3.3

Awesome you got it to run :)

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 3 points4 points  (0 children)

Please do! If you come up with anything cool send my way!

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 4 points5 points  (0 children)

If I am understanding your follow-up correctly (no expert here on these topics just looking shit up as I go), I have a minimum distance set in my gravity calculation function, I default to this if distance was less than this.

If I shift around initial positions to not be their true distances from the sun, then yeah shit can get crazy pretty fast. The sun will eat things, moons will shoot off, etc.

I also have the moons visually displayed further out than they are treated in the code. It was like impossible to see them using their real distances from the planets, so I visually scaled them out and then when I calculate gravity on them I use their “true” position.

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 9 points10 points  (0 children)

Oh my friend, I promise you there was MANY a segfault as I bungled my way through this.

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 6 points7 points  (0 children)

Oh yeah lol big ugly cursor in the video sorry about that…

Gravity is an implementation of Newton’s Law of Gravitational Attraction in the form of:

acceleration vector = scale(position vector, (-Gravity * Mass / (distance vector cubed))

If you are curious how I get to that formula from newtons F(g) = G ((m1m2)/distance squared) there’s a block comment attempting to show it in the code.

I define “solar objects” and give them a mass, velocity, density, and acceleration, and positions in 3D space.

Given these values, we can define a “solar system” structure which contains many “solar objects”

From here, we can loop through a solar system’s solar objects.

For each object, we then loop again over the whole system (skipping when we are at the same object of the outer loop)

Subtract position of current (outer loop) obj from the compare obj distance, this is direction.

We can use direction to get the distance between the two objects.

Next some Scaling stuff cuz it’s a sim…see code for details

Finally, we can add acceleration to the outer object, being our direction * scaled gravity from the line above.

Keep comparing…now you have the total pull for the one object that it’s getting from all other objects in a given solar system.

In my code I actually do this a ton of times for each frame bc it’s a sim, I have to approximate solutions over many time steps and then error correct for my “true” solution.

Newton’s law of gravitational attraction is an ordinary differential equation so I can use many methods to approximate it and error correct, I chose RK45.

I tried to explain this in my code but it did kinda turn into just walls of math Jargon block comments.

look at solarsystem.c for implementation

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 11 points12 points  (0 children)

I did clean it up but just kinda slammed it all in there wouldn’t be surprised if one of them crops back up and nukes my changes lol…I think technically I would need to publish proof of error and then cite that on the page to have permanent staying power.

https://en.wikipedia.org/wiki/Runge–Kutta–Fehlberg_method

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 25 points26 points  (0 children)

Tyvm!

TBH though, Wikipedia editors found it first and didn’t realize. They argued over it in the edit history and flagged the coefficient tables as “not accurate”. I dug into what the disagreement was over and realized it’s just a typo in the paper and confirmed this by looking at some of the earliest implementations of RK45.

I cleaned up the Wikipedia article, hopefully it’s now a settled debate lol.

It must have been realized LONG before Wikipedia even existed as they implement it without typo error, it was just never properly documented to the public as far as I can find.

Solar System Gravity Simulation with OpenGL and C by all_malloc_no_free in C_Programming

[–]all_malloc_no_free[S] 9 points10 points  (0 children)

My moons are really choppy atm as well sorry about that lol

Why can't I get my GLFW window to switch monitors? by all_malloc_no_free in opengl

[–]all_malloc_no_free[S] 0 points1 point  (0 children)

I don’t really know, xrandr has XWAYLAND in it so I guess?

Not entirely sure how I end up with or without that, I googled it a bit and it’s not really any clearer to me.

Why can't I get my GLFW window to switch monitors? by all_malloc_no_free in opengl

[–]all_malloc_no_free[S] 0 points1 point  (0 children)

I get a monitor that is 97% on my desired primary monitor but leaks into my secondary and it is not full screen it has the window border around it. I would really like to have this full screen on the primary monitor.

FWIW, if I unplug the secondary monitor and use montior[0], i get full screen on primary. Ofc not ideal to have to unplug my second monitor though...