- Joined
- Mar 28, 2008
- Messages
- 671
- Reaction score
- 32
- Points
- 43
Hello,
I must have a tomatos on my eyes:
when I try to calculate the next position of an Object on it's orbit (I know there are more exact ways that numeric integration, but it's just for fun) , this funktion will not work corectrly (There a no nice orbits):
And I'm wundering why the following seems to be working good (previous code, factor = gravitational constant). I got this by "try and error".
But i never read to divide somethink by r³, it's always r²...
So my main question is, why is the first method not working?
Used methodsof my VECTOR3 class:
I must have a tomatos on my eyes:
when I try to calculate the next position of an Object on it's orbit (I know there are more exact ways that numeric integration, but it's just for fun) , this funktion will not work corectrly (There a no nice orbits):
Code:
//Calling method:
private void updateGbodyPositions()
{
for (int i = 0; i < (gbodies.size()); i++)
{
GBody body1 = gbodies.get(i);
if (body1.m > centerstar.m) centerstar = body1;
for (int i2 = i+1 ; i2 < (gbodies.size()) ; i2++)
{
GBody body2 = gbodies.get(i2);
updateV(body1, body2);
//....
}
}
}
private void updateV(GBody body1, GBody body2)
{
VECTOR3 er = new VECTOR3(body1.pos.x - body2.pos.x, body1.pos.y - body2.pos.y, body1.pos.z - body2.pos.z);
float distance = er.length();
er = er.normalize(); //Normalize the vector to value "1"
VECTOR3 f1 = er.mul(GRAV_CONST * body1.m * body2.m / distance * distance); // body.m = mass of the body
VECTOR3 f2 = f1.neg();
VECTOR3 a1 = f1.mul(1/body1.m); //dividide f1 by the mass of body1
VECTOR3 a2 = f2.mul(1/body2.m);
body1.v.add(a1); //addition
body2.v.add(a2);
}
}
And I'm wundering why the following seems to be working good (previous code, factor = gravitational constant). I got this by "try and error".
But i never read to divide somethink by r³, it's always r²...
Code:
private void updateV(GBody star1, GBody star2, double factor)
{
float dx = star1.pos.x - star2.pos.x;
float dy = star1.pos.y - star2.pos.y;
float dz = star1.pos.z - star2.pos.z;
float distxUp2 = dx * dx;
float distyUp2 = dy * dy;
float distzUp2 = dz * dz;
float distance = (float) Math.sqrt(distxUp2 + distyUp2 + distzUp2);
float distanceUp3 = distance * distance * distance;
if (distanceUp3 == 0)
{
return;
}
star2.v.x += factor * dx * star1.m / distanceUp3;
star2.v.y += factor * dy * star1.m / distanceUp3;
star2.v.z += factor * dz * star1.m / distanceUp3;
star1.v.x += factor * -dx * star2.m / distanceUp3;
star1.v.y += factor * -dy * star2.m / distanceUp3;
star1.v.z += factor * -dz * star2.m / distanceUp3;
}
So my main question is, why is the first method not working?
Used methodsof my VECTOR3 class:
Code:
package gl_sceneProject.src.sceneProject;
class VECTOR3
{
float x;
float y;
float z;
VECTOR3 (float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
VECTOR3 ()
{
this.x = 0;
this.y = 0;
this.z = 0;
}
public VECTOR3 neg()
{
VECTOR3 v3neg = new VECTOR3();
v3neg.x = -x;
v3neg.y = -y;
v3neg.z = -z;
return v3neg;
}
public VECTOR3 mul(float f)
{
VECTOR3 v3mul = new VECTOR3();
v3mul.x = x * f;
v3mul.y = y * f;
v3mul.z = z * f;
return v3mul;
}
void add(float x, float y, float z)
{
this.x += x;
this.y += y;
this.z += z;
}
void add(VECTOR3 v3)
{
this.x += v3.x;
this.y += v3.y;
this.z += v3.z;
}
public VECTOR3 normalize()
{
float a = (float) Math.sqrt(x*x+y*y+z*z);
x = x / Math.abs(a);
y = y / Math.abs(a);
z = z / Math.abs(a);
return this;
}
public VECTOR3 normalize(float length)
{
float a = (float) Math.sqrt(x*x+y*y+z*z);
x = x*length / Math.abs(a);
y = y*length / Math.abs(a);
z = z*length / Math.abs(a);
return this;
}
public float length()
{
return (float) Math.sqrt(x*x+y*y+z*z);
}
public float distance(VECTOR3 vector3)
{
VECTOR3 vdiff = new VECTOR3();
vdiff.x = Math.abs(vector3.x - x);
vdiff.y = Math.abs(vector3.y - y);
vdiff.z = Math.abs(vector3.z - z);
return vdiff.length();
}
//....
}
Last edited: