Is there a way to find out what edge sprite collisions are made.

Previous topic - Next topic

spicypixel

I not only want to check Sprite Collisions but also a quick way to know what edge of the object I'm colliding with. Is there any command to do this presently?
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Slydog

It would be nice if SPRCOLL() returned the pixel coordinate of the colliding pixel, but most time there would be multiple pixels colliding.

What you could do is when a collision is detected, do 4 more BOXCOLL() checks using a slice of pixels (1 wide for left / right, or 1 high for top / bottom) and see what side collides.

[Edit] If you want a more pixel perfect test, you could create 4 new sprites by splitting your sprite into 4 sections, one each for what you would consider a collision for that direction.  Then use SPRCOLL() using the same positions.  The 4 sprites would have the same dimensions as your original sprite, with the pixels in their original locations.  It all depends on how rectangular or irregular your sprites are and what method would work best.

Or compare with the previous frames sprite positions.  The sprite movement direction should tell you what side it collided on.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

spicypixel

Quote from: Slydog on 2011-Nov-04
It would be nice if SPRCOLL() returned the pixel coordinate of the colliding pixel, but most time there would be multiple pixels colliding.

What you could do is when a collision is detected, do 4 more BOXCOLL() checks using a slice of pixels (1 wide for left / right, or 1 high for top / bottom) and see what side collides.

Or compare with the previous frames sprite positions.  The sprite movement direction should tell you what side it collided on.

Yeh I think I will have to go with the BOXCOLL() method. I was contemplating using the sprite movement but then it becomes awkward (as far as my thinking stretches lol) if the sprites collide at their corner point. Trying to differentiate whether it was the top or side would then be tricky.
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

ampos

In my bat game, I just move in X axis and check collision; if true, X=-1*X. Then another check moving Y axis and checking collision.

BdR

If you want the general direction (up, down, left or right) in which the two spites are positioned relative to each other, then you can use this code:
Code (glbasic) Select
CONSTANT SPR_INVADER = 0
CONSTANT SPR_PLANET = 1

LOADSPRITE "Media/invader.png", SPR_INVADER
LOADSPRITE "Media/planet.png", SPR_PLANET

WHILE KEY(1) = FALSE

  RunTest()
  SHOWSCREEN
WEND

// -------------------------------------
// the collision test
// -------------------------------------
FUNCTION RunTest:

  LOCAL mx%, my%, b1%, b2%
  LOCAL objx%, objy%
  LOCAL xsize%, ysize%
  LOCAL str$
 
  // get mouse input
  MOUSESTATE mx%, my%, b1%, b2%
 
  // non moving thing, always centerd
  objx = 320
  objy = 240

  // draw
  DRAWSPRITE SPR_INVADER, mx, my
  DRAWSPRITE SPR_PLANET, objx, objy

  // check for collision
  IF SPRCOLL(SPR_INVADER, mx, my, SPR_PLANET, objx, objy)
    // correct for center of sprites
    GETSPRITESIZE SPR_INVADER, xsize, ysize
    mx = mx + (xsize / 2)
    my = my + (ysize / 2)
    GETSPRITESIZE SPR_PLANET, xsize, ysize
    objx = objx + (xsize / 2)
    objy = objy + (ysize / 2)
   
    // in which general are the sprites to each other
    IF ( ABS(my - objy) > ABS(mx - objx) )
      // y-delta is bigger than x-delta, so up or down
      IF (my < objy)
        // up
        str$ = "top"
      ELSE
        // down
        str$ = "bottom"
      ENDIF
    ELSE
      // y-delta is smaller than x-delta, so left or right
      IF (mx < objx)
        // left
        str$ = "left"
      ELSE
        // right
        str$ = "right"
      ENDIF
    ENDIF
    PRINT "COLLISION: invader collides from " + str$, 10, 10
  ELSE
    PRINT "no collision... :(", 10, 10
  ENDIF

ENDFUNCTION

spicypixel

Code (glbasic) Select
[quote author=BdR link=topic=7221.msg59298#msg59298 date=1320442027]
    // in which general are the sprites to each other
    IF ( ABS(my - objy) > ABS(mx - objx) )
      // y-delta is bigger than x-delta, so up or down
      IF (my < objy)
        // up
        str$ = "top"
      ELSE
        // down
        str$ = "bottom"
      ENDIF
    ELSE
      // y-delta is smaller than x-delta, so left or right
      IF (mx < objx)
        // left
        str$ = "left"
      ELSE
        // right
        str$ = "right"
      ENDIF
   ENDIF

[/quote]

Worked Flawlessly ;)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

BdR

Quote from: spicypixel on 2011-Nov-05
Worked Flawlessly ;)
Cool, but I'm curious what are you going to use it for? I guess the code example is usable in an Arkanoid-type game, but for a pool or billiards game you'd need a more precise collision angle.

spicypixel

Quote from: BdR on 2011-Nov-05
Quote from: spicypixel on 2011-Nov-05
Worked Flawlessly ;)
Cool, but I'm curious what are you going to use it for? I guess the code example is usable in an Arkanoid-type game, but for a pool or billiards game you'd need a more precise collision angle.

It's for a platform type game, I'm gonna use it for bouncy walls and floors. :)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.