Newtonian physics and orbit
by Arch Stanton · in Torque Game Builder · 12/06/2005 (4:35 pm) · 3 replies
I've been trying to simulate a dynamic solar system in torquescript. I tried coverting this Numerical Model from wikipedia to script but it left torque hanging with a black screen, either from too much math in script or some problems with double precision, I'm not sure. Has this been tried before? I haven't been able to find anything through search. Can anyone point me in the right direction, or would this be better run in code?
#2
Although it's not really the same thing, I did a basic example of playing with gravity for T2D objects here. It's doubtful that this now works as it stands with the alpha#1 though.
... and a little distraction. ;)
- Melv.
12/07/2005 (5:30 am)
It's definately something that you should implement in C++.Although it's not really the same thing, I did a basic example of playing with gravity for T2D objects here. It's doubtful that this now works as it stands with the alpha#1 though.
... and a little distraction. ;)
- Melv.
#3
12/14/2005 (1:37 pm)
Haven't had much luck implementing newton in C++, but I moved fxForce2D to the alpha. Just some renaming (fx*2D to t2d*) and then I had to change IntegrateObject to take const vars, as sceneobject has changed.
Torque Owner Scott Lahteine
To optimize this we wrote a class that models the gravitational fields over the whole space, and basically can provide the combined force for any given point. Each sprite requests this information about 20 times per second, and during the 1/20th second interval each sprite just keeps using the same force values.
As the size of our game playfield - and the suns - have gotten larger we've had to re-tune the gravity several times until it was just right. We wanted to be able to get pretty close to a sun at a middling speed and still be beyond the event horizon. Our gravity still falls off at distance-squared, but we scale the distance prior to calculating the inverse. Also we make sure the distance is never less than 1, because dividing 1 by a value less than one causes gravity to rapidly increase.
The class which provides the gravity for a given point is still recalculating the inverse of the distance squared to every sun each time it is called, so we haven't tuned it to a high degree yet. As I mentioned, our solution was simply to call it less frequently and rely on the values being "good enough" for our purposes. However, it would make some sense to build a system based on dividing the space into a grid, for example, and coming up with a shorthand for calculating the gravity within any particular grid-box.
The long and short of it is, unless you have a fairly small number of objects, or a single source of gravity - and maybe not even then - the math required to continuously calculate gravity might be too much for torquescript alone to handle. I'd suggest making a pair of complementary C++ classes such as myGravitySource and myGravitator that work together somehow and then refer to them from your script. At least you know you'll be getting the best speed and precision.