GMS 2 Moving Object Collision

Discussion in 'Programming' started by MuddyMuddy, Jan 12, 2020 at 4:52 PM.

  1. MuddyMuddy

    MuddyMuddy Member

    Joined:
    Mar 2, 2017
    Posts:
    184
    Hi - I have two blocks moving at a speed of 1 toward each other.

    One at a speed of 1 and the other -1 (x += dir)

    I am trying to make them bounce off each other (reverse direction) with the following code:

    Code:
    if  place_meeting(x+dir,y,o_solid){
        if dir = -1{dir = 1;}else{dir = 1;}       
    }
    
    x += dir;
    Except the first object to hit will change direction, meaning the second one will stay the same direction because the first is no longer colliding (so they both end up going the same way, next to each other).

    Any ideas on how I can solve this?
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,063
    Have the code also modify the colliding instance, or have a control object whose purpose is to control all moving blocks. In any case, nothing stops you from changing the instance variables of another instance at the same time as the current instance. Just need to be careful that you don't do something "twice".
     
    devKathy likes this.
  3. MuddyMuddy

    MuddyMuddy Member

    Joined:
    Mar 2, 2017
    Posts:
    184
    I know what you mean, and have tried this. However my 2 objects are the same object - so even id I change code of the id, both are running the code.
     
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,063
    Same object is fine and very common. Just use something like instance_position instead of place_meeting to find out which instance you are colliding with, and change both as needed. If the other instance then runs the same code you either have moved the instances apart so that it doesn't trigger or otherwise keep track of the fact that you have handled the collision already by some other means (instance variables, or some other state management).
     
  5. MuddyMuddy

    MuddyMuddy Member

    Joined:
    Mar 2, 2017
    Posts:
    184
    Got it working. Turns out I needed to change the '1' to a '-1'

    if dir = -1{dir = 1;}else{dir = -1;}

    Then I could add the colliding object code and it works fine.

    Final code:
    Code:
    if  place_meeting(x,y,o_solid){
    
        var type = instance_place(x,y,o_solid);
        if type.dir = -1{type.dir = 1;}else{type.dir = -1;}
        if dir = -1{dir = 1;}else{dir = -1;}
    }
    
    x += dir;
     
  6. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    204
  7. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    344
    this is a classic example of why you should use hspd and vspd.

    if you have a collision that you want to reverse simply multiply hspd or vspd by -1;
     
  8. MuddyMuddy

    MuddyMuddy Member

    Joined:
    Mar 2, 2017
    Posts:
    184
    @Sabnock Thanks that is the calculation that I was looking for
     
  9. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    344
    NP variable *= -1 is one of those things that should be part of any coders basic tool set. it is beautifully simple, wonderfully powerful and incredibly useful.
     

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