Author Topic: How to check EFFECTIVELY for many sprite coordinates? -- ASM TI83-84  (Read 7764 times)

0 Members and 1 Guest are viewing this topic.

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
I got a bunch of sprites on the screen, and now I want to check wich (if any) match some coordinates set by me.
So, lets say, there are 10 8x8 sprites on the screen, and I want to check if one has Xpos=30 and Ypos=40, plus I want to know which sprite that would be.
Is the code going to look like this?:

(...)
     LD     A, (XposSprite1)
     CP    30
     JP     Z, Sprite1XisTrue
     LD     A, (XposSprite2)
     CP     30
     JP     Z, Sprite2XisTrue
     LD     A, (XposSprite3)
     CP     30
     JP     Z, Sprite3XisTrue
ect.

And then make another for all Y-coordinates.
This doesn't look very efficient, so I wonder if anyone can make a small example of how to do this much better.

Thanks in advance!
« Last Edit: May 06, 2010, 07:37:55 am by Jerros »


79% of all statistics are made up randomly.

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
You should give more information what are the needs for your collision detection engine. Sometimes, it can be very specific.
The coding you have is not really the best for maintaining and efficiency... The main problem is really maintaining the code.

I will try to give you a more generic code:
Code: [Select]
; store the sprites in a table in memory, because you have various sprites
sprites_position:
;     X   Y
 .db 30,30
 .db 10,30
 .db 10,10
 .db 30,10
 .db 0,0
; 5 sprites for now

; routine to get X and Y
; input: A is sprite number (0 to 5)
; output: b is X and c is Y
get_x_y_sprite:
 ld h,0
 ld l,a
 add hl,hl
 ld de,sprites_position
 add hl,de ;hl has spriteX
 ld b,(hl)    ; b is X
 inc hl     ; skip to SpriteY
 ld c,(hl)   ; c is Y
 ret

testSprite3:
 ld a,3
 call get_x_y_sprite
 ld a,30
 cp b
 jr z,spriteXis30
 ld a,10
 cp c
 jr z,spriteYis10


ix register can be useful:
Code: [Select]
ld ix,sprites_position
; get XposSprite2 into A
 ld a,(ix+2)
 cp 30
 jr z,Sprite2XisTrue
 ret

Sprite2XisTrue:
; to get YposSprite2 into a
 ld A,(ix+3)
 cp 30
 jr z,Sprite2isinX30Y30
 ret

Sprite2isinX30Y30:
; do something when Sprite2 is in X=30 and Y=30


It all depends on what you need.
The code can have bugs, I coded quickly, but can be viewed as an exercise. :)
Hobbing in calculator projects.

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Perfect!
Thanks alot.
This will certainly help me much, thanks for the trouble.


79% of all statistics are made up randomly.