So I tried to see if I could do something like GSim in JS
So I tried to see if I could do something like GSim in JS
Here I am, and I made a super limited version of his sim:
https://dl.dropboxusercontent.com/u/556 ... avsim.html
(aside from having learned gravity laws from Andy's game, this was actually coded mostly from scratch)
Current Features:
- Gravity, with the r⁻² law
- Create/fling planets, with a 10 second long, accurate prediction. All planets manually created have a mass of 3.2 & a density of 1.
- Prediction will show if the planet you flung impacts anything (and whether or not it disappears/gains mass)
- Center of Mass is shown with a purple circle
- A button at the bottom will take the camera to the center of mass
- The screen can be rotated!
https://dl.dropboxusercontent.com/u/556 ... avsim.html
(aside from having learned gravity laws from Andy's game, this was actually coded mostly from scratch)
Current Features:
- Gravity, with the r⁻² law
- Create/fling planets, with a 10 second long, accurate prediction. All planets manually created have a mass of 3.2 & a density of 1.
- Prediction will show if the planet you flung impacts anything (and whether or not it disappears/gains mass)
- Center of Mass is shown with a purple circle
- A button at the bottom will take the camera to the center of mass
- The screen can be rotated!
Last edited by wtg62 on Fri Jul 10, 2015 4:17 pm, edited 6 times in total.
This message has been brought to you by wtg62, duh!
-
- Posts: 523
- Joined: Mon Jun 03, 2013 4:54 pm
Re: So I tried to see if I could do something like GSim in J
Awesome that you coded it from scratch! You could totally add some simple additions to this (ex. Fling, set speed, move camera, etc) easily. If anyone wants to see what happens to the setup later, just put this into your console:
(It modifies the tug function to make it 100 times stronger. I don't use JS much, but this worked for me and I'm happy with it
)
Speaking of tug: When implementing the standard Newtonian gravity, you don't actually need to compute angle, sin, and cos. If you know the displacements dx and dy, you can do this:
But the way you did it allows for more general functions. (You might be able to connect this to your function plotter, and have custom forces!)
Code: Select all
var i;
i = 0; while(i<4) { planets[i].vx *= 10; planets[i].vy *= 10; planets[i].tug = function(planet,law)
{
var relDX = this.x-planet.x;
var relDY = this.y-planet.y;
var relD = Math.sqrt(Math.pow(relDX,2)+Math.pow(relDY,2));
var relTheta = Math.atan2(relDY,relDX);
planet.thrust2(100*Math.pow(relD,-2)*this.mass,relTheta);
}; i+= 1; }

Speaking of tug: When implementing the standard Newtonian gravity, you don't actually need to compute angle, sin, and cos. If you know the displacements dx and dy, you can do this:
Code: Select all
d = sqrt(dx^2+dy^2)
vx += timestep * dx / d^3
vy += timestep * dy / d^3
$1 = 100¢ = (10¢)^2 = ($0.10)^2 = $0.01 = 1¢ [1]
Always check your units or you will have no money!
Always check your units or you will have no money!
Re: So I tried to see if I could do something like GSim in J
I could definitely add flinging, but I don't know how I would predict the trajectory.
This message has been brought to you by wtg62, duh!
-
- Posts: 523
- Joined: Mon Jun 03, 2013 4:54 pm
Re: So I tried to see if I could do something like GSim in J
Andy's sim works by essentially fixing the objects for the time being, and computing the results of forces on the new object. A simple O(n) solution, but it doesn't take into account any motion and the effect of adding the object.wtg62 wrote:I could definitely add flinging, but I don't know how I would predict the trajectory.
What I'd do is make a copy of the planet list, add the new object, and simulate the copy for a certain time, tracking the new object. This is O(n^2), though, and might be a bit hard to implement. Additionally, adding multiple objects would mean increased complexity. (The original simulator just stopped displaying predictions when you pressed x.)
You could, of course, try GSim's method, which doesn't require any copying. For each object in the list, simulate its pull on the new object, and track it.
Or you could add a line showing which way the object will move (show the first derivative of motion). Con: This shows almost nothing about the future path.
Or just have no prediction.
$1 = 100¢ = (10¢)^2 = ($0.10)^2 = $0.01 = 1¢ [1]
Always check your units or you will have no money!
Always check your units or you will have no money!
Re: So I tried to see if I could do something like GSim in J
I think what I am going to do is just pause time as you are flinging something (thus making calculations and implementation a whole lot easier) and then go with what you said. Once the planet is flung, the sim continues.
Edit:
Okay, I've updated the file and added this feature. Predictions aren't amazingly efficient, but they are very accurate!
Right now you can only fling a planet with a mass of 32, with the color "#fff" and with a density of 1, though.
In the prediction trajectory, if you see an orange circle, that represents a future impact that will result in the planet you flung disappearing.
I think later on when I add the ability to change the size of the planet you're creating, I'll have a different circle that represents an impact that doesn't result in the planet you flung disappearing.
Edit:
Okay, I've updated the file and added this feature. Predictions aren't amazingly efficient, but they are very accurate!
Right now you can only fling a planet with a mass of 32, with the color "#fff" and with a density of 1, though.
In the prediction trajectory, if you see an orange circle, that represents a future impact that will result in the planet you flung disappearing.
I think later on when I add the ability to change the size of the planet you're creating, I'll have a different circle that represents an impact that doesn't result in the planet you flung disappearing.
This message has been brought to you by wtg62, duh!
Re: So I tried to see if I could do something like GSim in J
I'm going to attempt to see if I can implement the Barnes-Hut algorithm, to speed things up.
Really simple and interesting algorithm too! Haha.
Really simple and interesting algorithm too! Haha.
This message has been brought to you by wtg62, duh!
- testtubegames
- Site Admin
- Posts: 1164
- Joined: Mon Nov 19, 2012 7:54 pm
Re: So I tried to see if I could do something like GSim in J
Yikes! You're on my tail, wtg!
Very well made -- and works great. I missed the first iteration of it, but with the additions it works quite smoothly. (And even as I reload it, I noticed another change! Wow! Go wtg. The new rotation is a nice addition.)
I noticed a little bug -- when you are flinging planets, they occasionally launch not at the place you put them, but where your mouse currently is. (After you've pulled back to fling.) I took a peek in the code, and it looks like it probably stems from you prediction code lagging the game a bit... and the loop that checks for whether the planet is flung doesn't trigger until it's too late and the cx, cy values have been reset. (Or some such thing.)
Also, kudos to you for trying to speed up the sim. I learned a whole bunch from trying to do that, myself. Barnes Hut is a good tool, too, and fits well with your quest to understand all the cool algorithms out there
Though, I imagine you could do some optimizing with the code you already have and make great strides. (As Random noted, you can avoid Sin/Cos calls, and that kind of thing.)
I'm jealous of your prediction lines... way better than mine, since they take into account the actual future. The downside of course is the lag, but it sure is neat to be able to know how your new planet will interact with the current ones in the distant future.
Very well made -- and works great. I missed the first iteration of it, but with the additions it works quite smoothly. (And even as I reload it, I noticed another change! Wow! Go wtg. The new rotation is a nice addition.)
I noticed a little bug -- when you are flinging planets, they occasionally launch not at the place you put them, but where your mouse currently is. (After you've pulled back to fling.) I took a peek in the code, and it looks like it probably stems from you prediction code lagging the game a bit... and the loop that checks for whether the planet is flung doesn't trigger until it's too late and the cx, cy values have been reset. (Or some such thing.)
Also, kudos to you for trying to speed up the sim. I learned a whole bunch from trying to do that, myself. Barnes Hut is a good tool, too, and fits well with your quest to understand all the cool algorithms out there

Though, I imagine you could do some optimizing with the code you already have and make great strides. (As Random noted, you can avoid Sin/Cos calls, and that kind of thing.)
I'm jealous of your prediction lines... way better than mine, since they take into account the actual future. The downside of course is the lag, but it sure is neat to be able to know how your new planet will interact with the current ones in the distant future.
Re: So I tried to see if I could do something like GSim in J
Going to say this first so you'll notice it:
If there's anything I need help with, it's figuring out how exactly I should modify the velocity of a planet after it merges with another.
It's things like this that make me wish that we still used our IRC channel, haha.
But, then again, I use steam for chatting much more than IRC
Sadly there isn't too much I can do besides keep them in the same loop, would have to find some way to do that.
All I really did was copy the array containing all the planets, and simulate them for whatever amount of time.
Sadly, this is super-duper inefficient!
I can probably only avoid calls to trig functions if the law is r^-2, and I do plan on adding other laws eventually.
If there's anything I need help with, it's figuring out how exactly I should modify the velocity of a planet after it merges with another.
It's things like this that make me wish that we still used our IRC channel, haha.
But, then again, I use steam for chatting much more than IRC

Thanks! Maybe I'll add a grid too.testtubegames wrote:Yikes! You're on my tail, wtg!
Very well made -- and works great. I missed the first iteration of it, but with the additions it works quite smoothly. (And even as I reload it, I noticed another change! Wow! Go wtg. The new rotation is a nice addition.)
Yeah, that's definitely lag.testtubegames wrote: I noticed a little bug -- when you are flinging planets, they occasionally launch not at the place you put them, but where your mouse currently is. (After you've pulled back to fling.) I took a peek in the code, and it looks like it probably stems from you prediction code lagging the game a bit... and the loop that checks for whether the planet is flung doesn't trigger until it's too late and the cx, cy values have been reset. (Or some such thing.)
Sadly there isn't too much I can do besides keep them in the same loop, would have to find some way to do that.
Haven't done it yet, but I am working on a separate program to test out the Barnes-Hut algorithm (no physics, just the creation of the tree).testtubegames wrote: Also, kudos to you for trying to speed up the sim. I learned a whole bunch from trying to do that, myself. Barnes Hut is a good tool, too, and fits well with your quest to understand all the cool algorithms out there![]()
If you're willing to sacrifice performance or the ability to see the prediction as you are flinging the planet, you could definitely have the accurate predictions I have! But hey, your predictions are adequate for the most part.testtubegames wrote: I'm jealous of your prediction lines... way better than mine, since they take into account the actual future. The downside of course is the lag, but it sure is neat to be able to know how your new planet will interact with the current ones in the distant future.
All I really did was copy the array containing all the planets, and simulate them for whatever amount of time.
Sadly, this is super-duper inefficient!

If I wanted to make anything big, I'd definitely rewrite the whole thing, and I'd definitely like to try to make something much more usable.testtubegames wrote: Though, I imagine you could do some optimizing with the code you already have and make great strides. (As Random noted, you can avoid Sin/Cos calls, and that kind of thing.)
I can probably only avoid calls to trig functions if the law is r^-2, and I do plan on adding other laws eventually.
This message has been brought to you by wtg62, duh!
- testtubegames
- Site Admin
- Posts: 1164
- Joined: Mon Nov 19, 2012 7:54 pm
Re: So I tried to see if I could do something like GSim in J
For merging, you just need to worry about (a) Conservation of Mass, and (b) Conservation of Momentum (mass*velocity). So make sure the new planet has the sum of the old masses (which I imagine you're already doing). And then sum up the momenta before, and use that to figure out the momentum (and thus velocity) afterwards.wtg62 wrote:Going to say this first so you'll notice it:
If there's anything I need help with, it's figuring out how exactly I should modify the velocity of a planet after it merges with another.
Actually, you can avoid trig functions entirely. In GSim, the only time it would calculate a trig function is if you use one in the force law itself.wtg62 wrote:I can probably only avoid calls to trig functions if the law is r^-2, and I do plan on adding other laws eventually.
-
- Posts: 523
- Joined: Mon Jun 03, 2013 4:54 pm
Re: So I tried to see if I could do something like GSim in J
True! You just need to remember that cos = x/d, and sin = y/d (soh-cah-toa anyone?), and replace it. So in general,testtubegames wrote:Actually, you can avoid trig functions entirely. In GSim, the only time it would calculate a trig function is if you use one in the force law itself.wtg62 wrote:I can probably only avoid calls to trig functions if the law is r^-2, and I do plan on adding other laws eventually.
vx += x / d * f(d)
vy += y / d * f(d) Also Andy has a tweet's characters more than twice my post count - 828 vs 484. Also the digits are all powers of 2, and mine is 22^2!
$1 = 100¢ = (10¢)^2 = ($0.10)^2 = $0.01 = 1¢ [1]
Always check your units or you will have no money!
Always check your units or you will have no money!