Physics Items keep stopping

T

TurtleBitAlex

Guest
Hello. Im currently trying to build a gravitational model of planets in Gamemaker2. So far gravity and everything is working great except one problem, Every like second or few the objects just randomly stop and basically the physics has to reset so all momentum and stuff is gone. Any Help? Code
Code:
n = instance_number(obj_object)
G = 6
for (var i = 0; i < instance_number(obj_object); i++) {
bodies[|i] = instance_find(obj_object, i);
}

for( var i = 0; i < (n-1); i += 1 ) {
    _a = bodies[|i];
    for ( var j = i + 1; j < n; j += 1 ) {
        _b = bodies[|j];
      if _b != _a {
        _x = _a.x - _b.x;
        _y = _a.y - _b.y;
        _f = G / power(_x*_x+_y*_y,1.5);
        _f1 = _a.mass * _f;
        _f2 = _b.mass * _f;
        if point_distance(_a.x,_a.y,_b.x,_b.y) > 64
        {
        _b.phy_speed_x += _x * _f1;
        _b.phy_speed_y += _y * _f1;
        _a.phy_speed_x -= _x * _f2;
        _a.phy_speed_y -= _y * _f2;
    }
  }
    _b.phy_speed_x = _b.phy_speed_x
}
}
Here is a current example. Executable https://drive.google.com/file/d/1o02ghdhWwqCH960ar5toSyL4a6XKDLjV/view?usp=sharing
 
P

Pyxus

Guest
Hello. Im currently trying to build a gravitational model of planets in Gamemaker2. So far gravity and everything is working great except one problem, Every like second or few the objects just randomly stop and basically the physics has to reset so all momentum and stuff is gone. Any Help? Code
Code:
n = instance_number(obj_object)
G = 6
for (var i = 0; i < instance_number(obj_object); i++) {
bodies[|i] = instance_find(obj_object, i);
}

for( var i = 0; i < (n-1); i += 1 ) {
    _a = bodies[|i];
    for ( var j = i + 1; j < n; j += 1 ) {
        _b = bodies[|j];
      if _b != _a {
        _x = _a.x - _b.x;
        _y = _a.y - _b.y;
        _f = G / power(_x*_x+_y*_y,1.5);
        _f1 = _a.mass * _f;
        _f2 = _b.mass * _f;
        if point_distance(_a.x,_a.y,_b.x,_b.y) > 64
        {
        _b.phy_speed_x += _x * _f1;
        _b.phy_speed_y += _y * _f1;
        _a.phy_speed_x -= _x * _f2;
        _a.phy_speed_y -= _y * _f2;
    }
  }
    _b.phy_speed_x = _b.phy_speed_x
}
}
Here is a current example. Executable https://drive.google.com/file/d/1o02ghdhWwqCH960ar5toSyL4a6XKDLjV/view?usp=sharing
have you tried applying forces rather than directly changing the object speed?
 
There's a lot going on in your code that doesn't seem to make sense to me. But nothing that explains why your objects should come to a halt randomly. And I've certainly never encountered that problem whenver I set speed manually in a physics room.

By the way, what are you using physics for here? Collisions?
 
T

TurtleBitAlex

Guest
have you tried applying forces rather than directly changing the object speed?
How would i apply the force from within a controller object? This is because I cant run physics_apply_force from inside the controller can i?
 
T

TinyGamesLab

Guest
How would i apply the force from within a controller object? This is because I cant run physics_apply_force from inside the controller can i?
Sure, you can always use a with statement:

with (other)
{
physics_apply_force(x, y, 0, -30);
}
 
T

TurtleBitAlex

Guest
How would i apply this to my code as the object changes and the variables have to be shared
 

chamaeleon

Member
@TurtleBitAlex Seeing all your code calculating physics attributes at the beginning of the post, and then seeing you using GameMaker physics, seems to be mixing two ways of doing things. I'd really recommend either sticking to doing all calculations yourself, like already seem to be, or skip doing all those things and solely rely on the GameMaker physics system. Mixing the two seems like a recipe for disaster.
 
Top