• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM *solved*Angle in 3d

Kahrabaa

Member
Hello.
I want two objects, in 3d, to repel each other based on their position and distance.

In 2d I would:
Code:
//Get the inverted distance between the two points (inverted cuz the repulsion needs to be stronger the closer the two objects are)
dis= 1 / point_distance( x1,y1, x2,y2);
//Get the direction
dir=point_direction(x1,y1,x2,y2);
//Accelerate
hspeed+=lengthdir_x( dis, dir);
vspeed+=lengthdir_y( dis, dir);
How would I do the same in 3d?
I found a point_distance_3d but no point_direction_3d.
My thinking is limited to 2d wich is probably why Im thinking incorrectly maybe.

One thing I tried was to move each axis independently. Like
Code:
//Get the inverted X difference between two objects
xdis= 1/(x2-x1);
ydis= 1/(y2-y1);
zdis= 1/(z2-z1);
//The add that to acceleration
hspeed+=xdis;
vspeed+=ydis;
"zspeed"+=zdis;
But this gives me incorrect behaviour. Because if the difference in Y is GREAT and the difference in X is small. They still repel each other in the X axis strongly even though they are far away wich is incorrect. This is because each axis is being affected independently. (Realised this after i tried it)

Would appreciate any help
Thank you!
 
You don't need to know angles for this.

var _xd = a.x - b.x;
var _yd = a.y - b.y;
var _zd = a.z - b.z;
var _force = Some_Amount * power(_xd*_xd+_yd*_yd+_zd*_zd,-1.5); //-1.5 drops with distance squared. use -1 for linear drop.
a.xsp += _xd * _force;
a.ysp += _yd * _force;
a.zsp += _zd * _dorce;
b.xsp -= _xd * _force;
b.ysp -= _yd * _force;
b.zsp -= _zd * _force;
 
Top