1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 RE : physic particle/how can I optimize this

Discussion in 'Programming' started by graviax, Jun 29, 2016.

  1. graviax

    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
     
  2. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    86
    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);
    }
     
  3. graviax

    graviax Guest

    So instead of using a place_meeting i should use the collision event?
    or should I just remove the while loop?
     
  4. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    86
    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;
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice