So I made a little prototype to test out some Gravity related code, along with a few other things, and then it kinda got a little carried away with unneeded options and stuff. So yeah, what the hell may as well share it.
Have fun, all instructions are shown when you run the file.
p.s. Any and all feedback as to code conventions, tips or anything else is highly welcome.
Gravity
-
- Prole
- Posts: 2
- Joined: Sun Aug 03, 2014 2:19 am
Gravity
Last edited by jellysnake on Mon Aug 04, 2014 1:28 pm, edited 1 time in total.
Re: Gravity
Backspace causes a crash, sim.lua line 84: attempt to get lenght of 'varPlanets' (nil).
Do you use Euler integration to calculate the orbits?
If so, and you're still interested in playing around with gravity I can recommend having a look at fourth order Runge-Kutta integration:
http://en.wikipedia.org/wiki/RK4
I just found a nice explaination (far better than I could ever write it probably) on why it is superior here:
http://gamedev.stackexchange.com/questi ... nt-gravity
I implemented it a while back, this might be easier to read than the pure mathematical approach:
The local variables k1, l1, and so on only serve readability. You could also do it all in one big ugly term.
Do you use Euler integration to calculate the orbits?
If so, and you're still interested in playing around with gravity I can recommend having a look at fourth order Runge-Kutta integration:
http://en.wikipedia.org/wiki/RK4
I just found a nice explaination (far better than I could ever write it probably) on why it is superior here:
http://gamedev.stackexchange.com/questi ... nt-gravity
I implemented it a while back, this might be easier to read than the pure mathematical approach:
Code: Select all
local function rk4( object, gameSpeed )
local k1, l1, k2, l2, k3, l3, k4, l4
local _mass, _position, _speed, _toOrigin = object.mass, object.position, object.speed, object.toOrigin
k1 = gameSpeed * _speed
l1 = gameSpeed * accelerationToOrigin( _toOrigin , _mass)
k2 = gameSpeed * (_speed + l1/2)
l2 = gameSpeed * accelerationToOrigin( _toOrigin + k1/2 , _mass)
k3 = gameSpeed * (_speed + l2/2)
l3 = gameSpeed * accelerationToOrigin( _toOrigin + k2/2 , _mass)
k4 = gameSpeed * (_speed + l3)
l4 = gameSpeed * accelerationToOrigin( _toOrigin + k3 , _mass)
local position = _position + k1/6 + k2/3 + k3/3 + k4/6
local speed = _speed + l1/6 + l2/3 + l3/3 + l4/6
return position, speed
end
-
- Prole
- Posts: 2
- Joined: Sun Aug 03, 2014 2:19 am
Re: Gravity
1. Bug is fixed. Thanks, thought I had fixed that.
2. I implemented the gravity using this line of code:
or simply:
Force to apply on the x axis is equal to the distance between the ball (on the x)and the planet multiplied by the strength of the gravity.
Same for the Y axis but obviously y instead of x.
Or even simpler using Newton's law of universal gravitation;
http://en.wikipedia.org/wiki/Newton's_l ... ravitation
I'm not using the other implementations I have seen that are using (I think) Euler integration like you talked about. However that function will come in very hand for something along the same lines that I am making in a different language.
2. I implemented the gravity using this line of code:
Code: Select all
varBalls[w][1]:applyForce((varPlanets[q][1]:getX()-varBalls[w][1]:getX())*(varPlanets[q][2]:getRadius()/10), (varPlanets[q][1]:getY()-varBalls[w][1]:getY())*(varPlanets[q][2]:getRadius()/10))
Force to apply on the x axis is equal to the distance between the ball (on the x)and the planet multiplied by the strength of the gravity.
Same for the Y axis but obviously y instead of x.
Or even simpler using Newton's law of universal gravitation;
http://en.wikipedia.org/wiki/Newton's_l ... ravitation
I'm not using the other implementations I have seen that are using (I think) Euler integration like you talked about. However that function will come in very hand for something along the same lines that I am making in a different language.
Who is online
Users browsing this forum: No registered users and 0 guests