For a small game i 'm making, i need a way to tell when the sprite is on the ground or when it should fall.

this is the sprite and the program should test for the two fields on the bottom left and bottom right of the sprite.

the x-coordinate is stored in D and the y-coordinate is stored in E

01110
10001
10001
10001
01110


i've tried a few times but it seemed to bug everytime.[/code]
First off, since this is your first post, welcome! I am going to assume that this is in TI-BASIC...

EDIT: I am really not that bright... this is in the assembly section! Well then, as ChickenDude says below, it is all a matter of how you want to do collision detection. Pixel checking tends to be the easiest, so all you would have to do is add the height of the sprite to the value in E, and then select which value of D you want, either 0 or the width, and check if there is a pixel there. Good luck!
Seeing as it's in the assembly section and they say the x/y coordinates are stored in de, i'll assume that it's in assembly.

You'll probably not want to use the registers for anything permanent, create a location in your program or use saferam to store the coordinates, like this:

Code:
x = saveSScreen
y = saveSScreen+1

ld hl,0
ld (x),hl   ;set x and x+1 (ie, y) to 0
That will set the x and y coordinates to 0. How you handle collision detection depends on how you handle everything else, is it just a picture and you need to check if a pixel is turned on? Or is it a tilemap? If it's a picture, i would pull the byte underneath it and check if it's empty (ie. if the value in the gbuf = 0), then check if your sprite is aligned (has an x value divisible by 8). If it's unaligned, you need to check the next byte in the gbuf (the one directly to the right).

EDIT: my 8) turned into a smiley...
First of all, i am actually working in assembly and the problem i currently struggle with is that i have tried various ways for checking the pixels and they always seemed to bug out.
The rest of the program works just fine and the coordinates of the sprite are correct, but still the sprite tends to stand in mid-air while near a floor but only on the left side, on the right side the sprite falls through the floor when close enough to the gap.
Another weird thing that occured was that this glitsh wasn't consistent, sometimes it worked just fine and other times it bugs out.
You didn't really answer chicken dude (; Tilemap or picture? That will change the way you check, err mostly. If it is a picture I would do it like so:
[code]
-Load x+[sprite width-1] to L (or H, dunno if it matters with the stupid endian stuff)
-Check H (or maybe a different register, I forget how it works) coordinate on your screen, if 0.. then set for fall (if y coord is also 0). If 1.. stay.
-Load y+[sprite height-1] to H (or L, blah blah)
-Check L (or another register, blah blah) on the screen.

Ahhh... Also don't forget that this is when you are moving to the right. You'll need to check the left side (as x,y) when moving left.
If you don't mind, try posting your code to Pastebin or some place. I'm not really sure what you're doing. It sounds like you're testing the first byte the sprite is in but not the second byte. It'd be easier to see what's going on with some code, though.
If there is a routine to check if a pixel is drawn at a specific location like (x, y), then it should just be a matter of checking a few pixels under the character's feet. If they aren't blank then you know your character is on the ground.

If you have background sprites and enemies you probably want to add in a simple bounding box collision routine for tile/enemy collision.
I personally would do something like the following:

Code:


collision_row_of_5:
;Inputs:
;    BC = (x,y)
;       x,y points to the left edge of the 5-pixel row to check
;Outputs:
;    NC if out of bounds or collision
;    C if in bounds and no collision
;    NZ if collision
;    Z if no collision
; Also assuming the object is not out of bounds.

   call GetPixelLoc
   ret nc         ;means out of bounds here
; Now HL points to the byte in plotSScren for where to check
; Now we have A as a mask with only one bit set.
; Something like %00100000
; What we need is to AND a mask that is 5 pixels wide
; So we need %00111110
; We could do a bunch of complicated maneuvers to adjust for byte boundaries
; I'll keep it simple and not necessarily optimized
   ld b,a
   ld c,0
   or b      ;reset the c flag
   rra \ rr c \ or b
   rra \ rr c \ or b
   rra \ rr c \ or b
   rra \ rr c \ or b
   rra \ rr c \ or b
;now 5 'set' bits span AC
;check if any bits are set (pixels are on) that match the mask:
   and (hl)   ;resets C flag, too
   ret nz      ;means pixels were set, -> collision
   ld a,c
   or a
   scf      ;sets carry flag
   ret z      ;means nothing crossed the byte boundary, so done checking, no collisions
   inc hl
   and (hl)
   ret nz
   scf
   ret

GetPixelLoc:
;Input:
;     b is X
;     c is y
;Output:
;     HL points to byte
;     A is the mask
;     nc if not computed, c if computed
   ld a,c
   cp 64 \ ret nc
   ld a,b
   cp 96 \ ret nc
   ld l,c
   ld h,0
   ld b,h
   add hl,hl
   add hl,bc
   add hl,hl
   add hl,hl
   ld b,a
   rrca \ rrca \ rrca
   and 0Fh
   add a,plotSScreen&255
   ld c,a
   ld a,b
   ld b,plotSScreen/256
   add hl,bc
   and 7
   ld b,a
   inc b
   ld a,1
     rrca
     djnz $-1
   scf
   ret

(untested)

So what you would do is put (x,y) into BC, where X,Y points to the row of pixels under the sprite. It then scans the 5 pixels (going right) and returns flags if there was a boundary or not. I hope it works x.x I haven't coded in what seems like months.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement