Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Collision Detection w/o Big Sprite

12 views
Skip to first unread message

justinbollig

unread,
May 4, 2008, 7:51:21 PM5/4/08
to wsu-assem...@googlegroups.com

Has anyone written a collision detection routine that doesn’t make use of BigSprite or a good path to take to create a routine that does such?  I’m hoping it will be easier to write a routine, than to reconfigure my code to use BS.

 

Thanks,

 

JuBo

Justin Bollig

unread,
May 4, 2008, 8:04:31 PM5/4/08
to wsu-assem...@googlegroups.com

This is the routine that I have started and I repeat it for each asteroid.  My sprites consist of two 8x16 side by side (16x16).  I first compare the X coordinates together and if they aren’t with 16, then there is no collision.  If they are within 16, then I proceed to check the y coordinate.  If it is also within 16, then there should be a collision.  If its not within 16, then no collision takes place.  Sometimes this gets me false collisions, or no collisions when there should be.  I believe my use of the carry flag might not be accurate.

 

check4collision:

.shipast1:

                GetSpriteXAddr                Sprite0

                ld            h,a

                GetSpriteYAddr                Sprite0

                ld            l,a

                GetSpriteXAddr                Sprite6

                ld            b,a

                GetSpriteYAddr                Sprite6

                ld            c,a

               

                ld            a,h

                sub         b                                             ;subtract two x coordinates

                cp           16                                           ; are they within 16?

                jr             nc,.shipast2                        ; no, then no collision

 

                ld            l,a                          

                sub         c                                              ;suptract two y coordinates

                cp           16                                           ;are they within 16?

                jr             nc,.shipast2                        ; no, then no collision

                ret                                                          ;collision detected

 

Any ideas of how to make this work a little better?

 

JuBo

John Harrison

unread,
May 4, 2008, 9:28:34 PM5/4/08
to wsu-assem...@googlegroups.com

justinbollig wrote:

Has anyone written a collision detection routine that doesn’t make use of BigSprite or a good path to take to create a routine that does such?


I think Matt Hart did...

Andrew Stanton

unread,
May 4, 2008, 11:56:32 PM5/4/08
to wsu-assem...@googlegroups.com
Actually I did recently.  I think I have an idea of what you are doing, but I think it is a little more complex than that.

I have written a loop that is called each time the program goes through the movement loop of our main sprites code.  Additionally, I have a macro that checks for collision with each sprite you have against your main sprite(s).

I will explain it in a bit of detail here, but if it doesnt make sense I would be willing to explain it in full in class lab on Monday.

The loop looks like this:

SpriteCollision:
    push    bc

    ld    a,0
    ld    [RESTART],a    ; Restart is a variable I later use to check if any of the sprites hit the main sprite
                                  ; Here I am resetting the variable to 0, so that I can go through the check again
   
    GetSpriteXAddr    SpriteHero   ; Here I get X addr of main sprite

    ld    [UP_L_X],a                   ; I store it in this variable called UP_L_X, which stands for the upper left X pixel
    add    a,7                            ; I add 7 to get the
    ld    [BOT_R_X],a                 ; Bottom Right X pixel of the sprite

    GetSpriteYAddr    SpriteHero  ; I do the same for the Y addresses.

    ld    [UP_L_Y],a
    add    a,7
    ld    [BOT_R_Y],a


    CheckAgainstSprite    SpriteBot0   ;Here is my macro for the check, you will see the macro after the loop
    CheckAgainstSprite    SpriteBot1   ; basically if the sprite is inside of the main sprite, then RESTART variable
    CheckAgainstSprite    SpriteBot2   ; gets set to 1, which then I have some logic to do what I want when I do have collision
    CheckAgainstSprite    SpriteBot3

    ld    a,[RESTART]   ; here is that logic, so I load a with the RESTART value
    ld    b,a                  ;store RESTART in b
    ld    a,1                  ; ld a with 1
    cp    b                   ; compare
    call    z,retry          ; call retry if compare = zero
   
    pop    bc
   
    ret

retry: 
basically then I call what I want to do here...

ret

; Okay so here is my macro, the logic for this is tough, so it may be easier to explain in class, I wrote it all out decently
; enough on paper... actually I don't think I can easily explain what I am doing here..
; so catch me in the lab and I will talk it out.


CheckAgainstSpritePrep: MACRO ; this macro just initializes the variables I need, you put in 'CheckAgainstSpritePrep'
      LoByteVar RESTART             ; near the beginning of your code so the variables are initialized before you use them
    LoByteVar UP_L_X
    LoByteVar UP_L_Y
    LoByteVar BOT_R_X
    LoByteVar BOT_R_Y
ENDM

CheckAgainstSprite:    MACRO  ; here is the macro to check for collision
   
    push    bc
  push  af

    ld    a,[UP_L_X] 
    ld    b,a
    GetSpriteXAddr    \1
  add 7
    cp    b
    jp    c,.skip\@

    ld    a,[UP_L_Y]
    ld    b,a
    GetSpriteYAddr    \1
  add 7
    cp    b
    jp    c,.skip\@

    ld    a,[BOT_R_X]
    ld    b,a
    GetSpriteXAddr    \1
    cp    b
    jp    nc,.skip\@

    ld    a,[BOT_R_Y]
    ld    b,a
    GetSpriteYAddr    \1
    cp    b
    jp    nc,.skip\@
 
  ld  a,1
    ld    [RESTART],a
 
  ;debugging
.skip\@
 
  pop af
    pop    bc
    ENDM

;Good luck!
; - Andrew

Justin Bollig

unread,
May 6, 2008, 3:04:14 PM5/6/08
to wsu-assem...@googlegroups.com

Thanks for the macros Andrew.  They worked well with minor tweaking for my program.

 

JuBo

 

From: wsu-assem...@googlegroups.com [mailto:wsu-assem...@googlegroups.com] On Behalf Of Andrew Stanton


Sent: Sunday, May 04, 2008 10:57 PM
To: wsu-assem...@googlegroups.com

Andrew Stanton

unread,
May 6, 2008, 9:23:41 PM5/6/08
to wsu-assem...@googlegroups.com
No Problem.
Reply all
Reply to author
Forward
0 new messages