base:sprite_collision_detection
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:sprite_collision_detection [2015-04-17 04:33] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Individual sprite ' | ||
+ | by Achim | ||
+ | |||
+ | |||
+ | The regular way of detecting a sprite to sprite collision is to define a box around the sprite, which means you add an offset to the sprite' | ||
+ | |||
+ | In most cases the ' | ||
+ | |||
+ | ===== Setup ===== | ||
+ | Set up sprite tables (for eight sprites): | ||
+ | |||
+ | < | ||
+ | spritey: | ||
+ | spritex: | ||
+ | spritemsb: | ||
+ | </ | ||
+ | |||
+ | The main program will use these tables to move the sprites and the irq will read the values to update the VIC registers every frame. | ||
+ | |||
+ | Next step is an offset table with four offset values for figuring out y1, y2, x1 and x2. | ||
+ | Here's an example sprite: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Spritey and spritex declare the upper left corner of the sprite.\\ | ||
+ | y coordinates of red box: y1= y+3px and y2= y1+12px\\ | ||
+ | x coordinates of red box: x1= x+8px and x2= x+16px | ||
+ | < | ||
+ | offsettable: | ||
+ | ... //and so on for all sprites... | ||
+ | </ | ||
+ | |||
+ | Finally you need tables for y1, y2, x1 and x2: | ||
+ | < | ||
+ | spritey1: | ||
+ | spritex1: | ||
+ | spritemsb1: | ||
+ | |||
+ | spritey2: | ||
+ | spritex2: | ||
+ | spritemsb2: | ||
+ | </ | ||
+ | |||
+ | ===== Calculation ===== | ||
+ | In this example the y-reg has to point at the correct position of ' | ||
+ | and x-reg at the correct position of ' | ||
+ | < | ||
+ | lda spritey, | ||
+ | clc | ||
+ | adc offsettable, | ||
+ | sta spritey1,x | ||
+ | adc offsettable+1, | ||
+ | sta spritey2,x | ||
+ | |||
+ | lda spritex,x | ||
+ | clc | ||
+ | adc offsettable+2, | ||
+ | sta spritex1,x | ||
+ | lda spritemsb,x | ||
+ | adc #$00 | ||
+ | sta spritemsb1, | ||
+ | |||
+ | lda spritex,x | ||
+ | clc | ||
+ | adc offsettabel+3, | ||
+ | sta spritex2,x | ||
+ | lda spritemsb,x | ||
+ | adc #$00 | ||
+ | sta spritemsb2, | ||
+ | </ | ||
+ | This has to be looped for all sprites. | ||
+ | |||
+ | ===== Detection ===== | ||
+ | The detection is similar to the 'one size fits all' approach mentioned | ||
+ | above. Let's assume the first slot (in spritey1, spritey2 etc.) is reserved for the player' | ||
+ | sprite, the other slots are used for enemy sprites. | ||
+ | < | ||
+ | ldx #$00 | ||
+ | loop: lda spritey2 | ||
+ | cmp spritey1+1, | ||
+ | bcc skip // | ||
+ | |||
+ | lda spritey2+1, | ||
+ | cmp spritey1 | ||
+ | bcc skip // | ||
+ | |||
+ | lda spritex1+1, | ||
+ | sec | ||
+ | sbc spritex2 | ||
+ | lda spritemsb1+1, | ||
+ | sbc spritemsb2 | ||
+ | bcs skip // | ||
+ | |||
+ | lda spritex1 // | ||
+ | sec | ||
+ | sbc spritex2+1, | ||
+ | lda spritemsb1 | ||
+ | sbc spritemsb2+1, | ||
+ | bcc hitbysprite // | ||
+ | |||
+ | skip: inx | ||
+ | cpx #$07 | ||
+ | bne loop | ||
+ | rts | ||
+ | |||
+ | hitbysprite: | ||
+ | inc $d020 | ||
+ | rts | ||
+ | </ | ||
+ | |||
+ | Using the 2*x trick makes life easier: Delete all msb tables, use 8bit additions to figure out x1 and x2 and use comparisons instead of subtractions for collision detection. | ||
+ | |||
+ | |||
base/sprite_collision_detection.txt · Last modified: 2015-04-17 04:33 by 127.0.0.1