• 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 RE : physic particle/how can I optimize this

G

graviax

Guest
create event:
Code:
alarm[0] = 200;
step event :
Code:
if(!place_meeting(x,y+1,obj_wall))
  {
  if(vsp < 30)vsp += 0.1;
  }

  //Horizontal Collision
  if (place_meeting(x+hsp,y,obj_wall))
  {
  while(!place_meeting(x+sign(hsp),y,obj_wall))
  {
  x += sign(hsp);
  }
  hsp = 0;
  vsp = 0;
  }
  x += hsp;

  //Vertical Collision
  if (place_meeting(x,y+vsp,obj_wall))
  {
  while(!place_meeting(x,y+sign(vsp),obj_wall))
  {
  y += sign(vsp);
  }
  hsp = 0;
  vsp = 0;
  }
  y += vsp;
alarm event :
Code:
instance_destroy();
and finally the collision event with another blood particle :
Code:
if(irandom_range(0,10) > 5)
{
  instance_destroy();
}
I'm pretty new to GML but I know a little about programming, but I don't have any clue on optimizing this and optimizing a bit of program in general.
I need help
 
J

Jordan Robinson

Guest
I guess the first question you should ask yourself is: Is this code causing your game to lag or degrading the performance more than you think it should?
If it's not, then there's no need to optimize it. If it is, then the first thing I would look at would be the collision events. If this is just for particles, then it may not be necessary to have "pixel perfect" collisions and so you could omit parts like:

Code:
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
    y += sign(vsp);
}
 
G

graviax

Guest
So instead of using a place_meeting i should use the collision event?
or should I just remove the while loop?
 
J

Jordan Robinson

Guest
So instead of using a place_meeting i should use the collision event?
or should I just remove the while loop?
just remove the while() loops

Well the while loop is just making sure the particles are precisely touching the wall object, so by getting rid of those then you will automatically reduce the number of collision checks by at least 2 every step for each particle. If you just get rid of the while loops, but leave the rest of the code as it is, the particles will still collide with the wall as you want, they just may stop a couple of pixels above the surface rather than being in "pixel perfect" collision. Does that make sense?

Also, this part:
Code:
if(!place_meeting(x,y+1,obj_wall))
{
    if(vsp < 30)vsp += 0.1;
}
The collision check isn't necessary, because as soon as the vsp is > 0 your vertical collision check will reset it to 0. So it would be more efficient to just have:
Code:
if(vsp < 30)vsp += 0.1;
 
Top