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

GM:S 1.4 Perfect Bullet Collision

Discussion in 'Programming' started by Omar Nava, Feb 12, 2019 at 4:08 PM.

  1. Omar Nava

    Omar Nava Member

    Joined:
    Tuesday
    Posts:
    1
    Hello all!

    This is my first post since the last forum page went down. I haven't quite practiced GMS since then and I am looking for help with bullet collisions. I have looked for similar threads on here but with no luck in figuring out how to do it.

    The trouble I am running into is either one of the following:

    A.) An enemy moves slightly which causes the bullet to get stuck
    B.) My bullet passes through enemies that are too close to me

    Here is the following code I am using in the bullet step event:

    Step Event:

    direction = dir;
    image_angle = dir;

    if (spd > 1){
    nextstep_hsp = hsp;
    nextstep_vsp = vsp;
    }

    scr_collision_bullet_beginStep();


    hsp = cos(degtorad(dir)) * spd;
    vsp = -sin(degtorad(dir)) * spd;


    scr_collision_bullet_endStep();


    scr_collision_bullet_beginStep:

    if (collision_line(x,y,x+nextstep_hsp,y+nextstep_vsp,obj_collision,0,0)){
    spd = 1;
    }

    if (team == 0){
    if instance_exists(obj_enemy){
    if (collision_line(x,y,x+nextstep_hsp,y+nextstep_vsp,obj_enemy,0,0)){
    spd = 1;
    //show_message("hi")
    }
    }
    }else{
    if instance_exists(obj_player){
    if (collision_line(x,y,x+nextstep_hsp,y+nextstep_vsp,obj_player,0,0)){
    spd = 1;
    }
    }
    }

    scr_collision_bullet_endStep:


    /*COLLISION WITH PEOPLE**********************************************************/

    if (team == 0){//Player Bullet


    if (collision_line(x,y,x + nextstep_hsp, y + nextstep_vsp, obj_enemy,1,0))
    {
    while (!place_meeting(x, y, obj_enemy))
    {
    if ( instance_exists(entity) ){
    y += (vsp);
    x += (hsp);
    show_debug_message("X = " + string(hsp));
    show_debug_message("Y = " + string(vsp));
    }

    }

    }else{
    spd = pre_spd;
    }


    }

    if (team == 1){//Enemy Bullet

    if (instance_exists(obj_player)){

    if (collision_line(x,y,x + nextstep_hsp, y + nextstep_vsp, obj_player,1,0))
    {

    while (!place_meeting(x, y, obj_player))
    {
    y += vsp;
    x += hsp;
    }

    }

    }

    }

    /*COLLISION WITH WALLS**********************************************************/

    if (collision_line(x,y,x + nextstep_hsp, y + nextstep_vsp, obj_collision,1,0)) &&
    (!collision_line(x,y,x + nextstep_hsp, y + nextstep_vsp, obj_enemy,0,0)){

    while (!place_meeting(x, y, obj_collision))
    {
    y += vsp;
    x += hsp;
    }
    }



    y += vsp;
    x += hsp;



    Any help would be appreciated! I am very rusty so if anybody can possible explain why my code is flawed that would be awesome too. Thank you all so much, glad to be back in the community!
     
  2. Daren Barnett

    Daren Barnett Member

    Joined:
    Feb 7, 2018
    Posts:
    4
    if you want a perfect bullet, make a collision line a point instead of a line and update it every step or twice every step for how fast you want it to go. You can also put a sprite on that point if you want. But hitscan should be able to do what you are looking for
     

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