3D Getting the angles from 'object a' to 'object b' in a 3d world. [Solved]

Bingdom

Googledom
I'm trying to make a small game that would show me 3d gravitational fields from planets and them pulling each other.

I've been having a hard time trying to figure out on getting the altitude and azimuth (pan, tilt) from one planet to another in a 3d world, which I would use to get the direction for accelerating the planet.

This is what I have found so far.
Code:
xx = other.x - x;
yy = other.y - y;
zz = other.z - z;
alt = radtodeg(arctan2(yy, sqrt(xx*xx + zz*zz)));
az = radtodeg(arctan2(-xx, -z));
Azimuth seems to always return -180 and altitude seem to return 0.
(This is inside a 'with' statement)

Can someone please help me out? :)
 
you don't need the angles or any trig.

accelerating a and b toward each other:

Code:
    var _x = b.x - a.x;
    var _y = b.y - a.y;
    var _z = b.z - a.z;

    var _f = G / power(point_distance_3d(0,0,0,_x,_y,_z),3);

    var _f2 = b.mass * _f;
    a.xsp += _x * _f2;
    a.ysp += _y * _f2;
    a.zsp += _z * _f2;

    _f2 = a.mass * _f;
    b.xsp -= _x * _f2;
    b.ysp -= _y * _f2;
    b.zsp -= _z * _f2;
G is your gravitational constant, which can have any value you want, bigger number = more gravity. Likewise for the mass of both bodies.

this line:
var _f = G / power(point_distance_3d(0,0,0,_x,_y,_z),3);
can be rewritten for a slight performance boost:
var _f = G / power(dot_product_3d(_x,_y_z,_x,_y_z),1.5);
 
Last edited:

Bingdom

Googledom
you don't need the angles or any trig.

accelerating a and b toward each other:

Code:
    var _x = b.x - a.x;
    var _y = b.y - a.y;
    var _z = b.z - a.z;

    var _f = G / power(point_distance_3d(0,0,0,_x,_y,_z),3);

    var _f2 = b.mass * _f;
    a.xsp += _x * _f2;
    a.ysp += _y * _f2;
    a.zsp += _z * _f2;

    _f2 = a.mass * _f;
    b.xsp -= _x * _f2;
    b.ysp -= _y * _f2;
    b.zsp -= _z * _f2;
G is your gravitational constant, which can have any value you want, bigger number = more gravity. Likewise for the mass of both bodies.

this line:
var _f = G / power(point_distance_3d(0,0,0,_x,_y,_z),3);
can be rewritten for a slight performance boost:
var _f = G / power(dot_product_3d(_x,_y_z,_x,_y_z),1.5);
I didn't think it would be this easy. Thank you!

Just one problem. xpsd, yspd and zspd seem to always result in NaN. I can't seem to figure it out.

I read that this problem appears when dividing by 0, but when I draw _f, it shows '1.'.

My variables
mass ranges from 10-60
G is 100
gravity range is no more than mass*4.
 
are the planets located in the same place? You'd get a divide by zero error in that case.

If that's not the problem, then the problem must exist in other parts of your code.
 

Bingdom

Googledom
are the planets located in the same place? You'd get a divide by zero error in that case.

If that's not the problem, then the problem must exist in other parts of your code.
It was a very noobish mistake.
The problem was that I was using the 'with' statement and I've been using the same coordinates with itself. I didn't run a check to see if it's id was matching or not.
 
Top