• 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!

MP Grid in a physics room

HighlandCoo

Member
I have a physics enabled object that needs to navigate across a physics enabled room - avoiding walls as it goes.

I tried creating a grid, and mp_grid_draw confirms there is plenty of space to move.

I used this code

Code:
if mp_grid_path(global.grid,path,x,y,suspicion_x,suspicion_y,1)
{
  phy_speed_x = lengthdir_x(spd,point_direction(x,y,path_get_point_x(path,0),path_get_point_y(path,0)))
    phy_speed_y = lengthdir_y(spd,point_direction(x,y,path_get_point_x(path,0),path_get_point_y(path,0)))
  }
  else{
    show_debug_message("no path found!")
  }
Using draw_path, I can see the path is created, but the object just goes skittering around the room in random directions.

What am I doing wrong??
 

johnwo

Member
Have you tried using phy_position_x/y instead of phy_speed_x/y ?
And why are you using lengthdir_x/y ?

Cheers!
 

HighlandCoo

Member
Hrm phy_position_x is fine (it actually follows the path yay!) but it's really jerky movement like it's only moving every 5th step or something.

Im using lengthdir_x/y to calculate where I need to move to in relation to the path? Seemed sensible to me, I know my speed, I know my direction and I know my time(1s) so I just needed to calculate the coords to move to, therefore - length_dir_x/y
 

GMWolf

aka fel666
You should nit set the position or speed of a physics object on this way.
Instead you should use physics apply impulse: calculate the exact impulse you need and apply it to your object.

To do that you should first find your objects current velocity.
Then find the new velocity you want.
Subtract one from the other to get the change in velocity needed.
Multiply that by the mass of the object to get the impulse.
 

HighlandCoo

Member
Hmmm okay I'm really terrible at physics!

Regarding the mass - how is that calculated? I know I set a density on my fixtures, and that there is a physics meters per pixel defined, but the numbers I get out of that are crazy - like 9999999kg mass for a 64 pixel object, is this correct?
 

Yal

šŸ§ *penguin noises*
GMC Elder
Actual physics density is mass per cubic meter, but since your objects are 2D, the Box2D physics simulation doesn't use the technically correct density. It just uses something with similar properties. So.. try not to think about it. Higher density just means the object is heavier and let's keep it at that.
 
Top