GM:S 1.4 [Solved!] Collisions: How to make collisions effect both objects involved?

Discussion in 'Programming' started by Rosieu, Sep 9, 2016.

  1. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Hi Everyone,

    I'm sorry if this is a REALLY obvious question, but for the life of me I can't seem to get my collision working correctly!

    Basically, what I'm trying to achieve is to have two objects collide, causing BOTH of them to bounce away from the point of impact. At the moment, however, only the object that actually caused the impact (the one moving) bounces away. I have tried putting two actions (one bounce for each object) after the collision event, but still nothing! Is this something I would have to look into physics for, or is my newbie brain (or rusty brain, more accurately) just missing something pivotal?

    Once again, sorry if this question is glaringly obvious to everyone but me, I haven't touched Game Maker since about version 7 or 8 and I'm ridiculously rusty!

    Thanks in advance for any help anyone can afford! :D

    Regards,
    Rosieu
     
  2. spe

    spe Member

    Joined:
    Aug 25, 2016
    Posts:
    101
    Personally, I'd try to use physics for something like this. I'm sure there's a way to do it without, but physics makes stuff like this pretty easy.
     
  3. That would have to use physics. There is at least one tutorial within GM which shows how to implement physics (in the style of angry birds, but that should at least give you an idea).
     
  4. What is probably happening is one objects' event is happening first, and moving it away from the second object, so the second object does not register the collision.

    So if you just have 2 same-size rectangles colliding with each other on the x axis at the same speed, you could write:
    Code:
    hspd=-hspd;
    other.hspd=-other.hspd;
    or something along those lines. (the keyword "other" refers to the other object in the collision)
    You could do some pretty easy physics maths to get things like transferring kinetic energy approximated.

    I mean, if you want complicated interactions like when the corners hit the rectangles spin, then you most definitely need a physics engine. But you can fake some basic box/circle collisions pretty easily. A physics engine for Re-creating Pong would certainly be overkill. :p
     
  5. LV154

    LV154 Member

    Joined:
    Sep 9, 2016
    Posts:
    43
    In the collusion event of one object, have you tried using the "with other..." statement?
    For example

    In a collision event of obj1:

    Collision with obj2:

    Direction-=180

    With other {direction-=180}
     
  6. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Hi Everyone!

    Sorry for the late reply, I have been trying out your answers today before I got back to you! Thank you so much for all your help everyone, while I'm still yet to get the collision to work the way I want (I think that is more my lack of knowledge than anything else), your help has put me in the right direction. If all else fails I'll turn to physics, but I hoping to somehow figure out where my code is wrong / what I'm missing!

    Anyway, thanks so much for all your help! :D
     
  7. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    508
    Howdy, Rosieu, and welcome back to GM and the GMC! $E^ J

    You totally should not need the advanced physics. I got what you wanted working using just D'n'D actions. Therefore, I have questions for ya!
    • Which actions did you use?
    • When you say "after the collision event" do you mean you added "Collide with such-and-such event" to the events list and then (while that event is highlighted) added the appropriate actions to the actions list? Or do you mean something else?
    • How did you apply your actions to the different instances you wish to bounce off each other? (EDIT: That is, you said you put in one bounce for each object, thus how did you go about making each bounce apply to each of the two colliding instances?)
    • Is either object/instance set to solid?
    Hopefully, you can find your way to the answer in a short time here. $:^ ]

    Regards,
    Bob
     
  8. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Hi Bob,
    at the moment I have an Event in the player object that is the collision with the second object, which triggers two separate 'Bounce' actions, one on self, and one on the object that the player object collided with. I have also mirrored these events on the other object (since it's a player 2 object). To give it context these are essentially two marbles knocking into each other, so when one marble knocks into another, it cues and event that contains a bounce action for both the object that caused the collision and the object it collided with.

    Both objects are solid, and the collision seems to work fine when BOTH are moving, but if one marble is stationary when the other collides with it, the moving marble bounces away but the stationary marble stays in place!

    I really appreciate the help everyone is giving! :D
     
  9. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    508
    Ah ha! the culprit! $:^ ]

    Lemme post my relevant object info for ya in that test I made earlier today. You'll notice, not a solid to be found -- and simple GM physics, no advanced Box2D fixtures or any of that.
    GM-collide-both-bounce.png
    The Keyboard Pressed Event is unimportant -- it just let me test slamming the two instances at each other in the room. $:^ ]

    The Collision Event uses the bouncing action -- the first applies to Self, the second (as you can see in the screenshot) applies to Other.

    Alternatively, you could simply put one bouncing action (applying to Self) in each object's collision event -- and in fact, if you only have one object (with multiple instances of it which can collide), this is surely the preferred way anyway.

    If this is clear how and why, great. If you need more info, ask away! $:^ J

    Bob
     
    Azure Spectre likes this.
  10. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Thanks so much for your help!

    I removed all the 'solid' settings and adjusted my collision actions accordingly, and still no luck! I just made a new project and set it up with exactly the events you had, and alas, still no bouncing away of the second collision object!

    I'm starting to wonder if it's a bug with my computer or something! o_O

    Thank you so much for your help though! :D

    Edit: Just as a point of reference, here is the info for the player object:

    Information about object: obj_player1
    Sprite: spr_Player1
    Solid: false
    Visible: true
    Depth: 0
    Persistent: false
    Parent:
    Children:
    Mask:
    No Physics Object
    Collision Event with object obj_player2:
    bounce not precisely against all objects
    for other object: bounce not precisely against all objects
    Keyboard Event for <Space> Key:
    start moving in directions 000001000 with speed set to 10
     
    Last edited: Sep 11, 2016
  11. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    508
    (You're welcome of course!)

    Hmmmm. Maybe my Keyboard Pressed <Space> Event was more important than I thought...

    I set each instance in a blank room (at the same y value to each other) and used that Spacebar event to fire two actions:
    set horizontal speed of self to 8
    set horizontal speed of all o_collision_2 objects to -8

    They each bounce because they each move; bouncing (the D'n'D action and the GML function) only affects instances with speed.

    Is your goal to have a moving instance hit a stationary instance, and then they each move away?
     
  12. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Ahhh, that would explain why it seemed to work when the second object was moving! That's actually really handy to keep in mind for the future!

    That has solved everything! I cannot thank you enough! I'll need to refine my events / code to make things a little cleaner, but everything is working as it should be!

    The event/action I used to get things working properly was:

    Collision Event with object obj_player2:
    bounce not precisely against all objects
    for other object: set speed to obj_player1.speed and direction to point_direction(obj_player2.x,obj_player2.y,obj_player1.x,obj_player1.y) - 180

    Ah man, I'm stoked! (You'll have to excuse the ' 'Strayan' slang there!) I was totally flummoxed by this one!

    Seriously Bob, you've helped so much! :D

    Thank you to everyone who helped with this issue!
     
    ParodyKnaveBob likes this.
  13. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    508
    Great! Glad to hear! And you're quite welcome. $:^ ] $:^ ]

    (Now you probably want to edit your original post to add "[SOLVED]" to the beginning or end of its title.)
    (EDIT: Oh, you might've already done this but b/c of how I left stuff open I wouldn't've seen the change. Sorry! Good stuff! heheh)

    Cheers!
    Bob $:^ J
     
    Last edited: Sep 12, 2016
  14. Marcin

    Marcin Member

    Joined:
    Jan 26, 2017
    Posts:
    4
    Ok guys i know is bit late for simple answear but i just start today with this and i found simple solution without psychics:

    Insted of use move_bounce_solid(true) in step event i write simple code in collision event (on everything you want make collision)

    "move_bounce_solid(true)
    move_bounce_solid(false)"

    Then if you want something make happens on collision you can put this before this code (because bounce will be after collision happens) or you can do it normalny in other places because in this case, collision will happens anyway and bounce its collision affect not before it happens and make collision not possible.

    Hope it helps :)
     
  15. you are a genius it works
     

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