How do I detect the angle of a collision?

0 favourites
  • 6 posts
  • I'm trying to tell whether a sprite is colliding with another sprite from the top, left, right or bottom. I thought I could it with a combination of the angle of movement and the size/position of the sprites (assuming they don't rotate).

    It's a fairly involved calculation, so before I went too far down that route, is there a simple built in thing that can tell me the position where on a sprite a collision occurred?

    Failing that, is there a way to get the angle of motion from a sprite immediately before a collision? Getting it on the collision returns 0 (which is technically correct and useless in this context).

    Demo: https://drive.google.com/open?id=1pFre_1_kpMwmhPsk4iXKhXkoITL7qgAv

  • If you only want to know if it's Left, Right, Top, Bottom. You could use 'is overlapping at offset':

    + Goodie: Is overlapping Baddie at offset (1, 0)

    -> DebugOut: Set text to "right"

    + System: Else

    + Goodie: Is overlapping Baddie at offset (-1, 0)

    -> DebugOut: Set text to "Left"

    + System: Else

    + Goodie: Is overlapping Baddie at offset (0, 1)

    -> DebugOut: Set text to "Bottom"

    + System: Else

    + Goodie: Is overlapping Baddie at offset (0, -1)

    -> DebugOut: Set text to "Top"

    + System: Else

    -> DebugOut: Set text to "No collision"

  • Thanks Asmodean. It had just clicked that I was making it insanely more complicated than it needed to be.

    Your suggestion if really nice and clean.

  • I think your left and right are the wrong way around though.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I think your left and right are the wrong way around though.

    Not intentionally. :)

  • Looks like Asmodean already solved your problem but I just wanted to throw in that you can also do this:

    Goodie > On collision with Baddie >

    set angleOfCollision to angle(goodie.x, goodie.y, baddie.x, baddie.y)

    This will get the angle of baddie compared to goodie. This is the way I do it and then I have a function that converts angles to directions like

    if angle is between -45 and 45, return "Right"

    if angle is between 45 and 135, return "Down"

    etc

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)